KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > Main


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: Main.java 2230 2005-10-11 03:14:03Z jcscoobyrs $
44  */

45 package org.openejb.server;
46
47 import java.io.File JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.net.URL JavaDoc;
51 import java.util.Properties JavaDoc;
52
53 import org.openejb.cli.CommandFinder;
54 import org.openejb.loader.SystemInstance;
55 import org.openejb.util.JarUtils;
56
57 /**
58  * This class will parse all the command line arguments then
59  * create a properties object and pass that into the server.
60  *
61  * If you'd like to start a server from code, you can just
62  * call the Server class directly with your own set of properties.
63  *
64  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
65  */

66 public class Main {
67     
68     private static final String JavaDoc helpBase = "META-INF/org.openejb.cli/";
69
70     public static void main (String JavaDoc args[]) {
71
72         try{
73             Properties JavaDoc props = parseArguments(args);
74             SystemInstance.init(props);
75             SystemInstance system = SystemInstance.get();
76             File JavaDoc libs = system.getHome().getDirectory("lib");
77             system.getClassPath().addJarsToPath(libs);
78             initServer(props);
79         } catch (DontStartServerException e){
80             // OK, we won't start the server then
81
} catch (Exception JavaDoc e){
82             e.printStackTrace();
83         }
84     }
85
86
87     private static Properties JavaDoc parseArguments(String JavaDoc args[]) throws DontStartServerException {
88         Properties JavaDoc props = new Properties JavaDoc();
89         props.putAll(System.getProperties());
90
91         // -- Set Defaults -- //
92
props.put("openejb.server.ip", "127.0.0.1");
93         props.put("openejb.server.port", "4201");
94         props.put("openejb.server.threads", "20");
95
96         // Setting the handler system property should be the first thing
97
// OpenEJB does.
98
JarUtils.setHandlerSystemProperty();
99
100         for (int i=0; i < args.length; i++) {
101             if (args[i].equals("-h")) {
102                 if (args.length > i+1 ) {
103                     props.setProperty("openejb.server.ip", args[++i]);
104                 }
105             } else if (args[i].equals("-p")) {
106                 if (args.length > i+1 ) {
107                     props.setProperty("openejb.server.port", args[++i]);
108                 }
109             } else if (args[i].equals("-t")) {
110                 if (args.length > i+1 ) {
111                     props.setProperty("openejb.server.threads", args[++i]);
112                 }
113             } else if (args[i].equals("-conf")) {
114                 if (args.length > i+1 ) {
115                     props.setProperty("openejb.configuration", args[++i]);
116                 }
117             } else if (args[i].equals("-l")) {
118                 if (args.length > i+1 ) {
119                     props.setProperty("log4j.configuration", args[++i]);
120                 }
121             } else if (args[i].equals("-d")) {
122                 if (args.length > i+1 ) {
123                     props.setProperty("openejb.home", args[++i]);
124                 }
125             } else if (args[i].equals("--admin-ip")) {
126                 if (args.length > i+1 ) {
127                     props.setProperty("openejb.server.admin-ip", args[++i]);
128                 }
129             } else if (args[i].startsWith("--local-copy")) {
130                 if (args[i].endsWith("false") ||
131                     args[i].endsWith("FALSE") ||
132                     args[i].endsWith("no") ||
133                     args[i].endsWith("NO") ) {
134                     props.setProperty("openejb.localcopy", "false");
135                 } else {
136                     props.setProperty("openejb.localcopy", "true");
137                 }
138             } else if (args[i].equals("--help")) {
139                 printHelp();
140                 throw new DontStartServerException();
141             } else if (args[i].equals("-version")) {
142                 printVersion();
143                 throw new DontStartServerException();
144             } else if (args[i].equals("-examples")) {
145                 printExamples();
146                 throw new DontStartServerException();
147             }
148         }
149
150         props.setProperty("org/openejb/configuration_factory", "org.openejb.alt.config.ConfigurationFactory");
151
152         return props;
153     }
154
155     private static void printVersion() {
156         /*
157          * Output startup message
158          */

159         Properties JavaDoc versionInfo = new Properties JavaDoc();
160
161         try {
162             JarUtils.setHandlerSystemProperty();
163             versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
164         } catch (java.io.IOException JavaDoc e) {
165         }
166         System.out.println( "OpenEJB Remote Server " + versionInfo.get( "version" ) +" build: "+versionInfo.get( "date" )+"-"+versionInfo.get( "time" ));
167         System.out.println( "" + versionInfo.get( "url" ) );
168     }
169
170     private static void printHelp() {
171         String JavaDoc header = "OpenEJB Remote Server ";
172         try {
173             JarUtils.setHandlerSystemProperty();
174             Properties JavaDoc versionInfo = new Properties JavaDoc();
175             versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
176             header += versionInfo.get( "version" );
177         } catch (java.io.IOException JavaDoc e) {
178         }
179
180         System.out.println( header );
181         
182         
183
184         // Internationalize this
185
try {
186             InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResource(helpBase + "start.help").openConnection().getInputStream();
187
188             int b = in.read();
189             while (b != -1) {
190                 System.out.write( b );
191                 b = in.read();
192             }
193         } catch (java.io.IOException JavaDoc e) {
194         }
195     }
196
197     private static void printExamples() {
198         String JavaDoc header = "OpenEJB Remote Server ";
199         try {
200             JarUtils.setHandlerSystemProperty();
201             Properties JavaDoc versionInfo = new Properties JavaDoc();
202             versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
203             header += versionInfo.get( "version" );
204         } catch (java.io.IOException JavaDoc e) {
205         }
206
207         System.out.println( header );
208
209         // Internationalize this
210
try {
211             InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResource(helpBase + "start.examples").openConnection().getInputStream();
212
213             int b = in.read();
214             while (b != -1) {
215                 System.out.write( b );
216                 b = in.read();
217             }
218         } catch (java.io.IOException JavaDoc e) {
219         }
220     }
221
222
223
224     private static void initServer(Properties JavaDoc props) throws Exception JavaDoc{
225         Server server = new Server();
226
227         server.init(props);
228     }
229 }
230
231 class DontStartServerException extends Exception JavaDoc{
232 }
233
234
Popular Tags