KickJava   Java API By Example, From Geeks To Geeks.

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


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.server.e_app;
30
31 import com.caucho.config.Config;
32 import com.caucho.log.Log;
33 import com.caucho.server.deploy.ExpandDeployController;
34 import com.caucho.util.L10N;
35 import com.caucho.vfs.JarPath;
36 import com.caucho.vfs.Path;
37 import com.caucho.vfs.Vfs;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * A configuration entry for Enterprise Application clients
45  */

46 public class AppClientDeployController extends ExpandDeployController<EntAppClient> {
47   private static final Logger JavaDoc log = Log.open(AppClientDeployController.class);
48   private static final L10N L = new L10N(AppClientDeployController.class);
49
50   // The ear name
51
private String JavaDoc _name = "";
52
53   // private Var _hostVar = new Var();
54

55   private JarPath _clientJar;
56
57   private ArrayList JavaDoc<Path> _configList = new ArrayList JavaDoc<Path>();
58
59   private boolean _isInit;
60
61   public AppClientDeployController()
62   {
63     super("client");
64   }
65
66   /**
67    * Sets the root.
68    */

69   public void setRootDirectory(Path path)
70   {
71     super.setRootDirectory(path);
72   }
73
74   /**
75    * Adds a config file.
76    */

77   public void addConfig(Path path)
78   {
79     _configList.add(path);
80   }
81
82   /**
83    * Adds a config file.
84    */

85   public void addEar(Path path)
86   {
87     System.out.println("ADD EAR: " + path);
88     
89     JarPath jar = JarPath.create(path);
90
91     Path app = jar.lookup("META-INF/application.xml");
92
93     System.out.println(" APP: " + app + " " + app.exists());
94
95     if (app.exists())
96       addConfig(app);
97
98     app = jar.lookup("META-INF/resin-application.xml");
99
100     System.out.println(" APP: " + app + " " + app.exists());
101
102     if (app.exists())
103       addConfig(app);
104   }
105
106   /**
107    * Executes the main.
108    */

109   public void main(String JavaDoc []args)
110     throws Throwable JavaDoc
111   {
112     System.out.println("MAIN()");
113     init();
114     start();
115
116     EntAppClient appClient = request();
117     if (appClient != null)
118       appClient.main(args);
119   }
120
121   /**
122    * Executes the main.
123    */

124   public void main(String JavaDoc mainClass, String JavaDoc []args)
125     throws Throwable JavaDoc
126   {
127     System.out.println("MAIN: " + mainClass);
128     init();
129     start();
130
131     EntAppClient appClient = request();
132     System.out.println("MAIN: " + appClient + " " + mainClass);
133     if (appClient != null)
134       appClient.main(mainClass, args);
135   }
136
137   // XXX: temp
138
public ClassLoader JavaDoc getLoader()
139   {
140     init();
141     start();
142
143     EntAppClient appClient = request();
144
145     if (appClient != null)
146       return appClient.getClassLoader();
147     else
148       return null;
149   }
150
151   /**
152    * Returns the application object.
153    */

154   protected EntAppClient instantiateDeployInstance()
155   {
156     return new EntAppClient(this, getId());
157   }
158
159   /**
160    * Creates the application.
161    */

162   protected void configureInstance(EntAppClient appClient)
163     throws Throwable JavaDoc
164   {
165     Thread JavaDoc thread = Thread.currentThread();
166     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
167
168     Path rootDir = null;
169     try {
170       thread.setContextClassLoader(getParentClassLoader());
171
172       appClient.setArchivePath(getArchivePath());
173
174       rootDir = getRootDirectory();
175
176       if (rootDir == null)
177         throw new NullPointerException JavaDoc("Null root-directory");
178
179       /*
180         if (! rootDir.isDirectory()) {
181         throw new ConfigException(L.l("root-directory `{0}' must specify a directory.",
182         rootDir.getPath()));
183         }
184       */

185
186       appClient.setRootDirectory(rootDir);
187
188       thread.setContextClassLoader(appClient.getClassLoader());
189       Vfs.setPwd(rootDir);
190
191       addManifestClassPath();
192
193       configApplication(appClient);
194
195       // configClientApplication(appClient);
196

197       Path curr = rootDir;
198
199       Iterator JavaDoc<String JavaDoc> it = curr.iterator();
200
201       while (it.hasNext()) {
202
203         String JavaDoc s = it.next();
204
205         Path path = curr.lookup(s);
206
207         if (path.isDirectory()) {
208         }
209         else if (s.endsWith(".jar")) {
210           appClient.getClassLoader().addJar(path);
211
212           _clientJar = JarPath.create(path);
213
214           configClientApplication(appClient);
215         }
216       }
217
218       for (int i = 0; i < _configList.size(); i++)
219         configClientConfig(appClient, _configList.get(i));
220
221       appClient.init();
222     } finally {
223       thread.setContextClassLoader(oldLoader);
224     }
225   }
226
227   private void configApplication(EntAppClient appClient)
228     throws Exception JavaDoc
229   {
230     Path rootDir = getRootDirectory();
231
232     Path xml = rootDir.lookup("META-INF/application.xml");
233
234     if (xml.canRead())
235       new Config().configureBean(appClient, xml);
236
237     xml = rootDir.lookup("META-INF/resin-application.xml");
238
239     if (xml.canRead())
240       new Config().configureBean(appClient, xml);
241
242     /*
243      * XXX:
244      config = EnterpriseApplication.parseApplicationConfig(rootDir, xml);
245     */

246
247     /*
248       ArrayList<Path> ejbModules = config.getEjbModules();
249       EJBClientInterface ejbClient = null;
250
251       if (ejbModules.size() > 0) {
252       Class cl = Class.forName("com.caucho.iiop.IiopClient");
253       ejbClient = (EJBClientInterface) cl.newInstance();
254       }
255     */

256
257     /*
258       for (int i = 0; i < ejbModules.size(); i++) {
259       Path path = ejbModules.get(i);
260
261       appClient.getClassLoader().addJar(path);
262
263       if (ejbClient != null)
264       ejbClient.addEJBJar(path);
265       }
266
267       if (ejbClient != null)
268       ejbClient.initEJBs();
269
270       ArrayList<Path> javaModules = config.getJavaModules();
271
272       for (int i = 0; i < javaModules.size(); i++) {
273       Path path = javaModules.get(i);
274
275       appClient.getClassLoader().addJar(path);
276
277       _clientJar = JarPath.create(path);
278
279       Manifest manifest = _clientJar.getManifest();
280       String mainClass = manifest.getMainAttributes().getValue("Main-Class");
281
282       appClient.setMainClass(mainClass);
283       }
284     */

285   }
286
287   private void configClientApplication(EntAppClient appClient)
288     throws Exception JavaDoc
289   {
290     if (_clientJar == null)
291       return;
292
293     Path xml = _clientJar.lookup("META-INF/application-client.xml");
294
295     if (! xml.canRead())
296       return;
297
298     new Config().configureBean(appClient, xml,
299                                "com/caucho/server/e_app/app-client.rnc");
300   }
301
302   private void configClientConfig(EntAppClient appClient, Path xml)
303     throws Exception JavaDoc
304   {
305     if (! xml.canRead())
306       return;
307
308     new Config().configureBean(appClient, xml);
309   }
310
311   /**
312    * Returns equality.
313    */

314   public boolean equals(Object JavaDoc o)
315   {
316     if (! (o instanceof AppClientDeployController))
317       return false;
318
319     AppClientDeployController controller = (AppClientDeployController) o;
320
321     return getId().equals(controller.getId());
322   }
323
324   /**
325    * Returns a printable view.
326    */

327   public String JavaDoc toString()
328   {
329     return "AppClientDeployController[" + getId() + "]";
330   }
331 }
332
Popular Tags