src/share/classes/com/sun/corba/se/impl/activation/ServerMain.java

Tue, 06 Nov 2012 15:50:14 +0000

author
coffeys
date
Tue, 06 Nov 2012 15:50:14 +0000
changeset 446
f4f39d873b9a
parent 158
91006f157c46
child 748
6845b95cba6b
permissions
-rw-r--r--

7201066: Change modifiers on unused fields
Reviewed-by: alanb, skoivu

duke@1 1 /*
coffeys@446 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@158 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@158 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@158 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@158 22 * or visit www.oracle.com if you need additional information or have any
ohair@158 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.corba.se.impl.activation;
duke@1 27
duke@1 28 import java.lang.reflect.Method;
duke@1 29 import java.lang.reflect.Modifier;
duke@1 30 import java.io.*;
duke@1 31 import java.util.Date;
duke@1 32 import java.util.Properties ;
duke@1 33
duke@1 34 import org.omg.CORBA.ORB ;
duke@1 35 import com.sun.corba.se.spi.activation.Activator ;
duke@1 36 import com.sun.corba.se.spi.activation.ActivatorHelper ;
duke@1 37 import com.sun.corba.se.impl.orbutil.ORBConstants ;
duke@1 38
duke@1 39 /**
duke@1 40 * @author Ken Cavanaugh
duke@1 41 * @since JDK1.2
duke@1 42 */
duke@1 43 public class ServerMain
duke@1 44 {
duke@1 45 /* TODO:
duke@1 46 * 1. Rewrite all uses of ORB properties to use constants from someplace.
duke@1 47 * The strings are scattered between here, the ORB classes, and
duke@1 48 * ServerTableEntry.
duke@1 49 * 2. Consider a more general log facility.
duke@1 50 * 3. Remove ServerCallback from POAORB.
duke@1 51 * 4. Needs to be merged with Harold's changes to support SSL.
duke@1 52 * 5. Logs need to be internationalized.
duke@1 53 */
duke@1 54
duke@1 55 public final static int OK = 0;
duke@1 56 public final static int MAIN_CLASS_NOT_FOUND = 1;
duke@1 57 public final static int NO_MAIN_METHOD = 2;
duke@1 58 public final static int APPLICATION_ERROR = 3;
duke@1 59 public final static int UNKNOWN_ERROR = 4;
duke@1 60 public final static int NO_SERVER_ID = 5 ;
duke@1 61 public final static int REGISTRATION_FAILED = 6;
duke@1 62
duke@1 63 public static String printResult( int result )
duke@1 64 {
duke@1 65 switch (result) {
duke@1 66 case OK : return "Server terminated normally" ;
duke@1 67 case MAIN_CLASS_NOT_FOUND : return "main class not found" ;
duke@1 68 case NO_MAIN_METHOD : return "no main method" ;
duke@1 69 case APPLICATION_ERROR : return "application error" ;
duke@1 70 case NO_SERVER_ID : return "server ID not defined" ;
duke@1 71 case REGISTRATION_FAILED: return "server registration failed" ;
duke@1 72 default : return "unknown error" ;
duke@1 73 }
duke@1 74 }
duke@1 75
duke@1 76 private void redirectIOStreams()
duke@1 77 {
duke@1 78 // redirect out and err streams
duke@1 79 try {
duke@1 80 String logDirName =
duke@1 81 System.getProperty( ORBConstants.DB_DIR_PROPERTY ) +
duke@1 82 System.getProperty("file.separator") +
duke@1 83 ORBConstants.SERVER_LOG_DIR +
duke@1 84 System.getProperty("file.separator");
duke@1 85
duke@1 86 File logDir = new File(logDirName);
duke@1 87 String server = System.getProperty(
duke@1 88 ORBConstants.SERVER_ID_PROPERTY ) ;
duke@1 89
duke@1 90 FileOutputStream foutStream =
duke@1 91 new FileOutputStream(logDirName + server+".out", true);
duke@1 92 FileOutputStream ferrStream =
duke@1 93 new FileOutputStream(logDirName + server+".err", true);
duke@1 94
duke@1 95 PrintStream pSout = new PrintStream(foutStream, true);
duke@1 96 PrintStream pSerr = new PrintStream(ferrStream, true);
duke@1 97
duke@1 98 System.setOut(pSout);
duke@1 99 System.setErr(pSerr);
duke@1 100
duke@1 101 logInformation( "Server started" ) ;
duke@1 102
duke@1 103 } catch (Exception ex) {}
duke@1 104 }
duke@1 105
duke@1 106 /** Write a time-stamped message to the indicated PrintStream.
duke@1 107 */
duke@1 108 private static void writeLogMessage( PrintStream pstream, String msg )
duke@1 109 {
duke@1 110 Date date = new Date();
duke@1 111 pstream.print( "[" + date.toString() + "] " + msg + "\n");
duke@1 112 }
duke@1 113
duke@1 114 /** Write information to standard out only.
duke@1 115 */
duke@1 116 public static void logInformation( String msg )
duke@1 117 {
duke@1 118 writeLogMessage( System.out, " " + msg ) ;
duke@1 119 }
duke@1 120
duke@1 121 /** Write error message to standard out and standard err.
duke@1 122 */
duke@1 123 public static void logError( String msg )
duke@1 124 {
duke@1 125 writeLogMessage( System.out, "ERROR: " + msg ) ;
duke@1 126 writeLogMessage( System.err, "ERROR: " + msg ) ;
duke@1 127 }
duke@1 128
duke@1 129 /** Write final message to log(s) and then terminate by calling
duke@1 130 * System.exit( code ). If code == OK, write a normal termination
duke@1 131 * message to standard out, otherwise write an abnormal termination
duke@1 132 * message to standard out and standard error.
duke@1 133 */
duke@1 134 public static void logTerminal( String msg, int code )
duke@1 135 {
duke@1 136 if (code == 0) {
duke@1 137 writeLogMessage( System.out, " " + msg ) ;
duke@1 138 } else {
duke@1 139 writeLogMessage( System.out, "FATAL: " +
duke@1 140 printResult( code ) + ": " + msg ) ;
duke@1 141
duke@1 142 writeLogMessage( System.err, "FATAL: " +
duke@1 143 printResult( code ) + ": " + msg ) ;
duke@1 144 }
duke@1 145
duke@1 146 System.exit( code ) ;
duke@1 147 }
duke@1 148
duke@1 149 private Method getMainMethod( Class serverClass )
duke@1 150 {
duke@1 151 Class argTypes[] = new Class[] { String[].class } ;
duke@1 152 Method method = null ;
duke@1 153
duke@1 154 try {
duke@1 155 method = serverClass.getDeclaredMethod( "main", argTypes ) ;
duke@1 156 } catch (Exception exc) {
duke@1 157 logTerminal( exc.getMessage(), NO_MAIN_METHOD ) ;
duke@1 158 }
duke@1 159
duke@1 160 if (!isPublicStaticVoid( method ))
duke@1 161 logTerminal( "", NO_MAIN_METHOD ) ;
duke@1 162
duke@1 163 return method ;
duke@1 164 }
duke@1 165
duke@1 166 private boolean isPublicStaticVoid( Method method )
duke@1 167 {
duke@1 168 // check modifiers: public static
duke@1 169 int modifiers = method.getModifiers ();
duke@1 170 if (!Modifier.isPublic (modifiers) || !Modifier.isStatic (modifiers)) {
duke@1 171 logError( method.getName() + " is not public static" ) ;
duke@1 172 return false ;
duke@1 173 }
duke@1 174
duke@1 175 // check return type and exceptions
duke@1 176 if (method.getExceptionTypes ().length != 0) {
duke@1 177 logError( method.getName() + " declares exceptions" ) ;
duke@1 178 return false ;
duke@1 179 }
duke@1 180
duke@1 181 if (!method.getReturnType().equals (Void.TYPE)) {
duke@1 182 logError( method.getName() + " does not have a void return type" ) ;
duke@1 183 return false ;
duke@1 184 }
duke@1 185
duke@1 186 return true ;
duke@1 187 }
duke@1 188
duke@1 189 private Method getNamedMethod( Class serverClass, String methodName )
duke@1 190 {
duke@1 191 Class argTypes[] = new Class[] { org.omg.CORBA.ORB.class } ;
duke@1 192 Method method = null ;
duke@1 193
duke@1 194 try {
duke@1 195 method = serverClass.getDeclaredMethod( methodName, argTypes ) ;
duke@1 196 } catch (Exception exc) {
duke@1 197 return null ;
duke@1 198 }
duke@1 199
duke@1 200 if (!isPublicStaticVoid( method ))
duke@1 201 return null ;
duke@1 202
duke@1 203 return method ;
duke@1 204 }
duke@1 205
duke@1 206 private void run(String[] args)
duke@1 207 {
duke@1 208 try {
duke@1 209 redirectIOStreams() ;
duke@1 210
duke@1 211 String serverClassName = System.getProperty(
duke@1 212 ORBConstants.SERVER_NAME_PROPERTY ) ;
duke@1 213
duke@1 214 // determine the class loader to be used for loading the class
duke@1 215 // since ServerMain is going to be in JDK and we need to have this
duke@1 216 // class to load application classes, this is required here.
duke@1 217 ClassLoader cl = Thread.currentThread().getContextClassLoader();
duke@1 218
duke@1 219 if (cl == null)
duke@1 220 cl = ClassLoader.getSystemClassLoader();
duke@1 221
duke@1 222 // determine the main class
duke@1 223 Class serverClass = null;
duke@1 224
duke@1 225 try {
duke@1 226 // determine the main class, try loading with current class loader
duke@1 227 serverClass = Class.forName( serverClassName ) ;
duke@1 228 } catch (ClassNotFoundException ex) {
duke@1 229 // eat the exception and try to load using SystemClassLoader
duke@1 230 serverClass = Class.forName( serverClassName, true, cl);
duke@1 231 }
duke@1 232
duke@1 233 if (debug)
duke@1 234 System.out.println("class " + serverClassName + " found");
duke@1 235
duke@1 236 // get the main method
duke@1 237 Method mainMethod = getMainMethod( serverClass ) ;
duke@1 238
duke@1 239 // This piece of code is required, to verify the server definition
duke@1 240 // without launching it.
duke@1 241
duke@1 242 // verify the server
duke@1 243
duke@1 244 boolean serverVerifyFlag = Boolean.getBoolean(
duke@1 245 ORBConstants.SERVER_DEF_VERIFY_PROPERTY) ;
duke@1 246 if (serverVerifyFlag) {
duke@1 247 if (mainMethod == null)
duke@1 248 logTerminal("", NO_MAIN_METHOD);
duke@1 249 else {
duke@1 250 if (debug)
duke@1 251 System.out.println("Valid Server");
duke@1 252 logTerminal("", OK);
duke@1 253 }
duke@1 254 }
duke@1 255
duke@1 256
duke@1 257 registerCallback( serverClass ) ;
duke@1 258
duke@1 259 // build args to the main and call it
duke@1 260 Object params [] = new Object [1];
duke@1 261 params[0] = args;
duke@1 262 mainMethod.invoke(null, params);
duke@1 263
duke@1 264 } catch (ClassNotFoundException e) {
duke@1 265 logTerminal("ClassNotFound exception: " + e.getMessage(),
duke@1 266 MAIN_CLASS_NOT_FOUND);
duke@1 267 } catch (Exception e) {
duke@1 268 logTerminal("Exception: " + e.getMessage(),
duke@1 269 APPLICATION_ERROR);
duke@1 270 }
duke@1 271 }
duke@1 272
duke@1 273 public static void main(String[] args) {
duke@1 274 ServerMain server = new ServerMain();
duke@1 275 server.run(args);
duke@1 276 }
duke@1 277
duke@1 278 private static final boolean debug = false;
duke@1 279
duke@1 280 private int getServerId()
duke@1 281 {
duke@1 282 Integer serverId = Integer.getInteger( ORBConstants.SERVER_ID_PROPERTY ) ;
duke@1 283
duke@1 284 if (serverId == null)
duke@1 285 logTerminal( "", NO_SERVER_ID ) ;
duke@1 286
duke@1 287 return serverId.intValue() ;
duke@1 288 }
duke@1 289
duke@1 290 private void registerCallback( Class serverClass )
duke@1 291 {
duke@1 292 Method installMethod = getNamedMethod( serverClass, "install" ) ;
duke@1 293 Method uninstallMethod = getNamedMethod( serverClass, "uninstall" ) ;
duke@1 294 Method shutdownMethod = getNamedMethod( serverClass, "shutdown" ) ;
duke@1 295
duke@1 296 Properties props = new Properties() ;
duke@1 297 props.put( "org.omg.CORBA.ORBClass",
duke@1 298 "com.sun.corba.se.impl.orb.ORBImpl" ) ;
duke@1 299 // NOTE: Very important to pass this property, otherwise the
duke@1 300 // Persistent Server registration will be unsucessfull.
duke@1 301 props.put( ORBConstants.ACTIVATED_PROPERTY, "false" );
duke@1 302 String args[] = null ;
duke@1 303 ORB orb = ORB.init( args, props ) ;
duke@1 304
duke@1 305 ServerCallback serverObj = new ServerCallback( orb,
duke@1 306 installMethod, uninstallMethod, shutdownMethod ) ;
duke@1 307
duke@1 308 int serverId = getServerId() ;
duke@1 309
duke@1 310 try {
duke@1 311 Activator activator = ActivatorHelper.narrow(
duke@1 312 orb.resolve_initial_references( ORBConstants.SERVER_ACTIVATOR_NAME ));
duke@1 313 activator.active(serverId, serverObj);
duke@1 314 } catch (Exception ex) {
duke@1 315 logTerminal( "exception " + ex.getMessage(),
duke@1 316 REGISTRATION_FAILED ) ;
duke@1 317 }
duke@1 318 }
duke@1 319 }
duke@1 320
duke@1 321 class ServerCallback extends
duke@1 322 com.sun.corba.se.spi.activation._ServerImplBase
duke@1 323 {
duke@1 324 private ORB orb;
coffeys@446 325 private transient Method installMethod ;
coffeys@446 326 private transient Method uninstallMethod ;
coffeys@446 327 private transient Method shutdownMethod ;
duke@1 328 private Object methodArgs[] ;
duke@1 329
duke@1 330 ServerCallback(ORB orb, Method installMethod, Method uninstallMethod,
duke@1 331 Method shutdownMethod )
duke@1 332 {
duke@1 333 this.orb = orb;
duke@1 334 this.installMethod = installMethod ;
duke@1 335 this.uninstallMethod = uninstallMethod ;
duke@1 336 this.shutdownMethod = shutdownMethod ;
duke@1 337
duke@1 338 orb.connect( this ) ;
duke@1 339
duke@1 340 methodArgs = new Object[] { orb } ;
duke@1 341 }
duke@1 342
duke@1 343 private void invokeMethod( Method method )
duke@1 344 {
duke@1 345 if (method != null)
duke@1 346 try {
duke@1 347 method.invoke( null, methodArgs ) ;
duke@1 348 } catch (Exception exc) {
duke@1 349 ServerMain.logError( "could not invoke " + method.getName() +
duke@1 350 " method: " + exc.getMessage() ) ;
duke@1 351 }
duke@1 352 }
duke@1 353
duke@1 354 // shutdown the ORB and wait for completion
duke@1 355 public void shutdown()
duke@1 356 {
duke@1 357 ServerMain.logInformation( "Shutdown starting" ) ;
duke@1 358
duke@1 359 invokeMethod( shutdownMethod ) ;
duke@1 360
duke@1 361 orb.shutdown(true);
duke@1 362
duke@1 363 ServerMain.logTerminal( "Shutdown completed", ServerMain.OK ) ;
duke@1 364 }
duke@1 365
duke@1 366 public void install()
duke@1 367 {
duke@1 368 ServerMain.logInformation( "Install starting" ) ;
duke@1 369
duke@1 370 invokeMethod( installMethod ) ;
duke@1 371
duke@1 372 ServerMain.logInformation( "Install completed" ) ;
duke@1 373 }
duke@1 374
duke@1 375 public void uninstall()
duke@1 376 {
duke@1 377 ServerMain.logInformation( "uninstall starting" ) ;
duke@1 378
duke@1 379 invokeMethod( uninstallMethod ) ;
duke@1 380
duke@1 381 ServerMain.logInformation( "uninstall completed" ) ;
duke@1 382 }
duke@1 383 }

mercurial