001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3;
023:
024: import java.lang.reflect.Method;
025: import java.util.ArrayList;
026: import javax.ejb.EJBHome;
027: import javax.ejb.EJBObject;
028: import org.jboss.aop.advice.Interceptor;
029: import org.jboss.aop.joinpoint.MethodInvocation;
030: import org.jboss.aop.util.MethodHashing;
031: import org.jboss.aop.util.PayloadKey;
032: import org.jboss.aspects.asynch.AsynchMixin;
033: import org.jboss.aspects.asynch.AsynchProvider;
034: import org.jboss.aspects.asynch.AsynchProxyInterceptor;
035: import org.jboss.aspects.asynch.FutureHolder;
036: import org.jboss.ejb3.asynchronous.AsynchronousInterceptor;
037: import org.jboss.ejb3.remoting.Proxy;
038:
039: /**
040: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
041: * @version $Revision: 60233 $
042: */
043: public class ProxyUtils {
044: public final static Class ASYNCH_PROVIDER_CLASS = AsynchProvider.class;
045: public final static long GET_ASYNCHRONOUS;
046: public static final long TO_STRING;
047: public static final long EQUALS;
048: public static final long HASHCODE;
049: public static final Method GET_PRIMARY_KEY;
050: public static final Method GET_HANDLE;
051: public static final Method GET_EJB_HOME;
052: public static final Method IS_IDENTICAL;
053: public static final Method GET_HOME_HANDLE;
054: public static final Method GET_EJB_METADATA;
055: public static final Method REMOVE;
056:
057: static {
058: try {
059: Class[] empty = {};
060:
061: Method method = JBossProxy.class.getMethod(
062: "getAsynchronousProxy", empty);
063: GET_ASYNCHRONOUS = MethodHashing.calculateHash(method);
064: TO_STRING = MethodHashing.calculateHash(Object.class
065: .getDeclaredMethod("toString", empty));
066: EQUALS = MethodHashing.calculateHash(Object.class
067: .getDeclaredMethod("equals",
068: new Class[] { Object.class }));
069: HASHCODE = MethodHashing.calculateHash(Object.class
070: .getDeclaredMethod("hashCode", empty));
071:
072: GET_PRIMARY_KEY = EJBObject.class.getMethod(
073: "getPrimaryKey", empty);
074: GET_HANDLE = EJBObject.class.getMethod("getHandle", empty);
075: GET_EJB_HOME = EJBObject.class.getMethod("getEJBHome",
076: empty);
077: IS_IDENTICAL = EJBObject.class.getMethod("isIdentical",
078: new Class[] { EJBObject.class });
079: REMOVE = EJBObject.class.getMethod("remove", empty);
080:
081: GET_HOME_HANDLE = EJBHome.class.getMethod("getHomeHandle",
082: empty);
083: GET_EJB_METADATA = EJBHome.class.getMethod(
084: "getEJBMetaData", empty);
085: } catch (NoSuchMethodException e) {
086: throw new RuntimeException(e);
087: }
088: }
089:
090: public static boolean isAsynchronous(Class[] infs) {
091: for (int i = 0; i < infs.length; i++) {
092: if (infs[i] == ASYNCH_PROVIDER_CLASS) {
093: return true;
094: }
095: }
096: return false;
097: }
098:
099: public static Class[] addAsynchProviderInterface(Class[] infs) {
100: ArrayList interfaces = new ArrayList();
101:
102: for (int i = 0; i < infs.length; i++) {
103: if (infs[i] == ASYNCH_PROVIDER_CLASS) {
104: //This should not happen
105: continue;
106: }
107: interfaces.add(infs[i]);
108: }
109:
110: interfaces.add(ASYNCH_PROVIDER_CLASS);
111: return (Class[]) interfaces
112: .toArray(new Class[interfaces.size()]);
113: }
114:
115: public static Interceptor[] addAsynchProxyInterceptor(
116: AsynchMixin mixin, Interceptor[] interceptors) {
117: AsynchProxyInterceptor interceptor = new AsynchProxyInterceptor(
118: mixin);
119: Interceptor[] newInterceptors = null;
120: newInterceptors = new Interceptor[interceptors.length + 1];
121: newInterceptors[0] = interceptor;
122: System.arraycopy(interceptors, 0, newInterceptors, 1,
123: interceptors.length);
124: return newInterceptors;
125: }
126:
127: public static void addLocalAsynchronousInfo(
128: MethodInvocation invocation, FutureHolder provider) {
129: if (provider != null) {
130: invocation.getMetaData().addMetaData(
131: AsynchronousInterceptor.ASYNCH,
132: AsynchronousInterceptor.INVOKE_ASYNCH, "YES",
133: PayloadKey.AS_IS);
134: invocation.getMetaData().addMetaData(
135: AsynchronousInterceptor.ASYNCH,
136: AsynchronousInterceptor.FUTURE_HOLDER, provider,
137: PayloadKey.AS_IS);
138: }
139: }
140:
141: public static Object handleCallLocally(Object jbproxy, Proxy ih,
142: Method m, Object[] args) {
143: long hash = MethodHashing.calculateHash(m);
144: return handleCallLocally(hash, jbproxy, ih, m, args);
145: }
146:
147: public static Object handleCallLocally(long hash, Object jbproxy,
148: Proxy ih, Method m, Object[] args) {
149: if (hash == ProxyUtils.GET_ASYNCHRONOUS) {
150: return ih.getAsynchronousProxy((JBossProxy) jbproxy);
151: } else if (hash == TO_STRING) {
152: return ih.toString();
153: } else if (hash == HASHCODE) {
154: return new Integer(ih.toString().hashCode());
155: } else if (hash == EQUALS) {
156: return new Boolean(ih.toString().equals(args[0].toString()));
157: }
158: return null;
159: }
160: }
|