KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.ejb.EJBLocalHome JavaDoc;
25 import javax.ejb.EJBLocalObject JavaDoc;
26 import javax.ejb.RemoveException JavaDoc;
27 import java.lang.reflect.InvocationHandler JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 /**
31  * The proxy for an EJBLocalHome object.
32  *
33  * @author <a HREF="mailto:docodan@mvcsoft.com">Daniel OConnor</a>
34  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
35  * @version $Revision: 37459 $
36  */

37 public class LocalHomeProxy
38    extends LocalProxy
39    implements InvocationHandler JavaDoc
40 {
41    static final long serialVersionUID = 1762319499924478521L;
42
43    /** {@link javax.ejb.EJBHome#remove(Object)} method reference. */
44    protected static final Method JavaDoc REMOVE_BY_PRIMARY_KEY;
45    
46    /** {@link javax.ejb.EJBObject#remove} method reference. */
47    protected static final Method JavaDoc REMOVE_OBJECT;
48    
49    /**
50     * Initialize {@link javax.ejb.EJBHome} and {@link javax.ejb.EJBObject} method references.
51     */

52    static
53    {
54       try
55       {
56          final Class JavaDoc empty[] = {};
57          final Class JavaDoc type = EJBLocalHome JavaDoc.class;
58          
59          REMOVE_BY_PRIMARY_KEY = type.getMethod("remove", new Class JavaDoc[] {Object JavaDoc.class});
60          
61          // Get the "remove" method from the EJBObject
62
REMOVE_OBJECT = EJBLocalObject JavaDoc.class.getMethod("remove", empty);
63       }
64       catch (Exception JavaDoc e)
65       {
66          e.printStackTrace();
67          throw new ExceptionInInitializerError JavaDoc(e);
68       }
69    }
70    
71    public LocalHomeProxy(String JavaDoc jndiName, BaseLocalProxyFactory factory)
72    {
73       super(jndiName, factory);
74    }
75
76    protected Object JavaDoc getId()
77    {
78       return jndiName;
79    }
80
81    /**
82     * InvocationHandler implementation.
83     *
84     * @param proxy The proxy object.
85     * @param m The method being invoked.
86     * @param args The arguments for the method.
87     *
88     * @throws Throwable Any exception or error thrown while processing.
89     */

90    public Object JavaDoc invoke(final Object JavaDoc proxy, final Method JavaDoc m,
91       Object JavaDoc[] args)
92       throws Throwable JavaDoc
93    {
94       Object JavaDoc retValue = null;
95
96       if (args == null)
97          args = EMPTY_ARGS;
98
99       // Implement local methods
100
if (m.equals(TO_STRING))
101       {
102          retValue = jndiName + "Home";
103       }
104       else if (m.equals(EQUALS))
105       {
106          // equality of the proxy home is based on names...
107
Object JavaDoc temp = invoke(proxy, TO_STRING, args);
108          retValue = new Boolean JavaDoc(temp.equals(jndiName + "Home"));
109       }
110       else if (m.equals(HASH_CODE))
111       {
112          retValue = new Integer JavaDoc(this.hashCode());
113       }
114       else if (m.equals(REMOVE_BY_PRIMARY_KEY))
115       {
116          try
117          {
118             // The trick is simple we trick the container in believe it
119
// is a remove() on the instance
120
Object JavaDoc id = args[0];
121             retValue = factory.invoke(id, REMOVE_OBJECT, EMPTY_ARGS);
122          }
123          catch (Exception JavaDoc e)
124          {
125             RemoveException JavaDoc re = new RemoveException JavaDoc(e.getMessage());
126             re.initCause(e);
127             throw re;
128          }
129       }
130       // If not taken care of, go on and call the container
131
else
132       {
133          retValue = factory.invokeHome(m, args);
134       }
135
136       return retValue;
137    }
138 }
139
Popular Tags