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;
25 import java.lang.reflect.Method;
26 import javax.ejb.EJBHome;
27 import javax.ejb.EJBObject;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
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
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 TO_STRING;
52 protected static final Method HASH_CODE;
53 protected static final Method EQUALS;
54 protected static final Method GET_PRIMARY_KEY;
55 protected static final Method GET_HANDLE;
56 protected static final Method GET_EJB_HOME;
57 protected static final Method IS_IDENTICAL;
58
59 /** Initialize the static variables. */
60 static
61 {
62 try
63 {
64 // Get the methods from Object
65 Class[] empty = {};
66 Class type = Object.class;
67
68 TO_STRING = type.getMethod("toString", empty);
69 HASH_CODE = type.getMethod("hashCode", empty);
70 EQUALS = type.getMethod("equals", new Class[] { type });
71
72 // Get the methods from EJBObject
73 type = EJBObject.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[] { type });
79 }
80 catch (Exception e)
81 {
82 e.printStackTrace();
83 throw new ExceptionInInitializerError(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 getEJBHome(Invocation invocation) throws NamingException
96 {
97 // Look to the context for the home
98 InvocationContext ctx = invocation.getInvocationContext();
99 EJBHome home = (EJBHome) ctx.getValue(InvocationKey.EJB_HOME);
100 // If there is no home use the legacy lookup method
101 if( home == null )
102 {
103 String jndiName = (String) ctx.getValue(InvocationKey.JNDI_NAME);
104 InitialContext iniCtx = new InitialContext();
105 home = (EJBHome) iniCtx.lookup(jndiName);
106 }
107 return home;
108 }
109 }
110