1 22 package org.jboss.client; 23 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Properties ; 28 import java.util.jar.Attributes ; 29 import java.util.jar.Manifest ; 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 33 import org.jboss.logging.Logger; 34 import org.jboss.naming.client.java.javaURLContextFactory; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 40 46 public class AppClientMain 47 { 48 private static final Logger log = Logger.getLogger(AppClientMain.class); 50 51 52 public static final String JBOSS_CLIENT_PARAM = "-jbossclient"; 53 54 public static final String J2EE_CLIENT_PARAM = "-"+javaURLContextFactory.J2EE_CLIENT_NAME_PROP; 55 56 public static final String LAUNCHERS_PARAM = "-launchers"; 57 58 public static String [] DEFAULT_LAUNCHERS = {ReflectionLauncher.class.getName()}; 59 60 63 public static void main(String [] args) 64 throws Exception 65 { 66 log.debug("System Properties"); 67 Properties sysprops = System.getProperties(); 68 for (Object key : sysprops.keySet()) 69 log.debug(" " + key + "=" + sysprops.getProperty((String ) key)); 70 71 String clientClass = null; 73 String clientName = null; 74 ArrayList <String > newArgs = new ArrayList <String >(); 75 String [] launchers = DEFAULT_LAUNCHERS; 76 for (int i = 0; i < args.length; i++) 77 { 78 String 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 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 loader = Thread.currentThread().getContextClassLoader(); 109 if( loader == null ) 110 loader = AppClientMain.class.getClassLoader(); 111 if (clientClass == null) 113 { 114 clientClass = getMainClassName(loader); 115 throw new IllegalArgumentException ("Neither a Main-Class was found in the manifest, " 116 +"nor was a " + JBOSS_CLIENT_PARAM + " specified"); 117 } 118 if (clientName == null) 120 { 121 clientName = getClientName(loader); 122 } 123 124 String [] mainArgs = new String [newArgs.size()]; 125 newArgs.toArray(mainArgs); 126 127 for(String launcherName : launchers) 129 { 130 try 131 { 132 Class <AppClientLauncher> launcherClass = (Class <AppClientLauncher>) loader.loadClass(launcherName); 133 AppClientLauncher launcher = launcherClass.newInstance(); 134 launcher.launch(clientClass, clientName, mainArgs); 135 break; 136 } 137 catch(Throwable t) 138 { 139 log.warn("Failed to launch using: "+launcherName, t); 140 } 141 } 142 } 143 144 149 private static String getClientName(ClassLoader loader) 150 throws Exception 151 { 152 String clientName = null; 153 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 154 DocumentBuilder builder = factory.newDocumentBuilder(); 155 URL appXmlURL = loader.getResource("META-INF/application-client.xml"); 157 if( appXmlURL != null ) 158 { 159 InputStream is = appXmlURL.openStream(); 160 Document appXml = builder.parse(is); 161 is.close(); 162 Element root = appXml.getDocumentElement(); 163 clientName = root.getAttribute("id"); 164 if( clientName != null ) 165 return clientName; 166 } 167 168 URL jbossXmlURL = loader.getResource("META-INF/jboss-client.xml"); 170 if( appXmlURL != null ) 171 { 172 InputStream is = jbossXmlURL.openStream(); 173 Document jbossXml = builder.parse(is); 174 is.close(); 175 Element root = jbossXml.getDocumentElement(); 176 NodeList children = root.getChildNodes(); 177 for(int n = 0; n < children.getLength(); n ++) 178 { 179 Node node = children.item(n); 180 if( node.getLocalName().equals("jndi-name") ) 181 { 182 clientName = node.getNodeValue(); 183 return clientName; 184 } 185 } 186 } 187 return null; 189 } 190 191 198 private static String getMainClassName(ClassLoader loader) 199 throws Exception 200 { 201 URL mfURL = loader.getResource("META-INF/MANIFEST.MF"); 202 if(mfURL == null) 203 { 204 return null; 205 } 206 207 InputStream is = mfURL.openStream(); 208 Manifest mf; 209 try 210 { 211 mf = new Manifest (is); 212 } 213 finally 214 { 215 is.close(); 216 } 217 Attributes attrs = mf.getMainAttributes(); 218 String mainClassName = attrs.getValue(Attributes.Name.MAIN_CLASS); 219 return mainClassName; 220 } 221 222 } 223 | Popular Tags |