KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > BaseSessionContext


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;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.lang.reflect.InvocationHandler JavaDoc;
29 import java.security.Identity JavaDoc;
30 import java.security.Principal JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Properties JavaDoc;
33 import javax.ejb.EJBException JavaDoc;
34 import javax.ejb.EJBHome JavaDoc;
35 import javax.ejb.EJBLocalHome JavaDoc;
36 import javax.ejb.EJBLocalObject JavaDoc;
37 import javax.ejb.EJBObject JavaDoc;
38 import javax.ejb.MessageDrivenContext JavaDoc;
39 import javax.ejb.SessionContext JavaDoc;
40 import javax.ejb.TimerService JavaDoc;
41 import javax.ejb.TransactionManagementType JavaDoc;
42 import javax.naming.InitialContext JavaDoc;
43 import javax.naming.NamingException JavaDoc;
44 import javax.transaction.Status JavaDoc;
45 import javax.transaction.SystemException JavaDoc;
46 import javax.transaction.TransactionManager JavaDoc;
47 import javax.transaction.UserTransaction JavaDoc;
48 import javax.xml.rpc.handler.MessageContext JavaDoc;
49 import org.jboss.annotation.security.SecurityDomain;
50 import org.jboss.aop.Advisor;
51 import org.jboss.ejb3.tx.TxUtil;
52 import org.jboss.ejb3.tx.UserTransactionImpl;
53 import org.jboss.logging.Logger;
54 import org.jboss.security.RealmMapping;
55 import org.jboss.security.RunAsIdentity;
56 import org.jboss.security.SecurityAssociation;
57 import org.jboss.security.SimplePrincipal;
58 import org.jboss.util.NestedRuntimeException;
59
60 /**
61  * Comment
62  *
63  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
64  * @version $Revision: 46279 $
65  */

66 public class BaseSessionContext implements SessionContext JavaDoc, MessageDrivenContext JavaDoc, Externalizable JavaDoc
67 {
68    private static final Logger log = Logger.getLogger(BaseSessionContext.class);
69    protected transient Container container;
70    protected transient RealmMapping rm;
71    protected BaseContext baseContext;
72
73    public BaseSessionContext()
74    {
75    }
76
77    public void setBaseContext(BaseContext baseContext)
78    {
79       this.baseContext = baseContext;
80    }
81
82    public Container getContainer()
83    {
84       return container;
85    }
86
87    public void setContainer(Container container)
88    {
89       this.container = container;
90       try
91       {
92          InitialContext JavaDoc ctx = container.getInitialContext();
93          setupSecurityDomain(container, ctx);
94       }
95       catch (NamingException JavaDoc e)
96       {
97          throw new RuntimeException JavaDoc(e);
98       }
99    }
100
101    private void setupSecurityDomain(Container container, InitialContext JavaDoc ctx)
102            throws NamingException JavaDoc
103    {
104       SecurityDomain securityAnnotation = (SecurityDomain) ((Advisor) container).resolveAnnotation(SecurityDomain.class);
105       if (securityAnnotation == null) return;
106       Object JavaDoc domain = ctx.lookup("java:/jaas/" + securityAnnotation.value());
107       rm = (RealmMapping) domain;
108    }
109
110    protected RealmMapping getRm()
111    {
112       return rm;
113    }
114
115    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
116    {
117       out.writeUTF(container.getObjectName().getCanonicalName());
118    }
119
120    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
121    {
122       container = Ejb3Registry.getContainer(in.readUTF());
123       InitialContext JavaDoc ctx = container.getInitialContext();
124       try
125       {
126          setupSecurityDomain(container, ctx);
127       }
128       catch (NamingException JavaDoc e)
129       {
130          throw new RuntimeException JavaDoc(e);
131       }
132
133    }
134
135
136    //----------------
137

138    public Object JavaDoc lookup(String JavaDoc name)
139    {
140       String JavaDoc newName;
141       if (name.startsWith("/"))
142       {
143          newName = Container.ENC_CTX_NAME + "/env" + name;
144       }
145       else
146       {
147          newName = Container.ENC_CTX_NAME + "/env/" + name;
148       }
149       try
150       {
151          return getContainer().getInitialContext().lookup(newName);
152       }
153       catch (NamingException JavaDoc ignored)
154       {
155          try
156          {
157             return getContainer().getInitialContext().lookup(name);
158          }
159          catch (NamingException JavaDoc ignored2)
160          {
161             
162          }
163       }
164       return null;
165    }
166
167    public Identity JavaDoc getCallerIdentity()
168    {
169       throw new IllegalStateException JavaDoc("deprecated");
170    }
171
172    public Principal JavaDoc getCallerPrincipal()
173    {
174       Principal JavaDoc principal = SecurityAssociation.getCallerPrincipal();
175       if (getRm() != null)
176       {
177          principal = getRm().getPrincipal(principal);
178       }
179
180       // This method never returns null.
181
if (principal == null)
182          throw new java.lang.IllegalStateException JavaDoc("No valid security context for the caller identity");
183
184       return principal;
185    }
186
187    public boolean isCallerInRole(Identity JavaDoc role)
188    {
189       throw new IllegalStateException JavaDoc("deprecated");
190    }
191
192    public boolean isCallerInRole(String JavaDoc roleName)
193    {
194       // TODO revert to aspects.security.SecurityContext impl when JBoss AOP 1.1 is out.
195
Principal JavaDoc principal = getCallerPrincipal();
196       // Check the caller of this beans run-as identity
197
// todo use priveleged stuff in ejb class
198
RunAsIdentity runAsIdentity = SecurityActions.peekRunAsIdentity(1);
199
200       if (principal == null && runAsIdentity == null)
201          return false;
202
203       if (getRm() == null)
204       {
205          String JavaDoc msg = "isCallerInRole() called with no security context. "
206                       + "Check that a security-domain has been set for the application.";
207          throw new IllegalStateException JavaDoc(msg);
208       }
209
210       HashSet JavaDoc set = new HashSet JavaDoc();
211       set.add(new SimplePrincipal(roleName));
212
213       if (runAsIdentity == null)
214          return getRm().doesUserHaveRole(principal, set);
215       else
216          return runAsIdentity.doesUserHaveRole(set);
217    }
218
219    public TimerService JavaDoc getTimerService() throws IllegalStateException JavaDoc
220    {
221       return getContainer().getTimerService();
222    }
223
224    public UserTransaction JavaDoc getUserTransaction() throws IllegalStateException JavaDoc
225    {
226       TransactionManagementType JavaDoc type = TxUtil.getTransactionManagementType(((Advisor) getContainer()));
227       if (type != TransactionManagementType.BEAN) throw new IllegalStateException JavaDoc("Container " + getContainer().getEjbName() + ": it is illegal to inject UserTransaction into a CMT bean");
228
229       return new UserTransactionImpl();
230    }
231
232    public EJBHome JavaDoc getEJBHome()
233    {
234       throw new EJBException JavaDoc("EJB 3.0 does not have a home type.");
235    }
236
237    public EJBLocalHome JavaDoc getEJBLocalHome()
238    {
239       throw new EJBException JavaDoc("EJB 3.0 does not have a home type.");
240    }
241
242    public Properties JavaDoc getEnvironment()
243    {
244       throw new EJBException JavaDoc("Deprecated");
245    }
246
247    public void setRollbackOnly() throws IllegalStateException JavaDoc
248    {
249       // EJB1.1 11.6.1: Must throw IllegalStateException if BMT
250
TransactionManagementType JavaDoc type = TxUtil.getTransactionManagementType(((Advisor) getContainer()));
251       if (type != TransactionManagementType.CONTAINER) throw new IllegalStateException JavaDoc("Container " + getContainer().getEjbName() + ": it is illegal to call setRollbackOnly from BMT: " + type);
252
253       try
254       {
255          TransactionManager JavaDoc tm = TxUtil.getTransactionManager();
256
257          // The getRollbackOnly and setRollBackOnly method of the SessionContext interface should be used
258
// only in the session bean methods that execute in the context of a transaction.
259
if (tm.getTransaction() == null)
260             throw new IllegalStateException JavaDoc("setRollbackOnly() not allowed without a transaction.");
261
262          tm.setRollbackOnly();
263       }
264       catch (SystemException JavaDoc e)
265       {
266          log.warn("failed to set rollback only; ignoring", e);
267       }
268    }
269
270    public boolean getRollbackOnly() throws IllegalStateException JavaDoc
271    {
272       // EJB1.1 11.6.1: Must throw IllegalStateException if BMT
273
TransactionManagementType JavaDoc type = TxUtil.getTransactionManagementType(((Advisor) getContainer()));
274       if (type != TransactionManagementType.CONTAINER) throw new IllegalStateException JavaDoc("Container " + getContainer().getEjbName() + ": it is illegal to call getRollbackOnly from BMT: " + type);
275
276       try
277       {
278          TransactionManager JavaDoc tm = TxUtil.getTransactionManager();
279
280          // The getRollbackOnly and setRollBackOnly method of the SessionContext interface should be used
281
// only in the session bean methods that execute in the context of a transaction.
282
if (tm.getTransaction() == null)
283             throw new IllegalStateException JavaDoc("setRollbackOnly() not allowed without a transaction.");
284
285          return tm.getStatus() == Status.STATUS_MARKED_ROLLBACK;
286       }
287       catch (SystemException JavaDoc e)
288       {
289          log.warn("failed to set rollback only; ignoring", e);
290          return true;
291       }
292    }
293
294    public EJBLocalObject JavaDoc getEJBLocalObject() throws IllegalStateException JavaDoc
295    {
296       try
297       {
298          return (EJBLocalObject JavaDoc)container.getInitialContext().lookup(ProxyFactoryHelper.getLocalJndiName(container, false));
299       }
300       catch (NamingException JavaDoc e)
301       {
302          throw new IllegalStateException JavaDoc(e);
303       }
304    }
305
306    public EJBObject JavaDoc getEJBObject() throws IllegalStateException JavaDoc
307    {
308       try
309       {
310          return (EJBObject JavaDoc)container.getInitialContext().lookup(ProxyFactoryHelper.getRemoteJndiName(container, false));
311       }
312       catch (NamingException JavaDoc e)
313       {
314          throw new IllegalStateException JavaDoc(e);
315       }
316    }
317
318    public Object JavaDoc getBusinessObject(Class JavaDoc businessInterface) throws IllegalStateException JavaDoc
319    {
320       return ((EJBContainer)container).getBusinessObject(baseContext, businessInterface);
321    }
322    
323    public Class JavaDoc getInvokedBusinessInterface() throws IllegalStateException JavaDoc
324    {
325       return ((SessionContainer)container).getInvokedBusinessInterface();
326    }
327
328    public MessageContext JavaDoc getMessageContext() throws IllegalStateException JavaDoc
329    {
330       throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
331    }
332
333 }
334
Popular Tags