KickJava   Java API By Example, From Geeks To Geeks.

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


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.Config;
33 import com.caucho.config.ConfigException;
34 import com.caucho.config.types.EjbRef;
35 import com.caucho.ejb.AbstractStubLoader;
36 import com.caucho.ejb.EJBClientInterface;
37 import com.caucho.java.WorkDir;
38 import com.caucho.lifecycle.Lifecycle;
39 import com.caucho.loader.Environment;
40 import com.caucho.loader.EnvironmentBean;
41 import com.caucho.loader.EnvironmentClassLoader;
42 import com.caucho.log.Log;
43 import com.caucho.naming.Jndi;
44 import com.caucho.server.deploy.DeployInstance;
45 import com.caucho.util.Alarm;
46 import com.caucho.util.L10N;
47 import com.caucho.vfs.Depend;
48 import com.caucho.vfs.JarPath;
49 import com.caucho.vfs.Path;
50 import com.caucho.vfs.Vfs;
51
52 import javax.annotation.PostConstruct;
53 import javax.naming.InitialContext JavaDoc;
54 import javax.rmi.PortableRemoteObject JavaDoc;
55 import java.lang.reflect.Method JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.HashMap JavaDoc;
58 import java.util.Hashtable JavaDoc;
59 import java.util.jar.Manifest JavaDoc;
60 import java.util.logging.Level JavaDoc;
61 import java.util.logging.Logger JavaDoc;
62
63 /**
64  * An enterprise application (ear)
65  */

66 public class EntAppClient implements DeployInstance, EnvironmentBean {
67   /* implements EnvironmentBean, EnvironmentListener, AlarmListener */
68
69   static final L10N L = new L10N(EntAppClient.class);
70   static final Logger JavaDoc log = Log.open(EntAppClient.class);
71
72   private EnvironmentClassLoader _loader;
73
74   private String JavaDoc _name;
75
76   private Path _rootDir;
77
78   private Path _archivePath;
79
80   private String JavaDoc _mainClass;
81
82   private String JavaDoc _prefix = "";
83
84   private AppClientDeployController _entry;
85
86   private AbstractStubLoader _stubLoader;
87
88   private ApplicationConfig _config;
89
90   private JarPath _clientJar;
91   
92   private EJBClientInterface _ejbClient;
93   
94   private AppClientConfig _appClientConfig;
95
96   private ArrayList JavaDoc<EjbLink> _links = new ArrayList JavaDoc<EjbLink>();
97
98   private HashMap JavaDoc<String JavaDoc,EjbRef> _ejbRefMap = new HashMap JavaDoc<String JavaDoc,EjbRef>();
99
100   private Throwable JavaDoc _configException;
101
102   private Alarm _alarm;
103   private final Lifecycle _lifecycle;
104
105   private static void log(String JavaDoc message)
106   {
107     System.out.println(EntAppClient.class.getSimpleName() + ": " + message);
108   }
109
110   /**
111    * Creates the application.
112    */

113   EntAppClient(AppClientDeployController entry, String JavaDoc name)
114   {
115     _entry = entry;
116     _name = name;
117
118     ClassLoader JavaDoc parentLoader = Thread.currentThread().getContextClassLoader();
119
120     _loader = new EnvironmentClassLoader(parentLoader);
121     //_loader.setOwner(this);
122
_loader.setId("EntAppClient[" + name + "]");
123
124     _lifecycle = new Lifecycle(log, toString(), Level.INFO);
125
126     if (entry.getArchivePath() != null)
127       Environment.addDependency(new Depend(entry.getArchivePath()), _loader);
128
129     // _alarm = new Alarm(this);
130
}
131
132   /**
133    * Sets the name.
134    */

135   public void setName(String JavaDoc name)
136   {
137     _name = name;
138     _loader.setId("EntAppClient[" + name + "]");
139   }
140
141   /**
142    * Gets the name.
143    */

144   public String JavaDoc getName()
145   {
146     return _name;
147   }
148
149   /**
150    * Returns the ejb client.
151    */

152   public EJBClientInterface getEJBClient()
153     throws ClassNotFoundException JavaDoc,
154        InstantiationException JavaDoc,
155        IllegalAccessException JavaDoc
156   {
157     if (_ejbClient == null) {
158       Class JavaDoc cl = Class.forName("com.caucho.iiop.IiopClient");
159       _ejbClient = (EJBClientInterface) cl.newInstance();
160     }
161
162     return _ejbClient;
163   }
164
165
166   /**
167    * Sets the root directory.
168    */

169   public void setRootDirectory(Path rootDir)
170   {
171     _rootDir = rootDir;
172   }
173
174   /**
175    * Sets the root directory.
176    */

177   public Path getRootDirectory()
178   {
179     return _rootDir;
180   }
181
182   /**
183    * Sets the archive path
184    */

185   public void setArchivePath(Path archivePath)
186   {
187     _archivePath = archivePath;
188   }
189
190   /**
191    * Sets the description.
192    */

193   public void setDescription(String JavaDoc description)
194   {
195   }
196
197   /**
198    * Sets the display-name.
199    */

200   public void setDisplayName(String JavaDoc displayName)
201   {
202   }
203   
204   /**
205    * Adds a module.
206    */

207   public Module createModule()
208   {
209     return new Module();
210   }
211
212   /**
213    * Adds an ejb-reference.
214    */

215   public void addEjbRef(EjbRef ejbRef)
216   {
217     _ejbRefMap.put(ejbRef.getEjbRefName(), ejbRef);
218
219     addIiopStub(ejbRef.getHome());
220     addIiopStub(ejbRef.getRemote());
221   }
222
223   private void addIiopStub(Class JavaDoc stubClass)
224   {
225     if (stubClass == null)
226       return;
227
228     try {
229       if (_stubLoader == null) {
230     Class JavaDoc iiopClass = Class.forName("com.caucho.iiop.IiopStubLoader");
231     _stubLoader = (AbstractStubLoader) iiopClass.newInstance();
232     _stubLoader.setPath(WorkDir.getLocalWorkDir());
233     _loader.addLoader(_stubLoader);
234       }
235
236       _stubLoader.addStubClass(stubClass.getName());
237     } catch (Throwable JavaDoc e) {
238       e.printStackTrace();
239       
240       log.info(e.toString());
241     }
242   }
243
244   /**
245    * Gets an ejb home.
246    */

247   Class JavaDoc getEjbHome(String JavaDoc ejbName)
248     throws ConfigException
249   {
250     EjbRef ref = _ejbRefMap.get(ejbName);
251     
252     if (ref != null)
253       return ref.getHome();
254     else
255       return null;
256   }
257
258   /**
259    * Sets the main class.
260    */

261   public void setMainClass(String JavaDoc mainClass)
262   {
263     _mainClass = mainClass;
264   }
265
266   /**
267    * Returns the class loader.
268    */

269   public EnvironmentClassLoader getClassLoader()
270   {
271     return _loader;
272   }
273
274   /**
275    * Sets the class loader.
276    */

277   public void setEnvironmentClassLoader(EnvironmentClassLoader loader)
278   {
279     _loader = loader;
280   }
281
282   /**
283    * Sets the config exception.
284    */

285   public void setConfigException(Throwable JavaDoc e)
286   {
287     _configException = e;
288   }
289
290   /**
291    * Gets the config exception.
292    */

293   public Throwable JavaDoc getConfigException()
294   {
295     return _configException;
296   }
297
298   /**
299    * Adds an EjbLink.
300    */

301   public EjbLink createEjbLink()
302   {
303     EjbLink link = new EjbLink();
304
305     _links.add(link);
306
307     return link;
308   }
309
310   /**
311    * Adds an EjbLink.
312    */

313   public SecurityRole createSecurityRole()
314   {
315     return new SecurityRole();
316   }
317
318   /**
319    * Stub for the xsi:schemaLocation tag.
320    */

321   public void setSchemaLocation(String JavaDoc s)
322   {
323   }
324
325   /**
326    * Stub for the version tag.
327    */

328   public void setVersion(String JavaDoc s)
329   {
330   }
331
332   /**
333    * Initialize the client.
334    */

335   @PostConstruct
336   public void init()
337   {
338     log("INIT: " + _links);
339     if (! _lifecycle.toInit())
340       return;
341
342     Thread JavaDoc thread = Thread.currentThread();
343     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
344
345     try {
346       thread.setContextClassLoader(_loader);
347
348       Path rootDir = getRootDirectory();
349       Vfs.setPwd(rootDir);
350
351       Path workDir = getRootDirectory().lookup("META-INF/work");
352       _loader.addJar(workDir);
353
354       WorkDir.setLocalWorkDir(workDir);
355
356       for (EjbLink link : _links) {
357     link.deploy();
358       }
359
360       // configApplication();
361
} catch (Throwable JavaDoc e) {
362       e.printStackTrace();
363       log.log(Level.WARNING, e.toString(), e);
364
365       _configException = e;
366     } finally {
367       thread.setContextClassLoader(oldLoader);
368     }
369   }
370
371   private void configResinBinding()
372     throws Exception JavaDoc
373   {
374     Path rootDir = getRootDirectory();
375
376     Path xml = rootDir.lookup("META-INF/resin-client.xml");
377
378     if (! xml.canRead())
379       return;
380
381     // AppClientBinding binding = new AppClientBinding(this);
382

383     // builder.setCompactSchema("com/caucho/server/e_app/app-client.rnc");
384

385     new Config().configure(this, xml);
386   }
387
388   /**
389    * Return true for modified.
390    */

391   public boolean isModified()
392   {
393     return false;
394   }
395
396   /**
397    * Return true for modified.
398    */

399   public boolean isModifiedNow()
400   {
401     return false;
402   }
403
404   /**
405    * Return true is the deployment had an error.
406    */

407   public boolean isDeployError()
408   {
409     return _configException != null;
410   }
411
412   /**
413    * Return true is the deployment is idle
414    */

415   public boolean isDeployIdle()
416   {
417     return false;
418   }
419
420   /**
421    * Start the client.
422    */

423   public void start()
424   {
425     init();
426
427     if (! _lifecycle.toActive())
428       return;
429   }
430
431   /**
432    * Execute the main class.
433    */

434   public void main(String JavaDoc []args)
435     throws Throwable JavaDoc
436   {
437     if (_mainClass == null)
438       throw new IllegalStateException JavaDoc(L.l("main() method require a main class"));
439
440     main(_mainClass, args);
441   }
442
443   /**
444    * Execute the main class.
445    */

446   public void main(String JavaDoc mainClassName, String JavaDoc []args)
447     throws Throwable JavaDoc
448   {
449     start();
450
451     log("MAIN: " + mainClassName);
452     log("C: " + System.getProperty("java.class.path"));
453
454     if (_configException != null)
455       throw _configException;
456
457     if (! _lifecycle.isActive())
458       throw new IllegalStateException JavaDoc(L.l("{0} is not active.",
459                       this));
460
461     Thread JavaDoc thread = Thread.currentThread();
462     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
463
464     try {
465       thread.setContextClassLoader(_loader);
466
467       Class JavaDoc mainClass = Class.forName(mainClassName, false, _loader);
468
469       log("MAIN:");
470       Method JavaDoc main = mainClass.getMethod("main",
471                     new Class JavaDoc[] { String JavaDoc[].class });
472
473       try {
474     Class JavaDoc cl = Class.forName("com.sun.ts.lib.implementation.sun.common.SunRIURL", false, _loader);
475     log("CL: " + cl);
476       } catch (Throwable JavaDoc e) {
477     e.printStackTrace();
478       }
479
480       main.invoke(null, new Object JavaDoc[] { args});
481     } finally {
482       thread.setContextClassLoader(oldLoader);
483     }
484   }
485
486   /**
487    * Destroys the client.
488    */

489   public void destroy()
490   {
491     if (! _lifecycle.toDestroy())
492       return;
493   }
494
495   public String JavaDoc toString()
496   {
497     return "EntAppClient[" + getName() + "]";
498   }
499
500   public class EjbLink {
501     private String JavaDoc _ejbName;
502     private String JavaDoc _jndiName;
503     private Class JavaDoc _api;
504
505     public void setEjbName(String JavaDoc ejbName)
506       throws ConfigException
507     {
508       _ejbName = ejbName;
509     }
510
511     public void setJndiName(String JavaDoc jndiName)
512     {
513       _jndiName = jndiName;
514     }
515
516     public void deploy()
517       throws Exception JavaDoc
518     {
519       log("LINK: " + _jndiName + " " + _ejbName);
520       String JavaDoc orbHost = System.getProperty("org.omg.CORBA.ORBInitialHost");
521       String JavaDoc orbPort = System.getProperty("org.omg.CORBA.ORBInitialPort");
522
523       Hashtable JavaDoc env = new Hashtable JavaDoc();
524       env.put("java.naming.factory.initial",
525           "com.sun.jndi.cosnaming.CNCtxFactory");
526       env.put("java.naming.provider.url", "iiop://" + orbHost + ":" + orbPort);
527       javax.naming.Context JavaDoc ic = new InitialContext JavaDoc(env);
528
529       Object JavaDoc ior = ic.lookup(_jndiName);
530       
531       _api = getEjbHome(_ejbName);
532
533       if (_api == null)
534     throw new ConfigException(L.l("'{0}' is an unknown ejb name.",
535                       _ejbName));
536       
537       Object JavaDoc value = PortableRemoteObject.narrow(ior, _api);
538
539       log("VALUE: " + value + " " + value.getClass() + " " + _api);
540       Jndi.rebindDeepShort(_ejbName, value);
541     }
542   }
543
544   public class SecurityRole {
545     public void setId(String JavaDoc id)
546     {
547     }
548
549     public void addDescription(String JavaDoc description)
550     {
551     }
552
553     public void setRoleName(String JavaDoc roleName)
554     {
555     }
556
557     public void setRoleLink(String JavaDoc roleLink)
558     {
559     }
560   }
561
562   public class Module {
563     /**
564      * Sets the module id.
565      */

566     public void setId(String JavaDoc id)
567     {
568     }
569     
570     /**
571      * Creates a new web module.
572      */

573     public void addWeb(WebModule web)
574       throws Exception JavaDoc
575     {
576     }
577     
578     /**
579      * Adds a new ejb module.
580      */

581     public void addEjb(Path path)
582       throws Exception JavaDoc
583     {
584       getClassLoader().addJar(path);
585
586       getEJBClient().addEJBJar(path);
587
588       getEJBClient().initEJBs();
589     }
590     
591     /**
592      * Adds a new java module.
593      */

594     public void addJava(Path path)
595       throws Exception JavaDoc
596     {
597       if (! path.canRead())
598     throw new ConfigException(L.l("<java> module {0} must be a valid path.",
599                       path));
600
601       
602       getClassLoader().addJar(path);
603     
604       _clientJar = JarPath.create(path);
605
606       Manifest JavaDoc manifest = _clientJar.getManifest();
607       String JavaDoc mainClass = manifest.getMainAttributes().getValue("Main-Class");
608
609       setMainClass(mainClass);
610
611       Path appClient = _clientJar.lookup("META-INF/application-client.xml");
612
613       if (appClient.canRead())
614     new Config().configureBean(EntAppClient.this, appClient);
615     }
616     
617     /**
618      * Adds a new connector
619      */

620     public void addConnector(String JavaDoc path)
621     {
622     }
623     
624     /**
625      * Adds a new alt-dd module.
626      */

627     public void addAltDD(String JavaDoc path)
628     {
629     }
630   }
631 }
632
Popular Tags