KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > stateful > StatefulHandleImpl


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.ejb3.stateful;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.rmi.ServerException JavaDoc;
26 import java.security.AccessControlException JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.lang.reflect.InvocationHandler JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.lang.reflect.Proxy JavaDoc;
31
32 import javax.ejb.EJBObject JavaDoc;
33 import javax.ejb.Handle JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35
36 import org.jboss.invocation.Invoker;
37 import org.jboss.logging.Logger;
38 import org.jboss.naming.NamingContextFactory;
39
40 /**
41  * An EJB stateful session bean handle.
42  *
43  * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
44  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
45  * @author <a HREF="bill@burkecentral.com">Bill Burke</a>
46  * @author <a HREF="bdecoste@jboss.com">William DeCoste</a>
47  * @version $Revision: 40600 $
48  */

49 public class StatefulHandleImpl
50    implements Handle JavaDoc
51 {
52    private static final Logger log = Logger.getLogger(StatefulHandleImpl.class);
53    
54    /** Serial Version Identifier. */
55    static final long serialVersionUID = -6324520755180597156L;
56
57    /** A reference to {@link Handle#getEJBObject}. */
58    protected static final Method JavaDoc GET_EJB_OBJECT;
59
60    /** The value of our local Invoker.ID to detect when we are local. */
61    private Object JavaDoc invokerID = null;
62
63    /**
64     * Initialize <tt>Handle</tt> method references.
65     */

66    static
67    {
68       try
69       {
70          GET_EJB_OBJECT = Handle JavaDoc.class.getMethod("getEJBObject", new Class JavaDoc[0]);
71       }
72       catch(Exception JavaDoc e)
73       {
74          e.printStackTrace();
75          throw new ExceptionInInitializerError JavaDoc(e);
76       }
77    }
78    
79    public StatefulHandleImpl()
80    {
81       
82    }
83    
84    /** The identity of the bean. */
85    public int objectName;
86    public String JavaDoc jndiName;
87    public String JavaDoc invokerProxyBinding;
88    public Invoker invoker;
89    public Object JavaDoc id;
90
91    /** The JNDI env in effect when the home handle was created */
92    protected Hashtable JavaDoc jndiEnv;
93
94    /** Create an ejb handle for a stateful session bean.
95     * @param objectName - the session container jmx name
96     * @param jndiName - the session home ejb name
97     * @param invoker - the invoker to request the EJBObject from
98     * @param invokerProxyBinding - the type of invoker binding
99     * @param id - the session id
100     */

101    public StatefulHandleImpl(
102       int objectName,
103       String JavaDoc jndiName,
104       Invoker invoker,
105       String JavaDoc invokerProxyBinding,
106       Object JavaDoc id,
107       Object JavaDoc invokerID)
108    {
109       this.jndiName = jndiName;
110       this.id = id;
111       this.jndiEnv = (Hashtable JavaDoc) NamingContextFactory.lastInitialContextEnv.get();
112       try
113       {
114          String JavaDoc property = System.getProperty("org.jboss.ejb.sfsb.handle.V327");
115          if (property != null)
116          {
117             this.invokerProxyBinding = invokerProxyBinding;
118             this.invokerID = invokerID;
119             this.objectName = objectName;
120             this.invoker = invoker;
121          }
122       }
123       catch (AccessControlException JavaDoc ignored)
124       {
125       }
126
127    }
128
129    /**
130     * @return the internal session identifier
131     */

132    public Object JavaDoc getID()
133    {
134       return id;
135    }
136
137    /**
138     * @return the jndi name
139     */

140    public String JavaDoc getJNDIName()
141    {
142       return jndiName;
143    }
144
145    /**
146     * Handle implementation.
147     *
148     * This differs from Stateless and Entity handles which just invoke
149     * standard methods (<tt>create</tt> and <tt>findByPrimaryKey</tt>
150     * respectively) on the Home interface (proxy).
151     * There is no equivalent option for stateful SBs, so a direct invocation
152     * on the container has to be made to locate the bean by its id (the
153     * stateful SB container provides an implementation of
154     * <tt>getEJBObject</tt>).
155     *
156     * This means the security context has to be set here just as it would
157     * be in the Proxy.
158     *
159     * @return <tt>EJBObject</tt> reference.
160     *
161     * @throws ServerException Could not get EJBObject.
162     */

163    public EJBObject JavaDoc getEJBObject() throws RemoteException JavaDoc
164    {
165       try
166       {
167          InitialContext JavaDoc ic = null;
168          if( jndiEnv != null )
169             ic = new InitialContext JavaDoc(jndiEnv);
170          else
171             ic = new InitialContext JavaDoc();
172     
173          Proxy JavaDoc proxy = (Proxy JavaDoc) ic.lookup(jndiName);
174
175          return (EJBObject JavaDoc) proxy;
176       }
177       catch (Throwable JavaDoc t)
178       {
179          t.printStackTrace();
180          throw new RemoteException JavaDoc("Error during getEJBObject", t);
181       }
182    }
183 }
184
185
Popular Tags