KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > StatefulSessionEnterpriseContext


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;
23
24 import java.io.IOException JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28
29 import java.rmi.RemoteException JavaDoc;
30 import java.security.Principal JavaDoc;
31 import java.util.Date JavaDoc;
32
33 import javax.ejb.EJBLocalObject JavaDoc;
34 import javax.ejb.*;
35 import javax.transaction.UserTransaction JavaDoc;
36 import javax.xml.rpc.handler.MessageContext JavaDoc;
37
38
39 /**
40  * The enterprise context for stateful session beans.
41  *
42  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
43  * @author <a HREF="mailto:docodan@mvcsoft.com">Daniel OConnor</a>
44  * @version $Revision: 46279 $
45  */

46 public class StatefulSessionEnterpriseContext
47    extends EnterpriseContext
48    implements Serializable JavaDoc
49 {
50    // Constants -----------------------------------------------------
51

52    // Attributes ----------------------------------------------------
53
private EJBObject ejbObject;
54    private EJBLocalObject JavaDoc ejbLocalObject;
55    private SessionContext ctx;
56
57    // Static --------------------------------------------------------
58

59    // Constructors --------------------------------------------------
60

61    public StatefulSessionEnterpriseContext(Object JavaDoc instance, Container con)
62       throws RemoteException JavaDoc
63    {
64       super(instance, con);
65       ctx = new StatefulSessionContextImpl();
66       try
67       {
68          AllowedOperationsAssociation.pushInMethodFlag(IN_SET_SESSION_CONTEXT);
69          ((SessionBean)instance).setSessionContext(ctx);
70       }
71       finally
72       {
73          AllowedOperationsAssociation.popInMethodFlag();
74       }
75    }
76
77    // Public --------------------------------------------------------
78

79    public void discard() throws RemoteException JavaDoc
80    {
81       // Do nothing
82
}
83
84    public EJBContext getEJBContext()
85    {
86       return ctx;
87    }
88
89    /**
90     * During activation of stateful session beans we replace the instance
91     * by the one read from the file.
92     */

93    public void setInstance(Object JavaDoc instance)
94    {
95       this.instance = instance;
96       try
97       {
98          ((SessionBean)instance).setSessionContext(ctx);
99       }
100       catch (Exception JavaDoc x)
101       {
102          log.error("Failed to setSessionContext", x);
103       }
104    }
105
106    public void setEJBObject(EJBObject eo) {
107       ejbObject = eo;
108    }
109
110    public EJBObject getEJBObject() {
111       return ejbObject;
112    }
113
114    public void setEJBLocalObject(EJBLocalObject JavaDoc eo) {
115       ejbLocalObject = eo;
116    }
117
118    public EJBLocalObject JavaDoc getEJBLocalObject() {
119       return ejbLocalObject;
120    }
121     
122    public SessionContext getSessionContext()
123    {
124       return ctx;
125    }
126
127    // Package protected ---------------------------------------------
128

129    // Protected -----------------------------------------------------
130

131    // Private -------------------------------------------------------
132

133    private void writeObject(ObjectOutputStream JavaDoc out)
134       throws IOException JavaDoc, ClassNotFoundException JavaDoc
135    {
136       // No state
137
}
138     
139    private void readObject(ObjectInputStream JavaDoc in)
140       throws IOException JavaDoc, ClassNotFoundException JavaDoc
141    {
142       // No state
143
}
144
145    // Inner classes -------------------------------------------------
146

147    protected class StatefulSessionContextImpl
148       extends EJBContextImpl
149       implements SessionContext
150    {
151
152       public EJBHome getEJBHome()
153       {
154          AllowedOperationsAssociation.assertAllowedIn("getEJBHome",
155                  IN_SET_SESSION_CONTEXT |
156                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_BUSINESS_METHOD |
157                  IN_AFTER_BEGIN | IN_BEFORE_COMPLETION | IN_AFTER_COMPLETION);
158
159          return super.getEJBHome();
160       }
161
162       public EJBLocalHome getEJBLocalHome()
163       {
164          AllowedOperationsAssociation.assertAllowedIn("getEJBLocalHome",
165                  IN_SET_SESSION_CONTEXT |
166                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_BUSINESS_METHOD |
167                  IN_AFTER_BEGIN | IN_BEFORE_COMPLETION | IN_AFTER_COMPLETION);
168
169          return super.getEJBLocalHome();
170       }
171
172       /** Get the Principal for the current caller. This method
173        cannot return null according to the ejb-spec.
174        */

175       public Principal JavaDoc getCallerPrincipal()
176       {
177          AllowedOperationsAssociation.assertAllowedIn("getCallerPrincipal",
178                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_BUSINESS_METHOD |
179                  IN_AFTER_BEGIN | IN_BEFORE_COMPLETION | IN_AFTER_COMPLETION);
180
181          return super.getCallerPrincipal();
182       }
183
184       public boolean isCallerInRole(String JavaDoc id)
185       {
186          AllowedOperationsAssociation.assertAllowedIn("isCallerInRole",
187                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_BUSINESS_METHOD |
188                  IN_AFTER_BEGIN | IN_BEFORE_COMPLETION | IN_AFTER_COMPLETION);
189
190          return super.isCallerInRole(id);
191       }
192
193       public EJBObject getEJBObject()
194       {
195          AllowedOperationsAssociation.assertAllowedIn("getEJBObject",
196                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_BUSINESS_METHOD |
197                  IN_AFTER_BEGIN | IN_BEFORE_COMPLETION | IN_AFTER_COMPLETION);
198
199          if (((StatefulSessionContainer)con).getRemoteClass()==null)
200             throw new IllegalStateException JavaDoc( "No remote interface defined." );
201
202          if (ejbObject == null)
203          {
204             EJBProxyFactory proxyFactory = con.getProxyFactory();
205             if(proxyFactory == null)
206             {
207                String JavaDoc defaultInvokerName = con.getBeanMetaData().
208                   getContainerConfiguration().getDefaultInvokerName();
209                proxyFactory = con.lookupProxyFactory(defaultInvokerName);
210             }
211             ejbObject = (EJBObject) proxyFactory.getStatefulSessionEJBObject(id);
212          }
213
214          return ejbObject;
215       }
216       
217       public Object JavaDoc getBusinessObject(Class JavaDoc businessInterface) throws IllegalStateException JavaDoc
218       {
219          throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
220       }
221       
222       public Class JavaDoc getInvokedBusinessInterface() throws IllegalStateException JavaDoc
223       {
224          throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
225       }
226
227       public EJBLocalObject JavaDoc getEJBLocalObject()
228       {
229          AllowedOperationsAssociation.assertAllowedIn("getEJBLocalObject",
230                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_BUSINESS_METHOD |
231                  IN_AFTER_BEGIN | IN_BEFORE_COMPLETION | IN_AFTER_COMPLETION);
232
233          if (con.getLocalHomeClass()==null)
234             throw new IllegalStateException JavaDoc( "No local interface for bean." );
235          if (ejbLocalObject == null)
236          {
237             ejbLocalObject = ((StatefulSessionContainer)con).getLocalProxyFactory().getStatefulSessionEJBLocalObject(id);
238          }
239          return ejbLocalObject;
240       }
241
242       public boolean getRollbackOnly()
243       {
244          AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly",
245                  IN_BUSINESS_METHOD | IN_AFTER_BEGIN | IN_BEFORE_COMPLETION);
246
247          return super.getRollbackOnly();
248       }
249
250       public void setRollbackOnly()
251       {
252          AllowedOperationsAssociation.assertAllowedIn("setRollbackOnly",
253                  IN_BUSINESS_METHOD | IN_AFTER_BEGIN | IN_BEFORE_COMPLETION);
254
255          super.setRollbackOnly();
256       }
257
258       public UserTransaction JavaDoc getUserTransaction()
259       {
260          AllowedOperationsAssociation.assertAllowedIn("getUserTransaction",
261                  IN_EJB_CREATE | IN_EJB_REMOVE | IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_BUSINESS_METHOD);
262
263          return super.getUserTransaction();
264       }
265
266       public TimerService getTimerService() throws IllegalStateException JavaDoc
267       {
268          throw new IllegalStateException JavaDoc("getTimerService should not be access from a stateful session bean");
269       }
270
271       public MessageContext JavaDoc getMessageContext() throws IllegalStateException JavaDoc
272       {
273          AllowedOperationsAssociation.assertAllowedIn("getMessageContext",
274                  NOT_ALLOWED);
275          return null;
276       }
277
278       public Object JavaDoc getPrimaryKey()
279       {
280          return id;
281       }
282    }
283 }
284
Popular Tags