1 23 package com.sun.enterprise.appclient; 24 25 import com.sun.appserv.naming.S1ASCtxFactory; 26 import com.sun.enterprise.appclient.AppContainer; 27 import com.sun.enterprise.appclient.HttpAuthenticator; 28 import com.sun.enterprise.appclient.jws.TemplateCache; 29 import com.sun.enterprise.appclient.jws.Util; 30 import com.sun.enterprise.config.clientbeans.CertDb; 31 import com.sun.enterprise.config.clientbeans.ClientBeansFactory; 32 import com.sun.enterprise.config.clientbeans.ClientContainer; 33 import com.sun.enterprise.config.clientbeans.ClientCredential; 34 import com.sun.enterprise.config.clientbeans.ElementProperty; 35 import com.sun.enterprise.config.clientbeans.Security; 36 import com.sun.enterprise.config.clientbeans.Ssl; 37 import com.sun.enterprise.config.clientbeans.TargetServer; 38 import com.sun.enterprise.config.ConfigContext; 39 import com.sun.enterprise.config.ConfigException; 40 import com.sun.enterprise.config.ConfigFactory; 41 import com.sun.enterprise.connectors.ActiveResourceAdapter; 42 import com.sun.enterprise.connectors.ConnectorRegistry; 43 import com.sun.enterprise.connectors.ConnectorRuntime; 44 import com.sun.enterprise.deployment.*; 45 import com.sun.enterprise.deployment.archivist.AppClientArchivist; 46 import com.sun.enterprise.deployment.archivist.ApplicationArchivist; 47 import com.sun.enterprise.deployment.archivist.Archivist; 48 import com.sun.enterprise.deployment.archivist.ArchivistFactory; 49 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 50 import com.sun.enterprise.deployment.deploy.shared.Archive; 51 import com.sun.enterprise.deployment.deploy.shared.ArchiveFactory; 52 import com.sun.enterprise.deployment.deploy.shared.FileArchive; 53 import com.sun.enterprise.deployment.deploy.shared.InputJarArchive; 54 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapperFactory; 55 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapperFactoryMgr; 56 import com.sun.enterprise.InjectionManager; 57 import com.sun.enterprise.J2EESecurityManager; 58 import com.sun.enterprise.loader.EJBClassLoader; 59 import com.sun.enterprise.loader.InstrumentableClassLoader; 60 import com.sun.enterprise.naming.ProviderManager; 61 import com.sun.enterprise.security.GUIErrorDialog; 62 import com.sun.enterprise.security.SSLUtils; 63 import com.sun.enterprise.server.logging.ACCLogManager; 64 import com.sun.enterprise.Switch; 65 import com.sun.enterprise.util.FileUtil; 66 import com.sun.enterprise.util.i18n.StringManager; 67 import com.sun.enterprise.util.JarClassLoader; 68 import com.sun.enterprise.util.ORBManager; 69 import com.sun.enterprise.util.shared.ArchivistUtils; 70 import com.sun.enterprise.util.Utility; 71 import com.sun.logging.LogDomains; 72 import com.sun.web.server.HttpsURLStreamHandlerFactory; 73 74 import java.io.*; 75 import java.net.*; 76 import java.util.Iterator ; 77 import java.util.Set ; 78 import java.util.concurrent.atomic.AtomicInteger ; 79 import java.util.jar.JarFile ; 80 import java.util.logging.Handler ; 81 import java.util.logging.Level ; 82 import java.util.logging.LogRecord ; 83 import java.util.logging.Logger ; 84 import java.util.logging.LogManager ; 85 import java.util.Properties ; 86 import java.util.Vector ; 87 import java.util.logging.MemoryHandler ; 88 import java.util.logging.SimpleFormatter ; 89 import javax.enterprise.deploy.shared.ModuleType ; 90 import javax.naming.InitialContext ; 91 92 103 public class MainWithModuleSupport { 104 105 private static final String CLIENT = "-client"; 106 private static final String NAME = "-name"; 107 private static final String MAIN_CLASS = "-mainclass"; 108 private static final String TEXT_AUTH = "-textauth"; 109 private static final String XML_PATH = "-xml"; 110 private static final String ACC_CONFIG_XML = "-configxml"; 111 private static final String DEFAULT_CLIENT_CONTAINER_XML = "sun-acc.xml"; 112 private static final String SUNACC_XML_URL = "sun-acc.xml.url"; 114 private static final String NO_APP_INVOKE = "-noappinvoke"; 115 private static final String USER = "-user"; 117 private static final String PASSWORD = "-password"; 118 private static final String PASSWORD_FILE = "-passwordfile"; 119 private static final String LOGIN_NAME = "j2eelogin.name"; 120 private static final String LOGIN_PASSWORD = "j2eelogin.password"; 121 private static final String DASH = "-"; 122 123 private static final String lineSep = System.getProperty("line.separator"); 124 125 129 public static final String APPCLIENT_IIOP_DEFAULTHOST_PROPERTYNAME = "com.sun.aas.jws.iiop.defaultHost"; 130 public static final String APPCLIENT_IIOP_DEFAULTPORT_PROPERTYNAME = "com.sun.aas.jws.iiop.defaultPort"; 131 public static final String APPCLIENT_IIOP_FAILOVER_ENDPOINTS_PROPERTYNAME = "com.sun.aas.jws.iiop.failover.endpoints"; 132 public static final String APPCLIENT_PROBE_CLASSNAME_PROPERTYNAME = "com.sun.aas.jws.probeClassName"; 133 134 135 public static final String APPCLIENT_RETAIN_TEMP_FILES_PROPERTYNAME = "com.sun.aas.jws.retainTempFiles"; 136 137 138 public static final String APPCLIENT_ISJWS_PROPERTYNAME = "com.sun.aas.jws.isJWS"; 139 140 141 public static final String APPCLIENT_DOWNLOAD_HOST_PROPERTYNAME = "com.sun.aas.jws.download.host"; 142 143 148 public static final String SUN_ACC_SECURITY_CONFIG_PROPERTY = "security.config.file"; 149 150 151 private static final String LOGIN_CONF_FILE_PREFIX = "login"; 152 private static final String LOGIN_CONF_FILE_SUFFIX = ".conf"; 153 154 155 private static final String LOGIN_CONF_PROPERTY_NAME = "java.security.auth.login.config"; 156 157 158 private static final String DEFAULT_TEMPLATE_PREFIX = "jws/templates/"; 159 private static final String SUN_ACC_DEFAULT_TEMPLATE = DEFAULT_TEMPLATE_PREFIX + "default-sun-accTemplate.xml"; 160 private static final String WSS_CLIENT_CONFIG_TEMPLATE = DEFAULT_TEMPLATE_PREFIX + "default-wss-client-configTemplate.xml"; 161 private static final String LOGIN_CONF_TEMPLATE = DEFAULT_TEMPLATE_PREFIX + "appclientlogin.conf"; 162 163 164 private static final String WSS_CLIENT_CONFIG_PREFIX = "wsscc"; 165 private static final String WSS_CLIENT_CONFIG_SUFFIX = ".xml"; 166 private static final String SUN_ACC_PREFIX = "sunacc"; 167 private static final String SUN_ACC_SUFFIX = ".xml"; 168 169 protected static Logger _logger; 170 171 protected final boolean debug = false; 172 protected StringManager localStrings = 173 StringManager.getManager(MainWithModuleSupport.class); 174 protected boolean guiAuth = false; protected boolean runClient=true; 176 177 protected String host; 178 179 protected String port; 180 181 182 protected String [] args; 183 184 185 protected boolean isJWS; 186 187 188 protected boolean retainTempFiles = false; 189 190 protected String clientJar = null; 191 protected String displayName = null; 192 protected String mainClass = null; 193 protected String xmlPath = null; 194 protected String accConfigXml = null; 195 protected String jwsACCConfigXml = null; 196 protected Vector <String > appArgs = new Vector <String >(); 197 protected String classFileFromCommandLine = null; 198 protected boolean useTextAuth = false; 199 200 public MainWithModuleSupport(String [] args) { 201 202 206 BufferedLogger tempLogger = prepareBufferedLogging(); 207 208 212 _logger = tempLogger; 213 214 prepareJWSSettings(); 215 216 220 appArgs = processCommandLine(args); 221 222 226 String classNameForClassFile = determineClassNameForClassFileLaunch(clientJar, isJWS, mainClass, classFileFromCommandLine); 227 228 233 String xmlPathToUse = chooseConfigFile(xmlPath, accConfigXml, isJWS); 234 validateXMLFile(xmlPathToUse); 235 236 241 _logger = prepareLogging(tempLogger, xmlPathToUse, isJWS); 242 243 246 prepareJWSLoginConfig(); 247 248 Utility.checkJVMVersion(); 249 prepareSecurity(); 250 251 Throwable terminatingException = null; 252 AppClientInfo appClientInfo = null; 253 254 ClassLoader jcl = null; 255 256 try { 257 Switch.getSwitch().setProviderManager(ProviderManager.getProviderManager()); 258 setTargetServerProperties(xmlPathToUse); 260 261 int exitCode = 0; prepareURLStreamHandling(); 263 264 269 File appClientJarOrDir = locateAppclientJarOrDir(clientJar, classNameForClassFile, isJWS); 270 271 282 Properties iiopProperties = setupIIOP(); 283 InitialContext ic = new InitialContext (iiopProperties); 286 287 appClientInfo = AppClientInfoFactory.buildAppClientInfo(isJWS, _logger, appClientJarOrDir, mainClass, displayName, classFileFromCommandLine); 288 289 ApplicationClientDescriptor appDesc = appClientInfo.getAppClient(); 290 291 AppContainer container = createAppContainer(appDesc, guiAuth); 292 Cleanup cleanup = prepareShutdownCleanup(container, appClientInfo); 293 294 Authenticator.setDefault(new HttpAuthenticator(container)); 298 299 String appMainClass = container.preInvoke(iiopProperties); 300 301 304 jcl = appClientInfo.getClassLoader(); 305 Class cl = loadMainClientClass(jcl, appMainClass); 306 307 311 Thread.currentThread().setContextClassLoader(jcl); 312 313 System.setProperty("javax.xml.ws.spi.Provider", 316 "com.sun.enterprise.webservice.spi.ProviderImpl"); 317 318 InjectionManager injMgr = Switch.getSwitch().getInjectionManager(); 323 cleanup.setInjectionManager(injMgr, cl, appDesc); 324 325 injMgr.injectClass(cl, appDesc); 326 327 if(runClient) { 328 String [] applicationArgs = appArgs.toArray(new String [appArgs.size()]); 329 Utility.invokeApplicationMain(cl, applicationArgs); 330 _logger.info("Application main() returned; GUI elements may be continuing to run"); 331 } 332 333 334 } catch (java.lang.reflect.InvocationTargetException ite) { 338 Throwable tt = ite.getTargetException(); 339 _logger.log(Level.WARNING, "acc.app_exception", tt); 340 throw new RuntimeException (ite); 341 } catch (Throwable t) { 342 if (t instanceof javax.security.auth.login.FailedLoginException ){ 343 344 _logger.info("acc.login_error"); 345 boolean isGui = 346 Boolean.valueOf 347 (System.getProperty ("auth.gui","true")).booleanValue(); 348 String errorMessage = 349 localStrings.getString 350 ("main.exception.loginError", 351 "Incorrect login and/or password"); 352 353 if (isGui) { 354 GUIErrorDialog ged = new GUIErrorDialog (errorMessage); 355 ged.show (); 356 } 357 } 358 359 _logger.log(Level.WARNING, "acc.app_exception", t); 360 361 if (t instanceof javax.naming.NamingException ) { 362 _logger.log(Level.WARNING, "acc.naming_exception_received"); 363 } 364 throw new RuntimeException (t); 365 } finally { 366 shutDownSystemAdapters(); 367 } 375 } 376 377 382 private void prepareJWSSettings() { 383 isJWS = Boolean.getBoolean(APPCLIENT_ISJWS_PROPERTYNAME); 384 retainTempFiles = Boolean.getBoolean(APPCLIENT_RETAIN_TEMP_FILES_PROPERTYNAME); 385 } 386 387 390 private void prepareSecurity() { 391 395 if ( ! useTextAuth) { 396 guiAuth = Boolean.valueOf 397 (System.getProperty("auth.gui", "true")).booleanValue(); 398 } 399 400 401 SecurityManager secMgr = System.getSecurityManager(); 402 if (!isJWS && secMgr != null && 403 !(J2EESecurityManager.class.equals(secMgr.getClass()))) { 404 J2EESecurityManager mgr = new J2EESecurityManager(); 405 System.setSecurityManager(mgr); 406 } 407 if (_logger.isLoggable(Level.INFO)) { 408 if (secMgr != null) { 409 _logger.info("acc.secmgron"); 410 } else { 411 _logger.info("acc.secmgroff"); 412 } 413 } 414 415 com.sun.enterprise.security.KeyTool.initProvider(); 416 try{ 417 421 SSLUtils.initStoresAtStartup(); 422 } catch (Exception e){ 423 427 if(_logger.isLoggable(Level.FINER)){ 428 _logger.log(Level.FINER, "main.ssl_keystore_init_failed", e); 430 } else{ 431 _logger.log(Level.WARNING, "main.ssl_keystore_init_failed"); 433 } 434 } 435 } 436 437 444 private Class loadMainClientClass(ClassLoader jcl, String clientMainClassName) { 445 Class result = null; 446 try { 447 result = jcl.loadClass(clientMainClassName); 448 } catch (java.lang.ClassNotFoundException cnf) { 449 String errorMessage = localStrings.getString 450 ("appclient.mainclass.not.found", clientMainClassName); 451 throw new RuntimeException (errorMessage, cnf); 452 } 453 _logger.log(Level.INFO, "acc.load_app_class", clientMainClassName); 454 return result; 455 } 456 457 465 private Logger prepareLogging(BufferedLogger tempLogger, String xmlPathToUse, boolean isJWS) { 466 Logger result = LogDomains.getLogger(LogDomains.ACC_LOGGER); 468 469 LogManager logMgr = LogManager.getLogManager(); 470 if (logMgr instanceof ACCLogManager) { 471 ((ACCLogManager) logMgr).init(xmlPathToUse); 472 } 473 474 477 tempLogger.pushTo(result); 478 479 return result; 480 } 481 482 502 private Vector <String > processCommandLine(String [] args) { 503 Vector <String > clientArgs = new Vector <String >(); 504 505 AtomicInteger i = new AtomicInteger (); 506 String arg = null; 507 508 if(args.length < 1) { 510 usage(); 511 } else { 512 while(i.get() < args.length) { 513 arg = args[i.getAndIncrement()]; 514 if(arg.equals(CLIENT)) { 515 clientJar = getRequiredCommandOptionValue(args, CLIENT, i, "appclient.clientWithoutValue"); 516 } else if (arg.equals(NAME)) { 517 displayName = getRequiredCommandOptionValue(args, NAME, i, "appclient.nameWithoutValue"); 518 ensureAtMostOneOfNameAndMainClass(); 519 } else if(arg.equals(MAIN_CLASS)) { 520 mainClass = getRequiredCommandOptionValue(args, MAIN_CLASS, i, "appclient.mainClassWithoutValue"); 521 ensureAtMostOneOfNameAndMainClass(); 522 } else if(arg.equals(XML_PATH) ) { 523 xmlPath = getRequiredUniqueCommandOptionValue(args, XML_PATH, xmlPath, i, "appclient.xmlWithoutValue"); 524 } else if(arg.equals(ACC_CONFIG_XML) ) { 525 accConfigXml = getRequiredUniqueCommandOptionValue(args, ACC_CONFIG_XML, accConfigXml, i, "appclient.accConfigXmlWithoutValue"); 526 } else if(arg.equals(TEXT_AUTH)) { 527 useTextAuth = true; 529 logOption(TEXT_AUTH); 530 } else if(arg.equals(NO_APP_INVOKE)) { 531 runClient = false; 532 logOption(NO_APP_INVOKE); 533 } else if(arg.equals(USER)) { 534 String userNameValue = getRequiredCommandOptionValue(args, USER, i, "appclient.userWithoutValue"); 535 System.setProperty(LOGIN_NAME, userNameValue); 536 } else if(arg.equals(PASSWORD)) { 537 String passwordValue = getRequiredCommandOptionValue(args, PASSWORD, i, "appclient.passwordWithoutValue"); 538 System.setProperty(LOGIN_PASSWORD, passwordValue); 539 } else if (arg.equals(PASSWORD_FILE)) { 540 String passwordFileValue = getRequiredCommandOptionValue(args, PASSWORD_FILE, i, "appclient.passwordFileWithoutValue"); 541 try { 542 System.setProperty(LOGIN_PASSWORD, 543 loadPasswordFromFile(passwordFileValue)); 544 } catch(IOException ex) { 545 throw new IllegalArgumentException (localStrings.getString("appclient.errorReadingFromPasswordFile", passwordFileValue), ex); 546 } 547 } else { 548 clientArgs.add(arg); 549 logArgument(arg); 550 } 551 } 552 553 561 if ((mainClass == null) && (clientJar == null) && ! isJWS) { 562 567 if (clientArgs.size() > 0) { 568 classFileFromCommandLine = clientArgs.elementAt(0); 569 clientArgs.removeElementAt(0); 570 logClassFileArgument(classFileFromCommandLine); 571 } else { 572 usage(); 573 } 574 } 575 } 576 logClientArgs(clientArgs); 577 return clientArgs; 578 } 579 580 588 private String getRequiredCommandOptionValue(String [] args, String optionName, AtomicInteger position, String errorKey) { 589 String result = null; 590 595 if(position.get() < args.length && !args[position.get()].startsWith(DASH)) { 596 result = args[position.getAndIncrement()]; 597 } else { 598 throw new IllegalArgumentException (localStrings.getString(errorKey)); 599 } 600 if (_logger.isLoggable(Level.FINE)) { 601 _logger.fine(localStrings.getString("appclient.optionValueIs", optionName, result)); 602 } 603 return result; 604 } 605 606 617 private String getRequiredUniqueCommandOptionValue(String [] args, String optionName, String currentValue, AtomicInteger position, String errorKey) { 618 if (currentValue != null) { 619 throw new IllegalArgumentException (localStrings.getString("appclient.duplicateValue", optionName, currentValue)); 620 } 621 return getRequiredCommandOptionValue(args, optionName, position, errorKey); 622 } 623 628 private void ensureAtMostOneOfNameAndMainClass() { 629 if (mainClass != null && displayName != null) { 630 throw new IllegalArgumentException (localStrings.getString("appclient.mainclassOrNameNotBoth")); 631 } 632 } 633 634 638 private void logOption(String optionName) { 639 if (_logger.isLoggable(Level.FINE)) { 640 _logger.fine(localStrings.getString("appclient.valuelessOptionFound", optionName)); 641 } 642 } 643 644 private void logArgument(String arg) { 645 if (_logger.isLoggable(Level.FINE)) { 646 _logger.fine(localStrings.getString("appclient.argumentValueFound", arg)); 647 } 648 } 649 650 private void logClassFileArgument(String classFile) { 651 if (_logger.isLoggable(Level.FINE)) { 652 _logger.fine(localStrings.getString("appclient.classFileUsed", classFile)); 653 } 654 } 655 656 private void logClientArgs(Vector <String > clientArgs) { 657 if (_logger.isLoggable(Level.FINE)) { 658 _logger.fine(localStrings.getString("appclient.clientArgs", clientArgs.toString())); 659 } 660 } 661 662 672 private String determineClassNameForClassFileLaunch(String clientJar, boolean isJWS, String mainClass, String classFileFromCommandLine) { 673 String result = null; 674 boolean isFine = _logger.isLoggable(Level.FINE); 675 if(clientJar == null && ! isJWS) { 676 String value; 679 680 687 if (classFileFromCommandLine != null) { 688 value = classFileFromCommandLine; 689 if (isFine) { 690 _logger.fine(localStrings.getString("appclient.classNameFromArg", classFileFromCommandLine)); 691 } 692 } else { 693 value = mainClass; 694 if (isFine) { 695 _logger.fine(localStrings.getString("appclient.classNameFromMainClass", mainClass)); 696 } 697 } 698 if (value.endsWith(".class")) { 699 result = value.substring(0, value.length()-".class".length()); 700 } else { 701 result = value; 702 } 703 704 String path = result.replace('.', File.separatorChar) + ".class"; 705 File file = new File (path); 706 if (!file.isAbsolute()) { 707 file = new File (System.getProperty("user.dir"), path); 708 } 709 717 if (!file.exists()) { 718 throw new IllegalArgumentException (localStrings.getString("appclient.cannotFindClassFile", result, file.getAbsolutePath())); 719 } else { 720 if (isFine) { 721 _logger.fine(localStrings.getString("appclient.usingClassFile", file.getAbsolutePath())); 722 } 723 } 724 } 725 return result; 726 } 727 728 736 private String chooseConfigFile(String xmlPath, String accConfigXml, boolean isJWS) { 737 String pathToUse = null; 738 boolean isFine = _logger.isLoggable(Level.FINE); 739 740 if (xmlPath != null) { 741 pathToUse = xmlPath; 742 if (isFine) { 743 _logger.fine(localStrings.getString("appclient.configFrom", XML_PATH, pathToUse)); 744 } 745 } else if (accConfigXml != null ) { 746 pathToUse = accConfigXml; if (isFine) { 748 _logger.fine(localStrings.getString("appclient.configFrom", ACC_CONFIG_XML, pathToUse)); 749 } 750 } else if (isJWS) { 751 756 try { 757 String jwsACCConfigXml = prepareJWSConfig(); 758 if (jwsACCConfigXml != null) { 759 pathToUse = jwsACCConfigXml; 760 if (isFine) { 761 _logger.fine(localStrings.getString("appclient.configFromJWSTemplate")); 762 } 763 } 764 } catch (Throwable thr) { 765 throw new RuntimeException (localStrings.getString("appclient.errorPrepConfig"), thr); 766 } 767 } 768 return pathToUse; 769 } 770 771 779 private File locateAppclientJarOrDir(String clientJar, String className, boolean isJWS) throws ClassNotFoundException , URISyntaxException { 780 File result = null; 781 boolean isFine = _logger.isLoggable(Level.FINE); 782 790 if (isJWS) { 791 794 result = findAppClientFileForJWSLaunch(); 795 if (isFine) { 796 _logger.fine(localStrings.getString("appclient.JWSAppClientFile", result.getAbsolutePath())); 797 } 798 } else if (clientJar==null) { 799 802 File userDir = new File (System.getProperty("user.dir")); 803 File appClientClass = new File (userDir, className); 804 result = appClientClass.getParentFile(); 805 if (isFine) { 806 _logger.fine(localStrings.getString("appclient.classFileAppClientFile", result.getAbsolutePath())); 807 } 808 } else { 809 812 result = new File (clientJar); 813 if (isFine) { 814 _logger.fine(localStrings.getString("appclient.clientJarAppClientFile", result.getAbsolutePath())); 815 } 816 } 817 return result; 818 } 819 820 826 private ClassLoader preparePreliminaryClassLoader(File jarOrDir) throws MalformedURLException { 827 ClassLoader result = null; 828 URL[] urls = new URL[1]; 829 urls[0] = jarOrDir.toURI().toURL(); 830 835 ClassLoader currentCL = Thread.currentThread().getContextClassLoader(); 836 result = new URLClassLoader(urls, currentCL); 837 return result; 839 } 840 841 private AppContainer createAppContainer(ApplicationClientDescriptor appDesc, boolean guiAuth) { 842 AppContainer result = new AppContainer(appDesc, guiAuth); 843 if(result == null) { 844 _logger.log(Level.WARNING, "acc.no_client_desc", 845 (displayName == null) ? mainClass : displayName); 846 847 System.exit(1); 848 } 849 return result; 850 } 851 852 853 856 private static Cleanup prepareShutdownCleanup( 857 AppContainer container, AppClientInfo appClientInfo) { 858 Cleanup cleanup = new Cleanup(); 861 Runtime runtime = Runtime.getRuntime(); 862 runtime.addShutdownHook(cleanup); 863 cleanup.setAppContainer(container); 864 cleanup.setAppClientInfo(appClientInfo); 865 return cleanup; 866 } 867 868 871 private static void prepareURLStreamHandling() { 872 java.security.AccessController.doPrivileged(new 874 java.security.PrivilegedAction () { 875 public Object run() { 876 URL.setURLStreamHandlerFactory(new 877 HttpsURLStreamHandlerFactory()); 878 return null; 879 } 880 }); 881 } 882 883 private Properties setupIIOP() { 884 _logger.log(Level.INFO, "acc.orb_host_name", host); 886 _logger.log(Level.INFO, "acc.orb_port_number", port); 887 888 Properties props = new Properties (); 889 props.put("org.omg.CORBA.ORBInitialHost", host); 890 props.put("org.omg.CORBA.ORBInitialPort", port); 891 return props; 892 } 893 894 private void addPersistenceSupport(File location, ApplicationClientDescriptor appDesc, InstrumentableClassLoader ldr) { 895 896 } 897 898 private void setTargetServerProperties(String clientXmlLocation) 899 throws ConfigException { 900 try { 902 if(clientXmlLocation == null || clientXmlLocation.equals("")) { 903 clientXmlLocation = DEFAULT_CLIENT_CONTAINER_XML; 904 } 905 906 System.setProperty(SUNACC_XML_URL, clientXmlLocation); 908 _logger.log(Level.INFO, "acc.using_xml_location", clientXmlLocation); 909 910 ConfigContext ctx = ConfigFactory.createConfigContext( 911 clientXmlLocation, true, 912 false, false, 913 ClientContainer.class, 914 new ACCEntityResolver()); 915 916 ClientContainer cc = ClientBeansFactory.getClientBean(ctx); 917 918 host = cc.getTargetServer(0).getAddress(); 919 port = cc.getTargetServer(0).getPort(); 920 921 TargetServer[] tServer = cc.getTargetServer(); 923 String targetServerEndpoints = null; 924 if (tServer.length > 1) { 925 for (int i = 0; i < tServer.length; i++) { 926 if (targetServerEndpoints == null) { 927 targetServerEndpoints = tServer[i].getAddress() + 928 ":" + tServer[i].getPort(); 929 } else { 930 targetServerEndpoints = targetServerEndpoints + "," + 931 tServer[i].getAddress() + 932 ":" + tServer[i].getPort(); 933 } 934 } 935 } 936 937 setSSLData(cc); 938 939 ClientCredential cCrd = cc.getClientCredential(); 941 if(cCrd != null) { 942 if (System.getProperty(LOGIN_NAME) == null) { 945 _logger.config("using login name from client container xml..."); 946 System.setProperty(LOGIN_NAME, cCrd.getUserName()); 947 } 948 if (System.getProperty(LOGIN_PASSWORD) == null) { 949 _logger.config("using password from client container xml..."); 950 System.setProperty(LOGIN_PASSWORD, cCrd.getPassword()); 951 } 952 } 953 String endpoints_property = null; 954 ElementProperty[] props = cc.getElementProperty(); 956 for ( int i=0; i<props.length; i++ ) { 957 if ( props[i].getName().equals("ssl") ) { 958 if ( props[i].getValue().equals("required") ) { 959 (ORBManager.getCSIv2Props()).put(ORBManager.ORB_SSL_CLIENT_REQUIRED, 960 "true"); 961 } 962 } 963 if ( props[i].getName().equals(S1ASCtxFactory.LOAD_BALANCING_PROPERTY) ) { 964 System.setProperty(props[i].getName(),props[i].getValue()); 965 } 966 if ( props[i].getName().equals(S1ASCtxFactory.IIOP_ENDPOINTS_PROPERTY) ) { 967 endpoints_property = props[i].getValue().trim(); 968 969 } 970 } 971 972 976 String jwsEndpointsProperty = null; 977 if (endpoints_property == null) { 978 jwsEndpointsProperty = System.getProperty(Main.APPCLIENT_IIOP_FAILOVER_ENDPOINTS_PROPERTYNAME); 979 endpoints_property = jwsEndpointsProperty; 980 } 981 982 _logger.fine("targetServerEndpoints = " + targetServerEndpoints + 983 "endpoints_property = " + 984 endpoints_property); 985 if (targetServerEndpoints == null && endpoints_property != null) { 986 System.setProperty( 987 S1ASCtxFactory.IIOP_ENDPOINTS_PROPERTY, 988 endpoints_property); 989 990 995 if (jwsEndpointsProperty == null) { 996 _logger.warning("acc.targetserver.endpoints.warning"); 997 } 998 } else if (targetServerEndpoints != null && endpoints_property == null) { 999 System.setProperty( 1000 S1ASCtxFactory.IIOP_ENDPOINTS_PROPERTY, 1001 targetServerEndpoints.trim()); 1002 } else if (targetServerEndpoints != null && endpoints_property != null) { 1003 System.setProperty( 1004 S1ASCtxFactory.IIOP_ENDPOINTS_PROPERTY, 1005 targetServerEndpoints.trim() + "," + 1006 endpoints_property); 1007 1012 if (jwsEndpointsProperty == null) { 1013 _logger.warning("acc.targetserver.endpoints.warning"); 1014 } 1015 _logger.fine("System.getProperty(S1ASCtxFactory.IIOP_ENDPOINTS_PROPERTY) ==> " + System.getProperty(S1ASCtxFactory.IIOP_ENDPOINTS_PROPERTY)); 1016 } 1017 } catch (ConfigException t) { 1018 _logger.log(Level.WARNING,"acc.acc_xml_file_error" , 1019 new Object [] {clientXmlLocation, t.getMessage()}); 1020 _logger.log(Level.FINE, "exception : " + t.toString(), t); 1021 throw t; 1022 } 1023 } 1024 1025 private static void setSSLData(ClientContainer cc) { 1026 try { 1027 TargetServer tServer = cc.getTargetServer(0); 1029 Security security = tServer.getSecurity(); 1034 if (security == null) { 1035 _logger.fine("No Security input set in ClientContainer.xml"); 1036 return; 1038 } 1039 Ssl ssl = security.getSsl(); 1040 if (ssl == null) { 1041 _logger.fine("No SSL input set in ClientContainer.xml"); 1042 return; 1044 1045 } 1046 SSLUtils.setAppclientSsl(ssl); 1049 } catch (Exception ex) { 1050 1051 } 1052 } 1053 1054 1055 private void validateXMLFile(String xmlFullName) 1056 { 1057 if(xmlFullName == null || 1058 xmlFullName.startsWith("-")){ usage(); 1060 } 1061 try { 1062 File f = new File (xmlFullName); 1063 if((f != null) && f.exists() && f.isFile() && f.canRead()){ 1064 return; 1065 }else{ xmlMessage(xmlFullName); 1067 usage(); 1068 } 1069 } catch (Exception ex) { 1070 xmlMessage(xmlFullName); 1071 usage(); 1072 } 1073 } 1074 1075 private void shutDownSystemAdapters() { 1078 try { 1079 com.sun.enterprise.PoolManager poolmgr = 1080 Switch.getSwitch().getPoolManager(); 1081 if ( poolmgr != null ) { 1082 Switch.getSwitch().getPoolManager().killFreeConnectionsInPools(); 1083 } 1084 } catch( Exception e ) { 1085 } 1087 1088 try { 1089 ConnectorRegistry registry = ConnectorRegistry.getInstance(); 1090 ActiveResourceAdapter activeRar = registry.getActiveResourceAdapter 1091 (ConnectorRuntime.DEFAULT_JMS_ADAPTER); 1092 if (activeRar != null) { 1093 activeRar.destroy(); 1094 } 1095 } catch (Exception e) { 1096 _logger.fine("Exception caught while shutting down system adapter:"+e.getMessage()); 1098 } 1099 } 1100 1101 private void usage() { 1102 System.out.println(localStrings.getString("main.usage", 1103 "appclient [ -client <appjar> | <classfile> ] [-mainclass <appClass-name>|-name <display-name>] [-xml <xml>] [-textauth] [-user <username>] [-password <password>|-passwordfile <password-file>] [app-args]")); 1104 System.exit(1); 1105 } 1106 1107 private void xmlMessage(String xmlFullName) 1108 { 1109 System.out.println(localStrings.getString("main.cannot_read_clientContainer_xml", xmlFullName, 1110 "Client Container xml: " + xmlFullName + " not found or unable to read.\nYou may want to use the -xml option to locate your configuration xml.")); 1111 1112 } 1113 1114 private String loadPasswordFromFile(String fileName) 1115 throws IOException { 1116 InputStream inputStream = null; 1117 try { 1118 inputStream = new BufferedInputStream(new FileInputStream(fileName)); 1119 Properties props = new Properties (); 1120 props.load(inputStream); 1121 return props.getProperty("PASSWORD"); 1122 } finally { 1123 if (inputStream != null) { 1124 inputStream.close(); 1125 } 1126 } 1127 } 1128 1129 private static class Cleanup extends Thread { 1130 private AppContainer appContainer = null; 1131 private AppClientInfo appClientInfo = null; 1132 private boolean cleanedUp = false; 1133 private InjectionManager injectionMgr = null; 1134 private ApplicationClientDescriptor appClient = null; 1135 private Class cls = null; 1136 1137 public Cleanup() { 1138 } 1139 1140 public void setAppContainer(AppContainer container) { 1141 appContainer = container; 1142 } 1143 1144 public void setAppClientInfo(AppClientInfo info) { 1145 appClientInfo = info; 1146 } 1147 1148 public void setInjectionManager(InjectionManager injMgr, Class cls, ApplicationClientDescriptor appDesc) { 1149 injectionMgr = injMgr; 1150 this.cls = cls; 1151 appClient = appDesc; 1152 } 1153 1154 public void run() { 1155 _logger.info("Clean-up starting"); 1156 cleanUp(); 1157 } 1158 1159 public void cleanUp() { 1160 if( !cleanedUp ) { 1161 try { 1162 if( appContainer != null ) { 1163 appContainer.postInvoke(); 1164 } 1165 if ( appClientInfo != null ) { 1166 appClientInfo.close(); 1167 } 1168 if ( injectionMgr != null) { 1169 injectionMgr.invokeClassPreDestroy(cls, appClient); 1171 injectionMgr = null; 1172 } 1173 } 1174 catch(Throwable t) { 1175 } 1176 finally { 1177 cleanedUp = true; 1178 } 1179 } } 1181 } 1182 1187 private String prepareJWSConfig() throws IOException, FileNotFoundException { 1188 return prepareJWSDefaultConfig(); 1189 } 1190 1191 1196 private String prepareJWSDefaultConfig() throws IOException, FileNotFoundException { 1197 String result = null; 1198 1199 1202 String sunACCTemplate = Util.loadResource(this.getClass(), SUN_ACC_DEFAULT_TEMPLATE); 1203 String wssClientConfigTemplate = Util.loadResource(this.getClass(), WSS_CLIENT_CONFIG_TEMPLATE); 1204 1205 1210 Properties tokenValues = new Properties (System.getProperties()); 1211 1212 1215 String wssClientConfig = Util.replaceTokens(wssClientConfigTemplate, tokenValues); 1216 File wssClientConfigFile = Util.writeTextToTempFile(wssClientConfig, WSS_CLIENT_CONFIG_PREFIX, WSS_CLIENT_CONFIG_SUFFIX, retainTempFiles); 1217 _logger.fine("Temporary wss-client-config.xml file: " + wssClientConfigFile.getAbsolutePath() + lineSep); 1218 1220 1229 String quotedConfigFileSpec = wssClientConfigFile.getAbsolutePath().replaceAll("\\\\", "\\\\\\\\"); 1230 tokenValues.setProperty(SUN_ACC_SECURITY_CONFIG_PROPERTY, quotedConfigFileSpec); 1231 1232 String sunaccContent = Util.replaceTokens(sunACCTemplate, tokenValues); 1233 File sunaccFile = Util.writeTextToTempFile(sunaccContent, SUN_ACC_PREFIX, SUN_ACC_SUFFIX, retainTempFiles); 1234 _logger.fine("Temporary sun-acc.xml file: " + sunaccFile.getAbsolutePath()); 1235 1237 return sunaccFile.getAbsolutePath(); 1238 } 1239 1240 1244 private void prepareJWSLoginConfig() { 1245 1249 if (isJWS) { 1250 try { 1251 prepareJWSDefaultLoginConfig(); 1252 } catch (Throwable thr) { 1253 throw new RuntimeException (localStrings.getString("appclient.errorPrepJWSLogginConfig"), thr); 1254 } 1255 } 1256 } 1257 1258 1262 private void prepareJWSDefaultLoginConfig() throws IOException, FileNotFoundException { 1263 1274 String configContent = Util.loadResource(this.getClass(), LOGIN_CONF_TEMPLATE); 1275 File configFile = Util.writeTextToTempFile(configContent, LOGIN_CONF_FILE_PREFIX, LOGIN_CONF_FILE_SUFFIX, retainTempFiles); 1276 String configFilePath = configFile.getAbsolutePath(); 1277 _logger.fine("Temporary appclientlogin.conf file: " + configFilePath); 1278 System.setProperty(LOGIN_CONF_PROPERTY_NAME, configFilePath); 1279 } 1280 1281 1286 private File findContainingJar(String target) throws IllegalArgumentException , URISyntaxException { 1287 File result = null; 1288 1289 1292 URL resourceURL = getClass().getResource(target); 1293 if (resourceURL != null) { 1294 URI uri = resourceURL.toURI(); 1295 String scheme = uri.getScheme(); 1296 String ssp = uri.getSchemeSpecificPart(); 1297 if (scheme.equals("jar")) { 1298 1304 int colon = ssp.indexOf(':'); 1305 String subscheme = ssp.substring(0, colon); 1306 int excl = ssp.indexOf('!'); 1307 String containingJarPath = ssp.substring(colon + 1, excl); 1308 result = new File (containingJarPath); 1309 } else if (scheme.equals("file")) { 1310 1315 int resourceNamePosition = ssp.indexOf(target); 1316 String containingFilePath = ssp.substring(0, resourceNamePosition); 1317 result = new File (containingFilePath); 1318 } else { 1319 throw new IllegalArgumentException (resourceURL.toExternalForm()); 1320 } 1321 } 1322 return result; 1323 } 1324 1325 1329 private File findAppClientFileForJWSLaunch() throws URISyntaxException { 1330 1334 File containingJar = new File (System.getProperty("com.sun.aas.downloaded.appclient.jar")); 1335 _logger.fine("Location of appclient jar file: " + containingJar.getAbsolutePath()); 1336 return containingJar; 1337 } 1338 1339 1344 private BufferedLogger prepareBufferedLogging() { 1345 1348 BufferedLogger logger = new BufferedLogger(); 1349 return logger; 1350 } 1351 1352 1357 private class BufferedLogger extends Logger { 1358 1359 1362 public BufferedLogger() { 1363 super(null, null); 1364 addHandler(new BufferedHandler()); 1365 } 1366 1367 1371 public void pushTo(Logger target) { 1372 for (Handler handler : getHandlers()) { 1373 if (handler instanceof BufferedHandler) { 1374 ((BufferedHandler) handler).pushTo(target); 1375 } 1376 } 1377 } 1378 } 1379 1380 1385 private class BufferedHandler extends Handler { 1386 1387 1388 private Vector <LogRecord > buffer = new Vector <LogRecord >(); 1389 1390 1393 public BufferedHandler() { 1394 setLevel(Level.ALL); 1395 setFilter(null); 1396 setFormatter(new SimpleFormatter ()); 1397 } 1398 1399 1405 public synchronized void publish(LogRecord record) { 1406 if (buffer == null) { 1407 throw new IllegalStateException ("Handler asked to publish log record after pushTo invoked"); 1408 } 1409 buffer.add(record); 1410 } 1411 1412 1416 public synchronized void pushTo(Logger target) { 1417 if (buffer == null) { 1418 throw new IllegalStateException ("Handler asked to push to target more than once"); 1419 } 1420 for (LogRecord record : buffer) { 1421 target.log(record); 1422 } 1423 close(); 1424 } 1425 1426 1429 public void close() throws SecurityException { 1430 buffer.clear(); 1431 buffer = null; 1432 } 1433 1434 1438 public void flush() { 1439 } 1440 } 1441} 1442 | Popular Tags |