proxyinstancefactoryvisitable.java

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

JAVA
127
字号
/*
 * 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.bussinessproxy.dyncproxy;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

import com.jdon.aop.AopClient;
import com.jdon.bussinessproxy.TargetMetaDef;
import com.jdon.bussinessproxy.meta.EJBTargetMetaDef;
import com.jdon.container.access.TargetMetaRequest;
import com.jdon.container.visitor.Visitable;
import com.jdon.util.Debug;

/**
 *  by using Proxy.newProxyInstance, create a DynamicProxyWeaving for
 *  a target service.
 * 
 *  @todo: create a DynamicProxyWeaving for all services.
 * 
 * @author <a href="mailto:banqiao@jdon.com">banq</a>
 *
 */
public class ProxyInstanceFactoryVisitable implements  Visitable{
    private final static String module = ProxyInstanceFactoryVisitable.class.getName();
    private AopClient aopClient;
    
    /**
     * @param aopClient
     */
    public ProxyInstanceFactoryVisitable(AopClient aopClient) {
        super();
        this.aopClient = aopClient;
    }
    
    /**
     * 
     */
    public Object accept(TargetMetaRequest targetMetaRequest) {
        Debug.logVerbose("enter Proxy.newProxyInstance ", module);
        Object dynamicProxy = null;
        try {          
          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
     
          DynamicProxyWeaving dynamicProxyWeaving = new DynamicProxyWeaving(targetMetaRequest, aopClient);
          dynamicProxy =
              Proxy.newProxyInstance(
              classLoader,
              getInterfaces(targetMetaRequest.getTargetMetaDef()),
              dynamicProxyWeaving);
        } catch (Exception ex) {
          Debug.logError(" Proxy.newProxyInstance error:" + ex, module);
        } catch (Throwable ex) {
          Debug.logError(" Proxy.newProxyInstance error:" + ex, module);
        }
        return dynamicProxy;
    }

    /**
     * get the interface of target class
     * if it is EJB, it is ejb local/remote interface
     * if it is pojo, it is a class .
     * 
     * 
     * @param targetMetaDef
     * @return
     */
    private Class[] getInterfaces(TargetMetaDef targetMetaDef){
        Class[] serviceClassses = null;
        try {          
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            Class configClasses = classLoader.loadClass(targetMetaDef.getClassName());
            if (targetMetaDef.isEJB()){
                EJBTargetMetaDef em = (EJBTargetMetaDef)targetMetaDef;
                if (em.getInterfaceClass() != null){
                    serviceClassses = new Class[] {configClasses, classLoader.loadClass(em.getInterfaceClass())};
                }else
                    serviceClassses = new Class[] {configClasses};
            }else
                serviceClassses = getAllInterfaces(configClasses);
                    
          } catch (Exception ex) {
            Debug.logError(" getInterfaces error:" + ex, module);
          } catch (Throwable ex) {
            Debug.logError(" getInterfaces error:" + ex, module);
          }
       if ((serviceClassses == null) || (serviceClassses.length == 0)){
           Debug.logError(" no find any interface for the service:" + targetMetaDef.getClassName(), module);
       }else{
           Debug.logVerbose("found the the below interfaces for the service:"+ targetMetaDef.getClassName(), module);
           for(int i=0; i<serviceClassses.length; i++){               
               Debug.logVerbose(serviceClassses[i].getName() + ";", module);               
           }
       }
       return serviceClassses;
    }

	
    public Class[] getAllInterfaces(Class configClasses) {
    		List interfaces = new ArrayList();    	
    		while (configClasses != null) {
    			for (int i = 0; i < configClasses.getInterfaces().length; i++) {
    				Class ifc = configClasses.getInterfaces()[i];
    				interfaces.add(ifc);
    			}
    			configClasses = configClasses.getSuperclass();
    		}    		
    		return (Class[])interfaces.toArray(new Class[interfaces.size()]);
    }    



}

⌨️ 快捷键说明

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