KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > start > Start


1 /*
2  * $Id: Start.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.start;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36 import java.net.ConnectException JavaDoc;
37 import java.net.InetAddress JavaDoc;
38 import java.net.ServerSocket JavaDoc;
39 import java.net.Socket JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Properties JavaDoc;
44
45 /**
46  * Start - OFBiz Container(s) Startup Class
47  *
48  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
49  * @version $Rev: 5462 $
50  * @since 2.1
51  */

52 public class Start implements Runnable JavaDoc {
53
54     private Classpath classPath = new Classpath(System.getProperty("java.class.path"));
55     private ClassLoader JavaDoc classloader = null;
56     private ServerSocket JavaDoc serverSocket = null;
57     private Thread JavaDoc serverThread = null;
58     private boolean serverRunning = true;
59     private List JavaDoc loaders = null;
60     private Config config = null;
61     private String JavaDoc[] loaderArgs = null;
62
63     private static final String JavaDoc SHUTDOWN_COMMAND = "SHUTDOWN";
64     private static final String JavaDoc STATUS_COMMAND = "STATUS";
65     private static final double REQUIRED_JDK = 1.4;
66
67     public void init(String JavaDoc[] args, boolean fullInit) throws IOException JavaDoc {
68         String JavaDoc firstArg = args.length > 0 ? args[0] : "";
69         String JavaDoc cfgFile = Start.getConfigFileName(firstArg);
70
71         this.loaders = new ArrayList JavaDoc();
72         this.config = new Config();
73
74         // read the default properties first
75
config.readConfig(cfgFile);
76
77         // parse the startup arguments
78
if (args.length > 1) {
79             this.loaderArgs = new String JavaDoc[args.length - 1];
80             for (int i = 1; i < args.length; i++) {
81                 this.loaderArgs[i - 1] = args[i];
82             }
83         }
84
85         if (fullInit) {
86             // initialize the classpath
87
initClasspath();
88
89             // initialize the log directory
90
initLogDirectory();
91
92             // initialize the listener thread
93
initListenerThread();
94
95             // initialize the startup loaders
96
initStartLoaders();
97
98             // set the shutdown hook
99
if (config.useShutdownHook) {
100                 setShutdownHook();
101             } else {
102                 System.out.println("Shutdown hook disabled");
103             }
104         }
105     }
106
107     public void init(String JavaDoc[] args) throws IOException JavaDoc {
108         init(args, true);
109     }
110
111     public void run() {
112         while (serverRunning) {
113             try {
114                 Socket JavaDoc clientSocket = serverSocket.accept();
115                 System.out.println("Received connection from - " + clientSocket.getInetAddress() + " : " + clientSocket.getPort());
116                 processClientRequest(clientSocket);
117                 clientSocket.close();
118             } catch (IOException JavaDoc e) {
119                 e.printStackTrace();
120             }
121         }
122         shutdownServer();
123         System.exit(0);
124     }
125
126     private void processClientRequest(Socket JavaDoc client) throws IOException JavaDoc {
127         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(client.getInputStream()));
128         String JavaDoc request = reader.readLine();
129
130         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(client.getOutputStream(), true);
131         writer.println(processRequest(request, client));
132         writer.flush();
133
134         writer.close();
135         reader.close();
136     }
137
138     private String JavaDoc processRequest(String JavaDoc request, Socket JavaDoc client) {
139         if (request != null) {
140             String JavaDoc key = request.substring(0, request.indexOf(':'));
141             String JavaDoc command = request.substring(request.indexOf(':') + 1);
142             if (!key.equals(config.adminKey)) {
143                 return "FAIL";
144             } else {
145                 if (command.equals(Start.SHUTDOWN_COMMAND)) {
146                     System.out.println("Shutdown initiated from: " + client.getInetAddress().getHostAddress() + ":" + client.getPort());
147                     serverRunning = false;
148                 } else if (command.equals(Start.STATUS_COMMAND)) {
149                     return serverRunning ? "Running" : "Stopped";
150                 }
151                 return "OK";
152             }
153         } else {
154             return "FAIL";
155         }
156     }
157
158     private void initListenerThread() throws IOException JavaDoc {
159         if (config.adminPort > 0) {
160             this.serverSocket = new ServerSocket JavaDoc(config.adminPort, 1, config.adminAddress);
161             this.serverThread = new Thread JavaDoc(this, this.toString());
162             this.serverThread.setDaemon(false);
163             System.out.println("Admin socket configured on - " + config.adminAddress + ":" + config.adminPort);
164         } else {
165             System.out.println("Admin socket not configured; set to port 0");
166         }
167     }
168
169     private void startListenerThread() {
170         if (serverSocket != null && serverThread != null) {
171             this.serverThread.start();
172         }
173     }
174
175     private void loadLibs(String JavaDoc path, boolean recurse) throws IOException JavaDoc {
176         File JavaDoc libDir = new File JavaDoc(path);
177         if (libDir.exists()) {
178             File JavaDoc files[] = libDir.listFiles();
179             for (int i = 0; i < files.length; i++) {
180                 String JavaDoc fileName = files[i].getName();
181                 if (files[i].isDirectory() && !"CVS".equals(fileName) && recurse) {
182                     loadLibs(files[i].getCanonicalPath(), recurse);
183                 } else if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) {
184                     classPath.addComponent(files[i]);
185                 }
186             }
187         }
188     }
189
190     private void initClasspath() throws IOException JavaDoc {
191         // load tools.jar
192
if (config.toolsJar != null) {
193             classPath.addComponent(config.toolsJar);
194         }
195
196         // load comm.jar
197
if (config.commJar != null) {
198             classPath.addComponent(config.commJar);
199         }
200
201         // add OFBIZ_HOME to CP & load libs
202
classPath.addClasspath(config.ofbizHome);
203         loadLibs(config.ofbizHome, false);
204
205         // load the lib directory
206
if (config.baseLib != null) {
207             loadLibs(config.baseLib, true);
208         }
209
210         // load the ofbiz-base.jar
211
if (config.baseJar != null) {
212             classPath.addComponent(config.baseJar);
213         }
214
215         // load the base schema directory
216
if (config.baseDtd != null) {
217             classPath.addComponent(config.baseDtd);
218         }
219
220         // load the config directory
221
if (config.baseConfig != null) {
222             classPath.addComponent(config.baseConfig);
223         }
224
225         // set the classpath/classloader
226
System.setProperty("java.class.path", classPath.toString());
227         this.classloader = classPath.getClassLoader();
228         Thread.currentThread().setContextClassLoader(classloader);
229         if (System.getProperty("DEBUG") != null) {
230             System.out.println("Startup Classloader: " + classloader.toString());
231             System.out.println("Startup Classpath: " + classPath.toString());
232         }
233     }
234
235     private void initLogDirectory() {
236         // stat the log directory
237
boolean createdDir = false;
238         File JavaDoc logDir = new File JavaDoc(config.logDir);
239         if (!logDir.exists()) {
240             logDir.mkdir();
241             createdDir = true;
242         }
243
244         if (createdDir) {
245             System.out.println("Created OFBiz log dir [" + logDir.getAbsolutePath() + "]");
246         }
247     }
248
249     private void initStartLoaders() {
250         // initialize the loaders
251
Iterator JavaDoc li = config.loaders.iterator();
252         while (li.hasNext()) {
253             String JavaDoc loaderClassName = (String JavaDoc) li.next();
254             try {
255                 Class JavaDoc loaderClass = classloader.loadClass(loaderClassName);
256                 StartupLoader loader = (StartupLoader) loaderClass.newInstance();
257                 loader.load(config, loaderArgs);
258                 loaders.add(loader);
259             } catch (Exception JavaDoc e) {
260                 e.printStackTrace();
261                 System.exit(99);
262             }
263         }
264     }
265
266     private void startStartLoaders() {
267         // start the loaders
268
Iterator JavaDoc i = loaders.iterator();
269         while (i.hasNext()) {
270             StartupLoader loader = (StartupLoader) i.next();
271             try {
272                 loader.start();
273             } catch (StartupException e) {
274                 e.printStackTrace();
275                 System.exit(99);
276             }
277         }
278     }
279
280     private void setShutdownHook() {
281         try {
282             Method JavaDoc shutdownHook = java.lang.Runtime JavaDoc.class.getMethod("addShutdownHook", new Class JavaDoc[]{java.lang.Thread JavaDoc.class});
283             Thread JavaDoc hook = new Thread JavaDoc() {
284                 public void run() {
285                     setName("OFBiz_Shutdown_Hook");
286                     shutdownServer();
287                     // Try to avoid JVM crash
288
try {
289                         Thread.sleep(1000);
290                     } catch (Exception JavaDoc e) {
291                         e.printStackTrace();
292                     }
293                 }
294             };
295
296             shutdownHook.invoke(Runtime.getRuntime(), new Object JavaDoc[]{hook});
297         } catch (Exception JavaDoc e) {
298             // VM Does not support shutdown hook
299
e.printStackTrace();
300         }
301     }
302
303     private void shutdownServer() {
304         if (loaders != null && loaders.size() > 0) {
305             Iterator JavaDoc i = loaders.iterator();
306             while (i.hasNext()) {
307                 StartupLoader loader = (StartupLoader) i.next();
308                 try {
309                     loader.unload();
310                 } catch (Exception JavaDoc e) {
311                     e.printStackTrace();
312                 }
313             }
314         }
315         serverRunning = false;
316     }
317
318     private void startServer() {
319         // start the listener thread
320
startListenerThread();
321
322         // start the startup loaders
323
startStartLoaders();
324     }
325
326     public void start() {
327         startServer();
328         if (config.shutdownAfterLoad) {
329             shutdownServer();
330             System.exit(0);
331         }
332     }
333
334     public void stop() {
335         shutdownServer();
336     }
337
338     public void destroy() {
339         this.serverSocket = null;
340         this.serverThread = null;
341         this.loaders = null;
342         this.config = null;
343         this.loaderArgs = null;
344     }
345
346     public String JavaDoc shutdown() throws IOException JavaDoc {
347         return sendSocketCommand(Start.SHUTDOWN_COMMAND);
348     }
349
350     public String JavaDoc status() throws IOException JavaDoc {
351         String JavaDoc status = null;
352         try {
353             status = sendSocketCommand(Start.STATUS_COMMAND);
354         } catch (ConnectException JavaDoc e) {
355             return "Not Running";
356         } catch (IOException JavaDoc e) {
357             throw e;
358         }
359         return status;
360     }
361
362     private String JavaDoc sendSocketCommand(String JavaDoc command) throws IOException JavaDoc, ConnectException JavaDoc {
363         Socket JavaDoc socket = new Socket JavaDoc(config.adminAddress, config.adminPort);
364
365         // send the command
366
PrintWriter JavaDoc writer = new PrintWriter JavaDoc(socket.getOutputStream(), true);
367         writer.println(config.adminKey + ":" + command);
368         writer.flush();
369
370         // read the reply
371
BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(socket.getInputStream()));
372         String JavaDoc response = null;
373         if (reader.ready()) {
374             response = reader.readLine();
375         }
376
377         reader.close();
378
379         // close the socket
380
writer.close();
381         socket.close();
382
383         return response;
384     }
385
386     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
387         String JavaDoc firstArg = args.length > 0 ? args[0] : "";
388         Start start = new Start();
389
390         if (firstArg.equals("-help") || firstArg.equals("-?")) {
391             System.out.println("");
392             System.out.println("Usage: java -jar ofbiz.jar [command] [arguments]");
393             System.out.println("-help, -? ----> This screen");
394             System.out.println("-install -----> Run install (create tables/load data)");
395             System.out.println("-setup -------> Run external application server setup");
396             System.out.println("-start -------> Start the server");
397             System.out.println("-status ------> Status of the server");
398             System.out.println("-shutdown ----> Shutdown the server");
399             System.out.println("-test --------> Run the JUnit test script");
400             System.out.println("[no config] --> Use default config");
401             System.out.println("[no command] -> Start the server w/ default config");
402         } else {
403             // hack for the status and shutdown commands
404
if (firstArg.equals("-status")) {
405                 start.init(args, false);
406                 System.out.println("Current Status : " + start.status());
407             } else if (firstArg.equals("-shutdown")) {
408                 start.init(args, false);
409                 System.out.println("Shutting down server : " + start.shutdown());
410             } else {
411                 // general start
412
start.init(args, true);
413                 start.start();
414             }
415         }
416     }
417
418     private static String JavaDoc getConfigFileName(String JavaDoc command) {
419         // default command is "start"
420
if (command == null || command.trim().length() == 0) {
421             command = "start";
422         }
423
424         // strip off the leading dash
425
if (command.startsWith("-")) {
426             command = command.substring(1);
427         }
428
429         // shutdown & status hack
430
if (command.equalsIgnoreCase("shutdown")) {
431             command = "start";
432         } else if (command.equalsIgnoreCase("status")) {
433             command = "start";
434         }
435
436         return "org/ofbiz/base/start/" + command + ".properties";
437     }
438
439     public static class Config {
440         public String JavaDoc containerConfig;
441         public String JavaDoc testConfig;
442         public InetAddress JavaDoc adminAddress;
443         public int adminPort;
444         public String JavaDoc adminKey;
445         public String JavaDoc ofbizHome;
446         public String JavaDoc baseJar;
447         public String JavaDoc toolsJar;
448         public String JavaDoc commJar;
449         public String JavaDoc baseLib;
450         public String JavaDoc baseDtd;
451         public String JavaDoc baseConfig;
452         public String JavaDoc logDir;
453         public List JavaDoc loaders;
454         public String JavaDoc awtHeadless;
455         public String JavaDoc splashLogo;
456         public boolean shutdownAfterLoad = false;
457         public boolean useShutdownHook = true;
458         public boolean requireToolsJar = false;
459         public boolean requireCommJar = false;
460
461         private Properties JavaDoc getPropertiesFile(String JavaDoc config) throws IOException JavaDoc {
462             InputStream JavaDoc propsStream = null;
463             Properties JavaDoc props = new Properties JavaDoc();
464             try {
465                 // first try classpath
466
propsStream = getClass().getClassLoader().getResourceAsStream(config);
467                 if (propsStream != null) {
468                     props.load(propsStream);
469                 } else {
470                     throw new IOException JavaDoc();
471                 }
472             } catch (IOException JavaDoc e) {
473                 // next try file location
474
File JavaDoc propsFile = new File JavaDoc(config);
475                 if (propsFile != null) {
476                     FileInputStream JavaDoc fis = null;
477                     try {
478                         fis = new FileInputStream JavaDoc(propsFile);
479                         if (fis != null) {
480                             props.load(fis);
481                         } else {
482                             throw new FileNotFoundException JavaDoc();
483                         }
484                     } catch (FileNotFoundException JavaDoc e2) {
485                         // do nothing; we will see empty props below
486
} finally {
487                         if (fis != null) {
488                             fis.close();
489                         }
490                     }
491                 }
492             } finally {
493                 if (propsStream != null) {
494                     propsStream.close();
495                 }
496             }
497
498             // check for empty properties
499
if (props.isEmpty()) {
500                 throw new IOException JavaDoc("Cannot load configuration properties : " + config);
501             }
502             return props;
503         }
504
505         public void readConfig(String JavaDoc config) throws IOException JavaDoc {
506             // check the java_version
507
String JavaDoc javaVersion = System.getProperty("java.version");
508             String JavaDoc javaVendor = System.getProperty("java.vendor");
509             double version = Double.parseDouble(javaVersion.substring(0, javaVersion.indexOf(".") + 2));
510             if (REQUIRED_JDK > version) {
511                 System.err.println("");
512                 System.err.println("Java Version - " + javaVendor + " " + javaVersion + " - is not supported by OFBiz.");
513                 System.err.println("Please install Java2 SDK " + REQUIRED_JDK + "+");
514                 System.err.println("");
515                 System.exit(-1);
516             }
517
518             Properties JavaDoc props = this.getPropertiesFile(config);
519
520             // set the ofbiz.home
521
if (ofbizHome == null) {
522                 ofbizHome = props.getProperty("ofbiz.home", ".");
523                 // get a full path
524
if (ofbizHome.equals(".")) {
525                     ofbizHome = System.getProperty("user.dir");
526                     ofbizHome = ofbizHome.replace('\\', '/');
527                     System.out.println("Set OFBIZ_HOME to - " + ofbizHome);
528                 }
529             }
530             System.setProperty("ofbiz.home", ofbizHome);
531
532             // base config directory
533
baseConfig = System.getProperty("ofbiz.base.config");
534             if (baseConfig == null) {
535                 baseConfig = ofbizHome + "/" + props.getProperty("ofbiz.base.config", "base/config");
536             }
537
538             // base schema directory
539
baseDtd = System.getProperty("ofbiz.base.schema");
540             if (baseDtd == null) {
541                 baseDtd = ofbizHome + "/" + props.getProperty("ofbiz.base.schema", "base/dtd");
542             }
543
544             // base lib directory
545
baseLib = System.getProperty("ofbiz.base.lib");
546             if (baseLib == null) {
547                 baseLib = ofbizHome + "/" + props.getProperty("ofbiz.base.lib", "base/lib");
548             }
549
550             // base jar file
551
baseJar = System.getProperty("ofbiz.base.jar");
552             if (baseJar == null) {
553                 baseJar = ofbizHome + "/" + props.getProperty("ofbiz.base.jar", "base/build/lib/ofbiz-base.jar");
554             }
555
556             // tools jar
557
String JavaDoc reqTJ = System.getProperty("java.tools.jar.required");
558             if (reqTJ == null) {
559                 reqTJ = props.getProperty("java.tools.jar.required", "false");
560             }
561             requireToolsJar = "true".equalsIgnoreCase(reqTJ);
562             toolsJar = this.findSystemJar(props, javaVendor, javaVersion, "tools.jar", requireToolsJar);
563
564             // comm jar
565
String JavaDoc reqCJ = System.getProperty("java.comm.jar.required");
566             if (reqTJ == null) {
567                 reqTJ = props.getProperty("java.comm.jar.required", "false");
568             }
569             requireCommJar = "true".equalsIgnoreCase(reqCJ);
570             commJar = this.findSystemJar(props, javaVendor, javaVersion, "comm.jar", requireCommJar);
571
572             // log directory
573
logDir = System.getProperty("ofbiz.log.dir");
574             if (logDir == null) {
575                 logDir = ofbizHome + "/" + props.getProperty("ofbiz.log.dir", "logs");
576             }
577
578             // container configuration
579
containerConfig = System.getProperty("ofbiz.container.config");
580             if (containerConfig == null) {
581                 containerConfig = ofbizHome + "/" + props.getProperty("ofbiz.container.config", "base/config/ofbiz-containers.xml");
582             }
583
584             // get the admin server info
585
String JavaDoc serverHost = System.getProperty("ofbiz.admin.host");
586             if (serverHost == null) {
587                 serverHost = props.getProperty("ofbiz.admin.host", "127.0.0.1");
588             }
589
590             String JavaDoc adminPortStr = System.getProperty("ofbiz.admin.port");
591             if (adminPortStr == null) {
592                 adminPortStr = props.getProperty("ofbiz.admin.port", "0");
593             }
594
595             // set the admin key
596
adminKey = System.getProperty("ofbiz.admin.key");
597             if (adminKey == null) {
598                 adminKey = props.getProperty("ofbiz.admin.key", "NA");
599             }
600
601             // create the host InetAddress
602
adminAddress = InetAddress.getByName(serverHost);
603
604             // parse the port number
605
try {
606                 adminPort = Integer.parseInt(adminPortStr);
607             } catch (Exception JavaDoc e) {
608                 adminPort = 0;
609             }
610
611             // set the Derby system home
612
String JavaDoc derbyPath = System.getProperty("derby.system.home");
613             if (derbyPath == null) {
614                 derbyPath = props.getProperty("derby.system.home", "data/derby");
615             }
616             System.setProperty("derby.system.home", derbyPath);
617
618             // set the property to tell Log4J to use debug.properties
619
String JavaDoc log4jConfig = System.getProperty("log4j.configuration");
620             if (log4jConfig == null) {
621                 log4jConfig = props.getProperty("log4j.configuration");
622             }
623
624             // build a default log4j configuration based on ofbizHome
625
if (log4jConfig == null) {
626                 log4jConfig = ofbizHome + "/base/config/debug.properties";
627             }
628
629             // set the log4j configuration property so we don't pick up one inside jars by mistake
630
System.setProperty("log4j.configuration", log4jConfig);
631
632             // check for shutdown hook
633
if (System.getProperty("ofbiz.enable.hook") != null && System.getProperty("ofbiz.enable.hook").length() > 0) {
634                 useShutdownHook = "true".equalsIgnoreCase(System.getProperty("ofbiz.enable.hook"));
635             } else if (props.getProperty("ofbiz.enable.hook") != null && props.getProperty("ofbiz.enable.hook").length() > 0) {
636                 useShutdownHook = "true".equalsIgnoreCase(props.getProperty("ofbiz.enable.hook"));
637             }
638
639             // check for auto-shutdown
640
if (System.getProperty("ofbiz.auto.shutdown") != null && System.getProperty("ofbiz.auto.shutdown").length() > 0) {
641                 shutdownAfterLoad = "true".equalsIgnoreCase(System.getProperty("ofbiz.auto.shutdown"));
642             } else if (props.getProperty("ofbiz.auto.shutdown") != null && props.getProperty("ofbiz.auto.shutdown").length() > 0) {
643                 shutdownAfterLoad = "true".equalsIgnoreCase(props.getProperty("ofbiz.auto.shutdown"));
644             }
645
646             // set AWT headless mode
647
awtHeadless = System.getProperty("java.awt.headless");
648             if (awtHeadless == null) {
649                 awtHeadless = props.getProperty("java.awt.headless");
650             }
651             if (awtHeadless != null) {
652                 System.setProperty("java.awt.headless", awtHeadless);
653             }
654
655             // get the splash logo
656
splashLogo = props.getProperty("ofbiz.start.splash.logo", null);
657
658             // set the property to tell Jetty to use 2.4 SessionListeners
659
System.setProperty("org.mortbay.jetty.servlet.AbstractSessionManager.24SessionDestroyed", "true");
660
661             // loader classes
662
loaders = new ArrayList JavaDoc();
663             int currentPosition = 1;
664             while (true) {
665                 String JavaDoc loaderClass = props.getProperty("ofbiz.start.loader" + currentPosition);
666                 if (loaderClass == null || loaderClass.length() == 0) {
667                     break;
668                 } else {
669                     loaders.add(loaderClass);
670                     currentPosition++;
671                 }
672             }
673         }
674
675         private String JavaDoc findSystemJar(Properties JavaDoc props, String JavaDoc javaVendor, String JavaDoc javaVersion, String JavaDoc jarName, boolean required) {
676             String JavaDoc fileSep = System.getProperty("file.separator");
677             String JavaDoc javaHome = System.getProperty("java.home");
678             String JavaDoc errorMsg = "Unable to locate " + jarName + " - ";
679             //String foundMsg = "Found " + jarName + " - ";
680
String JavaDoc jarLoc = "lib" + fileSep + jarName;
681             File JavaDoc tj = null;
682
683             if ("tools.jar".equals(jarName) && javaVendor.startsWith("Apple")) {
684                 // tools.jar is always available in Apple's JDK implementation
685
return null;
686             }
687
688             // check to see if it is in the OFBIZ_HOME directory
689
tj = new File JavaDoc(ofbizHome + fileSep + jarName);
690             if (tj.exists()) {
691                 return null;
692             }
693
694             // check to see if it is in the base/lib directory
695
tj = new File JavaDoc(baseLib + fileSep + jarName);
696             if (tj.exists()) {
697                 return null;
698             }
699
700             // try to locate tools.jar from the properties file
701
String JavaDoc jarProps = props.getProperty("java." + jarName, null);
702             if (jarProps != null) {
703                 tj = new File JavaDoc(jarProps);
704                 if (!tj.exists()) {
705                     if (required) {
706                         System.err.println(errorMsg + tj.getAbsolutePath());
707                     }
708                 } else {
709                     //System.out.println(foundMsg + tj.getAbsolutePath());
710
return jarProps;
711                 }
712             }
713
714             // next check the JAVA_HOME lib dir
715
tj = new File JavaDoc(javaHome + fileSep + jarLoc);
716             if (!tj.exists()) {
717                 if (required) {
718                     System.err.println(errorMsg + tj.getAbsolutePath());
719                 }
720             } else {
721                 //System.out.println(foundMsg + tj.getAbsolutePath());
722
return tj.getAbsolutePath();
723             }
724
725             // next if we are a JRE dir check the parent dir
726
String JavaDoc jreExt = fileSep + "jre";
727             if (javaHome.toLowerCase().endsWith(jreExt)) {
728                 javaHome = javaHome.substring(0, javaHome.lastIndexOf(fileSep));
729                 tj = new File JavaDoc(javaHome + fileSep + jarLoc);
730                 if (!tj.exists()) {
731                     if (required) {
732                         System.err.println(errorMsg + tj.getAbsolutePath());
733                     }
734                 } else {
735                     //System.out.println(foundMsg + tj.getAbsolutePath());
736
return tj.getAbsolutePath();
737                 }
738             }
739
740             // special windows checking
741
if (javaHome.toLowerCase().charAt(1) == ':') {
742                 String JavaDoc driveLetter = javaHome.substring(0, 2);
743                 String JavaDoc windowsPath = driveLetter + fileSep + "j2sdk" + javaVersion;
744                 tj = new File JavaDoc(windowsPath + fileSep + jarLoc);
745                 if (!tj.exists()) {
746                     if (required) {
747                         System.err.println(errorMsg + tj.getAbsolutePath());
748                     }
749                 } else {
750                     //System.out.println(foundMsg + tj.getAbsolutePath());
751
return tj.getAbsolutePath();
752                 }
753             }
754
755             if (required) {
756                 System.err.println("");
757                 System.err.println("Required library " + jarName + " could not be located.");
758                 System.err.println("Make sure you using Java2 SDK " + REQUIRED_JDK + "+ and NOT the JRE.");
759                 System.err.println("You may need to copy " + jarName + " into a loadable lib directory");
760                 System.err.println("(i.e. OFBIZ_HOME or OFBIZ_HOME/base/lib)");
761                 System.err.println("");
762                 System.exit(-1);
763             }
764
765             return null;
766         }
767     }
768 }
769
770
Popular Tags