KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > Ini


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 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-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.compiere.plaf.CompiereLookAndFeel;
26 import org.compiere.plaf.CompiereTheme;
27
28 /**
29  * Load & Save INI Settings fopm property file
30  * Initiated in Compiere.startup
31  * Settings activated in ALogin.getIni
32  *
33  * @author Jorg Janke
34  * @version $Id: Ini.java,v 1.23 2003/11/06 07:10:00 jjanke Exp $
35  */

36 public final class Ini implements Serializable JavaDoc
37 {
38     public static final String JavaDoc COMPIERE_PROPERTY_FILE = "Compiere.properties";
39
40     // Property Constants and Default Values
41
public static final String JavaDoc P_UID = "ApplicationUserID";
42     private static final String JavaDoc DEFAULT_UID = "System";
43     //
44
public static final String JavaDoc P_PWD = "ApplicationPassword";
45     private static final String JavaDoc DEFAULT_PWD = "System";
46     //
47
public static final String JavaDoc P_STORE_PWD = "StorePassword";
48     private static final boolean DEFAULT_STORE_PWD = true;
49     //
50
public static final String JavaDoc P_DEBUGLEVEL = "DebugLevel";
51     private static final int DEFAULT_DEBUGLEVEL = 0;
52     //
53
public static final String JavaDoc P_LANGUAGE = "Language";
54     private static final String JavaDoc DEFAULT_LANGUAGE = Language.getName
55         (System.getProperty("user.language") + "_" + System.getProperty("user.country"));
56     //
57
public static final String JavaDoc P_INI = "FileNameINI";
58     private static final String JavaDoc DEFAULT_INI = "";
59     //
60
public static final String JavaDoc P_CONNECTION = "Connection";
61     private static final String JavaDoc DEFAULT_CONNECTION = "";
62     //
63
public static final String JavaDoc P_CONTEXT = "DataSource";
64     private static final String JavaDoc DEFAULT_CONTEXT = "OracleDS";
65     //
66
public static final String JavaDoc P_OBJECTS = "ServerObjects";
67     private static final boolean DEFAULT_OBJECTS = false;
68     //
69
public static final String JavaDoc P_UI_LOOK = "UILookFeel";
70     private static final String JavaDoc DEFAULT_UI_LOOK = CompiereLookAndFeel.NAME;
71     //
72
public static final String JavaDoc P_UI_THEME = "UITheme";
73     private static final String JavaDoc DEFAULT_UI_THEME = CompiereTheme.NAME;
74     //
75
public static final String JavaDoc P_A_COMMIT = "AutoCommit";
76     private static final boolean DEFAULT_A_COMMIT = true;
77     //
78
public static final String JavaDoc P_A_LOGIN = "AutoLogin";
79     private static final boolean DEFAULT_A_LOGIN = false;
80     //
81
public static final String JavaDoc P_COMPIERESYS = "CompiereSys"; // Save system records
82
private static final boolean DEFAULT_COMPIERESYS = false;
83     //
84
public static final String JavaDoc P_SHOW_ACCT = "ShowAcct";
85     private static final boolean DEFAULT_SHOW_ACCT = true;
86     //
87
public static final String JavaDoc P_SHOW_TRL = "ShowTrl";
88     private static final boolean DEFAULT_SHOW_TRL = false;
89     //
90
public static final String JavaDoc P_TEMP_DIR = "TempDir";
91     private static final String JavaDoc DEFAULT_TEMP_DIR = "";
92     //
93
public static final String JavaDoc P_ROLE = "Role";
94     private static final String JavaDoc DEFAULT_ROLE = "";
95     //
96
public static final String JavaDoc P_CLIENT = "Client";
97     private static final String JavaDoc DEFAULT_CLIENT = "";
98     //
99
public static final String JavaDoc P_ORG = "Organization";
100     private static final String JavaDoc DEFAULT_ORG = "";
101     //
102
public static final String JavaDoc P_PRINTER = "Printer";
103     private static final String JavaDoc DEFAULT_PRINTER = "";
104     //
105
public static final String JavaDoc P_WAREHOUSE = "Warehouse";
106     private static final String JavaDoc DEFAULT_WAREHOUSE = "";
107     //
108
public static final String JavaDoc P_TODAY = "Today";
109     private static final Timestamp JavaDoc DEFAULT_TODAY = new Timestamp JavaDoc(System.currentTimeMillis());
110     //
111
public static final String JavaDoc P_PRINTPREVIEW = "PrintPreview";
112     private static final boolean DEFAULT_PRINTPREVIEW = false;
113     //
114
private static final String JavaDoc P_WARNING = "Warning";
115     private static final String JavaDoc DEFAULT_WARNING = "Do_not_change_any_of_the_data_as_they_will_have_undocumented_side_effects.";
116     private static final String JavaDoc P_WARNING_de = "WarningD";
117     private static final String JavaDoc DEFAULT_WARNING_de ="Einstellungen_nicht_aendern,_da_diese_undokumentierte_Nebenwirkungen_haben.";
118
119     //
120
private static final String JavaDoc[] PROPERTIES = new String JavaDoc[] {
121         P_UID, P_PWD, P_DEBUGLEVEL, P_LANGUAGE, P_INI,
122         P_CONNECTION, P_OBJECTS, P_STORE_PWD,
123         P_UI_LOOK, P_UI_THEME,
124         P_A_COMMIT, P_A_LOGIN, P_COMPIERESYS, P_SHOW_ACCT, P_SHOW_TRL,
125         P_CONTEXT, P_TEMP_DIR,
126         P_ROLE, P_CLIENT, P_ORG, P_PRINTER, P_WAREHOUSE, P_TODAY,
127         P_PRINTPREVIEW,
128         P_WARNING, P_WARNING_de
129     };
130     private static final String JavaDoc[] VALUES = new String JavaDoc[] {
131         DEFAULT_UID, DEFAULT_PWD, String.valueOf(DEFAULT_DEBUGLEVEL), DEFAULT_LANGUAGE, DEFAULT_INI,
132         DEFAULT_CONNECTION, DEFAULT_OBJECTS?"Y":"N", DEFAULT_STORE_PWD?"Y":"N",
133         DEFAULT_UI_LOOK, DEFAULT_UI_THEME,
134         DEFAULT_A_COMMIT?"Y":"N", DEFAULT_A_LOGIN?"Y":"N", DEFAULT_COMPIERESYS?"Y":"N", DEFAULT_SHOW_ACCT?"Y":"N", DEFAULT_SHOW_TRL?"Y":"N",
135         DEFAULT_CONTEXT, DEFAULT_TEMP_DIR,
136         DEFAULT_ROLE, DEFAULT_CLIENT, DEFAULT_ORG, DEFAULT_PRINTER, DEFAULT_WAREHOUSE, DEFAULT_TODAY.toString(),
137         DEFAULT_PRINTPREVIEW?"Y":"N",
138         DEFAULT_WARNING, DEFAULT_WARNING_de
139     };
140
141     /** Container for Properties */
142     private static Properties JavaDoc s_prop = new Properties JavaDoc();
143
144     /**
145      * Save INI parameters to disk
146      * @param tryUserHome get user home first
147      */

148     public static void saveProperties (boolean tryUserHome)
149     {
150         String JavaDoc fileName = getFileName (tryUserHome);
151         FileOutputStream JavaDoc fos = null;
152         try
153         {
154             File JavaDoc f = new File JavaDoc(fileName);
155             fos = new FileOutputStream JavaDoc(f);
156             s_prop.store(fos, "Compiere");
157             fos.flush();
158             fos.close();
159         }
160         catch (Exception JavaDoc e)
161         {
162             System.err.println ("Ini.saveProperties - Cannot save Properties to " + fileName + " - " + e.toString());
163             return;
164         }
165         catch (Throwable JavaDoc t)
166         {
167             System.err.println ("Ini.saveProperties - Cannot save Properties to " + fileName + " - " + t.toString());
168             return;
169         }
170     // System.out.println("Ini.saveProperties: " + fileName);
171
} // save
172

173     /**
174      * Load INI parameters from disk
175      * @param reload reload
176      */

177     public static void loadProperties (boolean reload)
178     {
179         if (reload || s_prop.size() == 0)
180             loadProperties(getFileName(s_client));
181     } // loadProperties
182

183     /**
184      * Load INI parameters from filename
185      *
186      * @param filename to load
187      * @return true if first time
188      */

189     public static boolean loadProperties (String JavaDoc filename)
190     {
191         System.out.println("Ini.loadProperties: " + filename);
192         //
193
boolean loadOK = true;
194         boolean firstTime = false;
195         s_prop = new Properties JavaDoc();
196         FileInputStream JavaDoc fis = null;
197         try
198         {
199             fis = new FileInputStream JavaDoc(filename);
200             s_prop.load(fis);
201             fis.close();
202         }
203         catch (FileNotFoundException JavaDoc e)
204         {
205             System.err.println ("Ini.loadProperties "
206                 + filename + " not found");
207             loadOK = false;
208         }
209         catch (Exception JavaDoc e)
210         {
211             System.err.println ("Ini.loadProperties "
212                 + filename + " - " + e.toString());
213             loadOK = false;
214         }
215         catch (Throwable JavaDoc t)
216         {
217             System.err.println ("Ini.loadProperties "
218                 + filename + " - " + t.toString());
219             loadOK = false;
220         }
221         if (!loadOK)
222         {
223             firstTime = true;
224             if (!IniDialog.accept())
225                 System.exit(-1);
226         }
227
228         // Check/set properties defaults
229
for (int i = 0; i < PROPERTIES.length; i++)
230         {
231             if (VALUES[i].length() > 0)
232                 checkProperty(PROPERTIES[i], VALUES[i]);
233         }
234         //
235
String JavaDoc tempDir = System.getProperty("java.io.tmpdir");
236         if (tempDir == null || tempDir.length() == 1)
237             tempDir = getCompiereHome();
238         if (tempDir == null)
239             tempDir = "";
240         checkProperty(P_TEMP_DIR, tempDir);
241
242         // Save if not exist or could not be read
243
if (!loadOK)
244             saveProperties(true);
245         s_loaded = true;
246         return firstTime;
247     } // loadProperties
248

249     /**
250      * Load property and set to default, if not existing
251      *
252      * @param key Key
253      * @param defaultValue Default Value
254      * @return Property
255      */

256     private static String JavaDoc checkProperty (String JavaDoc key, String JavaDoc defaultValue)
257     {
258         String JavaDoc result = null;
259         if (key.equals(P_WARNING) || key.equals(P_WARNING_de))
260             result = defaultValue;
261         else if (!isClient())
262             result = s_prop.getProperty (key, Secure.CLEARTEXT + defaultValue);
263         else
264             result = s_prop.getProperty (key, Secure.encrypt(defaultValue));
265         s_prop.setProperty (key, result);
266         return result;
267     } // checkProperty
268

269     /**
270      * Return File Name of INI file
271      * <pre>
272      * Examples:
273      * C:\WinNT\Profiles\jjanke\Compiere.properties
274      * D:\Compiere2\Compiere.properties
275      * Compiere.properties
276      * </pre>
277      * Can be overwritten by -DPropertyFile=myFile allowing multiple
278      * configurations / property files.
279      * @param tryUserHome get user home first
280      * @return file name
281      */

282     private static String JavaDoc getFileName (boolean tryUserHome)
283     {
284         if (System.getProperty("PropertyFile") != null)
285             return System.getProperty("PropertyFile");
286         //
287
String JavaDoc base = null;
288         if (tryUserHome && s_client)
289             base = System.getProperty("user.home");
290         // Server
291
if (!s_client || base == null || base.length() == 0)
292         {
293             String JavaDoc home = getCompiereHome();
294             if (home != null)
295                 base = home;
296         }
297         if (base != null && !base.endsWith(File.separator))
298             base += File.separator;
299         if (base == null)
300             base = "";
301         //
302
return base + COMPIERE_PROPERTY_FILE;
303     } // getFileName
304

305     /*************************************************************************/
306
307     /**
308      * Set Property
309      * @param key Key
310      * @param value Value
311      */

312     public static void setProperty (String JavaDoc key, String JavaDoc value)
313     {
314         if (s_prop == null)
315             s_prop = new Properties JavaDoc();
316         if (key.equals(P_WARNING) || key.equals(P_WARNING_de))
317             s_prop.setProperty(key, value);
318         else if (!isClient())
319             s_prop.setProperty(key, Secure.CLEARTEXT + value);
320         else
321             s_prop.setProperty(key, Secure.encrypt(value));
322     } // setProperty
323

324     /**
325      * Set Property
326      * @param key Key
327      * @param value Value
328      */

329     public static void setProperty(String JavaDoc key, boolean value)
330     {
331         setProperty (key, value ? "Y" : "N");
332     } // setProperty
333

334     /**
335      * Set Property
336      * @param key Key
337      * @param value Value
338      */

339     public static void setProperty(String JavaDoc key, int value)
340     {
341         setProperty (key, String.valueOf(value));
342     } // setProperty
343

344     /**
345      * Get Propery
346      * @param key Key
347      * @return Value
348      */

349     public static String JavaDoc getProperty (String JavaDoc key)
350     {
351         if (key == null)
352             return "";
353         String JavaDoc retStr = s_prop.getProperty(key, "");
354         if (retStr == null || retStr.length() == 0)
355             return "";
356         return Secure.decrypt(retStr);
357     } // getProperty
358

359     /**
360      * Get Propery as Boolean
361      * @param key Key
362      * @return Value
363      */

364     public static boolean getPropertyBool (String JavaDoc key)
365     {
366         return getProperty (key).equals("Y");
367     } // getProperty
368

369     /*************************************************************************/
370
371     /**
372      * Get Properties
373      *
374      * @return Ini properties
375      */

376     public static Properties JavaDoc getProperties()
377     {
378         return s_prop;
379     } // getProperties
380

381     /**
382      * toString
383      * @return String representation
384      */

385     public static String JavaDoc getAsString()
386     {
387         StringBuffer JavaDoc buf = new StringBuffer JavaDoc ("Ini - ");
388         Enumeration JavaDoc e = s_prop.keys();
389         while (e.hasMoreElements())
390         {
391             String JavaDoc key = (String JavaDoc)e.nextElement();
392             buf.append(key).append("=");
393             buf.append(getProperty(key)).append("; ");
394         }
395         return buf.toString();
396     } // toString
397

398     /*************************************************************************/
399
400     /** System environment prefix */
401     public static final String JavaDoc ENV_PREFIX = "env.";
402     /** System Property Value of COMPIERE_HOME */
403     public static final String JavaDoc COMPIERE_HOME = "COMPIERE_HOME";
404
405     /** IsClient Internal marker */
406     private static boolean s_client = true;
407     /** IsClient Internal marker */
408     private static boolean s_loaded = false;
409
410     /**
411      * Are we in Client Mode ?
412      * @return true if client
413      */

414     public static boolean isClient()
415     {
416         return s_client;
417     } // isClient
418

419     /**
420      * Set Client Mode
421      * @param client client
422      */

423     public static void setClient (boolean client)
424     {
425         s_client = client;
426     } // setClient
427

428     /**
429      * Are the properties loaded?
430      * @return true if properties loaded.
431      */

432     public static boolean isLoaded()
433     {
434         return s_loaded;
435     } // isLoaded
436

437     /**
438      * Get Compiere Home
439      * @return CompiereHome or null
440      */

441     public static String JavaDoc getCompiereHome()
442     {
443         String JavaDoc env = System.getProperty (ENV_PREFIX + COMPIERE_HOME);
444         if (env == null)
445             env = System.getProperty (COMPIERE_HOME);
446         return env;
447     } // getCompiereHome
448

449     /**
450      * Set Compiere Home
451      * @param CompiereHome COMPIERE_HOME
452      */

453     public static void setCompiereHome (String JavaDoc CompiereHome)
454     {
455         if (CompiereHome != null && CompiereHome.length() > 0)
456             System.setProperty (COMPIERE_HOME, CompiereHome);
457     } // setCompiereHome
458

459 } // Ini
460
Popular Tags