webapputil.java

来自「一个非常好的FRAMWRK!是一个外国组织做的!不!」· Java 代码 · 共 184 行

JAVA
184
字号
/**
 * Copyright 2003-2005 the original author or authors.
 * 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.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.aopalliance.intercept.MethodInterceptor;

import com.jdon.aop.interceptor.InterceptorsChain;
import com.jdon.aop.joinpoint.Pointcut;
import com.jdon.bussinessproxy.TargetMetaDef;
import com.jdon.container.ContainerWrapper;
import com.jdon.container.builder.ContainerBuilder;
import com.jdon.container.finder.ComponentKeys;
import com.jdon.container.finder.ServletContainerFinder;
import com.jdon.controller.service.ServiceFacade;
import com.jdon.controller.service.ServiceFactory;

/**
 * Using WebAppUtil, framework's user can get his businesss service object that
 * defined in jdonframework.xml
 * 
 * this is main and important client class for framework's user.
 * 
 * 
 * @author banq
 */
public class WebAppUtil {
    private final static String module = WebAppUtil.class.getName();

    private final static ServletContainerFinder scf = new ServletContainerFinder();

    /**
     * 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
     * 
     * usage: in jdonframework.xml: <pojoService name="userJdbcDao"
     * class="news.container.UserJdbcDao"> <constructor value="java:/NewsDS"/>
     * </pojoService>
     * 
     * UserDao ud = (UserDao)WebAppUtil.getService(“userJdbcDao”, request);
     * 
     * UserDao is the interface news.container.UserJdbcDao, here you must
     * downcast the result wtith a interface type, not a class type.
     * 
     * 
     * 
     * @param name
     *            String
     * @param request
     *            HttpServletRequest
     * @return Object
     * @throws Exception
     */
    public static Object getService(String name, HttpServletRequest request) {
        ServiceFacade serviceFacade = new ServiceFacade();
        ServiceFactory serviceFactory = serviceFacade.getServiceFactory(request);
        return serviceFactory.getService(name, request);
    }

    public static Object getService(TargetMetaDef targetMetaDef, HttpServletRequest request) {
        ServiceFacade serviceFacade = new ServiceFacade();
        ServiceFactory serviceFactory = serviceFacade.getServiceFactory(request);
        return serviceFactory.getService(targetMetaDef, request);
    }

    /**
     * get a component that registered in container. the component is not
     * different from the service.
     * 
     * the component instance is single instance
     * 
     * @param name
     * @param request
     * @return
     */
    public static Object getComponentInstance(String name, HttpServletRequest request) {
        ContainerWrapper containerWrapper = scf.findContainer(request);
        return containerWrapper.lookup(name);
    }

    public static Object getComponentInstance(String name, ServletContext sc) {
        ContainerWrapper containerWrapper = scf.findContainer(sc);
        return containerWrapper.lookup(name);
    }

    /**
     * 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 getService(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 ContainerBuilder.APPLICATION_CONTEXT_ATTRIBUTE_NAME;
    }

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

    /**
     * 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 for all services how to implements a
     * MethodInterceptor,reference:
     * 
     * 
     * @param interceptor
     *            MethodInterceptor
     */
    public static void addInterceptor(MethodInterceptor interceptor, HttpServletRequest request) throws Exception {
        addInterceptor(Pointcut.TARGET_PROPS_SERVICES, interceptor, request);
    }

    /**
     * add a MethodInterceptor for pointcut
     * 
     * @param pointcut
     *            Pointcut values
     * @param interceptor
     * @param request
     * @throws Exception
     */
    public static void addInterceptor(String pointcut, MethodInterceptor interceptor, HttpServletRequest request) throws Exception {
        ContainerWrapper cw = getContainer(request);
        if (cw == null) {
            throw new Exception("Please at first init the JdonFramework!");
        }
        InterceptorsChain interceptorsChain = (InterceptorsChain)cw.lookup(ComponentKeys.INTERCEPTOR_CHAIN);
        interceptorsChain.addInterceptor(pointcut, interceptor);
    }

}

⌨️ 快捷键说明

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