KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > ProxyUtils


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import javax.ejb.EJBHome JavaDoc;
27 import javax.ejb.EJBObject JavaDoc;
28 import org.jboss.aop.advice.Interceptor;
29 import org.jboss.aop.joinpoint.MethodInvocation;
30 import org.jboss.aop.util.MethodHashing;
31 import org.jboss.aop.util.PayloadKey;
32 import org.jboss.aspects.asynch.AsynchMixin;
33 import org.jboss.aspects.asynch.AsynchProvider;
34 import org.jboss.aspects.asynch.AsynchProxyInterceptor;
35 import org.jboss.aspects.asynch.FutureHolder;
36 import org.jboss.ejb3.asynchronous.AsynchronousInterceptor;
37 import org.jboss.ejb3.remoting.Proxy;
38
39 /**
40  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
41  * @version $Revision: 43261 $
42  */

43 public class ProxyUtils
44 {
45    public final static Class JavaDoc ASYNCH_PROVIDER_CLASS = AsynchProvider.class;
46    public final static long GET_ASYNCHRONOUS;
47    public static final long TO_STRING;
48    public static final long EQUALS;
49    public static final long HASHCODE;
50    public static final Method JavaDoc GET_PRIMARY_KEY;
51    public static final Method JavaDoc GET_HANDLE;
52    public static final Method JavaDoc GET_EJB_HOME;
53    public static final Method JavaDoc IS_IDENTICAL;
54    public static final Method JavaDoc GET_HOME_HANDLE;
55    public static final Method JavaDoc GET_EJB_METADATA;
56    public static final Method JavaDoc REMOVE;
57
58    static
59    {
60       try
61       {
62          Class JavaDoc[] empty = {};
63          
64          Method JavaDoc method = JBossProxy.class.getMethod("getAsynchronousProxy", empty);
65          GET_ASYNCHRONOUS = MethodHashing.calculateHash(method);
66          TO_STRING = MethodHashing.calculateHash(Object JavaDoc.class.getDeclaredMethod("toString", empty));
67          EQUALS = MethodHashing.calculateHash(Object JavaDoc.class.getDeclaredMethod("equals", new Class JavaDoc[]{Object JavaDoc.class}));
68          HASHCODE = MethodHashing.calculateHash(Object JavaDoc.class.getDeclaredMethod("hashCode", empty));
69                
70          GET_PRIMARY_KEY = EJBObject JavaDoc.class.getMethod("getPrimaryKey", empty);
71          GET_HANDLE = EJBObject JavaDoc.class.getMethod("getHandle", empty);
72          GET_EJB_HOME = EJBObject JavaDoc.class.getMethod("getEJBHome", empty);
73          IS_IDENTICAL = EJBObject JavaDoc.class.getMethod("isIdentical", new Class JavaDoc[] { EJBObject JavaDoc.class });
74          REMOVE = EJBObject JavaDoc.class.getMethod("remove", empty);
75                  
76          GET_HOME_HANDLE = EJBHome JavaDoc.class.getMethod("getHomeHandle", empty);
77          GET_EJB_METADATA = EJBHome JavaDoc.class.getMethod("getEJBMetaData", empty);
78       }
79       catch (NoSuchMethodException JavaDoc e)
80       {
81          throw new RuntimeException JavaDoc(e);
82       }
83    }
84
85    public static boolean isAsynchronous(Class JavaDoc[] infs)
86    {
87       for (int i = 0; i < infs.length; i++)
88       {
89          if (infs[i] == ASYNCH_PROVIDER_CLASS)
90          {
91             return true;
92          }
93       }
94       return false;
95    }
96
97    public static Class JavaDoc[] addAsynchProviderInterface(Class JavaDoc[] infs)
98    {
99       ArrayList JavaDoc interfaces = new ArrayList JavaDoc();
100
101       for (int i = 0; i < infs.length; i++)
102       {
103          if (infs[i] == ASYNCH_PROVIDER_CLASS)
104          {
105             //This should not happen
106
continue;
107          }
108          interfaces.add(infs[i]);
109       }
110
111       interfaces.add(ASYNCH_PROVIDER_CLASS);
112       return (Class JavaDoc[]) interfaces.toArray(new Class JavaDoc[interfaces.size()]);
113    }
114
115    public static Interceptor[] addAsynchProxyInterceptor(AsynchMixin mixin, Interceptor[] interceptors)
116    {
117       AsynchProxyInterceptor interceptor = new AsynchProxyInterceptor(mixin);
118       Interceptor[] newInterceptors = null;
119       newInterceptors = new Interceptor[interceptors.length + 1];
120       newInterceptors[0] = interceptor;
121       System.arraycopy(interceptors, 0, newInterceptors, 1, interceptors.length);
122       return newInterceptors;
123    }
124
125    public static void addLocalAsynchronousInfo(MethodInvocation invocation, FutureHolder provider)
126    {
127       if (provider != null)
128       {
129          invocation.getMetaData().addMetaData(AsynchronousInterceptor.ASYNCH, AsynchronousInterceptor.INVOKE_ASYNCH, "YES", PayloadKey.AS_IS);
130          invocation.getMetaData().addMetaData(AsynchronousInterceptor.ASYNCH, AsynchronousInterceptor.FUTURE_HOLDER, provider, PayloadKey.AS_IS);
131       }
132    }
133
134    public static Object JavaDoc handleCallLocally(Object JavaDoc jbproxy, Proxy ih, Method JavaDoc m, Object JavaDoc[] args)
135    {
136       long hash = MethodHashing.calculateHash(m);
137       return handleCallLocally(hash, jbproxy, ih, m, args);
138    }
139
140    public static Object JavaDoc handleCallLocally(long hash, Object JavaDoc jbproxy, Proxy ih, Method JavaDoc m, Object JavaDoc[] args)
141    {
142       if (hash == ProxyUtils.GET_ASYNCHRONOUS)
143       {
144          return ih.getAsynchronousProxy((JBossProxy)jbproxy);
145       }
146       else if (hash == TO_STRING)
147       {
148          return ih.toString();
149       }
150       else if (hash == HASHCODE)
151       {
152          return new Integer JavaDoc(ih.toString().hashCode());
153       }
154       else if (hash == EQUALS)
155       {
156          return new Boolean JavaDoc(ih.toString().equals(args[0].toString()));
157       }
158       return null;
159    }
160 }
161
Popular Tags