KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > ejb > HomeInterceptor


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.proxy.ejb;
23
24 import java.lang.reflect.Method JavaDoc;
25
26 import javax.ejb.EJBHome JavaDoc;
27 import javax.ejb.EJBMetaData JavaDoc;
28 import javax.ejb.RemoveException JavaDoc;
29 import javax.ejb.Handle JavaDoc;
30 import javax.ejb.EJBHome JavaDoc;
31 import javax.ejb.EJBObject JavaDoc;
32 import javax.ejb.HomeHandle JavaDoc;
33
34 import org.jboss.proxy.ejb.handle.HomeHandleImpl;
35 import org.jboss.invocation.Invocation;
36 import org.jboss.invocation.InvocationContext;
37 import org.jboss.invocation.InvocationKey;
38 import org.jboss.invocation.InvocationType;
39
40 /**
41  * The client-side proxy for an EJB Home object.
42  *
43  * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
44  * @version $Revision: 37459 $
45  */

46 public class HomeInterceptor
47    extends GenericEJBInterceptor
48 {
49    /** Serial Version Identifier. @since 1.6 */
50    private static final long serialVersionUID = 1333656107035759718L;
51
52    // Static --------------------------------------------------------
53

54    protected static final Object JavaDoc[] EMPTY_ARGS = {};
55    
56    /** {@link EJBHome#getEJBMetaData} method reference. */
57    protected static final Method JavaDoc GET_EJB_META_DATA;
58    
59    /** {@link EJBHome#getHomeHandle} method reference. */
60    protected static final Method JavaDoc GET_HOME_HANDLE;
61    
62    /** {@link EJBHome#remove(Handle)} method reference. */
63    protected static final Method JavaDoc REMOVE_BY_HANDLE;
64    
65    /** {@link EJBHome#remove(Object)} method reference. */
66    protected static final Method JavaDoc REMOVE_BY_PRIMARY_KEY;
67    
68    /** {@link EJBObject#remove} method reference. */
69    protected static final Method JavaDoc REMOVE_OBJECT;
70    
71    static
72    {
73       try
74       {
75          final Class JavaDoc empty[] = {};
76          final Class JavaDoc type = EJBHome JavaDoc.class;
77          
78          GET_EJB_META_DATA = type.getMethod("getEJBMetaData", empty);
79          GET_HOME_HANDLE = type.getMethod("getHomeHandle", empty);
80          REMOVE_BY_HANDLE = type.getMethod("remove", new Class JavaDoc[] {
81                Handle JavaDoc.class
82             });
83          REMOVE_BY_PRIMARY_KEY = type.getMethod("remove", new Class JavaDoc[] {
84                Object JavaDoc.class
85             });
86          
87          // Get the "remove" method from the EJBObject
88
REMOVE_OBJECT = EJBObject JavaDoc.class.getMethod("remove", empty);
89       }
90       catch (Exception JavaDoc e) {
91          e.printStackTrace();
92          throw new ExceptionInInitializerError JavaDoc(e);
93       }
94    }
95    
96    // Attributes ----------------------------------------------------
97

98    // Constructors --------------------------------------------------
99

100    /**
101    * No-argument constructor for externalization.
102    */

103    public HomeInterceptor() {}
104    
105    // Public --------------------------------------------------------
106

107    /**
108    * InvocationHandler implementation.
109    *
110    * @throws Throwable Any exception or error thrown while processing.
111    */

112    public Object JavaDoc invoke(Invocation invocation)
113       throws Throwable JavaDoc
114    {
115       InvocationContext ctx = invocation.getInvocationContext();
116       
117       Method JavaDoc m = invocation.getMethod();
118       
119       // Implement local methods
120
if (m.equals(TO_STRING))
121       {
122          return ctx.getValue(InvocationKey.JNDI_NAME).toString() + "Home";
123       }
124       else if (m.equals(EQUALS))
125       {
126          // equality of the proxy home is based on names...
127
Object JavaDoc[] args = invocation.getArguments();
128          String JavaDoc argsString = args[0] != null ? args[0].toString() : "";
129          String JavaDoc thisString = ctx.getValue(InvocationKey.JNDI_NAME).toString() + "Home";
130          return new Boolean JavaDoc(thisString.equals(argsString));
131       }
132       else if (m.equals(HASH_CODE))
133       {
134          return new Integer JavaDoc(this.hashCode());
135       }
136       
137       // Implement local EJB calls
138
else if (m.equals(GET_HOME_HANDLE))
139       {
140          return new HomeHandleImpl(
141          (String JavaDoc)ctx.getValue(InvocationKey.JNDI_NAME));
142       }
143       else if (m.equals(GET_EJB_META_DATA))
144       {
145          return ctx.getValue(InvocationKey.EJB_METADATA);
146       }
147       else if (m.equals(REMOVE_BY_HANDLE))
148       {
149          // First get the EJBObject
150
EJBObject JavaDoc object =
151          ((Handle JavaDoc) invocation.getArguments()[0]).getEJBObject();
152          
153          // remove the object from here
154
object.remove();
155          
156          // Return Void
157
return Void.TYPE;
158       }
159       else if (m.equals(REMOVE_BY_PRIMARY_KEY))
160       {
161          // Session beans must throw RemoveException (EJB 1.1, 5.3.2)
162
if(((EJBMetaData JavaDoc)ctx.getValue(InvocationKey.EJB_METADATA)).isSession())
163             throw new RemoveException JavaDoc("Session beans cannot be removed " +
164             "by primary key.");
165          
166          // The trick is simple we trick the container in believe it
167
// is a remove() on the instance
168
Object JavaDoc id = invocation.getArguments()[0];
169          
170          // Just override the Invocation going out
171
invocation.setId(id);
172          invocation.setType(InvocationType.REMOTE);
173          invocation.setMethod(REMOVE_OBJECT);
174          invocation.setArguments(EMPTY_ARGS);
175          return getNext().invoke(invocation);
176       }
177       
178       // If not taken care of, go on and call the container
179
else
180       {
181          
182          invocation.setType(InvocationType.HOME);
183          // Create an Invocation
184
return getNext().invoke(invocation);
185       }
186    }
187 }
188
Popular Tags