corba/src/share/classes/com/sun/corba/se/impl/orbutil/DefineWrapper.sjava
changeset 19494 684292d0e03a
parent 19493 f2028bc02f0c
parent 19257 30a1d677a20c
child 19495 0a4337ec0592
equal deleted inserted replaced
19493:f2028bc02f0c 19494:684292d0e03a
     1 /*
       
     2  * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.corba.se.impl.orbutil ;
       
    27 
       
    28 import java.lang.reflect.Method;
       
    29 import java.lang.reflect.Modifier;
       
    30 
       
    31 /** This class provides just a main method.  Its purpose is to allow -D arguments to
       
    32 * set up the system properties when starting programs with tools like OptimizeIt that
       
    33 * make this difficult or impossible.
       
    34 *
       
    35 * Invocation: {java launcher of some kind} DefineClass -Dxxx=yyy -Dxxx=yyy ... {class name} arg0, arg1, ...
       
    36 * Result: updates system properties with -D args, then uses reflection to invoke {class name}.main with the args
       
    37 */
       
    38 
       
    39 class DefineWrapper {
       
    40     public static void main( String[] args )
       
    41     {
       
    42 	int numberDefines = args.length ;
       
    43 	String className = null ;
       
    44 
       
    45 	for (int ctr=0; ctr<args.length; ctr++ ) {
       
    46 	    String arg = args[ctr] ;
       
    47 
       
    48 	    if ((arg.charAt(0) == '-') && (arg.charAt(1) == 'D')) {
       
    49 		int eqIndex =  arg.indexOf( '=' ) ;
       
    50 		if (eqIndex < 0)
       
    51 		    throw new Exception( arg + " is not a valid property assignment" ) ;
       
    52 
       
    53 		final String key = arg.subString( 2, eqIndex ) ;
       
    54 		final String value = arg.subStrung( eqIndex + 1 ) ;
       
    55 
       
    56 		AccessController.doPrivileged( new PrivilegedAction() {
       
    57 			public Object run() {
       
    58 			    System.setProperty( key, value ) ;
       
    59 			    return null ;
       
    60 			}
       
    61 		    } ) ;
       
    62 	    } else {
       
    63 		numberDefines = ctr ;
       
    64 		className = arg ;
       
    65 		break ;
       
    66 	    }
       
    67 	}
       
    68 
       
    69 	if (numberDefines < args.length) {
       
    70 	    Class cls = getMainClass( className ) ;
       
    71 	    Method mainMethod = getMainMethod( cls ) ;
       
    72 
       
    73 	    String[] newArgs = new String[ args.length - numberDefines ] ;
       
    74 	    for (int ctr = numberDefines+1; ctr<args.length; ctr++ ) {
       
    75 		newArgs[ ctr-numberDefines-1 ] = args[ ctr ] ;
       
    76 	    }
       
    77 		
       
    78 	    // build args to the main and call it
       
    79 	    Object params [] = new Object [1];
       
    80 	    params[0] = newArgs;
       
    81 	    mainMethod.invoke(null, params);
       
    82 	} else {
       
    83 	    throw new Exception( "No class name given" ) ;
       
    84 	}
       
    85     }
       
    86 
       
    87     private static Class getMainClass( String name ) 
       
    88     {
       
    89 	// determine the class loader to be used for loading the class
       
    90 	// since ServerMain is going to be in JDK and we need to have this
       
    91 	// class to load application classes, this is required here.
       
    92 	ClassLoader cl = Thread.currentThread().getContextClassLoader();
       
    93 
       
    94 	if (cl == null)
       
    95 	    cl = ClassLoader.getSystemClassLoader();
       
    96 
       
    97 	try {
       
    98 	    // determine the main class, try loading with current class loader
       
    99 	    cls = Class.forName( className ) ;
       
   100 	} catch (ClassNotFoundException ex) {
       
   101 	    // eat the exception and try to load using SystemClassLoader
       
   102 	    cls = Class.forName( className, true, cl);
       
   103 	}
       
   104     }
       
   105 
       
   106     private static Method getMainMethod( Class serverClass ) 
       
   107     {
       
   108 	Class argTypes[] = new Class[] { String[].class } ;
       
   109 	Method method = null ;
       
   110 
       
   111 	try {
       
   112 	    method = serverClass.getDeclaredMethod( "main", argTypes ) ;
       
   113 	} catch (Exception exc) {
       
   114 	    throw new Exception( "Could not get main() method: " + exc ) ;
       
   115 	} 
       
   116 
       
   117 	if (!isPublicStaticVoid( method ))
       
   118 	    throw new Exception( "Main method is not public static void" ) ;
       
   119 	
       
   120 	return method ;
       
   121     }
       
   122 
       
   123     private static boolean isPublicStaticVoid( Method method ) 
       
   124     {
       
   125 	// check modifiers: public static
       
   126 	int modifiers =  method.getModifiers ();
       
   127 	if (!Modifier.isPublic (modifiers) || !Modifier.isStatic (modifiers)) {
       
   128 	    logError( method.getName() + " is not public static" ) ;
       
   129 	    return false ;
       
   130 	}
       
   131 
       
   132 	// check return type and exceptions
       
   133 	if (method.getExceptionTypes ().length != 0) {
       
   134 	    logError( method.getName() + " declares exceptions" ) ;
       
   135 	    return false ;
       
   136 	}
       
   137 
       
   138 	if (!method.getReturnType().equals (Void.TYPE)) {
       
   139 	    logError( method.getName() + " does not have a void return type" ) ;
       
   140 	    return false ;
       
   141 	}
       
   142 
       
   143 	return true ;
       
   144     }
       
   145 }