KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > client > ClientLauncher


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.ejb3.client;
23
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLClassLoader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.jboss.client.AppClientLauncher;
31 import org.jboss.ejb3.metamodel.ApplicationClientDD;
32 import org.jboss.ejb3.metamodel.ApplicationClientDDObjectFactory;
33 import org.jboss.ejb3.metamodel.JBossClientDDObjectFactory;
34 import org.jboss.util.NotImplementedException;
35 import org.jboss.xb.binding.JBossXBException;
36
37 /**
38  * This class launches a JavaEE 5 application client.
39  *
40  * The first argument is either a jar file containing the client deployment files or the application client class name.
41  * The manifest file Main-Class attribute must point to the application client class.
42  * It must also contain an application client deployment descriptor file (META-INF/application-client.xml).
43  *
44  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
45  * @version $Revision: $
46  */

47 public class ClientLauncher
48    implements AppClientLauncher
49 {
50    private static URL JavaDoc findResource(String JavaDoc resourceName)
51    {
52       URL JavaDoc url;
53       if(Thread.currentThread().getContextClassLoader() instanceof URLClassLoader JavaDoc)
54          url = ((URLClassLoader JavaDoc) Thread.currentThread().getContextClassLoader()).findResource("META-INF/application-client.xml");
55       else
56          url = Thread.currentThread().getContextClassLoader().getResource("META-INF/application-client.xml");
57       return url;
58    }
59    
60    /**
61     * Convenience method for launching a client container.
62     *
63     * @param xml
64     * @param mainClassName
65     * @param applicationClientName
66     * @param args
67     * @throws Exception
68     */

69    public static void launch(ApplicationClientDD xml, String JavaDoc mainClassName, String JavaDoc applicationClientName, String JavaDoc args[]) throws Exception JavaDoc
70    {
71       Class JavaDoc mainClass = Class.forName(mainClassName);
72       
73       ClientContainer container = new ClientContainer(xml, mainClass, applicationClientName);
74       
75       // TODO: postContruct
76

77       container.invokeMain(args);
78       
79       // TODO: preDestroy
80
}
81
82    /**
83     * Convenience method to load the XML descriptor.
84     *
85     * @return
86     * @throws IOException
87     * @throws JBossXBException
88     */

89    public static ApplicationClientDD loadXML() throws JBossXBException, IOException JavaDoc
90    {
91       URL JavaDoc url = findResource("META-INF/application-client.xml");
92       URL JavaDoc jbossClientURL = findResource("META-INF/jboss-client.xml");
93       return loadXML(url, jbossClientURL);
94    }
95    
96    @Deprecated JavaDoc
97    public static ApplicationClientDD loadXML(String JavaDoc urlSpec) throws JBossXBException, IOException JavaDoc
98    {
99       URL JavaDoc url = new URL JavaDoc(urlSpec);
100       return loadXML(url, null);
101    }
102    
103    public static ApplicationClientDD loadXML(URL JavaDoc url, URL JavaDoc jbossClientURL) throws JBossXBException, IOException JavaDoc
104    {
105       ApplicationClientDD dd = ApplicationClientDDObjectFactory.parse(url);
106       dd = JBossClientDDObjectFactory.parse(jbossClientURL, dd);
107       return dd;
108    }
109    
110    /**
111     * Work in progress.
112     *
113     * @param args the arguments for the launcher
114     */

115    public static void main(String JavaDoc[] args)
116    {
117       try
118       {
119          if(args.length < 1)
120             throw new IllegalArgumentException JavaDoc("expected a jar filename as argument");
121          
122          Class JavaDoc<?> mainClass;
123          
124          String JavaDoc name = args[0];
125          if(name.endsWith(".jar"))
126          {
127             throw new NotImplementedException();
128 // JarFile jarFile = new JarFile(jarName);
129
}
130          else
131          {
132             String JavaDoc mainClassName = name;
133             mainClass = Class.forName(mainClassName);
134          }
135          
136          URL JavaDoc appXmlURL = mainClass.getClassLoader().getResource("META-INF/application-client.xml");
137          if(appXmlURL == null)
138             throw new RuntimeException JavaDoc("Can't find META-INF/application-client.xml");
139          
140          ApplicationClientDD xml = ApplicationClientDDObjectFactory.parse(appXmlURL);
141          
142          // FIXME: j2ee.clientName
143

144          List JavaDoc<String JavaDoc> newArgs = new ArrayList JavaDoc<String JavaDoc>();
145          for(int i = 1; i < args.length; i++)
146          {
147             newArgs.add(args[i]);
148          }
149          args = newArgs.toArray(args);
150          
151          // FIXME: when jar gets implemented this won't work anymore
152
String JavaDoc mainClassName = name;
153          launch(xml, mainClassName, "FIXME", args);
154       }
155       catch (Exception JavaDoc e)
156       {
157          e.printStackTrace();
158          System.exit(1);
159       }
160    }
161
162    /**
163     * The AppClientLauncher method for launching a client container.
164     *
165     * @param mainClassName - the class whose main(String[]) will be invoked
166     * @param clientName - the client name that maps to the server side JNDI ENC
167     * @param args - the args to pass to main method
168     * @throws Throwable
169     */

170    public void launch(String JavaDoc mainClassName, String JavaDoc clientName, String JavaDoc args[])
171       throws Throwable JavaDoc
172    {
173       ApplicationClientDD xml = loadXML();
174       launch(xml, mainClassName, clientName, args);
175    }
176
177 }
178
Popular Tags