001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.corba;
017:
018: import java.lang.reflect.Method;
019: import java.rmi.AccessException;
020: import java.rmi.MarshalException;
021: import java.rmi.NoSuchObjectException;
022: import java.rmi.Remote;
023: import java.rmi.RemoteException;
024: import javax.transaction.InvalidTransactionException;
025: import javax.transaction.TransactionRequiredException;
026: import javax.transaction.TransactionRolledbackException;
027:
028: import net.sf.cglib.proxy.Callback;
029: import net.sf.cglib.proxy.CallbackFilter;
030: import net.sf.cglib.proxy.Enhancer;
031: import net.sf.cglib.proxy.MethodInterceptor;
032: import net.sf.cglib.proxy.MethodProxy;
033: import net.sf.cglib.proxy.NoOp;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.omg.CORBA.INVALID_TRANSACTION;
037: import org.omg.CORBA.MARSHAL;
038: import org.omg.CORBA.NO_PERMISSION;
039: import org.omg.CORBA.OBJECT_NOT_EXIST;
040: import org.omg.CORBA.TRANSACTION_REQUIRED;
041: import org.omg.CORBA.TRANSACTION_ROLLEDBACK;
042: import org.omg.CORBA.UNKNOWN;
043:
044: /**
045: * @version $Revision: 465108 $ $Date: 2006-10-17 17:23:40 -0700 (Tue, 17 Oct 2006) $
046: */
047: public class AdapterProxyFactory {
048:
049: private final static Log log = LogFactory
050: .getLog(AdapterProxyFactory.class);
051: private final static AdapterMethodInterceptor interceptor = new AdapterMethodInterceptor();
052: private final Enhancer enhancer;
053:
054: public AdapterProxyFactory(Class clientInterface) {
055: this (clientInterface, clientInterface.getClassLoader());
056: }
057:
058: public AdapterProxyFactory(Class clientInterface,
059: ClassLoader classLoader) {
060: this (new Class[] { clientInterface }, classLoader);
061: }
062:
063: public AdapterProxyFactory(Class[] clientInterfaces,
064: ClassLoader classLoader) {
065: assert clientInterfaces != null;
066: assert areAllInterfaces(clientInterfaces);
067:
068: enhancer = new Enhancer();
069: enhancer.setClassLoader(classLoader);
070: enhancer.setSuperclass(AdapterDelegate.class);
071: enhancer.setInterfaces(clientInterfaces);
072: enhancer.setCallbackTypes(new Class[] { NoOp.class,
073: MethodInterceptor.class });
074: enhancer.setCallbackFilter(FILTER);
075: enhancer.setUseFactory(false);
076: }
077:
078: private static boolean areAllInterfaces(Class[] clientInterfaces) {
079: for (int i = 0; i < clientInterfaces.length; i++) {
080: if (clientInterfaces[i] == null
081: || !clientInterfaces[i].isInterface()) {
082: return false;
083: }
084: }
085: return true;
086: }
087:
088: public Object create(Remote delegate, ClassLoader classLoader) {
089: return create(new Class[] { Remote.class, ClassLoader.class },
090: new Object[] { delegate, classLoader });
091: }
092:
093: public synchronized Object create(Class[] types, Object[] arguments) {
094: enhancer.setCallbacks(new Callback[] { NoOp.INSTANCE,
095: interceptor });
096: return enhancer.create(types, arguments);
097: }
098:
099: private final static class AdapterMethodInterceptor implements
100: MethodInterceptor {
101:
102: public final Object intercept(Object o, Method method,
103: Object[] args, MethodProxy methodProxy)
104: throws Throwable {
105: ClassLoader savedCL = Thread.currentThread()
106: .getContextClassLoader();
107: try {
108: AdapterDelegate delegate = (AdapterDelegate) o;
109: Thread.currentThread().setContextClassLoader(
110: delegate.getClassLoader());
111:
112: if (log.isDebugEnabled())
113: log.debug("Calling " + method.getName());
114:
115: return methodProxy.invoke(delegate.getDelegate(), args);
116: } catch (TransactionRolledbackException e) {
117: log.debug("TransactionRolledbackException", e);
118: throw new TRANSACTION_ROLLEDBACK(e.toString())
119: .initCause(e);
120: } catch (TransactionRequiredException e) {
121: log.debug("TransactionRequiredException", e);
122: throw new TRANSACTION_REQUIRED(e.toString())
123: .initCause(e);
124: } catch (InvalidTransactionException e) {
125: log.debug("InvalidTransactionException", e);
126: throw new INVALID_TRANSACTION(e.toString())
127: .initCause(e);
128: } catch (NoSuchObjectException e) {
129: log.debug("NoSuchObjectException", e);
130: throw new OBJECT_NOT_EXIST(e.toString()).initCause(e);
131: } catch (AccessException e) {
132: log.debug("AccessException", e);
133: throw new NO_PERMISSION(e.toString()).initCause(e);
134: } catch (MarshalException e) {
135: log.debug("MarshalException", e);
136: throw new MARSHAL(e.toString()).initCause(e);
137: } catch (RemoteException e) {
138: log.debug("RemoteException", e);
139: throw new UNKNOWN(e.toString()).initCause(e);
140: } finally {
141: Thread.currentThread().setContextClassLoader(savedCL);
142: }
143: }
144: }
145:
146: private static final CallbackFilter FILTER = new CallbackFilter() {
147: public int accept(Method method) {
148: if (method.getName().equals("finalize")
149: && method.getParameterTypes().length == 0
150: && method.getReturnType() == Void.TYPE) {
151: return 0;
152: }
153: return 1;
154: }
155: };
156: }
|