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.util;
017:
018: import java.rmi.AccessException;
019: import java.rmi.MarshalException;
020: import java.rmi.NoSuchObjectException;
021: import java.rmi.Remote;
022: import java.rmi.RemoteException;
023: import java.lang.reflect.Proxy;
024: import java.lang.reflect.InvocationHandler;
025: import javax.rmi.CORBA.Stub;
026: import javax.rmi.CORBA.Tie;
027: import javax.rmi.CORBA.UtilDelegate;
028: import javax.rmi.CORBA.ValueHandler;
029: import javax.transaction.InvalidTransactionException;
030: import javax.transaction.TransactionRequiredException;
031: import javax.transaction.TransactionRolledbackException;
032: import javax.ejb.EJBHome;
033: import javax.ejb.EJBObject;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037: import org.omg.CORBA.CompletionStatus;
038: import org.omg.CORBA.INVALID_TRANSACTION;
039: import org.omg.CORBA.MARSHAL;
040: import org.omg.CORBA.NO_PERMISSION;
041: import org.omg.CORBA.OBJECT_NOT_EXIST;
042: import org.omg.CORBA.ORB;
043: import org.omg.CORBA.SystemException;
044: import org.omg.CORBA.TRANSACTION_REQUIRED;
045: import org.omg.CORBA.TRANSACTION_ROLLEDBACK;
046: import org.omg.CORBA.UNKNOWN;
047: import org.omg.CORBA.portable.InputStream;
048: import org.omg.CORBA.portable.OutputStream;
049:
050: import org.apache.geronimo.corba.AdapterWrapper;
051: import org.apache.geronimo.corba.CORBAException;
052: import org.apache.geronimo.corba.RefGenerator;
053: import org.apache.geronimo.corba.StandardServant;
054: import org.apache.openejb.InterfaceType;
055: import org.apache.openejb.BeanType;
056: import org.apache.openejb.core.ivm.BaseEjbProxyHandler;
057: import org.apache.openejb.core.CoreDeploymentInfo;
058:
059: /**
060: * @version $Revision: 451417 $ $Date: 2006-09-29 13:13:22 -0700 (Fri, 29 Sep 2006) $
061: */
062: public final class UtilDelegateImpl implements UtilDelegate {
063:
064: private final Log log = LogFactory.getLog(UtilDelegateImpl.class);
065: private final UtilDelegate delegate;
066: private static ClassLoader classLoader;
067:
068: private final static String DELEGATE_NAME = "org.apache.geronimo.corba.UtilDelegateClass";
069:
070: public UtilDelegateImpl() throws ClassNotFoundException,
071: IllegalAccessException, InstantiationException {
072: String value = System.getProperty(DELEGATE_NAME);
073: if (value == null) {
074: log.error("No delegate specfied via " + DELEGATE_NAME);
075: throw new IllegalStateException("The property "
076: + DELEGATE_NAME + " must be defined!");
077: }
078:
079: if (log.isDebugEnabled())
080: log.debug("Set delegate " + value);
081: delegate = (UtilDelegate) Class.forName(value).newInstance();
082: }
083:
084: static void setClassLoader(ClassLoader classLoader) {
085: UtilDelegateImpl.classLoader = classLoader;
086: }
087:
088: public void unexportObject(Remote target)
089: throws NoSuchObjectException {
090: delegate.unexportObject(target);
091: }
092:
093: public boolean isLocal(Stub stub) throws RemoteException {
094: return delegate.isLocal(stub);
095: }
096:
097: public ValueHandler createValueHandler() {
098: return delegate.createValueHandler();
099: }
100:
101: public Object readAny(InputStream in) {
102: return delegate.readAny(in);
103: }
104:
105: public void writeAbstractObject(OutputStream out, Object obj) {
106: delegate.writeAbstractObject(out, obj);
107: }
108:
109: public void writeAny(OutputStream out, Object obj) {
110: delegate.writeAny(out, obj);
111: }
112:
113: public void writeRemoteObject(OutputStream out, Object obj) {
114: try {
115: if (obj instanceof Tie
116: && ((Tie) obj).getTarget() instanceof Proxy) {
117: obj = ((Tie) obj).getTarget();
118: }
119: if (obj instanceof Proxy) {
120: obj = convertEJBToCORBAObject((Proxy) obj);
121: }
122: if (obj instanceof StandardServant) {
123: StandardServant servant = (StandardServant) obj;
124: InterfaceType servantType = servant.getInterfaceType();
125: String deploymentId = servant.getEjbDeployment()
126: .getDeploymentId();
127: try {
128: RefGenerator refGenerator = AdapterWrapper
129: .getRefGenerator(deploymentId);
130: if (refGenerator == null) {
131: throw new MARSHAL(
132: "Could not find RefGenerator for deployment id: "
133: + deploymentId);
134: }
135: if (InterfaceType.EJB_HOME == servantType) {
136: obj = refGenerator.genHomeReference();
137: } else if (InterfaceType.EJB_OBJECT == servantType) {
138: obj = refGenerator.genObjectReference(servant
139: .getPrimaryKey());
140: } else {
141: log
142: .error("Encountered unknown local invocation handler of type "
143: + servantType
144: + ":"
145: + deploymentId);
146: throw new MARSHAL(
147: "Internal server error while marshaling the reply",
148: 0, CompletionStatus.COMPLETED_YES);
149: }
150: } catch (CORBAException e) {
151: log
152: .error("Encountered unknown local invocation handler of type "
153: + servantType + ":" + deploymentId);
154: throw (MARSHAL) new MARSHAL(
155: "Internal server error while marshaling the reply",
156: 0, CompletionStatus.COMPLETED_YES)
157: .initCause(e);
158: }
159: }
160: delegate.writeRemoteObject(out, obj);
161: } catch (Throwable e) {
162: log
163: .error(
164: "Received unexpected exception while marshaling an object reference:",
165: e);
166: throw (MARSHAL) new MARSHAL(
167: "Internal server error while marshaling the reply",
168: 0, CompletionStatus.COMPLETED_YES).initCause(e);
169: }
170: }
171:
172: public String getCodebase(Class clz) {
173: return delegate.getCodebase(clz);
174: }
175:
176: public void registerTarget(Tie tie, Remote target) {
177: delegate.registerTarget(tie, target);
178: }
179:
180: public RemoteException wrapException(Throwable obj) {
181: return delegate.wrapException(obj);
182: }
183:
184: public RemoteException mapSystemException(SystemException ex) {
185: if (ex instanceof TRANSACTION_ROLLEDBACK) {
186: TransactionRolledbackException transactionRolledbackException = new TransactionRolledbackException(
187: ex.getMessage());
188: transactionRolledbackException.detail = ex;
189: return transactionRolledbackException;
190: }
191: if (ex instanceof TRANSACTION_REQUIRED) {
192: TransactionRequiredException transactionRequiredException = new TransactionRequiredException(
193: ex.getMessage());
194: transactionRequiredException.detail = ex;
195: return transactionRequiredException;
196: }
197: if (ex instanceof INVALID_TRANSACTION) {
198: InvalidTransactionException invalidTransactionException = new InvalidTransactionException(
199: ex.getMessage());
200: invalidTransactionException.detail = ex;
201: return invalidTransactionException;
202: }
203: if (ex instanceof OBJECT_NOT_EXIST) {
204: NoSuchObjectException noSuchObjectException = new NoSuchObjectException(
205: ex.getMessage());
206: noSuchObjectException.detail = ex;
207: return noSuchObjectException;
208: }
209: if (ex instanceof NO_PERMISSION) {
210: return new AccessException(ex.getMessage(), ex);
211: }
212: if (ex instanceof MARSHAL) {
213: return new MarshalException(ex.getMessage(), ex);
214: }
215: if (ex instanceof UNKNOWN) {
216: return new RemoteException(ex.getMessage(), ex);
217: }
218: return delegate.mapSystemException(ex);
219: }
220:
221: public Tie getTie(Remote target) {
222: return delegate.getTie(target);
223: }
224:
225: public Object copyObject(Object obj, ORB orb)
226: throws RemoteException {
227: return delegate.copyObject(obj, orb);
228: }
229:
230: public Object[] copyObjects(Object[] obj, ORB orb)
231: throws RemoteException {
232: return delegate.copyObjects(obj, orb);
233: }
234:
235: public Class loadClass(String className, String remoteCodebase,
236: ClassLoader loader) throws ClassNotFoundException {
237: if (log.isDebugEnabled())
238: log.debug("Load class: " + className + ", "
239: + remoteCodebase + ", " + loader);
240:
241: Class result = null;
242: try {
243: result = delegate.loadClass(className, remoteCodebase,
244: loader);
245: } catch (ClassNotFoundException e) {
246: if (log.isDebugEnabled())
247: log.debug("Unable to load class from delegate");
248: }
249: if (result == null && classLoader != null) {
250: if (log.isDebugEnabled())
251: log.debug("Attempting to load " + className
252: + " from the static class loader");
253:
254: try {
255: result = classLoader.loadClass(className);
256: } catch (ClassNotFoundException e) {
257: if (log.isDebugEnabled())
258: log.debug("Unable to load " + className
259: + " from the static class loader");
260: throw e;
261: }
262:
263: if (log.isDebugEnabled())
264: log.debug("result: "
265: + (result == null ? "NULL" : result.getName()));
266: }
267:
268: return result;
269: }
270:
271: /**
272: * handle activation
273: */
274: private Object convertEJBToCORBAObject(Proxy proxy) {
275: InvocationHandler invocationHandler = Proxy
276: .getInvocationHandler(proxy);
277: if (!(invocationHandler instanceof BaseEjbProxyHandler)) {
278: return proxy;
279: }
280:
281: BaseEjbProxyHandler ejbProxyHandler = (BaseEjbProxyHandler) invocationHandler;
282: CoreDeploymentInfo deploymentInfo = ejbProxyHandler
283: .getDeploymentInfo();
284: String deploymentId = (String) deploymentInfo.getDeploymentID();
285: try {
286: RefGenerator refGenerator = AdapterWrapper
287: .getRefGenerator(deploymentId);
288: if (refGenerator == null) {
289: throw new MARSHAL(
290: "Could not find RefGenerator for deployment id: "
291: + deploymentId);
292: }
293: if (proxy instanceof EJBHome) {
294: return refGenerator.genHomeReference();
295: } else if (proxy instanceof EJBObject) {
296: Object primaryKey = null;
297: if (deploymentInfo.getComponentType() != BeanType.STATELESS) {
298: EJBObject ejbObject = (EJBObject) proxy;
299: primaryKey = ejbObject.getPrimaryKey();
300: }
301: return refGenerator.genObjectReference(primaryKey);
302: } else {
303: log
304: .error("Encountered unknown local invocation handler of type "
305: + proxy.getClass().getSuperclass()
306: + ":" + deploymentId);
307: throw new MARSHAL(
308: "Internal server error while marshaling the reply",
309: 0, CompletionStatus.COMPLETED_YES);
310: }
311: } catch (CORBAException e) {
312: log
313: .error("Encountered unknown local invocation handler of type "
314: + proxy.getClass().getSuperclass()
315: + ":"
316: + deploymentId);
317: throw (MARSHAL) new MARSHAL(
318: "Encountered unknown local invocation handler of type "
319: + proxy.getClass().getSuperclass() + ":"
320: + deploymentId, 0,
321: CompletionStatus.COMPLETED_YES).initCause(e);
322: } catch (RemoteException e) {
323: log
324: .error("Unable to get primary key from bean from bean of type "
325: + proxy.getClass().getSuperclass()
326: + ":"
327: + deploymentId);
328: throw (MARSHAL) new MARSHAL(
329: "Unable to get primary key from bean from bean of type "
330: + proxy.getClass().getSuperclass() + ":"
331: + deploymentId, 0,
332: CompletionStatus.COMPLETED_YES).initCause(e);
333: }
334: }
335: }
|