KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > local > LocalProxy


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.ejb.plugins.local;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import javax.ejb.EJBLocalObject JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31
32 /** Abstract superclass of local interface proxies.
33
34  @author <a HREF="mailto:docodan@mvcsoft.com">Daniel OConnor</a>
35  @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
36  @version $Revision: 37459 $
37  */

38 public abstract class LocalProxy implements Serializable JavaDoc
39 {
40    // Constants -----------------------------------------------------
41
static final long serialVersionUID = 8387750757101826407L;
42    // Attributes ----------------------------------------------------
43

44    // Static --------------------------------------------------------
45

46    /** An empty method parameter list. */
47    protected static final Object JavaDoc[] EMPTY_ARGS = {};
48
49    /** {@link Object#toString} method reference. */
50    protected static final Method JavaDoc TO_STRING;
51    
52    /** {@link Object#hashCode} method reference. */
53    protected static final Method JavaDoc HASH_CODE;
54    
55    /** {@link Object#equals} method reference. */
56    protected static final Method JavaDoc EQUALS;
57    
58    /** {@link EJBLocalObject#getPrimaryKey} method reference. */
59    protected static final Method JavaDoc GET_PRIMARY_KEY;
60    
61    /** {@link EJBLocalObject#getEJBLocalHome} method reference. */
62    protected static final Method JavaDoc GET_EJB_HOME;
63    
64    /** {@link EJBLocalObject#isIdentical} method reference. */
65    protected static final Method JavaDoc IS_IDENTICAL;
66
67    /** {@link EJBLocalObject#remove} method reference. */
68    protected static final Method JavaDoc REMOVE;
69
70    protected String JavaDoc jndiName;
71    protected transient BaseLocalProxyFactory factory;
72
73    /**
74     * Initialize {@link EJBLocalObject} method references.
75     */

76    static
77    {
78       try
79       {
80          final Class JavaDoc[] empty = {};
81          final Class JavaDoc type = EJBLocalObject JavaDoc.class;
82          
83          GET_PRIMARY_KEY = type.getMethod("getPrimaryKey", empty);
84          GET_EJB_HOME = type.getMethod("getEJBLocalHome", empty);
85          IS_IDENTICAL = type.getMethod("isIdentical", new Class JavaDoc[] { type });
86          REMOVE = type.getMethod("remove", empty);
87       }
88       catch (Exception JavaDoc e)
89       {
90          e.printStackTrace();
91          throw new ExceptionInInitializerError JavaDoc(e);
92       }
93    }
94    
95    /**
96     * Initialize {@link Object} method references.
97     */

98    static
99    {
100       try
101       {
102          final Class JavaDoc[] empty = {};
103          final Class JavaDoc type = Object JavaDoc.class;
104          
105          TO_STRING = type.getMethod("toString", empty);
106          HASH_CODE = type.getMethod("hashCode", empty);
107          EQUALS = type.getMethod("equals", new Class JavaDoc[] { type });
108       }
109       catch (Exception JavaDoc e)
110       {
111          e.printStackTrace();
112          throw new ExceptionInInitializerError JavaDoc(e);
113       }
114    }
115    
116    protected String JavaDoc getJndiName()
117    {
118       return jndiName;
119    }
120    protected abstract Object JavaDoc getId();
121    
122    public LocalProxy(String JavaDoc jndiName, BaseLocalProxyFactory factory)
123    {
124       this.jndiName = jndiName;
125       this.factory = factory;
126    }
127
128    /**
129     * Test the identitiy of an <tt>EJBObject</tt>.
130     *
131     * @param a <tt>EJBObject</tt>.
132     * @param b Object to test identity with.
133     * @return True if objects are identical.
134     *
135     * @throws ClassCastException Not an EJBObject instance.
136     */

137    Boolean JavaDoc isIdentical(final Object JavaDoc a, final Object JavaDoc b)
138    {
139       final EJBLocalObject JavaDoc ejb = (EJBLocalObject JavaDoc)a;
140       Boolean JavaDoc isIdentical = Boolean.FALSE;
141       if( ejb != null )
142       {
143          isIdentical = new Boolean JavaDoc(ejb.toString().equals(b));
144       }
145       return isIdentical;
146    }
147
148    /**
149     * Implementation of toString for EJBLocalObject.
150     * @return String representation of EJBLocalObject.
151     */

152    String JavaDoc toStringImpl()
153    {
154       return jndiName + ":" + getId();
155    }
156
157    public Object JavaDoc invoke(final Object JavaDoc proxy, final Method JavaDoc m, Object JavaDoc[] args)
158       throws Throwable JavaDoc
159    {
160       Object JavaDoc id = getId();
161       Object JavaDoc retValue = null;
162
163       // Implement local methods
164
if (m.equals(TO_STRING))
165       {
166          retValue = toStringImpl();
167       }
168       else if (m.equals(EQUALS))
169       {
170          retValue = invoke(proxy, IS_IDENTICAL, args );
171       }
172       else if (m.equals(HASH_CODE))
173       {
174          retValue = new Integer JavaDoc(id.hashCode());
175       }
176       
177       // Implement local EJB calls
178
else if (m.equals(GET_PRIMARY_KEY))
179       {
180          retValue = id;
181       }
182       else if (m.equals(GET_EJB_HOME))
183       {
184          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
185          return ctx.lookup(jndiName);
186       }
187       else if (m.equals(IS_IDENTICAL))
188       {
189          retValue = isIdentical(args[0], toStringImpl());
190       }
191       return retValue;
192    }
193
194    /** Restore the jndiName using default serialization and then lookup
195     the BaseLocalProxyFactory using the jndiName
196     */

197    private void readObject(ObjectInputStream JavaDoc in)
198      throws IOException JavaDoc, ClassNotFoundException JavaDoc
199    {
200       in.defaultReadObject();
201       factory = (BaseLocalProxyFactory) BaseLocalProxyFactory.invokerMap.get(jndiName);
202    }
203
204    private void writeObject(ObjectOutputStream JavaDoc out)
205       throws IOException JavaDoc
206    {
207       out.defaultWriteObject();
208    }
209
210 }
211
Popular Tags