KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jndi > browser > model > ejb > EJBObjectProxy


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.jndi.browser.model.ejb;
8
9 import java.awt.Component JavaDoc;
10 import java.beans.BeanInfo JavaDoc;
11 import java.beans.Customizer JavaDoc;
12 import java.beans.Introspector JavaDoc;
13 import java.beans.PropertyChangeListener JavaDoc;
14 import java.beans.PropertyChangeSupport JavaDoc;
15 import java.lang.reflect.InvocationHandler JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17 import java.lang.reflect.Proxy JavaDoc;
18
19 import javax.ejb.EJBObject JavaDoc;
20
21 import org.ejtools.adwt.GenericCustomizer;
22 import org.ejtools.jndi.browser.model.JNDIEntry;
23
24 /**
25  * Proxy that hide the remote aspect of the EJB remote interface. Allows the execution of remote methods on the specified EJB.
26  *
27  * @author letiemble
28  * @created 2 janvier 2002
29  * @version $Revision: 1.2 $
30  * @todo Reference to Rickard Öberg
31  * @javabean:class displayName="Enterprise Java Bean"
32  * shortDescription="EJB Remote interface"
33  * @javabean:icons color16="/toolbarButtonGraphics/development/EnterpriseJavaBean16.gif"
34  */

35 public class EJBObjectProxy extends JNDIEntry implements InvocationHandler JavaDoc
36 {
37    /** Description of the Field */
38    protected transient Customizer JavaDoc c;
39    /** Description of the Field */
40    protected String JavaDoc desc;
41    /** Description of the Field */
42    protected BeanInfo JavaDoc info;
43    /** Description of the Field */
44    protected Object JavaDoc obj;
45    /** Description of the Field */
46    protected Object JavaDoc proxy;
47    /** Description of the Field */
48    protected PropertyChangeSupport JavaDoc ps;
49    /** Description of the Field */
50    protected Class JavaDoc remote;
51
52
53    /**
54     * Constructor for the EJBObjectProxy object
55     *
56     * @param obj Description of Parameter
57     * @param cl Description of Parameter
58     * @exception Exception Description of Exception
59     */

60    public EJBObjectProxy(Object JavaDoc obj, ClassLoader JavaDoc cl)
61       throws Exception JavaDoc
62    {
63       this.obj = obj;
64
65       Class JavaDoc[] intf = obj.getClass().getInterfaces();
66       for (int i = 0; i < intf.length; i++)
67       {
68          if (EJBObject JavaDoc.class.isAssignableFrom(intf[i]))
69          {
70             remote = intf[i];
71          }
72       }
73
74       this.info = Introspector.getBeanInfo(this.remote);
75       this.proxy = Proxy.newProxyInstance(cl, new Class JavaDoc[]{BeanInfo JavaDoc.class, remote, PropertyChanger.class}, this);
76       this.ps = new PropertyChangeSupport JavaDoc(proxy);
77    }
78
79
80    /**
81     * Gets the component attribute of the EJBObjectProxy object
82     *
83     * @return The component value
84     */

85    public Component JavaDoc getComponent()
86    {
87       if (c == null)
88       {
89          c = new GenericCustomizer(proxy);
90       }
91       return (Component JavaDoc) c;
92    }
93
94
95    /**
96     * Description of the Method
97     *
98     * @param proxy Description of Parameter
99     * @param method Description of Parameter
100     * @param args Description of Parameter
101     * @return Description of the Returned Value
102     * @exception Throwable Description of Exception
103     */

104    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
105       throws Throwable JavaDoc
106    {
107       if (method.getDeclaringClass().equals(BeanInfo JavaDoc.class))
108       {
109          return method.invoke(info, args);
110       }
111       else if (method.getDeclaringClass().equals(Object JavaDoc.class))
112       {
113          return method.invoke(this, args);
114       }
115       else if (method.getDeclaringClass().equals(PropertyChanger.class))
116       {
117          if (method.getName().startsWith("add"))
118          {
119             ps.addPropertyChangeListener((PropertyChangeListener JavaDoc) args[0]);
120          }
121          else
122          {
123             ps.removePropertyChangeListener((PropertyChangeListener JavaDoc) args[0]);
124          }
125          return null;
126       }
127       else
128       {
129          try
130          {
131             return method.invoke(obj, args);
132          }
133          finally
134          {
135             if (!(method.getName().startsWith("get") || method.getName().startsWith("is")))
136             {
137                ps.firePropertyChange(null, null, null);
138             }
139          }
140       }
141    }
142
143
144    /**
145     * Description of the Method
146     *
147     * @return Description of the Returned Value
148     */

149    public String JavaDoc toString()
150    {
151       if (desc == null)
152       {
153          try
154          {
155             Method JavaDoc m = EJBObject JavaDoc.class.getMethod("getPrimaryKey", new Class JavaDoc[0]);
156             desc = m.invoke(obj, new Object JavaDoc[0]).toString();
157          }
158          catch (Exception JavaDoc e)
159          {
160
161             desc = remote.getName().substring(remote.getName().lastIndexOf(".") + 1);
162             //desc = remote.toString();
163

164          }
165
166       }
167       return desc;
168    }
169 }
170
171
Popular Tags