KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Externalizable JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import javax.ejb.EJBHome JavaDoc;
27 import javax.ejb.EJBObject JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30
31 import org.jboss.invocation.Invocation;
32 import org.jboss.invocation.InvocationKey;
33 import org.jboss.invocation.InvocationContext;
34 import org.jboss.proxy.Interceptor;
35
36 /**
37  * The base EJB behavior interceptor.
38  *
39  * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
40  * @author Scott.Stark@jboss.org
41  * @version $Revision: 37459 $
42  */

43 public abstract class GenericEJBInterceptor
44    extends Interceptor
45    implements Externalizable JavaDoc
46 {
47    /** Serial Version Identifier. @since 1.3 */
48    private static final long serialVersionUID = 3844706474734439975L;
49
50    // Static method references
51
protected static final Method JavaDoc TO_STRING;
52    protected static final Method JavaDoc HASH_CODE;
53    protected static final Method JavaDoc EQUALS;
54    protected static final Method JavaDoc GET_PRIMARY_KEY;
55    protected static final Method JavaDoc GET_HANDLE;
56    protected static final Method JavaDoc GET_EJB_HOME;
57    protected static final Method JavaDoc IS_IDENTICAL;
58    
59    /** Initialize the static variables. */
60    static
61    {
62       try
63       {
64          // Get the methods from Object
65
Class JavaDoc[] empty = {};
66          Class JavaDoc type = Object JavaDoc.class;
67          
68          TO_STRING = type.getMethod("toString", empty);
69          HASH_CODE = type.getMethod("hashCode", empty);
70          EQUALS = type.getMethod("equals", new Class JavaDoc[] { type });
71          
72          // Get the methods from EJBObject
73
type = EJBObject JavaDoc.class;
74          
75          GET_PRIMARY_KEY = type.getMethod("getPrimaryKey", empty);
76          GET_HANDLE = type.getMethod("getHandle", empty);
77          GET_EJB_HOME = type.getMethod("getEJBHome", empty);
78          IS_IDENTICAL = type.getMethod("isIdentical", new Class JavaDoc[] { type });
79       }
80       catch (Exception JavaDoc e)
81       {
82          e.printStackTrace();
83          throw new ExceptionInInitializerError JavaDoc(e);
84       }
85    }
86    
87    /**
88     * A public, no-args constructor for externalization to work.
89     */

90    public GenericEJBInterceptor()
91    {
92       // For externalization to work
93
}
94    
95    protected EJBHome JavaDoc getEJBHome(Invocation invocation) throws NamingException JavaDoc
96    {
97       // Look to the context for the home
98
InvocationContext ctx = invocation.getInvocationContext();
99       EJBHome JavaDoc home = (EJBHome JavaDoc) ctx.getValue(InvocationKey.EJB_HOME);
100       // If there is no home use the legacy lookup method
101
if( home == null )
102       {
103          String JavaDoc jndiName = (String JavaDoc) ctx.getValue(InvocationKey.JNDI_NAME);
104          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
105          home = (EJBHome JavaDoc) iniCtx.lookup(jndiName);
106       }
107       return home;
108    }
109 }
110   
111
Popular Tags