KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > ejb > handle > 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.proxy.ejb.handle;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.rmi.ServerException JavaDoc;
26 import java.lang.reflect.InvocationHandler JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.Proxy JavaDoc;
29
30 import java.security.AccessControlException JavaDoc;
31 import java.security.AccessController JavaDoc;
32 import java.security.PrivilegedAction JavaDoc;
33 import java.security.Principal JavaDoc;
34 import java.util.Hashtable JavaDoc;
35
36 import javax.ejb.EJBHome JavaDoc;
37 import javax.ejb.Handle JavaDoc;
38 import javax.ejb.EJBObject JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40
41 import org.jboss.invocation.Invoker;
42 import org.jboss.invocation.Invocation;
43 import org.jboss.invocation.InvocationKey;
44 import org.jboss.invocation.InvocationType;
45 import org.jboss.invocation.InvokerInterceptor;
46 import org.jboss.invocation.PayloadKey;
47 import org.jboss.logging.Logger;
48 import org.jboss.naming.NamingContextFactory;
49 import org.jboss.security.SecurityAssociation;
50
51 /**
52  * An EJB stateful session bean handle.
53  *
54  * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
55  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
56  * @author <a HREF="bill@burkecentral.com">Bill Burke</a>
57  * @version $Revision: 37459 $
58  */

59 public class StatefulHandleImpl
60    implements Handle JavaDoc
61 {
62    private static final Logger log = Logger.getLogger(StatefulHandleImpl.class);
63    
64    /** Serial Version Identifier. */
65    static final long serialVersionUID = -6324520755180597156L;
66
67    /** A reference to {@link Handle#getEJBObject}. */
68    protected static final Method JavaDoc GET_EJB_OBJECT;
69
70    /** The value of our local Invoker.ID to detect when we are local. */
71    private Object JavaDoc invokerID = null;
72
73    /**
74     * Initialize <tt>Handle</tt> method references.
75     */

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

111    public StatefulHandleImpl(
112       int objectName,
113       String JavaDoc jndiName,
114       Invoker invoker,
115       String JavaDoc invokerProxyBinding,
116       Object JavaDoc id,
117       Object JavaDoc invokerID)
118    {
119       this.jndiName = jndiName;
120       this.id = id;
121       this.jndiEnv = (Hashtable JavaDoc) NamingContextFactory.lastInitialContextEnv.get();
122       try
123       {
124          String JavaDoc property = System.getProperty("org.jboss.ejb.sfsb.handle.V327");
125          if (property != null)
126          {
127             this.invokerProxyBinding = invokerProxyBinding;
128             this.invokerID = invokerID;
129             this.objectName = objectName;
130             this.invoker = invoker;
131          }
132       }
133       catch (AccessControlException JavaDoc ignored)
134       {
135       }
136
137    }
138
139    /**
140     * @return the internal session identifier
141     */

142    public Object JavaDoc getID()
143    {
144       return id;
145    }
146
147    /**
148     * @return the jndi name
149     */

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

173    public EJBObject JavaDoc getEJBObject() throws RemoteException JavaDoc
174    {
175       try
176       {
177          InitialContext JavaDoc ic = null;
178          if( jndiEnv != null )
179             ic = new InitialContext JavaDoc(jndiEnv);
180          else
181             ic = new InitialContext JavaDoc();
182
183          Proxy JavaDoc proxy = (Proxy JavaDoc) ic.lookup(jndiName);
184
185          // call findByPrimary on the target
186
InvocationHandler JavaDoc ih = Proxy.getInvocationHandler(proxy);
187          return (EJBObject JavaDoc) ih.invoke(proxy, GET_EJB_OBJECT, new Object JavaDoc[] {id});
188       }
189       catch (RemoteException JavaDoc e)
190       {
191          throw e;
192       }
193       catch (Throwable JavaDoc t)
194       {
195          t.printStackTrace();
196          throw new RemoteException JavaDoc("Error during getEJBObject", t);
197       }
198    }
199 }
200
201
Popular Tags