KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > activation > ServerMain


1 /*
2  * @(#)ServerMain.java 1.19 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * @(#)ServerMain.java 1.19 03/12/19
9  *
10  * Copyright 1993-1997 Sun Microsystems, Inc. 901 San Antonio Road,
11  * Palo Alto, California, 94303, U.S.A. All Rights Reserved.
12  *
13  * This software is the confidential and proprietary information of Sun
14  * Microsystems, Inc. ("Confidential Information"). You shall not
15  * disclose such Confidential Information and shall use it only in
16  * accordance with the terms of the license agreement you entered into
17  * with Sun.
18  *
19  * CopyrightVersion 1.2
20  *
21  */

22
23 package com.sun.corba.se.impl.activation;
24
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Modifier JavaDoc;
27 import java.io.*;
28 import java.util.Date JavaDoc;
29 import java.util.Properties JavaDoc ;
30
31 import org.omg.CORBA.ORB JavaDoc ;
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 /**
37  * @version 1.8, 99/11/02
38  * @author Ken Cavanaugh
39  * @since JDK1.2
40  */

41 public class ServerMain
42 {
43     /* TODO:
44     * 1. Rewrite all uses of ORB properties to use constants from someplace.
45     * The strings are scattered between here, the ORB classes, and
46     * ServerTableEntry.
47     * 2. Consider a more general log facility.
48     * 3. Remove ServerCallback from POAORB.
49     * 4. Needs to be merged with Harold's changes to support SSL.
50     * 5. Logs need to be internationalized.
51     */

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 JavaDoc 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     // redirect out and err streams
77
try {
78         String JavaDoc 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 JavaDoc 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 JavaDoc ex) {}
102     }
103
104     /** Write a time-stamped message to the indicated PrintStream.
105     */

106     private static void writeLogMessage( PrintStream pstream, String JavaDoc msg )
107     {
108     Date JavaDoc date = new Date JavaDoc();
109     pstream.print( "[" + date.toString() + "] " + msg + "\n");
110     }
111     
112     /** Write information to standard out only.
113     */

114     public static void logInformation( String JavaDoc msg )
115     {
116     writeLogMessage( System.out, " " + msg ) ;
117     }
118
119     /** Write error message to standard out and standard err.
120     */

121     public static void logError( String JavaDoc msg )
122     {
123     writeLogMessage( System.out, "ERROR: " + msg ) ;
124     writeLogMessage( System.err, "ERROR: " + msg ) ;
125     }
126
127     /** Write final message to log(s) and then terminate by calling
128     * System.exit( code ). If code == OK, write a normal termination
129     * message to standard out, otherwise write an abnormal termination
130     * message to standard out and standard error.
131     */

132     public static void logTerminal( String JavaDoc 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 JavaDoc getMainMethod( Class JavaDoc serverClass )
148     {
149     Class JavaDoc argTypes[] = new Class JavaDoc[] { String JavaDoc[].class } ;
150     Method JavaDoc method = null ;
151
152     try {
153         method = serverClass.getDeclaredMethod( "main", argTypes ) ;
154     } catch (Exception JavaDoc 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 JavaDoc method )
165     {
166     // check modifiers: public static
167
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     // check return type and exceptions
174
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 JavaDoc getNamedMethod( Class JavaDoc serverClass, String JavaDoc methodName )
188     {
189     Class JavaDoc argTypes[] = new Class JavaDoc[] { org.omg.CORBA.ORB JavaDoc.class } ;
190     Method JavaDoc method = null ;
191
192     try {
193         method = serverClass.getDeclaredMethod( methodName, argTypes ) ;
194     } catch (Exception JavaDoc exc) {
195         return null ;
196     }
197
198     if (!isPublicStaticVoid( method ))
199         return null ;
200
201     return method ;
202     }
203
204     private void run(String JavaDoc[] args)
205     {
206     try {
207         redirectIOStreams() ;
208         
209         String JavaDoc serverClassName = System.getProperty(
210         ORBConstants.SERVER_NAME_PROPERTY ) ;
211
212         // determine the class loader to be used for loading the class
213
// since ServerMain is going to be in JDK and we need to have this
214
// class to load application classes, this is required here.
215
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
216
217             if (cl == null)
218                 cl = ClassLoader.getSystemClassLoader();
219
220             // determine the main class
221
Class JavaDoc serverClass = null;
222
223         try {
224             // determine the main class, try loading with current class loader
225
serverClass = Class.forName( serverClassName ) ;
226         } catch (ClassNotFoundException JavaDoc ex) {
227                 // eat the exception and try to load using SystemClassLoader
228
serverClass = Class.forName( serverClassName, true, cl);
229             }
230
231         if (debug)
232         System.out.println("class " + serverClassName + " found");
233         
234         // get the main method
235
Method JavaDoc mainMethod = getMainMethod( serverClass ) ;
236
237         // This piece of code is required, to verify the server definition
238
// without launching it.
239

240             // verify the server
241

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         // build args to the main and call it
258
Object JavaDoc params [] = new Object JavaDoc [1];
259         params[0] = args;
260         mainMethod.invoke(null, params);
261
262         } catch (ClassNotFoundException JavaDoc e) {
263         logTerminal("ClassNotFound exception: " + e.getMessage(),
264         MAIN_CLASS_NOT_FOUND);
265     } catch (Exception JavaDoc e) {
266         logTerminal("Exception: " + e.getMessage(),
267         APPLICATION_ERROR);
268     }
269     }
270
271     public static void main(String JavaDoc[] 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 JavaDoc 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 JavaDoc serverClass )
289     {
290     Method JavaDoc installMethod = getNamedMethod( serverClass, "install" ) ;
291     Method JavaDoc uninstallMethod = getNamedMethod( serverClass, "uninstall" ) ;
292     Method JavaDoc shutdownMethod = getNamedMethod( serverClass, "shutdown" ) ;
293
294     Properties JavaDoc props = new Properties JavaDoc() ;
295     props.put( "org.omg.CORBA.ORBClass",
296         "com.sun.corba.se.impl.orb.ORBImpl" ) ;
297         // NOTE: Very important to pass this property, otherwise the
298
// Persistent Server registration will be unsucessfull.
299
props.put( ORBConstants.ACTIVATED_PROPERTY, "false" );
300     String JavaDoc args[] = null ;
301     ORB JavaDoc 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 JavaDoc 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 JavaDoc orb;
323     private Method JavaDoc installMethod ;
324     private Method JavaDoc uninstallMethod ;
325     private Method JavaDoc shutdownMethod ;
326     private Object JavaDoc methodArgs[] ;
327
328     ServerCallback(ORB JavaDoc orb, Method JavaDoc installMethod, Method JavaDoc uninstallMethod,
329     Method JavaDoc 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 JavaDoc[] { orb } ;
339     }
340
341     private void invokeMethod( Method JavaDoc method )
342     {
343     if (method != null)
344         try {
345         method.invoke( null, methodArgs ) ;
346         } catch (Exception JavaDoc exc) {
347         ServerMain.logError( "could not invoke " + method.getName() +
348             " method: " + exc.getMessage() ) ;
349         }
350     }
351
352     // shutdown the ORB and wait for completion
353
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