1 23 package com.sun.enterprise.appclient.jws.boot; 24 25 import com.sun.enterprise.appclient.Main; 26 import com.sun.enterprise.appclient.jws.Util; 27 import java.io.BufferedReader ; 28 import java.io.BufferedWriter ; 29 import java.io.File ; 30 import java.io.FileNotFoundException ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.InputStreamReader ; 35 import java.io.OutputStreamWriter ; 36 import java.lang.reflect.InvocationTargetException ; 37 import java.lang.reflect.Method ; 38 import java.net.InetAddress ; 39 import java.net.MalformedURLException ; 40 import java.net.URI ; 41 import java.net.URISyntaxException ; 42 import java.net.URL ; 43 import java.net.URLClassLoader ; 44 import java.security.Policy ; 45 import java.text.MessageFormat ; 46 import java.util.Arrays ; 47 import java.util.Vector ; 48 import java.util.jar.JarFile ; 49 import javax.swing.SwingUtilities ; 50 51 67 public class JWSACCMain implements Runnable { 68 69 72 73 private static final String PERMISSIONS_TEMPLATE_NAME = "jwsclient.policy"; 74 75 76 private static final String GRANT_CLAUSES_PROPERTY_EXPR = "${grant.clauses}"; 77 78 79 private static final String lineSep = System.getProperty("line.separator"); 80 81 82 private static Main accMain = null; 83 84 85 private static String jwsPolicyTemplateURL = null; 86 87 88 private static final String JWSACC_ARGUMENT_PREFIX = "-jwsacc"; 89 90 private static final String JWSACC_EXIT_AFTER_RETURN = "ExitAfterReturn"; 91 92 93 private static final String GRANT_CLAUSE_TEMPLATE = "grant codeBase \"{0}\" '{'\n" + 94 " permission java.security.AllPermission;\n" + 95 "'}';"; 96 97 103 private static boolean exitAfterReturn = false; 104 105 106 private static ClassPathManager classPathManager = null; 107 108 109 private static URL [] downloadedJarURLs; 110 111 112 private String args[]; 113 114 115 public JWSACCMain(String [] args) { 116 this.args = args; 117 } 118 119 122 public static void main(String [] args) { 123 try { 124 classPathManager = getClassPathManager(); 125 args = prepareJWSArgs(args); 126 downloadedJarURLs = classPathManager.locateDownloadedJars(); 127 } catch (Throwable thr) { 128 System.err.println("Error locating downloaded jars"); 129 thr.printStackTrace(); 130 System.exit(1); 131 } 132 133 137 setPermissions(); 138 139 143 JWSACCMain jwsACCMain = new JWSACCMain(args); 144 145 try { 146 SwingUtilities.invokeAndWait(jwsACCMain); 147 151 } catch (Throwable thr) { 152 System.err.println("Error attempting to launch JWSACCMain.main"); 153 thr.printStackTrace(System.err); 154 System.exit(1); 155 } 156 157 } 158 159 public void run() { 160 int exitValue = 0; 162 try { 163 File downloadedAppclientJarFile = findAppClientFileForJWSLaunch(getClass().getClassLoader()); 164 165 ClassLoader loader = prepareClassLoader(downloadedAppclientJarFile); 166 167 171 System.setProperty("com.sun.aas.downloaded.appclient.jar", downloadedAppclientJarFile.getAbsolutePath()); 172 173 Thread.currentThread().setContextClassLoader(loader); 174 175 179 Method mainMethod = null; 180 Class mainClass = Class.forName("com.sun.enterprise.appclient.Main", false , loader); 181 mainMethod = mainClass.getMethod( 182 "main", 183 new Class [] { String [].class } ); 184 Object params [] = new Object [1]; 185 params[0] = args; 186 mainMethod.invoke(null , params); 187 } catch(Throwable thr) { 188 exitValue = 1; 189 throw new RuntimeException (thr); 190 } finally { 191 196 if (exitAfterReturn) { 197 SwingUtilities.invokeLater(new Runnable () { 198 private int statusValue; 199 public void run() { 200 System.out.printf("Exiting after return from client with status %1$d%n", statusValue); 201 System.exit(statusValue); 202 } 203 204 public Runnable init(int exitStatus) { 205 statusValue = exitStatus; 206 return this; 207 } 208 }.init(exitValue)); 209 } 210 } 211 } 212 213 220 private static String [] prepareJWSArgs(String [] args) { 221 Vector <String > JWSACCArgs = new Vector <String >(); 222 Vector <String > nonJWSACCArgs = new Vector <String >(); 223 for (String arg : args) { 224 if (arg.startsWith(JWSACC_ARGUMENT_PREFIX)) { 225 JWSACCArgs.add(arg.substring(JWSACC_ARGUMENT_PREFIX.length())); 226 } else { 227 nonJWSACCArgs.add(arg); 228 } 229 } 230 231 processJWSArgs(JWSACCArgs); 232 return nonJWSACCArgs.toArray(new String [nonJWSACCArgs.size()]); 233 } 234 235 239 private static void processJWSArgs(Vector <String > args) { 240 for (String arg : args) { 241 if (arg.equals(JWSACC_EXIT_AFTER_RETURN)) { 242 exitAfterReturn = true; 243 } 244 } 245 } 246 247 private static void setPermissions() { 248 String JWSACCMainClassName = JWSACCMain.class.getName(); 249 try { 250 253 String permissionsTemplate = Util.loadResource(JWSACCMain.class, PERMISSIONS_TEMPLATE_NAME); 254 255 259 StringBuilder grantClauses = new StringBuilder (); 260 261 for (URL url : downloadedJarURLs) { 262 grantClauses.append(MessageFormat.format(GRANT_CLAUSE_TEMPLATE, url.toExternalForm())); 263 } 264 265 String substitutedPermissionsTemplate = permissionsTemplate.replace(GRANT_CLAUSES_PROPERTY_EXPR, grantClauses.toString()); 266 boolean retainTempFiles = Boolean.getBoolean(Main.APPCLIENT_RETAIN_TEMP_FILES_PROPERTYNAME); 267 File policyFile = writeTextToTempFile(substitutedPermissionsTemplate, "jwsacc", ".policy", retainTempFiles); 268 269 refreshPolicy(policyFile); 270 271 } catch (IOException ioe) { 272 throw new RuntimeException ("Error loading permissions template", ioe); 273 } 274 } 275 276 280 public static int firstFreePolicyIndex() { 281 int i = 0; 282 String propValue; 283 do { 284 propValue = java.security.Security.getProperty("policy.url." + String.valueOf(++i)); 285 } while ((propValue != null) && ( ! propValue.equals(""))); 286 287 return i; 288 } 289 290 295 public static void refreshPolicy(File policyFile) { 296 int idx = firstFreePolicyIndex(); 297 URI policyFileURI = policyFile.toURI(); 298 java.security.Security.setProperty("policy.url." + idx, policyFileURI.toASCIIString()); 299 Policy p = Policy.getPolicy(); 300 p.refresh(); 301 } 302 303 310 311 320 private static File writeTextToTempFile(String content, String prefix, String suffix, boolean retainTempFiles) throws IOException , FileNotFoundException { 321 BufferedWriter wtr = null; 322 try { 323 File result = File.createTempFile(prefix, suffix); 324 if ( ! retainTempFiles) { 325 result.deleteOnExit(); 326 } 327 FileOutputStream fos = new FileOutputStream (result); 328 wtr = new BufferedWriter (new OutputStreamWriter (fos)); 329 wtr.write(content); 330 wtr.close(); 331 return result; 332 } finally { 333 if (wtr != null) { 334 wtr.close(); 335 } 336 } 337 } 338 339 358 private static ClassLoader prepareClassLoader(File downloadedAppclientJarFile) throws IOException , URISyntaxException , ClassNotFoundException , NoSuchMethodException , IllegalAccessException , InvocationTargetException { 359 ClassLoader ldr = new URLClassLoader (downloadedJarURLs, classPathManager.getParentClassLoader()); 360 return ldr; 361 } 362 363 369 private static File findContainingJar(String target, ClassLoader loader) throws IllegalArgumentException , URISyntaxException , MalformedURLException , IllegalAccessException , InvocationTargetException { 370 373 URL resourceURL = loader.getResource(target); 374 return classPathManager.findContainingJar(resourceURL); 375 } 376 377 383 private File findAppClientFileForJWSLaunch(ClassLoader loader) throws URISyntaxException , MalformedURLException , IllegalAccessException , InvocationTargetException { 384 389 File containingJar = findContainingJar("META-INF/application.xml", loader); 390 if (containingJar == null) { 391 containingJar = findContainingJar("META-INF/application-client.xml", loader); 392 } 393 if (containingJar == null) { 394 throw new IllegalArgumentException ("Could not locate META-INF/application.xml or META-INF/application-client.xml"); 397 } 398 return containingJar; 399 } 400 401 405 public static ClassPathManager getClassPathManager() throws ClassNotFoundException , NoSuchMethodException { 406 return ClassPathManager.getClassPathManager(); 407 } 408 } 409 | Popular Tags |