| 1 | /* |
| 2 | * $Id: DeveloperProperties.java,v 1.3 2006/02/01 04:42:14 hastings Exp $ |
| 3 | * |
| 4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This material may be reproduced by or for the U. S. Government |
| 9 | * pursuant to the copyright license under the clause at |
| 10 | * DFARS 252.227-7014 (OCT 2001). |
| 11 | */ |
| 12 | |
| 13 | package com.moesol.util; |
| 14 | |
| 15 | import java.io.File; |
| 16 | import java.io.FileInputStream; |
| 17 | import java.io.FileNotFoundException; |
| 18 | import java.io.IOException; |
| 19 | import java.util.Properties; |
| 20 | import java.util.logging.Level; |
| 21 | import java.util.logging.Logger; |
| 22 | |
| 23 | /** |
| 24 | * @author robert |
| 25 | */ |
| 26 | public class DeveloperProperties { |
| 27 | private static final Logger s_logger = Logger.getLogger(DeveloperProperties.class.getName()); |
| 28 | |
| 29 | public static String getProperty(String name) { |
| 30 | return getProperty(name, null); |
| 31 | } |
| 32 | public static String getProperty(String name, String default_value) { |
| 33 | if (System.getProperty(name) != null) { |
| 34 | return System.getProperty(name); |
| 35 | } |
| 36 | if (mustUseSystem()) { |
| 37 | return default_value; |
| 38 | } |
| 39 | return getPropertyCache().getProperty(name, default_value); |
| 40 | } |
| 41 | private static boolean mustUseSystem() { |
| 42 | if (s_properties_cache == null) { |
| 43 | getPropertyCache(); |
| 44 | } |
| 45 | return s_must_use_system; |
| 46 | } |
| 47 | private static Properties getPropertyCache() { |
| 48 | if (s_properties_cache != null) { |
| 49 | return s_properties_cache; |
| 50 | } |
| 51 | try { |
| 52 | s_properties_cache = new Properties(System.getProperties()); |
| 53 | extendPropertiesFromUserHome(s_properties_cache); |
| 54 | } catch (SecurityException e) { |
| 55 | // This may happen in some security contexts |
| 56 | s_must_use_system = true; |
| 57 | } |
| 58 | return s_properties_cache; |
| 59 | } |
| 60 | private static void extendPropertiesFromUserHome(Properties p) { |
| 61 | String file_name = getDeveloperPropertiesPath(); |
| 62 | s_logger.log(Level.FINE, "Using: {0}", file_name); |
| 63 | FileInputStream fis; |
| 64 | try { |
| 65 | fis = new FileInputStream(file_name); |
| 66 | try { |
| 67 | p.load(fis); |
| 68 | } catch (IOException e) { |
| 69 | logger.log(Level.INFO, "error loading?", e); |
| 70 | } finally { |
| 71 | fis.close(); |
| 72 | } |
| 73 | } catch (FileNotFoundException e) { |
| 74 | // It's okay for there to be no file |
| 75 | } catch (IOException e) { |
| 76 | logger.log(Level.INFO, "error closing?", e); |
| 77 | } |
| 78 | } |
| 79 | private static String getDeveloperPropertiesPath() { |
| 80 | return System.getProperty("user.home") + File.separator + "com.moesol.developer.properties"; |
| 81 | } |
| 82 | |
| 83 | private static boolean s_must_use_system = false; |
| 84 | private static Properties s_properties_cache; |
| 85 | private static Logger logger = Logger.getLogger(DeveloperProperties.class.getName()); |
| 86 | } |