KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb;
31
32 import com.caucho.config.BuilderProgram;
33 import com.caucho.ejb.protocol.AbstractHandle;
34 import com.caucho.ejb.protocol.EjbProtocolManager;
35 import com.caucho.ejb.protocol.HandleEncoder;
36 import com.caucho.ejb.protocol.SameJVMClientContainer;
37 import com.caucho.ejb.session.SessionServer;
38 import com.caucho.ejb.session.StatelessServer;
39 import com.caucho.ejb.xa.EjbTransactionManager;
40 import com.caucho.ejb.xa.TransactionContext;
41 import com.caucho.loader.DynamicClassLoader;
42 import com.caucho.loader.EnvironmentBean;
43 import com.caucho.loader.EnvironmentClassLoader;
44 import com.caucho.util.L10N;
45 import com.caucho.util.Log;
46
47 import javax.ejb.*;
48 import javax.naming.Context JavaDoc;
49 import javax.naming.InitialContext JavaDoc;
50 import javax.naming.NamingException JavaDoc;
51 import javax.sql.DataSource JavaDoc;
52 import javax.transaction.UserTransaction JavaDoc;
53 import java.rmi.RemoteException JavaDoc;
54 import java.sql.Connection JavaDoc;
55 import java.sql.SQLException JavaDoc;
56 import java.util.HashMap JavaDoc;
57 import java.util.logging.Level JavaDoc;
58 import java.util.logging.Logger JavaDoc;
59
60 /**
61  * Base server for a single home/object bean pair.
62  */

63 abstract public class AbstractServer implements EnvironmentBean {
64   protected final static Logger JavaDoc log = Log.open(AbstractServer.class);
65   private static final L10N L = new L10N(AbstractServer.class);
66
67   protected String JavaDoc _ejbName;
68   protected String JavaDoc _jndiName;
69   protected String JavaDoc _serverId;
70   protected String JavaDoc _handleServerId;
71
72   private Context _jndiEnv;
73
74   protected EjbServerManager _ejbManager;
75   private BuilderProgram _serverProgram;
76
77   protected HashMap JavaDoc<String JavaDoc,HandleEncoder> _protocolEncoderMap;
78   protected HandleEncoder _handleEncoder;
79
80   protected DataSource JavaDoc _dataSource;
81
82   protected DynamicClassLoader _loader;
83
84   protected SameJVMClientContainer _jvmClient;
85
86   // The class for the extended bean
87
protected Class JavaDoc _contextImplClass;
88
89   protected Class JavaDoc _remoteHomeClass;
90   protected Class JavaDoc _remoteObjectClass;
91   protected Class JavaDoc _primaryKeyClass;
92
93   protected Class JavaDoc _remoteStubClass;
94   protected Class JavaDoc _homeStubClass;
95
96   protected HomeHandle _homeHandle;
97   protected EJBHome _remoteHome;
98   protected EJBHome _remoteHomeView;
99   protected EJBLocalHome _localHome;
100   protected EJBMetaDataImpl _metaData;
101
102   protected long _transactionTimeout;
103
104   protected BuilderProgram _initProgram;
105
106   /**
107    * Creates a new server container
108    *
109    * @param manager the owning server container
110    */

111   public AbstractServer(EjbServerManager manager)
112   {
113     _ejbManager = manager;
114
115     _loader = new EnvironmentClassLoader(manager.getClassLoader());
116   }
117
118   public String JavaDoc getId()
119   {
120     // XXX: s/b module-path#ejb-name
121
return getEJBName();
122   }
123   
124   /**
125    * Sets the ejb name.
126    */

127   public void setEJBName(String JavaDoc ejbName)
128   {
129     _ejbName = ejbName;
130   }
131
132   /**
133    * Returns the ejb's name
134    */

135   public String JavaDoc getEJBName()
136   {
137     return _ejbName;
138   }
139
140   /**
141    * Sets the jndi name.
142    */

143   public void setJndiName(String JavaDoc jndiName)
144   {
145     if (jndiName == null)
146       return;
147
148     while (jndiName.startsWith("/"))
149       jndiName = jndiName.substring(1);
150
151     while (jndiName.endsWith("/"))
152       jndiName = jndiName.substring(0, jndiName.length() - 1);
153
154
155     _jndiName = jndiName;
156   }
157
158   /**
159    * Returns the jndi name.
160    */

161   public String JavaDoc getJndiName()
162   {
163     return _jndiName;
164   }
165
166   /**
167    * Returns the protocol id, used by implementations of
168    * {@link com.caucho.ejb.protocol.ProtocolContainer} as the identity of the
169    * home. Always starts with a "/" but does not end with a "/".
170    */

171   public String JavaDoc getProtocolId()
172   {
173     if (_serverId == null) {
174       String JavaDoc serverId = getJndiName();
175
176       if (serverId == null)
177     serverId = getEJBName();
178
179       if (serverId.startsWith("java:comp/env"))
180         serverId = serverId.substring(13);
181
182       if (! serverId.startsWith("/"))
183         serverId = "/" + serverId;
184
185       while (serverId.endsWith("/"))
186         serverId = serverId.substring(0, serverId.length() - 1);
187
188       _serverId = serverId;
189     }
190
191     return _serverId;
192   }
193
194   /**
195    * Sets the context implementation class.
196    */

197   public void setContextImplClass(Class JavaDoc cl)
198   {
199     _contextImplClass = cl;
200   }
201
202   /**
203    * Sets the remote home class.
204    */

205   public void setRemoteHomeClass(Class JavaDoc cl)
206   {
207     _remoteHomeClass = cl;
208   }
209
210   /**
211    * Gets the remote home class.
212    */

213   public Class JavaDoc getRemoteHomeClass()
214   {
215     return _remoteHomeClass;
216   }
217
218   /**
219    * Sets the remote object class.
220    */

221   public void setRemoteObjectClass(Class JavaDoc cl)
222   {
223     _remoteObjectClass = cl;
224   }
225
226   /**
227    * Gets the remote object class.
228    */

229   public Class JavaDoc getRemoteObjectClass()
230   {
231     return _remoteObjectClass;
232   }
233
234   public HandleEncoder getHandleEncoder(String JavaDoc protocol)
235   {
236     HandleEncoder encoder;
237
238     if (_protocolEncoderMap != null) {
239       encoder = _protocolEncoderMap.get(protocol);
240
241       if (encoder != null)
242         return encoder;
243     }
244
245     try {
246       Class JavaDoc keyClass = getPrimaryKeyClass();
247
248       encoder = _ejbManager.getProtocolManager().createHandleEncoder(this, keyClass, protocol);
249     } catch (Exception JavaDoc e) {
250       throw EJBExceptionWrapper.createRuntime(e);
251     }
252
253     if (_protocolEncoderMap == null)
254       _protocolEncoderMap = new HashMap JavaDoc<String JavaDoc,HandleEncoder>(8);
255
256     _protocolEncoderMap.put(protocol, encoder);
257
258     return encoder;
259   }
260
261   /**
262    * Returns the encoded id.
263    */

264   public String JavaDoc encodeId(Object JavaDoc primaryKey)
265   {
266     return String.valueOf(primaryKey);
267   }
268
269   public HandleEncoder addHandleEncoder(String JavaDoc protocol, String JavaDoc serverId)
270   {
271     HandleEncoder encoder;
272
273     if (_protocolEncoderMap != null) {
274       encoder = _protocolEncoderMap.get(protocol);
275
276       if (encoder != null)
277         return encoder;
278     }
279
280     try {
281       Class JavaDoc keyClass = getPrimaryKeyClass();
282
283       encoder = new HandleEncoder(this, serverId + _ejbName);
284     } catch (Exception JavaDoc e) {
285       throw EJBExceptionWrapper.createRuntime(e);
286     }
287
288     if (_protocolEncoderMap == null)
289       _protocolEncoderMap = new HashMap JavaDoc<String JavaDoc,HandleEncoder>(8);
290
291     _protocolEncoderMap.put(protocol, encoder);
292
293     return encoder;
294   }
295
296   public HandleEncoder getHandleEncoder()
297   {
298     return getHandleEncoder(EjbProtocolManager.getThreadProtocol());
299   }
300
301   public void setHandleEncoder(HandleEncoder encoder)
302   {
303     if (_homeHandle != null)
304       _homeHandle = null;
305
306     _handleEncoder = encoder;
307   }
308
309   public String JavaDoc getHandleServerId()
310   {
311     if (_handleServerId == null)
312       _handleServerId = getHandleEncoder().getServerId();
313
314     return _handleServerId;
315   }
316
317   /**
318    * Looks up the JNDI object.
319    */

320   public Object JavaDoc lookup(String JavaDoc jndiName)
321   {
322     try {
323       if (_jndiEnv == null)
324     _jndiEnv = (Context) new InitialContext JavaDoc().lookup("java:comp/env");
325       
326       // XXX: not tested
327
return _jndiEnv.lookup(jndiName);
328     } catch (NamingException JavaDoc e) {
329       throw new IllegalArgumentException JavaDoc(e);
330     }
331   }
332   
333
334   public UserTransaction JavaDoc getUserTransaction()
335   {
336     return _ejbManager.getTransactionManager().getUserTransaction();
337   }
338
339   /**
340    * Returns the owning container.
341    */

342   public EjbServerManager getContainer()
343   {
344     return _ejbManager;
345   }
346
347   /**
348    * Returns the owning container.
349    */

350   public EjbServerManager getServerManager()
351   {
352     return _ejbManager;
353   }
354
355   /**
356    * Sets the server program.
357    */

358   public void setServerProgram(BuilderProgram serverProgram)
359   {
360     _serverProgram = serverProgram;
361   }
362
363   /**
364    * Sets the server program.
365    */

366   public BuilderProgram getServerProgram()
367   {
368     return _serverProgram;
369   }
370
371   /**
372    * Sets the transaction timeout.
373    */

374   public void setTransactionTimeout(long timeout)
375   {
376     _transactionTimeout = timeout;
377   }
378
379   /**
380    * Gets the transaction timeout.
381    */

382   public long getTransactionTimeout()
383   {
384     return _transactionTimeout;
385   }
386
387   /**
388    * Invalidates caches.
389    */

390   public void invalidateCache()
391   {
392   }
393
394   /**
395    * Remove an object.
396    */

397   public void remove(AbstractHandle handle)
398   {
399     throw new UnsupportedOperationException JavaDoc();
400   }
401
402   /**
403    * Remove an object.
404    */

405   public void remove(Object JavaDoc primaryKey)
406   {
407     throw new UnsupportedOperationException JavaDoc();
408   }
409
410   /**
411    * Gets the class loader
412    */

413   public DynamicClassLoader getClassLoader()
414   {
415     return _loader;
416   }
417
418   /**
419    * Sets the class loader
420    */

421   public void setClassLoader(DynamicClassLoader loader)
422   {
423     _loader = loader;
424   }
425
426   /**
427    * Gets the generated skeleton class
428    */

429   public Class JavaDoc getBeanSkelClass()
430   {
431     return _contextImplClass;
432   }
433
434   public Class JavaDoc getRemoteStubClass()
435   {
436     return _remoteStubClass;
437   }
438
439   public Class JavaDoc getHomeStubClass()
440   {
441     return _homeStubClass;
442   }
443
444   /**
445    * Returns the meta data
446    */

447   public EJBMetaData getEJBMetaData()
448   {
449     if (_metaData == null) {
450       try {
451         _metaData = new EJBMetaDataImpl(getEJBHome(),
452                                         getRemoteHomeClass(),
453                                         getRemoteObjectClass(),
454                                         getPrimaryKeyClass());
455       } catch (Exception JavaDoc e) {
456         log.log(Level.WARNING, e.toString(), e);
457       }
458
459       if (this instanceof StatelessServer) {
460         _metaData.setSession(true);
461         _metaData.setStatelessSession(true);
462       }
463       else if (this instanceof SessionServer) {
464         _metaData.setSession(true);
465       }
466     }
467
468     return _metaData;
469   }
470
471   /**
472    * Returns the home handle for the container
473    */

474   public HomeHandle getHomeHandle()
475   {
476     if (_homeHandle == null)
477       _homeHandle = getHandleEncoder().createHomeHandle();
478
479     return _homeHandle;
480   }
481
482   void setEJBHome(EJBHome remoteHome)
483   {
484     _remoteHome = remoteHome;
485   }
486
487   /**
488    * Returns the EJBHome stub for the container
489    */

490   public EJBHome getEJBHome()
491     throws RemoteException JavaDoc
492   {
493     return _remoteHomeView;
494   }
495
496   /**
497    * Returns the EJBHome stub for the container
498    */

499   EJBHome getClientHome()
500     throws RemoteException JavaDoc
501   {
502     return getEJBHome();
503   }
504
505   /**
506    * Returns the EJBHome stub for the container
507    */

508   public Object JavaDoc getHomeObject()
509   {
510     return _remoteHomeView;
511   }
512
513   /**
514    * Returns the EJBHome stub for the container
515    */

516   public Object JavaDoc getRemoteObject()
517   {
518     return getHomeObject();
519   }
520
521   /**
522    * Returns the EJBHome stub for the container
523    */

524   public Object JavaDoc getClientLocalHome()
525   {
526     return _localHome;
527   }
528
529   /**
530    * Returns the EJBHome stub for the container
531    */

532   public Object JavaDoc getClientObject()
533   {
534     return getClientLocalHome();
535   }
536
537   /**
538    * Returns the EJBLocalHome stub for the container
539    */

540   public EJBLocalHome getEJBLocalHome()
541   {
542     return _localHome;
543   }
544
545   /**
546    * Returns the object key from a handle.
547    */

548   public Class JavaDoc getPrimaryKeyClass()
549   {
550     return _primaryKeyClass;
551   }
552
553   /**
554    * Creates the local stub for the object in the context.
555    */

556   EJBLocalObject getEJBLocalObject(AbstractContext context)
557   {
558     throw new UnsupportedOperationException JavaDoc();
559   }
560
561   public EJBObject getEJBObject(Object JavaDoc key)
562     throws FinderException
563   {
564     return getContext(key).getEJBObject();
565   }
566
567   public AbstractContext getContext(Object JavaDoc key)
568     throws FinderException
569   {
570     return getContext(key, true);
571   }
572
573   public AbstractContext getContext(long key)
574     throws FinderException
575   {
576     return getContext(new Long JavaDoc(key));
577   }
578
579   /**
580    * Returns the context with the given key
581    */

582   abstract public AbstractContext getContext(Object JavaDoc key, boolean forceLoad)
583     throws FinderException;
584
585   /**
586    * Returns the UserTransaction for the request.
587    */

588   /*
589   public UserTransaction getUserTransaction()
590   {
591     return _ejbManager.getUserTransaction();
592   }
593   */

594
595   /**
596    * Returns the currrent transaction context.
597    *
598    * @return the transaction context for the request
599    */

600   public EjbTransactionManager getTransactionManager()
601   {
602     return _ejbManager.getTransactionManager();
603   }
604
605   /**
606    * Returns the currrent transaction context.
607    *
608    * @return the transaction context for the request
609    */

610   public TransactionContext getTransaction()
611   {
612     return _ejbManager.getTransactionManager().getTransactionContext();
613   }
614
615   /**
616    * Returns the server's DataSource
617    */

618   public DataSource JavaDoc getDataSource()
619   {
620     return _dataSource;
621   }
622
623   /**
624    * Sets the server's DataSource
625    */

626   public void setDataSource(DataSource JavaDoc dataSource)
627   {
628     _dataSource = dataSource;
629   }
630
631   /**
632    * Sets the init program.
633    */

634   public void setInitProgram(BuilderProgram init)
635   {
636     _initProgram = init;
637   }
638
639   /**
640    * Gets the init program.
641    */

642   public BuilderProgram getInitProgram()
643   {
644     return _initProgram;
645   }
646
647   /**
648    * Initialize an instance
649    */

650   public void initInstance(Object JavaDoc instance)
651     throws Throwable JavaDoc
652   {
653     if (_initProgram != null) {
654       Thread JavaDoc thread = Thread.currentThread();
655       ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
656
657       try {
658     thread.setContextClassLoader(_loader);
659
660     _initProgram.configure(instance);
661
662       } finally {
663     thread.setContextClassLoader(oldLoader);
664       }
665     }
666   }
667
668   public void init()
669     throws Exception JavaDoc
670   {
671     _loader.setId("EnvironmentLoader[ejb:" + getId() + "]");
672   }
673
674   public void start()
675     throws Exception JavaDoc
676   {
677   }
678
679   /**
680    * Returns true is there is a local home or local client object for the bean.
681    */

682   public boolean isLocal()
683   {
684     return _localHome != null;
685   }
686
687   /**
688    * Returns true is there is a remote home or remote client object for the bean.
689    */

690   public boolean isRemote()
691   {
692     return _remoteHome != null || _remoteHomeView != null;
693   }
694
695   /**
696    * Returns true if the server is dead.
697    */

698   public boolean isDead()
699   {
700     return _ejbManager == null;
701   }
702
703   /**
704    * Cleans up the server on shutdown
705    */

706   protected void destroy()
707   {
708     _ejbManager = null;
709   }
710
711   public Connection JavaDoc getConnection()
712     throws SQLException JavaDoc
713   {
714     return getDataSource().getConnection();
715   }
716
717   public String JavaDoc toString()
718   {
719     return getClass().getSimpleName() + "[" + getEJBName() + "," + getJndiName() + "]";
720   }
721 }
722
Popular Tags