KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > AbstractContext


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb;
30
31 import com.caucho.ejb.xa.TransactionContext;
32 import com.caucho.security.SecurityContext;
33 import com.caucho.security.SecurityContextException;
34 import com.caucho.util.L10N;
35 import com.caucho.util.Log;
36
37 import javax.ejb.*;
38 import javax.transaction.UserTransaction JavaDoc;
39 import java.rmi.RemoteException JavaDoc;
40 import java.security.Identity JavaDoc;
41 import java.security.Principal JavaDoc;
42 import java.util.Properties JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * Base class for an abstract context
48  */

49 abstract public class AbstractContext implements EJBContext {
50   protected static final L10N L = new L10N(AbstractContext.class);
51   protected static final Logger JavaDoc log = Log.open(AbstractContext.class);
52   
53   private boolean _isDead;
54   
55   /**
56    * Returns true if the context is dead.
57    */

58   public boolean isDead()
59   {
60     return _isDead;
61   }
62
63   /**
64    * Returns the server which owns this bean.
65    */

66   protected abstract AbstractServer getServer();
67
68   /**
69    * Returns the EJB's meta data.
70    */

71   public EJBMetaData getEJBMetaData()
72   {
73     return getServer().getEJBMetaData();
74   }
75   
76   /**
77    * Returns the EJBHome stub for the container.
78    */

79   public EJBHome getEJBHome()
80   {
81     try {
82       return getServer().getEJBHome();
83     } catch (RuntimeException JavaDoc e) {
84       throw e;
85     } catch (Exception JavaDoc e) {
86       log.log(Level.WARNING, e.toString(), e);
87       
88       return null;
89     }
90   }
91   
92   /**
93    * Returns the EJBLocalHome stub for the container.
94    */

95   public EJBLocalHome getEJBLocalHome()
96   {
97     try {
98       return getServer().getEJBLocalHome();
99     } catch (RuntimeException JavaDoc e) {
100       throw e;
101     } catch (Exception JavaDoc e) {
102       return null;
103     }
104   }
105
106   /**
107    * Returns the object's handle.
108    */

109   public Handle getHandle()
110   {
111     throw new UnsupportedOperationException JavaDoc();
112   }
113
114   /**
115    * Returns the object's home handle.
116    */

117   public HomeHandle getHomeHandle()
118   {
119     return getServer().getHomeHandle();
120   }
121
122   /**
123    * Returns the local object in the context.
124    */

125   public EJBLocalObject getEJBLocalObject()
126     throws IllegalStateException JavaDoc
127   {
128     throw new IllegalStateException JavaDoc(L.l("`{0}' has no local interface. Local beans need a local-home and a local interface. Remote beans must be called with a remote context.",
129                                         getServer()));
130   }
131
132   /**
133    * Looks up an object in the current JNDI context.
134    */

135   public Object JavaDoc lookup(String JavaDoc name)
136   {
137     return getServer().lookup(name);
138   }
139   
140   /**
141    * Returns the EJBObject stub for the container.
142    */

143   public EJBObject getEJBObject()
144   {
145     return getRemoteView();
146     /*
147     throw new IllegalStateException(L.l("`{0}' has no remote interface. Remote beans need a home and a remote interface. Local beans must be called with a local context.",
148                                         getServer().getEJBName()));
149     */

150   }
151   
152   /**
153    * Returns the underlying bean
154    */

155   public EJBObject getRemoteView()
156   {
157     throw new IllegalStateException JavaDoc(L.l("`{0}' has no remote interface. Remote beans need a home and a remote interface. Local beans must be called with a local context.",
158                                         getServer()));
159   }
160
161   /**
162    * Create the home view.
163    */

164   public EJBHome createRemoteHomeView()
165   {
166     return null;
167     /*
168     throw new IllegalStateException(L.l("`{0}' has no remote interface. Remote beans need a home and a remote interface. Local beans must be called with a local context.",
169                                         getServer().getEJBName()));
170     */

171   }
172
173   /**
174    * Create the local home view.
175    */

176   public EJBLocalHome createLocalHome()
177   {
178     return null;
179     /*
180     throw new IllegalStateException(L.l("`{0}' has no local interface. Local beans need a local-home and a local interface. Remote beans must be called with a remote context.",
181                                         getServer().getEJBName()));
182     */

183   }
184
185   /**
186    * Obsolete method which returns the EJB 1.0 environment.
187    */

188   public Properties JavaDoc getEnvironment()
189   {
190     return new Properties JavaDoc();
191   }
192
193   /**
194    * Obsolete method returns null.
195    */

196   public Identity JavaDoc getCallerIdentity()
197   {
198     return null;
199   }
200
201   /**
202    * Returns the principal
203    */

204   public Principal JavaDoc getCallerPrincipal()
205   {
206     try {
207       return SecurityContext.getUserPrincipal();
208     } catch (SecurityContextException e) {
209       log.log(Level.WARNING, e.toString(), e);
210       
211       return null;
212     }
213   }
214
215   /**
216    * Obsolete method returns false.
217    */

218   public boolean isCallerInRole(Identity JavaDoc role)
219   {
220     return false;
221   }
222
223   /**
224    * Returns true if the caller is in the named role.
225    */

226   public boolean isCallerInRole(String JavaDoc roleName)
227   {
228     return SecurityContext.isUserInRole(roleName);
229   }
230
231   public void remove()
232     throws RemoveException
233   {
234     EJBObject obj = null;
235     try {
236       obj = getEJBObject();
237     } catch (Exception JavaDoc e) {
238     }
239
240     try {
241       if (obj != null) {
242         obj.remove();
243         return;
244       }
245     } catch (RemoteException JavaDoc e) {
246     }
247     
248     EJBLocalObject local = null;
249     try {
250       local = getEJBLocalObject();
251     } catch (Exception JavaDoc e) {
252     }
253
254     if (local != null) {
255       local.remove();
256       return;
257     }
258   }
259
260   /**
261    * Returns the current UserTransaction. Only Session beans with
262    * bean-managed transactions may use this.
263    */

264   public UserTransaction JavaDoc getUserTransaction()
265     throws IllegalStateException JavaDoc
266   {
267     return getServer().getUserTransaction();
268   }
269
270   /**
271    * Looks up an object in the current JNDI context.
272    */

273   public TimerService getTimerService()
274   {
275     throw new UnsupportedOperationException JavaDoc();
276   }
277
278   /**
279    * Forces a rollback of the current transaction.
280    */

281   public void setRollbackOnly()
282     throws IllegalStateException JavaDoc
283   {
284     TransactionContext trans = getServer().getTransaction();
285
286     if (trans != null)
287       trans.setRollbackOnly();
288     else
289       throw new IllegalStateException JavaDoc("invalid transaction");
290   }
291
292   /**
293    * Returns true if the current transaction will rollback.
294    */

295   public boolean getRollbackOnly()
296     throws IllegalStateException JavaDoc
297   {
298     TransactionContext trans = getServer().getTransaction();
299
300     if (trans != null)
301       return trans.getRollbackOnly();
302     else
303       throw new IllegalStateException JavaDoc("invalid transaction");
304   }
305
306   /**
307    * Destroy the context.
308    */

309   public void destroy() throws Exception JavaDoc
310   {
311     _isDead = true;
312   }
313 }
314
315
316
Popular Tags