KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > Compiere


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere;
15
16 import java.awt.*;
17 import java.net.*;
18 import java.io.*;
19 import java.util.jar.*;
20 import javax.swing.*;
21 import javax.naming.*;
22
23 import org.compiere.util.*;
24 import org.compiere.plaf.*;
25 import org.compiere.db.*;
26
27 /**
28  * Compiere Control Class
29  *
30  * @author Jorg Janke
31  * @version $Id: Compiere.java,v 1.26 2003/11/01 07:08:18 jjanke Exp $
32  */

33 public final class Compiere
34 {
35     /** Timestamp */
36     static public final String JavaDoc TIMESTAMP = "$Date: 2003/11/01 07:08:18 $";
37     /** Main Version String */
38     static public final String JavaDoc MAIN_VERSION = "Version 2.5.0d";
39     /** Detail Version as date Used for Client/Server */
40     static public final String JavaDoc DATE_VERSION = "2003-11-01";
41     /** Database Version as date Compared with AD_System */
42     static public final String JavaDoc DB_VERSION = "2003-11-01";
43
44     /** Product Name */
45     static public final String JavaDoc NAME = "Compiere\u2122";
46     /** URL of Product */
47     static public final String JavaDoc URL = "www.compiere.org";
48     /** 16*16 Product Image */
49     static private final String JavaDoc s_File16x16 = "images/C16.gif";
50     /** 32*32 Product Image */
51     static private final String JavaDoc s_file32x32 = "images/C32.gif";
52     /** 100*3- Product Image */
53     static private final String JavaDoc s_file100x30 = "images/C10030.gif";
54     /** 48*15 Product Image */
55     static private final String JavaDoc s_file48x15 = "images/Compiere.gif";
56     /** Support Email */
57     static private String JavaDoc s_supportEmail = "info@company.com";
58
59     /** Subtitle */
60     static public final String JavaDoc SUB_TITLE = " Smart ERP & CRM ";
61     /** Powered By - Don't modify this - Program will fail unexpectedly */
62     static public final String JavaDoc POWERED_BY = " Powered by Compiere\u2122 ";
63     /** Copyright Notice - Don't modify this - Program will fail unexpectedly */
64     static public final String JavaDoc COPYRIGHT = "Copyright \u00A9 1999-2003 Jorg Janke";
65
66     static private String JavaDoc s_ImplementationVersion = null;
67     static private String JavaDoc s_ImplementationVendor = null;
68
69     static private Image s_image16;
70     static private Image s_image48x15;
71     static private Image s_imageLogo;
72     static private ImageIcon s_imageIcon32;
73     static private ImageIcon s_imageIconLogo;
74
75     /** Logging */
76     private static Logger s_log = null;
77
78     /**
79      * Get Product Name w/o TM
80      * @return Application Name
81      */

82     public static String JavaDoc getName()
83     {
84         return NAME;
85     } // getName
86

87
88     /**
89      * Short Summary (Windows)
90      * @return summary
91      */

92     public static String JavaDoc getSum()
93     {
94         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
95         sb.append(NAME).append(" ").append(MAIN_VERSION).append(SUB_TITLE);
96         return sb.toString();
97     } // getSum
98

99     /**
100      * Summary (Windows)
101      * @return Summary in Windows character set
102      */

103     public static String JavaDoc getSummary()
104     {
105         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
106         sb.append(NAME).append(" ")
107             .append(MAIN_VERSION).append("/").append(DATE_VERSION)
108             .append(" -").append(SUB_TITLE)
109             .append("- ").append(COPYRIGHT)
110             .append("; Implementation: ").append(getImplementationVersion())
111             .append(" - ").append(getImplementationVendor());
112         return sb.toString();
113     } // getSummary
114

115     /**
116      * Set Jar Info
117      */

118     private static void setJarInfo()
119     {
120         if (s_ImplementationVendor != null)
121             return;
122         s_ImplementationVendor = "?";
123         s_ImplementationVersion = "?";
124
125         try
126         {
127             // Get Jar File
128
JarFile jar = ZipUtil.getJar("CClient.jar");
129             if (jar == null)
130                 jar = ZipUtil.getJar("CTools.jar");
131             if (jar == null)
132                 return;
133
134         // JarEntry je = jar.getJarEntry(JarFile.MANIFEST_NAME);
135
// if (je != null)
136
// System.out.println("Time " + new Date(je.getTime()));
137
Manifest mf = jar.getManifest();
138             if (mf != null)
139             {
140                 Attributes atts = mf.getMainAttributes();
141                 s_ImplementationVendor = atts.getValue("Implementation-Vendor");
142                 s_ImplementationVersion = atts.getValue("Implementation-Version");
143             }
144         }
145         catch (IOException ex)
146         {
147             System.err.println(ex);
148         }
149     } // setJarInfo
150

151     /**
152      * Get Jar Implementation Version
153      * @return Implementation-Version
154      */

155     public static String JavaDoc getImplementationVersion()
156     {
157         if (s_ImplementationVersion == null)
158             setJarInfo();
159         return s_ImplementationVersion;
160     } // getImplementationVersion
161

162     /**
163      * Get Jar Implementation Vendor
164      * @return Implementation-Vendor
165      */

166     public static String JavaDoc getImplementationVendor()
167     {
168         if (s_ImplementationVendor == null)
169             setJarInfo();
170         return s_ImplementationVendor;
171     } // getImplementationVendor
172

173     /**
174      * Get Checksum
175      * @return checksum
176      */

177     public static int getCheckSum()
178     {
179         return getSum().hashCode();
180     } // getCheckSum
181

182     /**
183      * Summary in ASCII
184      * @return Summary in ASCII
185      */

186     public static String JavaDoc getSummaryAscii()
187     {
188         String JavaDoc retValue = getSummary();
189         // Trademark
190
retValue = Util.replace(retValue, "\u00AE", "(r)");
191         // Trademark
192
retValue = Util.replace(retValue, "\u2122", "(tm)");
193         // Copyright
194
retValue = Util.replace(retValue, "\u00A9", "(c)");
195         // Cr
196
retValue = Util.replace(retValue, Env.NL, " ");
197         retValue = Util.replace(retValue, "\n", " ");
198         return retValue;
199     } // getSummaryAscii
200

201     /**
202      * Get Java VM Info
203      * @return VM info
204      */

205     public static String JavaDoc getJavaInfo()
206     {
207         return System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version");
208     } // getJavaInfo
209

210     /**
211      * Get Operating System Info
212      * @return OS info
213      */

214     public static String JavaDoc getOSInfo()
215     {
216         return System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("sun.os.patch.level");
217     } // getJavaInfo
218

219     /**
220      * Get full URL
221      * @return URL
222      */

223     public static String JavaDoc getURL()
224     {
225         return "http://" + URL;
226     } // getURL
227

228     /**
229      * Get Powered
230      * @return Powered By Compiere
231      */

232     public static String JavaDoc getPowered()
233     {
234         return POWERED_BY;
235     } // getPowered
236

237     /**
238      * Get Sub Title
239      * @return Subtitle
240      */

241     public static String JavaDoc getSubtitle()
242     {
243         return SUB_TITLE;
244     } // getSubitle
245

246     /**
247      * Get 16x16 Image
248      * @return Image Icon
249      */

250     public static Image getImage16()
251     {
252         if (s_image16 == null)
253         {
254             Toolkit tk = Toolkit.getDefaultToolkit();
255             URL url = org.compiere.Compiere.class.getResource(s_File16x16);
256         // System.out.println(url);
257
if (url == null)
258                 return null;
259             s_image16 = tk.getImage(url);
260         }
261         return s_image16;
262     } // getImage16
263

264     /**
265      * Get 28*15 Logo Image
266      * @return Image Icon
267      */

268     public static Image getImageLogoSmall()
269     {
270         if (s_image48x15 == null)
271         {
272             Toolkit tk = Toolkit.getDefaultToolkit();
273             URL url = org.compiere.Compiere.class.getResource(s_file48x15);
274         // System.out.println(url);
275
if (url == null)
276                 return null;
277             s_image48x15 = tk.getImage(url);
278         }
279         return s_image48x15;
280     } // getImageLogoSmall
281

282     /**
283      * Get Logo Image
284      * @return Image Logo
285      */

286     public static Image getImageLogo()
287     {
288         if (s_imageLogo == null)
289         {
290             Toolkit tk = Toolkit.getDefaultToolkit();
291             URL url = org.compiere.Compiere.class.getResource(s_file100x30);
292         // System.out.println(url);
293
if (url == null)
294                 return null;
295             s_imageLogo = tk.getImage(url);
296         }
297         return s_imageLogo;
298     } // getImageLogo
299

300     /**
301      * Get 32x32 ImageIcon
302      * @return Image Icon
303      */

304     public static ImageIcon getImageIcon32()
305     {
306         if (s_imageIcon32 == null)
307         {
308             URL url = org.compiere.Compiere.class.getResource(s_file32x32);
309         // System.out.println(url);
310
if (url == null)
311                 return null;
312             s_imageIcon32 = new ImageIcon(url);
313         }
314         return s_imageIcon32;
315     } // getImageIcon32
316

317     /**
318      * Get 100x30 ImageIcon
319      * @return Image Icon
320      */

321     public static ImageIcon getImageIconLogo()
322     {
323         if (s_imageIconLogo == null)
324         {
325             URL url = org.compiere.Compiere.class.getResource(s_file100x30);
326         // System.out.println(url);
327
if (url == null)
328                 return null;
329             s_imageIconLogo = new ImageIcon(url);
330         }
331         return s_imageIconLogo;
332     } // getImageIconLogo
333

334     /**
335      * Get default (Home) directory
336      * @return Home directory
337      */

338     public static String JavaDoc getCompiereHome()
339     {
340         // Try Environment
341
String JavaDoc retValue = Ini.getCompiereHome();
342         // Look in current Directory
343
if (retValue == null && System.getProperty("user.dir").indexOf("Compiere2") != -1)
344         {
345             retValue = System.getProperty("user.dir");
346             int pos = retValue.indexOf("Compiere2");
347             retValue = retValue.substring(pos+9);
348         }
349         if (retValue == null)
350             retValue = File.separator + "Compiere2";
351         return retValue;
352     } // getHome
353

354     /**
355      * Get Support Email
356      * @return Support mail address
357      */

358     public static String JavaDoc getSupportEMail()
359     {
360         return s_supportEmail;
361     } // getSupportEMail
362

363     /**
364      * Set Support Email
365      * @param email Support mail address
366      */

367     public static void setSupportEMail(String JavaDoc email)
368     {
369         s_supportEmail = email;
370     } // setSupportEMail
371

372     /*************************************************************************/
373
374     /**
375      * Startup Client.
376      * - Print greeting,
377      * - Check Java version and
378      * - load ini parameters
379      * If it is a client, load/set PLAF and exit if error
380      *
381      * @return successful startup
382      */

383     public static boolean startupClient ()
384     {
385         // Already started
386
if (s_log != null)
387             return true;
388
389         initClientLog();
390         // Greeting
391
s_log.info(getSummaryAscii());
392         s_log.info(getCompiereHome() + " - " + getJavaInfo() + " - " + getOSInfo());
393
394         // Get Defaults
395
Ini.setClient (true);
396         // Check Version
397
if (!Env.isJavaOK())
398             System.exit(1);
399
400         // Load System environment
401
// EnvLoader.load(Ini.ENV_PREFIX);
402

403         // Set XML environment explicitly to standard 1.4.0 distribution
404
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
405             "org.apache.crimson.jaxp.DocumentBuilderFactoryImpl"); // System Default
406
// "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
407
System.setProperty("javax.xml.parsers.SAXParserFactory",
408             "org.apache.crimson.jaxp.SAXParserFactoryImpl"); // System Default
409
// "org.apache.xerces.jaxp.SAXParserFactoryImpl");
410

411         // System properties
412
Ini.loadProperties (false);
413         // Set Log (use defaults)
414
Log.initLog();
415
416         // Set UI
417
CompiereTheme.load();
418         CompierePLAF.setPLAF (null);
419
420         // Set Default Database Connection
421
DB.setDBTarget(CConnection.get());
422         return true;
423     } // startupClient
424

425     /**
426      * Startup Server.
427      * - Print greeting,
428      * - Check Java version and
429      * - load ini parameters.
430      * If not in JBoss environment, call initLog before starting.
431      * @param context optional context
432      * @return successful startup
433      */

434     public static boolean startupServer (Context context)
435     {
436         // Already started
437
if (s_log != null && Ini.isLoaded())
438             return true;
439
440         // Init Log
441
s_log = Logger.getCLogger(Compiere.class);
442         // Greeting
443
s_log.info(getSummaryAscii());
444         s_log.info(getCompiereHome() + " - " + getJavaInfo() + " - " + getOSInfo());
445
446         // Get Defaults
447
Ini.setClient (false);
448         // Check Version
449
Env.isJavaOK();
450         // Load System environment
451
// EnvLoader.load(Ini.ENV_PREFIX);
452

453         // Set XML environment explicitly to standard 1.4.0 distribution
454
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
455             "org.apache.crimson.jaxp.DocumentBuilderFactoryImpl"); // System Default
456
// "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
457
System.setProperty("javax.xml.parsers.SAXParserFactory",
458             "org.apache.crimson.jaxp.SAXParserFactoryImpl"); // System Default
459
// "org.apache.xerces.jaxp.SAXParserFactoryImpl");
460

461         // System properties
462
Ini.loadProperties (false);
463         // Set Log (use defaults)
464
Log.initLog();
465
466         // Set Default Database Connection from Ini
467
CConnection cc = CConnection.get();
468         DB.setDBTarget(cc);
469         //
470
if (context != null)
471             cc.setDataSource (context, Ini.getProperty(Ini.P_CONTEXT));
472         if (!cc.isDataSource())
473         {
474             s_log.warn("startupServer - no DataSource");
475             return false;
476         }
477         //
478
return true;
479     } // startupServer
480

481     /**
482      * Init Log.
483      * Automalically called when the client is started. Not necessary for Server as set by JBoss.
484      * For Server Actions, call this before calling startupServer
485      */

486     public static void initClientLog()
487     {
488         // Init Log
489
s_log = Logger.getCLogger(Compiere.class);
490         org.apache.log4j.LogManager.resetConfiguration();
491         org.apache.log4j.Logger root = Logger.getRootLogger();
492         // root.addAppender(new ConsoleAppender(new PatternLayout("%d{ABSOLUTE} %-5p [%c{1}] %m%n")));
493
root.addAppender(new org.apache.log4j.ConsoleAppender(new LogLayout()));
494         root.setLevel(org.apache.log4j.Level.ALL);
495         // Client Log
496
root.addAppender(LogBuffer.get());
497     } // initLog
498

499
500     /**
501      * Main Method
502      *
503      * @param args optional start class
504      */

505     public static void main (String JavaDoc[] args)
506     {
507         Splash.getSplash();
508         startupClient (); // error exit and initUI
509

510         // Start with class as argument - or if nothing provided with Client
511
String JavaDoc className = "org.compiere.apps.AMenu";
512         for (int i = 0; i < args.length; i++)
513         {
514             if (!args[i].equals("-debug")) // ignore -debug
515
{
516                 className = args[i];
517                 break;
518             }
519         }
520         //
521
try
522         {
523             Class JavaDoc startClass = Class.forName(className);
524             startClass.newInstance();
525         }
526         catch (Exception JavaDoc e)
527         {
528             System.err.println("Cannot start: " + className + " - " + e.toString());
529             e.printStackTrace();
530         }
531     } // main
532
} // Compiere
533
Popular Tags