001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.e_app;
030:
031: import com.caucho.config.Config;
032: import com.caucho.log.Log;
033: import com.caucho.server.deploy.ExpandDeployController;
034: import com.caucho.util.L10N;
035: import com.caucho.vfs.JarPath;
036: import com.caucho.vfs.Path;
037: import com.caucho.vfs.Vfs;
038:
039: import java.util.ArrayList;
040: import java.util.Iterator;
041: import java.util.logging.Logger;
042:
043: /**
044: * A configuration entry for Enterprise Application clients
045: */
046: public class AppClientDeployController extends
047: ExpandDeployController<EntAppClient> {
048: private static final Logger log = Log
049: .open(AppClientDeployController.class);
050: private static final L10N L = new L10N(
051: AppClientDeployController.class);
052:
053: // The ear name
054: private String _name = "";
055:
056: // private Var _hostVar = new Var();
057:
058: private JarPath _clientJar;
059:
060: private ArrayList<Path> _configList = new ArrayList<Path>();
061:
062: private boolean _isInit;
063:
064: public AppClientDeployController() {
065: super ("client");
066: }
067:
068: /**
069: * Sets the root.
070: */
071: public void setRootDirectory(Path path) {
072: super .setRootDirectory(path);
073: }
074:
075: /**
076: * Adds a config file.
077: */
078: public void addConfig(Path path) {
079: _configList.add(path);
080: }
081:
082: /**
083: * Adds a config file.
084: */
085: public void addEar(Path path) {
086: System.out.println("ADD EAR: " + path);
087:
088: JarPath jar = JarPath.create(path);
089:
090: Path app = jar.lookup("META-INF/application.xml");
091:
092: System.out.println(" APP: " + app + " " + app.exists());
093:
094: if (app.exists())
095: addConfig(app);
096:
097: app = jar.lookup("META-INF/resin-application.xml");
098:
099: System.out.println(" APP: " + app + " " + app.exists());
100:
101: if (app.exists())
102: addConfig(app);
103: }
104:
105: /**
106: * Executes the main.
107: */
108: public void main(String[] args) throws Throwable {
109: System.out.println("MAIN()");
110: init();
111: start();
112:
113: EntAppClient appClient = request();
114: if (appClient != null)
115: appClient.main(args);
116: }
117:
118: /**
119: * Executes the main.
120: */
121: public void main(String mainClass, String[] args) throws Throwable {
122: System.out.println("MAIN: " + mainClass);
123: init();
124: start();
125:
126: EntAppClient appClient = request();
127: System.out.println("MAIN: " + appClient + " " + mainClass);
128: if (appClient != null)
129: appClient.main(mainClass, args);
130: }
131:
132: // XXX: temp
133: public ClassLoader getLoader() {
134: init();
135: start();
136:
137: EntAppClient appClient = request();
138:
139: if (appClient != null)
140: return appClient.getClassLoader();
141: else
142: return null;
143: }
144:
145: /**
146: * Returns the application object.
147: */
148: protected EntAppClient instantiateDeployInstance() {
149: return new EntAppClient(this , getId());
150: }
151:
152: /**
153: * Creates the application.
154: */
155: protected void configureInstance(EntAppClient appClient)
156: throws Throwable {
157: Thread thread = Thread.currentThread();
158: ClassLoader oldLoader = thread.getContextClassLoader();
159:
160: Path rootDir = null;
161: try {
162: thread.setContextClassLoader(getParentClassLoader());
163:
164: appClient.setArchivePath(getArchivePath());
165:
166: rootDir = getRootDirectory();
167:
168: if (rootDir == null)
169: throw new NullPointerException("Null root-directory");
170:
171: /*
172: if (! rootDir.isDirectory()) {
173: throw new ConfigException(L.l("root-directory `{0}' must specify a directory.",
174: rootDir.getPath()));
175: }
176: */
177:
178: appClient.setRootDirectory(rootDir);
179:
180: thread.setContextClassLoader(appClient.getClassLoader());
181: Vfs.setPwd(rootDir);
182:
183: addManifestClassPath();
184:
185: configApplication(appClient);
186:
187: // configClientApplication(appClient);
188:
189: Path curr = rootDir;
190:
191: Iterator<String> it = curr.iterator();
192:
193: while (it.hasNext()) {
194:
195: String s = it.next();
196:
197: Path path = curr.lookup(s);
198:
199: if (path.isDirectory()) {
200: } else if (s.endsWith(".jar")) {
201: appClient.getClassLoader().addJar(path);
202:
203: _clientJar = JarPath.create(path);
204:
205: configClientApplication(appClient);
206: }
207: }
208:
209: for (int i = 0; i < _configList.size(); i++)
210: configClientConfig(appClient, _configList.get(i));
211:
212: appClient.init();
213: } finally {
214: thread.setContextClassLoader(oldLoader);
215: }
216: }
217:
218: private void configApplication(EntAppClient appClient)
219: throws Exception {
220: Path rootDir = getRootDirectory();
221:
222: Path xml = rootDir.lookup("META-INF/application.xml");
223:
224: if (xml.canRead())
225: new Config().configureBean(appClient, xml);
226:
227: xml = rootDir.lookup("META-INF/resin-application.xml");
228:
229: if (xml.canRead())
230: new Config().configureBean(appClient, xml);
231:
232: /*
233: * XXX:
234: config = EnterpriseApplication.parseApplicationConfig(rootDir, xml);
235: */
236:
237: /*
238: ArrayList<Path> ejbModules = config.getEjbModules();
239: EJBClientInterface ejbClient = null;
240:
241: if (ejbModules.size() > 0) {
242: Class cl = Class.forName("com.caucho.iiop.IiopClient");
243: ejbClient = (EJBClientInterface) cl.newInstance();
244: }
245: */
246:
247: /*
248: for (int i = 0; i < ejbModules.size(); i++) {
249: Path path = ejbModules.get(i);
250:
251: appClient.getClassLoader().addJar(path);
252:
253: if (ejbClient != null)
254: ejbClient.addEJBJar(path);
255: }
256:
257: if (ejbClient != null)
258: ejbClient.initEJBs();
259:
260: ArrayList<Path> javaModules = config.getJavaModules();
261:
262: for (int i = 0; i < javaModules.size(); i++) {
263: Path path = javaModules.get(i);
264:
265: appClient.getClassLoader().addJar(path);
266:
267: _clientJar = JarPath.create(path);
268:
269: Manifest manifest = _clientJar.getManifest();
270: String mainClass = manifest.getMainAttributes().getValue("Main-Class");
271:
272: appClient.setMainClass(mainClass);
273: }
274: */
275: }
276:
277: private void configClientApplication(EntAppClient appClient)
278: throws Exception {
279: if (_clientJar == null)
280: return;
281:
282: Path xml = _clientJar.lookup("META-INF/application-client.xml");
283:
284: if (!xml.canRead())
285: return;
286:
287: new Config().configureBean(appClient, xml,
288: "com/caucho/server/e_app/app-client.rnc");
289: }
290:
291: private void configClientConfig(EntAppClient appClient, Path xml)
292: throws Exception {
293: if (!xml.canRead())
294: return;
295:
296: new Config().configureBean(appClient, xml);
297: }
298:
299: /**
300: * Returns equality.
301: */
302: public boolean equals(Object o) {
303: if (!(o instanceof AppClientDeployController))
304: return false;
305:
306: AppClientDeployController controller = (AppClientDeployController) o;
307:
308: return getId().equals(controller.getId());
309: }
310:
311: /**
312: * Returns a printable view.
313: */
314: public String toString() {
315: return "AppClientDeployController[" + getId() + "]";
316: }
317: }
|