1 7 22 23 package com.sun.corba.se.impl.activation; 24 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Modifier ; 27 import java.io.*; 28 import java.util.Date ; 29 import java.util.Properties ; 30 31 import org.omg.CORBA.ORB ; 32 import com.sun.corba.se.spi.activation.Activator ; 33 import com.sun.corba.se.spi.activation.ActivatorHelper ; 34 import com.sun.corba.se.impl.orbutil.ORBConstants ; 35 36 41 public class ServerMain 42 { 43 52 53 public final static int OK = 0; 54 public final static int MAIN_CLASS_NOT_FOUND = 1; 55 public final static int NO_MAIN_METHOD = 2; 56 public final static int APPLICATION_ERROR = 3; 57 public final static int UNKNOWN_ERROR = 4; 58 public final static int NO_SERVER_ID = 5 ; 59 public final static int REGISTRATION_FAILED = 6; 60 61 public static String printResult( int result ) 62 { 63 switch (result) { 64 case OK : return "Server terminated normally" ; 65 case MAIN_CLASS_NOT_FOUND : return "main class not found" ; 66 case NO_MAIN_METHOD : return "no main method" ; 67 case APPLICATION_ERROR : return "application error" ; 68 case NO_SERVER_ID : return "server ID not defined" ; 69 case REGISTRATION_FAILED: return "server registration failed" ; 70 default : return "unknown error" ; 71 } 72 } 73 74 private void redirectIOStreams() 75 { 76 try { 78 String logDirName = 79 System.getProperty( ORBConstants.DB_DIR_PROPERTY ) + 80 System.getProperty("file.separator") + 81 ORBConstants.SERVER_LOG_DIR + 82 System.getProperty("file.separator"); 83 84 File logDir = new File(logDirName); 85 String server = System.getProperty( 86 ORBConstants.SERVER_ID_PROPERTY ) ; 87 88 FileOutputStream foutStream = 89 new FileOutputStream(logDirName + server+".out", true); 90 FileOutputStream ferrStream = 91 new FileOutputStream(logDirName + server+".err", true); 92 93 PrintStream pSout = new PrintStream(foutStream, true); 94 PrintStream pSerr = new PrintStream(ferrStream, true); 95 96 System.setOut(pSout); 97 System.setErr(pSerr); 98 99 logInformation( "Server started" ) ; 100 101 } catch (Exception ex) {} 102 } 103 104 106 private static void writeLogMessage( PrintStream pstream, String msg ) 107 { 108 Date date = new Date (); 109 pstream.print( "[" + date.toString() + "] " + msg + "\n"); 110 } 111 112 114 public static void logInformation( String msg ) 115 { 116 writeLogMessage( System.out, " " + msg ) ; 117 } 118 119 121 public static void logError( String msg ) 122 { 123 writeLogMessage( System.out, "ERROR: " + msg ) ; 124 writeLogMessage( System.err, "ERROR: " + msg ) ; 125 } 126 127 132 public static void logTerminal( String msg, int code ) 133 { 134 if (code == 0) { 135 writeLogMessage( System.out, " " + msg ) ; 136 } else { 137 writeLogMessage( System.out, "FATAL: " + 138 printResult( code ) + ": " + msg ) ; 139 140 writeLogMessage( System.err, "FATAL: " + 141 printResult( code ) + ": " + msg ) ; 142 } 143 144 System.exit( code ) ; 145 } 146 147 private Method getMainMethod( Class serverClass ) 148 { 149 Class argTypes[] = new Class [] { String [].class } ; 150 Method method = null ; 151 152 try { 153 method = serverClass.getDeclaredMethod( "main", argTypes ) ; 154 } catch (Exception exc) { 155 logTerminal( exc.getMessage(), NO_MAIN_METHOD ) ; 156 } 157 158 if (!isPublicStaticVoid( method )) 159 logTerminal( "", NO_MAIN_METHOD ) ; 160 161 return method ; 162 } 163 164 private boolean isPublicStaticVoid( Method method ) 165 { 166 int modifiers = method.getModifiers (); 168 if (!Modifier.isPublic (modifiers) || !Modifier.isStatic (modifiers)) { 169 logError( method.getName() + " is not public static" ) ; 170 return false ; 171 } 172 173 if (method.getExceptionTypes ().length != 0) { 175 logError( method.getName() + " declares exceptions" ) ; 176 return false ; 177 } 178 179 if (!method.getReturnType().equals (Void.TYPE)) { 180 logError( method.getName() + " does not have a void return type" ) ; 181 return false ; 182 } 183 184 return true ; 185 } 186 187 private Method getNamedMethod( Class serverClass, String methodName ) 188 { 189 Class argTypes[] = new Class [] { org.omg.CORBA.ORB .class } ; 190 Method method = null ; 191 192 try { 193 method = serverClass.getDeclaredMethod( methodName, argTypes ) ; 194 } catch (Exception exc) { 195 return null ; 196 } 197 198 if (!isPublicStaticVoid( method )) 199 return null ; 200 201 return method ; 202 } 203 204 private void run(String [] args) 205 { 206 try { 207 redirectIOStreams() ; 208 209 String serverClassName = System.getProperty( 210 ORBConstants.SERVER_NAME_PROPERTY ) ; 211 212 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 216 217 if (cl == null) 218 cl = ClassLoader.getSystemClassLoader(); 219 220 Class serverClass = null; 222 223 try { 224 serverClass = Class.forName( serverClassName ) ; 226 } catch (ClassNotFoundException ex) { 227 serverClass = Class.forName( serverClassName, true, cl); 229 } 230 231 if (debug) 232 System.out.println("class " + serverClassName + " found"); 233 234 Method mainMethod = getMainMethod( serverClass ) ; 236 237 240 242 boolean serverVerifyFlag = Boolean.getBoolean( 243 ORBConstants.SERVER_DEF_VERIFY_PROPERTY) ; 244 if (serverVerifyFlag) { 245 if (mainMethod == null) 246 logTerminal("", NO_MAIN_METHOD); 247 else { 248 if (debug) 249 System.out.println("Valid Server"); 250 logTerminal("", OK); 251 } 252 } 253 254 255 registerCallback( serverClass ) ; 256 257 Object params [] = new Object [1]; 259 params[0] = args; 260 mainMethod.invoke(null, params); 261 262 } catch (ClassNotFoundException e) { 263 logTerminal("ClassNotFound exception: " + e.getMessage(), 264 MAIN_CLASS_NOT_FOUND); 265 } catch (Exception e) { 266 logTerminal("Exception: " + e.getMessage(), 267 APPLICATION_ERROR); 268 } 269 } 270 271 public static void main(String [] args) { 272 ServerMain server = new ServerMain(); 273 server.run(args); 274 } 275 276 private static final boolean debug = false; 277 278 private int getServerId() 279 { 280 Integer serverId = Integer.getInteger( ORBConstants.SERVER_ID_PROPERTY ) ; 281 282 if (serverId == null) 283 logTerminal( "", NO_SERVER_ID ) ; 284 285 return serverId.intValue() ; 286 } 287 288 private void registerCallback( Class serverClass ) 289 { 290 Method installMethod = getNamedMethod( serverClass, "install" ) ; 291 Method uninstallMethod = getNamedMethod( serverClass, "uninstall" ) ; 292 Method shutdownMethod = getNamedMethod( serverClass, "shutdown" ) ; 293 294 Properties props = new Properties () ; 295 props.put( "org.omg.CORBA.ORBClass", 296 "com.sun.corba.se.impl.orb.ORBImpl" ) ; 297 props.put( ORBConstants.ACTIVATED_PROPERTY, "false" ); 300 String args[] = null ; 301 ORB orb = ORB.init( args, props ) ; 302 303 ServerCallback serverObj = new ServerCallback( orb, 304 installMethod, uninstallMethod, shutdownMethod ) ; 305 306 int serverId = getServerId() ; 307 308 try { 309 Activator activator = ActivatorHelper.narrow( 310 orb.resolve_initial_references( ORBConstants.SERVER_ACTIVATOR_NAME )); 311 activator.active(serverId, serverObj); 312 } catch (Exception ex) { 313 logTerminal( "exception " + ex.getMessage(), 314 REGISTRATION_FAILED ) ; 315 } 316 } 317 } 318 319 class ServerCallback extends 320 com.sun.corba.se.spi.activation._ServerImplBase 321 { 322 private ORB orb; 323 private Method installMethod ; 324 private Method uninstallMethod ; 325 private Method shutdownMethod ; 326 private Object methodArgs[] ; 327 328 ServerCallback(ORB orb, Method installMethod, Method uninstallMethod, 329 Method shutdownMethod ) 330 { 331 this.orb = orb; 332 this.installMethod = installMethod ; 333 this.uninstallMethod = uninstallMethod ; 334 this.shutdownMethod = shutdownMethod ; 335 336 orb.connect( this ) ; 337 338 methodArgs = new Object [] { orb } ; 339 } 340 341 private void invokeMethod( Method method ) 342 { 343 if (method != null) 344 try { 345 method.invoke( null, methodArgs ) ; 346 } catch (Exception exc) { 347 ServerMain.logError( "could not invoke " + method.getName() + 348 " method: " + exc.getMessage() ) ; 349 } 350 } 351 352 public void shutdown() 354 { 355 ServerMain.logInformation( "Shutdown starting" ) ; 356 357 invokeMethod( shutdownMethod ) ; 358 359 orb.shutdown(true); 360 361 ServerMain.logTerminal( "Shutdown completed", ServerMain.OK ) ; 362 } 363 364 public void install() 365 { 366 ServerMain.logInformation( "Install starting" ) ; 367 368 invokeMethod( installMethod ) ; 369 370 ServerMain.logInformation( "Install completed" ) ; 371 } 372 373 public void uninstall() 374 { 375 ServerMain.logInformation( "uninstall starting" ) ; 376 377 invokeMethod( uninstallMethod ) ; 378 379 ServerMain.logInformation( "uninstall completed" ) ; 380 } 381 } 382 | Popular Tags |