001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.client;
023:
024: import java.io.IOException;
025: import java.net.URL;
026: import java.net.URLClassLoader;
027: import java.util.ArrayList;
028: import java.util.List;
029:
030: //import org.jboss.client.AppClientLauncher;
031: import org.jboss.ejb3.metamodel.ApplicationClientDD;
032: import org.jboss.ejb3.metamodel.ApplicationClientDDObjectFactory;
033: import org.jboss.ejb3.metamodel.JBossClientDDObjectFactory;
034: import org.jboss.util.NotImplementedException;
035: import org.jboss.xb.binding.JBossXBException;
036:
037: /**
038: * This class launches a JavaEE 5 application client.
039: *
040: * The first argument is either a jar file containing the client deployment files or the application client class name.
041: * The manifest file Main-Class attribute must point to the application client class.
042: * It must also contain an application client deployment descriptor file (META-INF/application-client.xml).
043: *
044: * @author <a href="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
045: * @version $Revision: $
046: */
047: public class ClientLauncher
048: // implements AppClientLauncher
049: {
050: private static URL findResource(String resourceName) {
051: URL url;
052: if (Thread.currentThread().getContextClassLoader() instanceof URLClassLoader)
053: url = ((URLClassLoader) Thread.currentThread()
054: .getContextClassLoader())
055: .findResource(resourceName);
056: else
057: url = Thread.currentThread().getContextClassLoader()
058: .getResource(resourceName);
059: return url;
060: }
061:
062: /**
063: * Convenience method for launching a client container.
064: *
065: * @param xml
066: * @param mainClassName
067: * @param applicationClientName
068: * @param args
069: * @throws Exception
070: */
071: public static void launch(ApplicationClientDD xml,
072: String mainClassName, String applicationClientName,
073: String args[]) throws Exception {
074: Class mainClass = Class.forName(mainClassName);
075:
076: ClientContainer container = new ClientContainer(xml, mainClass,
077: applicationClientName);
078:
079: // TODO: postContruct
080:
081: container.invokeMain(args);
082:
083: // TODO: preDestroy
084: }
085:
086: /**
087: * Convenience method to load the XML descriptor.
088: *
089: * @return
090: * @throws IOException
091: * @throws JBossXBException
092: */
093: public static ApplicationClientDD loadXML()
094: throws JBossXBException, IOException {
095: URL url = findResource("META-INF/application-client.xml");
096: URL jbossClientURL = findResource("META-INF/jboss-client.xml");
097: return loadXML(url, jbossClientURL);
098: }
099:
100: @Deprecated
101: public static ApplicationClientDD loadXML(String urlSpec)
102: throws JBossXBException, IOException {
103: URL url = new URL(urlSpec);
104: return loadXML(url, null);
105: }
106:
107: public static ApplicationClientDD loadXML(URL url,
108: URL jbossClientURL) throws JBossXBException, IOException {
109: ApplicationClientDD dd = ApplicationClientDDObjectFactory
110: .parse(url);
111: dd = JBossClientDDObjectFactory.parse(jbossClientURL, dd);
112: return dd;
113: }
114:
115: /**
116: * Work in progress.
117: *
118: * @param args the arguments for the launcher
119: */
120: public static void main(String[] args) {
121: try {
122: if (args.length < 1)
123: throw new IllegalArgumentException(
124: "expected a jar filename as argument");
125:
126: Class<?> mainClass;
127:
128: String name = args[0];
129: if (name.endsWith(".jar")) {
130: throw new NotImplementedException();
131: // JarFile jarFile = new JarFile(jarName);
132: } else {
133: String mainClassName = name;
134: mainClass = Class.forName(mainClassName);
135: }
136:
137: URL appXmlURL = mainClass.getClassLoader().getResource(
138: "META-INF/application-client.xml");
139: if (appXmlURL == null)
140: throw new RuntimeException(
141: "Can't find META-INF/application-client.xml");
142:
143: ApplicationClientDD xml = ApplicationClientDDObjectFactory
144: .parse(appXmlURL);
145:
146: // FIXME: j2ee.clientName
147:
148: List<String> newArgs = new ArrayList<String>();
149: for (int i = 1; i < args.length; i++) {
150: newArgs.add(args[i]);
151: }
152: args = newArgs.toArray(args);
153:
154: // FIXME: when jar gets implemented this won't work anymore
155: String mainClassName = name;
156: launch(xml, mainClassName, "FIXME", args);
157: } catch (Exception e) {
158: e.printStackTrace();
159: System.exit(1);
160: }
161: }
162:
163: /**
164: * The AppClientLauncher method for launching a client container.
165: *
166: * @param mainClassName - the class whose main(String[]) will be invoked
167: * @param clientName - the client name that maps to the server side JNDI ENC
168: * @param args - the args to pass to main method
169: * @throws Throwable
170: */
171: public void launch(String mainClassName, String clientName,
172: String args[]) throws Throwable {
173: ApplicationClientDD xml = loadXML();
174: launch(xml, mainClassName, clientName, args);
175: }
176:
177: }
|