KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > e_app > EnterpriseApplication


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.server.e_app;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.ejb.EJBServer;
34 import com.caucho.java.WorkDir;
35 import com.caucho.lifecycle.Lifecycle;
36 import com.caucho.loader.Environment;
37 import com.caucho.loader.EnvironmentBean;
38 import com.caucho.loader.EnvironmentClassLoader;
39 import com.caucho.log.Log;
40 import com.caucho.server.deploy.EnvironmentDeployInstance;
41 import com.caucho.server.webapp.WebAppContainer;
42 import com.caucho.server.webapp.WebAppController;
43 import com.caucho.util.L10N;
44 import com.caucho.vfs.Depend;
45 import com.caucho.vfs.JarPath;
46 import com.caucho.vfs.Path;
47 import com.caucho.vfs.Vfs;
48
49 import javax.annotation.PostConstruct;
50 import java.util.ArrayList JavaDoc;
51 import java.util.logging.Level JavaDoc;
52 import java.util.logging.Logger JavaDoc;
53
54 /**
55  * An enterprise application (ear)
56  */

57 public class EnterpriseApplication
58   implements EnvironmentBean, EnvironmentDeployInstance
59 {
60   static final L10N L = new L10N(EnterpriseApplication.class);
61   static final Logger JavaDoc log = Log.open(EnterpriseApplication.class);
62
63   /*
64   protected static EnvironmentLocal<EJBServerInterface> _localServer
65     = new EnvironmentLocal<EJBServerInterface>("caucho.ejb-server");
66   */

67
68   private EnvironmentClassLoader _loader;
69
70   private String JavaDoc _name;
71
72   private String JavaDoc _ejbServerJndiName = "java:comp/env/cmp";
73   
74   private Path _rootDir;
75
76   private Path _earPath;
77
78   private String JavaDoc _prefix = "";
79
80   private EarDeployController _controller;
81
82   private Path _webappsPath;
83
84   private ApplicationConfig _config;
85
86   private WebAppContainer _container;
87
88   private boolean _hasModule;
89
90   // private WarDirApplicationGenerator _warDeploy;
91

92   private ArrayList JavaDoc<Path> _ejbPaths
93     = new ArrayList JavaDoc<Path>();
94   
95   private ArrayList JavaDoc<WebAppController> _webApps
96     = new ArrayList JavaDoc<WebAppController>();
97
98   private Throwable JavaDoc _configException;
99
100   private final Lifecycle _lifecycle;
101
102   /**
103    * Creates the application.
104    */

105   EnterpriseApplication(WebAppContainer container,
106             EarDeployController controller, String JavaDoc name)
107   {
108     _container = container;
109     
110     _controller = controller;
111     _name = name;
112
113     ClassLoader JavaDoc parentLoader = Thread.currentThread().getContextClassLoader();
114     
115     _loader = new EnvironmentClassLoader(container.getClassLoader());
116     _loader.setId("EnterpriseApplication[" + name + "]");
117
118     _webappsPath = _controller.getRootDirectory().lookup("webapps");
119     WorkDir.setLocalWorkDir(_controller.getRootDirectory().lookup("META-INF/work"),
120                 _loader);
121     
122     _lifecycle = new Lifecycle(log, toString(), Level.INFO);
123
124     if (controller.getArchivePath() != null)
125       Environment.addDependency(new Depend(controller.getArchivePath()), _loader);
126   }
127
128   /**
129    * Sets the name.
130    */

131   public void setName(String JavaDoc name)
132   {
133     _name = name;
134     _loader.setId("EnterpriseApplication[" + name + "]");
135   }
136
137   /**
138    * Gets the name.
139    */

140   public String JavaDoc getName()
141   {
142     return _name;
143   }
144
145   /**
146    * Sets the ejb-server jndi name.
147    */

148   public void setEjbServerJndiName(String JavaDoc name)
149   {
150     _ejbServerJndiName = name;
151   }
152
153   /**
154    * Sets the root directory.
155    */

156   public void setRootDirectory(Path rootDir)
157   {
158     _rootDir = rootDir;
159   }
160
161   /**
162    * Sets the root directory.
163    */

164   public Path getRootDirectory()
165   {
166     return _rootDir;
167   }
168
169   /**
170    * Returns the class loader.
171    */

172   public EnvironmentClassLoader getClassLoader()
173   {
174     return _loader;
175   }
176
177   /**
178    * Sets the class loader.
179    */

180   public void setEnvironmentClassLoader(EnvironmentClassLoader loader)
181   {
182     _loader = loader;
183   }
184
185   /**
186    * Sets the path to the .ear file
187    */

188   public void setEarPath(Path earPath)
189   {
190     _earPath = earPath;
191   }
192
193   /**
194    * Sets the path to the expanded webapps
195    */

196   public void setWebapps(Path webappsPath)
197   {
198     _webappsPath = webappsPath;
199   }
200
201   /**
202    * Sets the prefix URL for web applications.
203    */

204   public void setPrefix(String JavaDoc prefix)
205   {
206     _prefix = prefix;
207   }
208   
209   /**
210    * Sets the id
211    */

212   public void setId(String JavaDoc id)
213   {
214   }
215   
216   /**
217    * Sets the application version.
218    */

219   public void setVersion(String JavaDoc version)
220   {
221   }
222   
223   /**
224    * Sets the schema location
225    */

226   public void setSchemaLocation(String JavaDoc schema)
227   {
228   }
229
230   /**
231    * Sets the display name.
232    */

233   public void setDisplayName(String JavaDoc name)
234   {
235   }
236
237   /**
238    * Sets the description.
239    */

240   public void setDescription(String JavaDoc description)
241   {
242   }
243
244   /**
245    * Sets the icon.
246    */

247   public void setIcon(Icon icon)
248   {
249   }
250   
251   /**
252    * Adds a module.
253    */

254   public Module createModule()
255   {
256     _hasModule = true;
257     
258     return new Module();
259   }
260
261   /**
262    * Adds a security role.
263    */

264   public void addSecurityRole(SecurityRole role)
265   {
266   }
267
268   /**
269    * Returns true if it's modified.
270    */

271   public boolean isModified()
272   {
273     return _loader.isModified();
274   }
275
276   /**
277    * Returns true if it's modified.
278    */

279   public boolean isModifiedNow()
280   {
281     return _loader.isModifiedNow();
282   }
283
284   /**
285    * Returns true if it's modified.
286    */

287   public boolean isDeployError()
288   {
289     return _configException != null;
290   }
291
292   /**
293    * Returns true if the application is idle.
294    */

295   public boolean isDeployIdle()
296   {
297     return false;
298   }
299
300   /**
301    * Sets the config exception.
302    */

303   public void setConfigException(Throwable JavaDoc e)
304   {
305     _configException = e;
306
307     for (WebAppController controller : _webApps) {
308       controller.setConfigException(e);
309     }
310   }
311
312   /**
313    * Gets the config exception.
314    */

315   public Throwable JavaDoc getConfigException()
316   {
317     return _configException;
318   }
319
320   /**
321    * Configures the application.
322    */

323   @PostConstruct
324   public void init()
325     throws Exception JavaDoc
326   {
327     if (! _lifecycle.toInit())
328       return;
329       
330     log.fine(this + " initializing");
331       
332     Vfs.setPwd(_rootDir, _loader);
333
334     // XXX: only if application is missing (?)
335
fillDefaultModules();
336
337     if (_ejbPaths.size() != 0) {
338       EJBServer ejbServer = EJBServer.getLocal();
339
340       if (ejbServer == null)
341     throw new ConfigException(L.l("Expected configured <ejb-server> in " +
342                       Thread.currentThread().getContextClassLoader()));
343
344       for (Path path : _ejbPaths)
345     ejbServer.addEJBJar(path);
346
347       Path ejbJar = _rootDir.lookup("META-INF/ejb-jar.xml");
348       if (ejbJar.canRead()) {
349     ejbServer.addEJBDescriptor("META-INF/ejb-jar.xml");
350       }
351
352       ejbServer.initEJBs();
353     }
354
355     // updates the invocation caches
356
if (_container != null)
357       _container.clearCache();
358   }
359
360   private void fillDefaultModules()
361   {
362     try {
363       if (_rootDir.lookup("lib").isDirectory()) {
364     Path lib = _rootDir.lookup("lib");
365
366     for (String JavaDoc file : lib.list()) {
367       if (file.endsWith(".jar")) {
368         _loader.addJar(lib.lookup(file));
369       }
370     }
371       }
372       
373       for (String JavaDoc file : _rootDir.list()) {
374     if (file.endsWith(".jar")) {
375       Path path = _rootDir.lookup(file);
376       Path jar = JarPath.create(path);
377
378       if (jar.lookup("META-INF/application-client.xml").canRead()) {
379         // app-client jar
380
}
381       else if (jar.lookup("META-INF/ejb-jar.xml").canRead()) {
382         _ejbPaths.add(path);
383       
384         _loader.addJar(path);
385         _loader.addJarManifestClassPath(path);
386       }
387       else {
388         _ejbPaths.add(path);
389       
390         _loader.addJar(path);
391       }
392     }
393     else if (file.endsWith(".war")) {
394       Module module = createModule();
395       WebModule web = new WebModule();
396       web.setWebURI(file);
397       web.setContextRoot(file.substring(0, file.length() - 4));
398       
399       module.addWeb(web);
400     }
401       }
402     } catch (RuntimeException JavaDoc e) {
403       throw e;
404     } catch (Exception JavaDoc e) {
405       throw new ConfigException(e);
406     }
407   }
408
409   /**
410    * Configures the application.
411    */

412   public void start()
413   {
414     if (! _lifecycle.toStarting())
415       return;
416     
417     Thread JavaDoc thread = Thread.currentThread();
418     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
419
420     try {
421       thread.setContextClassLoader(getClassLoader());
422
423       getClassLoader().start();
424
425       /* XXX: double start?
426       for (int i = 0; i < _webApps.size(); i++) {
427     WebAppController controller = _webApps.get(i);
428
429     try {
430       controller.start();
431     } catch (Throwable e) {
432       log.log(Level.WARNING, e.toString(), e);
433     }
434       }
435       */

436
437       for (WebAppController webApp : _webApps) {
438     _container.getWebAppGenerator().update(webApp.getContextPath());
439       }
440     } finally {
441       _lifecycle.toActive();
442       
443       thread.setContextClassLoader(oldLoader);
444     }
445   }
446
447   /**
448    * Returns any matching web-app.
449    */

450   public WebAppController findWebAppEntry(String JavaDoc name)
451   {
452     for (int i = 0; i < _webApps.size(); i++) {
453       WebAppController controller = _webApps.get(i);
454
455       if (controller.isNameMatch(name))
456     return controller;
457     }
458
459     return null;
460   }
461
462   private void addDepend(Path path)
463   {
464     _loader.addDependency(new com.caucho.vfs.Depend(path));
465   }
466
467   /**
468    * Returns the webapps for the enterprise-application.
469    */

470   public ArrayList JavaDoc<WebAppController> getApplications()
471   {
472     return _webApps;
473   }
474   
475   /**
476    * Stops the e-application.
477    */

478   public void stop()
479   {
480     if (! _lifecycle.toStopping())
481       return;
482     
483     Thread JavaDoc thread = Thread.currentThread();
484     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
485
486     try {
487       thread.setContextClassLoader(_loader);
488
489       //log.info(this + " stopping");
490

491       _loader.stop();
492       
493       //log.fine(this + " stopped");
494
} finally {
495       _lifecycle.toStop();
496       
497       thread.setContextClassLoader(oldLoader);
498     }
499   }
500   
501   /**
502    * destroys the e-application.
503    */

504   public void destroy()
505   {
506     stop();
507
508     if (! _lifecycle.toDestroy())
509       return;
510     
511     Thread JavaDoc thread = Thread.currentThread();
512     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
513
514     try {
515       thread.setContextClassLoader(getClassLoader());
516
517       log.fine(this + " destroying");
518
519       ArrayList JavaDoc<WebAppController> webApps = _webApps;
520       _webApps = null;
521
522       if (webApps != null) {
523     for (WebAppController webApp : webApps) {
524       _container.getWebAppGenerator().update(webApp.getContextPath());
525     }
526       }
527     } finally {
528       thread.setContextClassLoader(oldLoader);
529
530       _loader.destroy();
531
532       log.fine(this + " destroyed");
533     }
534   }
535
536   public String JavaDoc toString()
537   {
538     return "EnterpriseApplication[" + getName() + "]";
539   }
540
541   public class Module {
542     /**
543      * Sets the module id.
544      */

545     public void setId(String JavaDoc id)
546     {
547     }
548     
549     /**
550      * Creates a new web module.
551      */

552     public void addWeb(WebModule web)
553       throws Exception JavaDoc
554     {
555       String JavaDoc webUri = web.getWebURI();
556       String JavaDoc contextUrl = web.getContextRoot();
557       Path path = _rootDir.lookup(webUri);
558
559       if (contextUrl == null)
560     contextUrl = webUri;
561
562       WebAppController controller = null;
563       if (webUri.endsWith(".war")) {
564     // server/2a16
565
String JavaDoc name = webUri.substring(0, webUri.length() - 4);
566     int p = name.lastIndexOf('/');
567     if (p > 0)
568       name = name.substring(p + 1);
569
570     // XXX:
571
if (contextUrl.equals(""))
572       contextUrl = "/" + name;
573
574     if (contextUrl.endsWith(".war"))
575       contextUrl = contextUrl.substring(0, contextUrl.length() - 4);
576
577     Path expandPath = _webappsPath;
578     expandPath.mkdirs();
579
580     controller = new WebAppController(contextUrl,
581                       expandPath.lookup(name),
582                       _container);
583
584     controller.setArchivePath(path);
585       } else {
586     // server/2a15
587
if (contextUrl.equals("")) {
588       String JavaDoc name = webUri;
589       int p = name.lastIndexOf('/');
590       if (p > 0)
591         name = name.substring(p + 1);
592       contextUrl = "/" + name;
593     }
594
595     // server/2a17
596
if (contextUrl.endsWith(".war"))
597       contextUrl = contextUrl.substring(0, contextUrl.length() - 4);
598     
599     controller = new WebAppController(contextUrl, path, _container);
600       }
601
602       controller.setDynamicDeploy(true);
603       if (_configException != null)
604     controller.setConfigException(_configException);
605
606       controller.setManifestClassLoader(_loader);
607
608       // XXX: hack for duplicates
609
if (findWebAppEntry(controller.getContextPath()) == null)
610     _webApps.add(controller);
611       else
612     controller = findWebAppEntry(controller.getContextPath());
613       
614       if (web.getWebApp() != null)
615     controller.addConfigDefault(web.getWebApp());
616     }
617     
618     /**
619      * Adds a new ejb module.
620      */

621     public void addEjb(Path path)
622       throws Exception JavaDoc
623     {
624       _ejbPaths.add(path);
625       
626       _loader.addJar(path);
627       // ejb/0853
628
_loader.addJarManifestClassPath(path);
629     }
630     
631     /**
632      * Adds a new java module.
633      */

634     public void addJava(Path path)
635       throws ConfigException
636     {
637       if (! path.canRead())
638     throw new ConfigException(L.l("<java> module {0} must be a valid path.",
639                       path));
640     }
641     
642     /**
643      * Adds a new connector
644      */

645     public void addConnector(String JavaDoc path)
646     {
647     }
648     
649     /**
650      * Adds a new alt-dd module.
651      */

652     public void addAltDD(String JavaDoc path)
653     {
654     }
655   }
656 }
657
Popular Tags