KickJava   Java API By Example, From Geeks To Geeks.

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


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.amber.entity.AmberEntityHome;
33 import com.caucho.amber.manager.AmberPersistenceUnit;
34 import com.caucho.bytecode.JClassLoader;
35 import com.caucho.config.ConfigException;
36 import com.caucho.ejb.admin.EJBAdmin;
37 import com.caucho.ejb.cfg.EjbConfig;
38 import com.caucho.ejb.entity.EntityKey;
39 import com.caucho.ejb.entity.EntityServer;
40 import com.caucho.ejb.entity.QEntityContext;
41 import com.caucho.ejb.protocol.EjbProtocolManager;
42 import com.caucho.ejb.protocol.ProtocolContainer;
43 import com.caucho.ejb.xa.EjbTransactionManager;
44 import com.caucho.java.WorkDir;
45 import com.caucho.lifecycle.Lifecycle;
46 import com.caucho.loader.Environment;
47 import com.caucho.loader.EnvironmentClassLoader;
48 import com.caucho.loader.EnvironmentListener;
49 import com.caucho.loader.SimpleLoader;
50 import com.caucho.util.L10N;
51 import com.caucho.util.Log;
52 import com.caucho.util.LruCache;
53 import com.caucho.vfs.Path;
54
55 import java.util.ArrayList JavaDoc;
56 import java.util.Collections JavaDoc;
57 import java.util.Comparator JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.Hashtable JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.logging.Level JavaDoc;
62 import java.util.logging.Logger JavaDoc;
63
64 /**
65  * Manages the beans in an environment.
66  */

67 public class EnvServerManager implements EnvironmentListener {
68   private static final L10N L = new L10N(EnvServerManager.class);
69   protected static final Logger JavaDoc log = Log.open(EnvServerManager.class);
70
71   /*
72   private static EnvironmentLocal<EnvServerManager> _localServerManager
73     = new EnvironmentLocal<EnvServerManager>("caucho.env-server");
74   */

75
76   private EnvironmentClassLoader _classLoader;
77
78   private Path _workPath;
79
80   private int _entityCacheSize = 32 * 1024;
81
82   private long _entityCacheTimeout = 5000L;
83
84   private ConfigException _initException;
85
86   private EJBAdmin _ejbAdmin;
87
88   private AmberPersistenceUnit _amberPersistenceUnit;
89
90   private EjbTransactionManager _ejbTransactionManager;
91
92   private EjbProtocolManager _protocolManager;
93
94   private ArrayList JavaDoc<EjbConfig> _ejbConfigList = new ArrayList JavaDoc<EjbConfig>();
95
96   private Hashtable JavaDoc<String JavaDoc,AbstractServer> _serverMap
97     = new Hashtable JavaDoc<String JavaDoc,AbstractServer>();
98
99   // handles remote stuff
100
protected ProtocolContainer _protocolContainer;
101   protected HashMap JavaDoc<String JavaDoc,ProtocolContainer> _protocolMap
102     = new HashMap JavaDoc<String JavaDoc,ProtocolContainer>();
103
104   private LruCache<EntityKey,QEntityContext> _entityCache;
105
106   private EntityKey _entityKey = new EntityKey();
107
108   private final Lifecycle _lifecycle = new Lifecycle(log, "ejb-manager");
109
110   /**
111    * Create a server with the given prefix name.
112    */

113   EnvServerManager(AmberPersistenceUnit amberPersistenceUnit)
114   {
115     try {
116       _amberPersistenceUnit = amberPersistenceUnit;
117       _amberPersistenceUnit.initLoaders();
118       _amberPersistenceUnit.setTableCacheTimeout(_entityCacheTimeout);
119
120       _classLoader = (EnvironmentClassLoader) Thread.currentThread().getContextClassLoader();
121       _workPath = WorkDir.getLocalWorkDir(_classLoader).lookup("ejb");
122       _classLoader.addLoader(new SimpleLoader(_workPath));
123
124       try {
125     _ejbTransactionManager = new EjbTransactionManager(this);
126       } catch (Throwable JavaDoc e) {
127     log.info("transactions are not available to EJB server");
128
129     log.log(Level.FINE, e.toString(), e);
130       }
131
132       _ejbAdmin = new EJBAdmin(this);
133
134       _protocolManager = new EjbProtocolManager(this);
135     } catch (RuntimeException JavaDoc e) {
136       throw e;
137     } catch (Exception JavaDoc e) {
138       throw new RuntimeException JavaDoc(e);
139     }
140   }
141
142   /**
143    * Gets the local server.
144    */

145   /*
146   public static EnvServerManager getLocal()
147   {
148     return _localServerManager.get();
149   }
150   */

151
152   /**
153    * Creates the local server.
154    */

155   /*
156   public static EnvServerManager createLocal()
157   {
158     synchronized (EnvServerManager.class) {
159       EnvServerManager serverManager = _localServerManager.getLevel();
160
161       if (serverManager == null) {
162     serverManager = new EnvServerManager();
163     _localServerManager.set(serverManager);
164       }
165
166       return serverManager;
167     }
168   }
169   */

170
171   /**
172    * Returns the loader.
173    */

174   public EnvironmentClassLoader getClassLoader()
175   {
176     return _classLoader;
177   }
178
179   /**
180    * Returns the protocol manager.
181    */

182   public EjbProtocolManager getProtocolManager()
183   {
184     return _protocolManager;
185   }
186
187   /**
188    * Returns the transaction manager.
189    */

190   public EjbTransactionManager getTransactionManager()
191   {
192     return _ejbTransactionManager;
193   }
194
195   /**
196    * Sets the Resin isolation.
197    */

198   public void setResinIsolation(int resinIsolation)
199   {
200     _ejbTransactionManager.setResinIsolation(resinIsolation);
201   }
202
203   /**
204    * Sets the Resin isolation for the container.
205    */

206   public int getResinIsolation()
207   {
208     return _ejbTransactionManager.getResinIsolation();
209   }
210
211   /**
212    * Sets the JDBC isolation.
213    */

214   public void setJDBCIsolation(int jdbcIsolation)
215   {
216     _ejbTransactionManager.setJDBCIsolation(jdbcIsolation);
217   }
218
219   /**
220    * Gets the JDBC isolation level.
221    */

222   public int getJDBCIsolation()
223   {
224     return _ejbTransactionManager.getJDBCIsolation();
225   }
226
227   /**
228    * Gets the transaction timeout
229    */

230   public long getTransactionTimeout()
231   {
232     return _ejbTransactionManager.getTransactionTimeout();
233   }
234
235   /**
236    * Sets the transaction timout.
237    */

238   public void setTransactionTimeout(long transactionTimeout)
239   {
240     _ejbTransactionManager.setTransactionTimeout(transactionTimeout);
241   }
242
243   /**
244    * Returns the work path.
245    */

246   public Path getWorkPath()
247   {
248     return _workPath;
249   }
250
251   /**
252    * Sets the work path.
253    */

254   public void setWorkPath(Path workPath)
255   {
256     _workPath = workPath;
257   }
258
259   /**
260    * Gets the cache timeout.
261    */

262   public long getCacheTimeout()
263   {
264     return _entityCacheTimeout;
265   }
266
267   /**
268    * Sets the cache timeout.
269    */

270   public void setCacheTimeout(long cacheTimeout)
271   {
272     _entityCacheTimeout = cacheTimeout;
273     // _amberPersistenceUnitenceUnitenceUnit.setTableCacheTimeout(cacheTimeout);
274
}
275
276   /**
277    * Gets the cache size.
278    */

279   public int getCacheSize()
280   {
281     return _entityCacheSize;
282   }
283
284   /**
285    * Sets the cache size.
286    */

287   public void setCacheSize(int cacheSize)
288   {
289     _entityCacheSize = cacheSize;
290   }
291
292   /**
293    * Returns the admin class.
294    */

295   public EJBAdmin getAdmin()
296   {
297     return _ejbAdmin;
298   }
299
300   /**
301    * Adds an ejb-config.
302    */

303   void addEjbConfig(EjbConfig ejbConfig)
304   {
305     _ejbConfigList.add(ejbConfig);
306   }
307
308   /**
309    * interface callback.
310    */

311   public void init()
312     throws Exception JavaDoc
313   {
314     build();
315
316     Environment.addEnvironmentListener(this);
317   }
318
319   /**
320    * Initialize the manager after all the configuration files have been read.
321    */

322   public void build()
323     throws ConfigException
324   {
325     try {
326       _amberPersistenceUnit.init();
327
328       /*
329       for (EjbConfig cfg : _ejbConfigList)
330     cfg.configure();
331       */

332
333       _entityCache = new LruCache<EntityKey,QEntityContext>(_entityCacheSize);
334
335       _protocolManager.init();
336     } catch (Exception JavaDoc e) {
337       throw new ConfigException(e);
338     }
339   }
340
341   public void start()
342     throws Exception JavaDoc
343   {
344     for (EjbConfig cfg : _ejbConfigList)
345       cfg.deploy();
346
347     Thread JavaDoc thread = Thread.currentThread();
348     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
349
350     for (AbstractServer server : _serverMap.values()) {
351       try {
352     thread.setContextClassLoader(server.getClassLoader());
353
354     log.fine(server + " starting");
355
356     server.start();
357       } finally {
358     thread.setContextClassLoader(oldLoader);
359       }
360     }
361   }
362
363   public AmberEntityHome getAmberEntityHome(String JavaDoc name)
364   {
365     return _amberPersistenceUnit.getEntityHome(name);
366   }
367
368   public AmberPersistenceUnit getAmberManager()
369   {
370     return _amberPersistenceUnit;
371   }
372
373   public JClassLoader getJClassLoader()
374   {
375     return getAmberManager().getJClassLoader();
376   }
377
378   /**
379    * Invalidates the caches for all the beans.
380    */

381   public void invalidateCache()
382   {
383   }
384
385   /**
386    * Adds a server.
387    */

388   public void addServer(AbstractServer server)
389   {
390     _serverMap.put(server.getEJBName(), server);
391
392     try {
393       _protocolManager.addServer(server);
394     } catch (Throwable JavaDoc e) {
395       log.log(Level.WARNING, e.toString(), e);
396     }
397   }
398
399
400   /**
401    * Returns the server specified by the serverId.
402    */

403   public AbstractServer getServer(Path path, String JavaDoc ejbName)
404   {
405     // XXX: incorrect, need to use path
406

407     AbstractServer server = _serverMap.get(ejbName);
408
409     return server;
410    }
411
412    /**
413     * Adds a new entity.
414     */

415   public QEntityContext getEntity(EntityServer server, Object JavaDoc key)
416   {
417     synchronized (_entityKey) {
418       _entityKey.init(server, key);
419
420       return _entityCache.get(_entityKey);
421     }
422   }
423
424   /**
425    * Adds a new entity.
426    */

427   public QEntityContext putEntityIfNew(EntityServer server, Object JavaDoc key,
428                        QEntityContext context)
429   {
430     return _entityCache.putIfNew(new EntityKey(server, key), context);
431   }
432
433   /**
434    * Adds a new entity.
435    */

436   public void removeEntity(EntityServer server, Object JavaDoc key)
437   {
438     synchronized (_entityKey) {
439       _entityKey.init(server, key);
440       _entityCache.remove(_entityKey);
441     }
442   }
443
444   /**
445    * Adds a new entity.
446    */

447   public void removeBeans(ArrayList JavaDoc<QEntityContext> beans, EntityServer server)
448   {
449     synchronized (_entityCache) {
450       Iterator JavaDoc<LruCache.Entry<EntityKey,QEntityContext>> iter;
451
452       iter = _entityCache.iterator();
453
454       while (iter.hasNext()) {
455     LruCache.Entry<EntityKey,QEntityContext> entry = iter.next();
456
457     beans.add(entry.getValue());
458
459     iter.remove();
460       }
461     }
462   }
463
464   /**
465    * Handles the case where the environment is starting (after init).
466    */

467   public void environmentStart(EnvironmentClassLoader loader)
468     throws Exception JavaDoc
469   {
470     start();
471   }
472
473   /**
474    * Handles the case where the environment is stopping
475    */

476   public void environmentStop(EnvironmentClassLoader loader)
477   {
478   }
479
480   /**
481    * Closes the container.
482    */

483   public void destroy()
484   {
485     if (! _lifecycle.toDestroy())
486       return;
487
488     try {
489       ArrayList JavaDoc<AbstractServer> servers;
490       servers = new ArrayList JavaDoc<AbstractServer>(_serverMap.values());
491       _serverMap.clear();
492
493       /*
494     for (int i = 0; i < _serverNames.size(); i++)
495     _staticServerMap.remove(_serverNames.get(i));
496       */

497
498       // only purpose of the sort is to make the qa order consistent
499
Collections.sort(servers, new ServerCmp());
500
501       for (AbstractServer server : servers) {
502     try {
503       _protocolManager.removeServer(server);
504     } catch (Throwable JavaDoc e) {
505       log.log(Level.WARNING, e.toString(), e);
506     }
507       }
508
509       for (AbstractServer server : servers) {
510     try {
511       server.destroy();
512     } catch (Throwable JavaDoc e) {
513       log.log(Level.WARNING, e.toString(), e);
514     }
515       }
516
517       _serverMap = null;
518       _protocolManager.destroy();
519       _protocolManager = null;
520       _ejbTransactionManager.destroy();
521       _ejbTransactionManager = null;
522       _amberPersistenceUnit = null;
523     } catch (Throwable JavaDoc e) {
524       log.log(Level.WARNING, e.toString(), e);
525     }
526   }
527
528   /**
529    * Sorts the servers so they can be destroyed in a consistent order.
530    * (To make QA sane.)
531    */

532   static class ServerCmp implements Comparator JavaDoc<AbstractServer> {
533     public int compare(AbstractServer a, AbstractServer b)
534     {
535       return a.getEJBName().compareTo(b.getEJBName());
536     }
537   }
538 }
539
540
Popular Tags