⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webapputil.java

📁 Java/J2EE框架Jdon-Framework系统的Sample
💻 JAVA
字号:
/**
 * Copyright 2005 Jdon.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jdon.controller;

import javax.servlet.http.HttpServletRequest;
import com.jdon.aop.interceptor.MethodInterceptor;

/**
 * by WebAppUtil, user can get his businesss object that
 * defined in jdonframework.xml.
 *
 * 面向Web容器中组件,Web容器中任何组件可作为客户端调用本类。
 *
 * <p>Copyright: Jdon.com Copyright (c) 2005</p>
 * <p></p>
 * @author banq
 * @version JdonFramework 2005 v1.0
 */
public class WebAppUtil {
  private final static String module = WebAppUtil.class.getName();
  private static ContainerClient containerClient = new ContainerClient();

  /**
   * get a service from jdonframework.xml's service configure.
   * the service maybe is a pojo service or a ejb service.
   *
   * if user has a business interface, so the interface can has
   * two implemention:pojo or ejb, if is ejb, the ejb's local/remote
   * interface must inherit the business interface.
   * so the application's MVC will completely seperate from his business
   * lay
   *
   * @param name String
   * @param request HttpServletRequest
   * @return Object
   * @throws Exception
   */
  public static Object getService(String name, HttpServletRequest request) throws
      Exception {
    Object o = containerClient.getPOJOService(name, request);
    if (o == null)
      o = containerClient.getEJBService(name, request);
    return o;
  }

  /**
   * get a EJB service directly.
   * @param name String
   * @param request HttpServletRequest
   * @return Object
   * @throws Exception
   */
  public static Object getEJBService(String name, HttpServletRequest request) throws
      Exception {
    return containerClient.getEJBService(name, request);
  }

  /**
   * get the key for the application container
   * user can directly get his container from servletcontext by the key.
   * @return String
   */
  public static String getContainerKey() {
    return KeyConstants.CONTAINER_FACTORY;
  }

  /**
   * get the key for the interceptor,
   * by the key, use can add his interceptor to the container
   * @return String
   */
  public static String getInterceptorKey() {
    return KeyConstants.CONTAINER_FACTORY;
  }

  /**
   * get this Web application's container
   * @param request HttpServletRequest
   * @return ContainerWrapper
   * @throws Exception
   */
  public static ContainerWrapper getContainer(HttpServletRequest request) throws
      Exception {
    ServletContainerFinder scf = new ServletContainerFinder();
    return scf.findContainer(request);
  }

  /**
   * add a MethodInterceptor
   * @param interceptor MethodInterceptor
   */
  public static void addInterceptor(MethodInterceptor interceptor,
                                    HttpServletRequest request) throws
      Exception {
    ContainerWrapper cw = getContainer(request);
    if (cw == null) {
      throw new Exception("Please at first init the JdonFramework!");
    }
    ContainerWrapper child = cw.getChild(getInterceptorKey());
    child.register(interceptor.getClass().getName(), interceptor);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -