KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > client > AppClientMain


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
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.client;
23
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.jar.Attributes JavaDoc;
29 import java.util.jar.Manifest JavaDoc;
30 import javax.xml.parsers.DocumentBuilder JavaDoc;
31 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.naming.client.java.javaURLContextFactory;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39
40 /**
41  * A java Main used to launch java ee app clients.
42  *
43  * @author Scott.Stark@jboss.org
44  * @version $Revision:$
45  */

46 public class AppClientMain
47 {
48    // logging support
49
private static final Logger log = Logger.getLogger(AppClientMain.class);
50
51    /** The name of the client class whose main(String[]) should be launched */
52    public static final String JavaDoc JBOSS_CLIENT_PARAM = "-jbossclient";
53    /** The name of the javaee client context used to identify the server side ENC */
54    public static final String JavaDoc J2EE_CLIENT_PARAM = "-"+javaURLContextFactory.J2EE_CLIENT_NAME_PROP;
55    /** Comma separated list of AppClientLauncher implementation classes */
56    public static final String JavaDoc LAUNCHERS_PARAM = "-launchers";
57    /** The default list of launchers */
58    public static String JavaDoc[] DEFAULT_LAUNCHERS = {ReflectionLauncher.class.getName()};
59
60    /**
61     * The main entry
62     */

63    public static void main(String JavaDoc[] args)
64       throws Exception JavaDoc
65    {
66       log.debug("System Properties");
67       Properties JavaDoc sysprops = System.getProperties();
68       for (Object JavaDoc key : sysprops.keySet())
69          log.debug(" " + key + "=" + sysprops.getProperty((String JavaDoc) key));
70
71       // read the client class from args
72
String JavaDoc clientClass = null;
73       String JavaDoc clientName = null;
74       ArrayList JavaDoc<String JavaDoc> newArgs = new ArrayList JavaDoc<String JavaDoc>();
75       String JavaDoc[] launchers = DEFAULT_LAUNCHERS;
76       for (int i = 0; i < args.length; i++)
77       {
78          String JavaDoc arg = args[i];
79          log.debug("arg=" + arg);
80          
81          if( arg.equals(JBOSS_CLIENT_PARAM) )
82          {
83             clientClass = args[i+1];
84             i ++;
85          }
86          else if( arg.equals(J2EE_CLIENT_PARAM) )
87          {
88             /* Set the j2ee.client system property so the AppContextFactory
89             sees what name the client app JNDI enc is bound under
90             */

91             clientName = args[i+1];
92             System.setProperty(javaURLContextFactory.J2EE_CLIENT_NAME_PROP, clientName);
93             log.info(javaURLContextFactory.J2EE_CLIENT_NAME_PROP + "=" + clientName);
94             i ++;
95          }
96          else if( arg.equals(LAUNCHERS_PARAM) )
97          {
98             launchers = args[i+1].split(",");
99             log.info(LAUNCHERS_PARAM + "=" + args[i+1]);
100             i ++;
101          }
102          else
103          {
104             newArgs.add(args[i]);
105          }
106       }
107
108       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
109       if( loader == null )
110          loader = AppClientMain.class.getClassLoader();
111       // Look for a manifest Main-Class
112
if (clientClass == null)
113       {
114          clientClass = getMainClassName(loader);
115          throw new IllegalArgumentException JavaDoc("Neither a Main-Class was found in the manifest, "
116                +"nor was a " + JBOSS_CLIENT_PARAM + " specified");
117       }
118       // If J2EE_CLIENT_NAME_PROP was not specified, look in the jar descriptors
119
if (clientName == null)
120       {
121          clientName = getClientName(loader);
122       }
123
124       String JavaDoc[] mainArgs = new String JavaDoc [newArgs.size()];
125       newArgs.toArray(mainArgs);
126
127       // Try each launcher in the order specified
128
for(String JavaDoc launcherName : launchers)
129       {
130          try
131          {
132             Class JavaDoc<AppClientLauncher> launcherClass = (Class JavaDoc<AppClientLauncher>) loader.loadClass(launcherName);
133             AppClientLauncher launcher = launcherClass.newInstance();
134             launcher.launch(clientClass, clientName, mainArgs);
135             break;
136          }
137          catch(Throwable JavaDoc t)
138          {
139             log.warn("Failed to launch using: "+launcherName, t);
140          }
141       }
142    }
143
144    /**
145     *
146     * @param loader - class loader used to load descriptors as resources
147     * @return
148     */

149    private static String JavaDoc getClientName(ClassLoader JavaDoc loader)
150       throws Exception JavaDoc
151    {
152       String JavaDoc clientName = null;
153       DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
154       DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
155       // Try META-INF/application-client.xml application-client@id first
156
URL JavaDoc appXmlURL = loader.getResource("META-INF/application-client.xml");
157       if( appXmlURL != null )
158       {
159          InputStream JavaDoc is = appXmlURL.openStream();
160          Document JavaDoc appXml = builder.parse(is);
161          is.close();
162          Element JavaDoc root = appXml.getDocumentElement();
163          clientName = root.getAttribute("id");
164          if( clientName != null )
165             return clientName;
166       }
167
168       // Try META-INF/jboss-client.xml jndi-name
169
URL JavaDoc jbossXmlURL = loader.getResource("META-INF/jboss-client.xml");
170       if( appXmlURL != null )
171       {
172          InputStream JavaDoc is = jbossXmlURL.openStream();
173          Document JavaDoc jbossXml = builder.parse(is);
174          is.close();
175          Element JavaDoc root = jbossXml.getDocumentElement();
176          NodeList JavaDoc children = root.getChildNodes();
177          for(int n = 0; n < children.getLength(); n ++)
178          {
179             Node JavaDoc node = children.item(n);
180             if( node.getLocalName().equals("jndi-name") )
181             {
182                clientName = node.getNodeValue();
183                return clientName;
184             }
185          }
186       }
187       // TODO: annotations on main class
188
return null;
189    }
190
191    /**
192     * Check the jar manifest for a Main-Class value.
193     *
194     * @param loader
195     * @return Main-Class value or null if none exists.
196     * @throws Exception
197     */

198    private static String JavaDoc getMainClassName(ClassLoader JavaDoc loader)
199       throws Exception JavaDoc
200    {
201       URL JavaDoc mfURL = loader.getResource("META-INF/MANIFEST.MF");
202       if(mfURL == null)
203       {
204          return null;
205       }
206
207       InputStream JavaDoc is = mfURL.openStream();
208       Manifest JavaDoc mf;
209       try
210       {
211          mf = new Manifest JavaDoc(is);
212       }
213       finally
214       {
215          is.close();
216       }
217       Attributes JavaDoc attrs = mf.getMainAttributes();
218       String JavaDoc mainClassName = attrs.getValue(Attributes.Name.MAIN_CLASS);
219       return mainClassName;
220    }
221
222 }
223
Popular Tags