1 14 package org.compiere.util; 15 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileNotFoundException ; 19 import java.io.FileOutputStream ; 20 import java.io.Serializable ; 21 import java.sql.Timestamp ; 22 import java.util.Enumeration ; 23 import java.util.Properties ; 24 25 import org.compiere.plaf.CompiereLookAndFeel; 26 import org.compiere.plaf.CompiereTheme; 27 28 36 public final class Ini implements Serializable 37 { 38 public static final String COMPIERE_PROPERTY_FILE = "Compiere.properties"; 39 40 public static final String P_UID = "ApplicationUserID"; 42 private static final String DEFAULT_UID = "System"; 43 public static final String P_PWD = "ApplicationPassword"; 45 private static final String DEFAULT_PWD = "System"; 46 public static final String P_STORE_PWD = "StorePassword"; 48 private static final boolean DEFAULT_STORE_PWD = true; 49 public static final String P_DEBUGLEVEL = "DebugLevel"; 51 private static final int DEFAULT_DEBUGLEVEL = 0; 52 public static final String P_LANGUAGE = "Language"; 54 private static final String DEFAULT_LANGUAGE = Language.getName 55 (System.getProperty("user.language") + "_" + System.getProperty("user.country")); 56 public static final String P_INI = "FileNameINI"; 58 private static final String DEFAULT_INI = ""; 59 public static final String P_CONNECTION = "Connection"; 61 private static final String DEFAULT_CONNECTION = ""; 62 public static final String P_CONTEXT = "DataSource"; 64 private static final String DEFAULT_CONTEXT = "OracleDS"; 65 public static final String P_OBJECTS = "ServerObjects"; 67 private static final boolean DEFAULT_OBJECTS = false; 68 public static final String P_UI_LOOK = "UILookFeel"; 70 private static final String DEFAULT_UI_LOOK = CompiereLookAndFeel.NAME; 71 public static final String P_UI_THEME = "UITheme"; 73 private static final String DEFAULT_UI_THEME = CompiereTheme.NAME; 74 public static final String P_A_COMMIT = "AutoCommit"; 76 private static final boolean DEFAULT_A_COMMIT = true; 77 public static final String P_A_LOGIN = "AutoLogin"; 79 private static final boolean DEFAULT_A_LOGIN = false; 80 public static final String P_COMPIERESYS = "CompiereSys"; private static final boolean DEFAULT_COMPIERESYS = false; 83 public static final String P_SHOW_ACCT = "ShowAcct"; 85 private static final boolean DEFAULT_SHOW_ACCT = true; 86 public static final String P_SHOW_TRL = "ShowTrl"; 88 private static final boolean DEFAULT_SHOW_TRL = false; 89 public static final String P_TEMP_DIR = "TempDir"; 91 private static final String DEFAULT_TEMP_DIR = ""; 92 public static final String P_ROLE = "Role"; 94 private static final String DEFAULT_ROLE = ""; 95 public static final String P_CLIENT = "Client"; 97 private static final String DEFAULT_CLIENT = ""; 98 public static final String P_ORG = "Organization"; 100 private static final String DEFAULT_ORG = ""; 101 public static final String P_PRINTER = "Printer"; 103 private static final String DEFAULT_PRINTER = ""; 104 public static final String P_WAREHOUSE = "Warehouse"; 106 private static final String DEFAULT_WAREHOUSE = ""; 107 public static final String P_TODAY = "Today"; 109 private static final Timestamp DEFAULT_TODAY = new Timestamp (System.currentTimeMillis()); 110 public static final String P_PRINTPREVIEW = "PrintPreview"; 112 private static final boolean DEFAULT_PRINTPREVIEW = false; 113 private static final String P_WARNING = "Warning"; 115 private static final String DEFAULT_WARNING = "Do_not_change_any_of_the_data_as_they_will_have_undocumented_side_effects."; 116 private static final String P_WARNING_de = "WarningD"; 117 private static final String DEFAULT_WARNING_de ="Einstellungen_nicht_aendern,_da_diese_undokumentierte_Nebenwirkungen_haben."; 118 119 private static final String [] PROPERTIES = new String [] { 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 [] VALUES = new String [] { 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 142 private static Properties s_prop = new Properties (); 143 144 148 public static void saveProperties (boolean tryUserHome) 149 { 150 String fileName = getFileName (tryUserHome); 151 FileOutputStream fos = null; 152 try 153 { 154 File f = new File (fileName); 155 fos = new FileOutputStream (f); 156 s_prop.store(fos, "Compiere"); 157 fos.flush(); 158 fos.close(); 159 } 160 catch (Exception e) 161 { 162 System.err.println ("Ini.saveProperties - Cannot save Properties to " + fileName + " - " + e.toString()); 163 return; 164 } 165 catch (Throwable t) 166 { 167 System.err.println ("Ini.saveProperties - Cannot save Properties to " + fileName + " - " + t.toString()); 168 return; 169 } 170 } 173 177 public static void loadProperties (boolean reload) 178 { 179 if (reload || s_prop.size() == 0) 180 loadProperties(getFileName(s_client)); 181 } 183 189 public static boolean loadProperties (String filename) 190 { 191 System.out.println("Ini.loadProperties: " + filename); 192 boolean loadOK = true; 194 boolean firstTime = false; 195 s_prop = new Properties (); 196 FileInputStream fis = null; 197 try 198 { 199 fis = new FileInputStream (filename); 200 s_prop.load(fis); 201 fis.close(); 202 } 203 catch (FileNotFoundException e) 204 { 205 System.err.println ("Ini.loadProperties " 206 + filename + " not found"); 207 loadOK = false; 208 } 209 catch (Exception e) 210 { 211 System.err.println ("Ini.loadProperties " 212 + filename + " - " + e.toString()); 213 loadOK = false; 214 } 215 catch (Throwable 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 for (int i = 0; i < PROPERTIES.length; i++) 230 { 231 if (VALUES[i].length() > 0) 232 checkProperty(PROPERTIES[i], VALUES[i]); 233 } 234 String 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 if (!loadOK) 244 saveProperties(true); 245 s_loaded = true; 246 return firstTime; 247 } 249 256 private static String checkProperty (String key, String defaultValue) 257 { 258 String 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 } 269 282 private static String getFileName (boolean tryUserHome) 283 { 284 if (System.getProperty("PropertyFile") != null) 285 return System.getProperty("PropertyFile"); 286 String base = null; 288 if (tryUserHome && s_client) 289 base = System.getProperty("user.home"); 290 if (!s_client || base == null || base.length() == 0) 292 { 293 String 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 return base + COMPIERE_PROPERTY_FILE; 303 } 305 306 307 312 public static void setProperty (String key, String value) 313 { 314 if (s_prop == null) 315 s_prop = new Properties (); 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 } 324 329 public static void setProperty(String key, boolean value) 330 { 331 setProperty (key, value ? "Y" : "N"); 332 } 334 339 public static void setProperty(String key, int value) 340 { 341 setProperty (key, String.valueOf(value)); 342 } 344 349 public static String getProperty (String key) 350 { 351 if (key == null) 352 return ""; 353 String retStr = s_prop.getProperty(key, ""); 354 if (retStr == null || retStr.length() == 0) 355 return ""; 356 return Secure.decrypt(retStr); 357 } 359 364 public static boolean getPropertyBool (String key) 365 { 366 return getProperty (key).equals("Y"); 367 } 369 370 371 376 public static Properties getProperties() 377 { 378 return s_prop; 379 } 381 385 public static String getAsString() 386 { 387 StringBuffer buf = new StringBuffer ("Ini - "); 388 Enumeration e = s_prop.keys(); 389 while (e.hasMoreElements()) 390 { 391 String key = (String )e.nextElement(); 392 buf.append(key).append("="); 393 buf.append(getProperty(key)).append("; "); 394 } 395 return buf.toString(); 396 } 398 399 400 401 public static final String ENV_PREFIX = "env."; 402 403 public static final String COMPIERE_HOME = "COMPIERE_HOME"; 404 405 406 private static boolean s_client = true; 407 408 private static boolean s_loaded = false; 409 410 414 public static boolean isClient() 415 { 416 return s_client; 417 } 419 423 public static void setClient (boolean client) 424 { 425 s_client = client; 426 } 428 432 public static boolean isLoaded() 433 { 434 return s_loaded; 435 } 437 441 public static String getCompiereHome() 442 { 443 String env = System.getProperty (ENV_PREFIX + COMPIERE_HOME); 444 if (env == null) 445 env = System.getProperty (COMPIERE_HOME); 446 return env; 447 } 449 453 public static void setCompiereHome (String CompiereHome) 454 { 455 if (CompiereHome != null && CompiereHome.length() > 0) 456 System.setProperty (COMPIERE_HOME, CompiereHome); 457 } 459 } | Popular Tags |