1 21 package com.presumo.util.config; 22 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.IOException ; 26 27 import java.util.Enumeration ; 28 import java.util.Properties ; 29 import java.util.Vector ; 30 31 34 public class PropertyFileConfiguration extends Configuration 35 { 36 37 private static final String JMS_BASEDIR_PROPERTY = "jmsbasedir"; 38 private static final String SYSTEM_PROPERTIES_NAME = "JmsConfig"; 39 private static final String SUFFIX = ".properties"; 40 private static final String CONFIG_SUBDIRECTORY = "config/"; 41 42 private String baseDir; 43 private String configDir; 44 private String mainPropFile; 45 46 47 51 PropertyFileConfiguration() 52 { 53 baseDir = System.getProperty(JMS_BASEDIR_PROPERTY, "./"); 54 55 if (! baseDir.endsWith("/")) 56 baseDir = baseDir + "/"; 57 58 configDir = baseDir + CONFIG_SUBDIRECTORY; 59 mainPropFile = configDir + SYSTEM_PROPERTIES_NAME + SUFFIX; 60 } 61 62 66 71 public Preferences getSystemPreferences() 72 { 73 PropertyPreferenceWrapper systemPreferences = null; 74 75 try { 76 FileInputStream fis = new FileInputStream (mainPropFile); 77 try { 78 Properties props = new Properties (); 79 props.load(fis); 80 systemPreferences = new PropertyPreferenceWrapper(props); 81 } finally { 82 fis.close(); 83 } 84 } catch (IOException ioe) { 85 System.err.println("An exception occurred while retrieving"+ 88 "Jms system properties:\n" + ioe.toString()); 89 } 90 91 return systemPreferences; 92 } 93 94 100 public Preferences getSubsystemPreferences(String subsystem) 101 { 102 PropertyPreferenceWrapper subPreferences = null; 103 104 try { 105 FileInputStream fis = new FileInputStream (configDir+subsystem+SUFFIX); 106 try { 107 Properties props = new Properties (); 108 props.load(fis); 109 subPreferences = new PropertyPreferenceWrapper(props); 110 } finally { 111 fis.close(); 112 } 113 } catch (IOException ioe) { 114 System.err.println("An exception occurred while retrieving"+ 115 "Jms subsystem properties:\n" + ioe.toString()); 116 } 117 118 return subPreferences; 119 } 120 121 124 public Enumeration getSubsystems() 125 { 126 String [] files = new File (configDir).list(); 127 128 Vector retval = new Vector (files.length); 129 for (int i=0; i < files.length; i++) { 130 if (files[i].endsWith(SUFFIX) && 131 ! ( files[i].startsWith(SYSTEM_PROPERTIES_NAME)) ) 132 { 133 retval.add(files[i].substring(0, files[i].length() - SUFFIX.length())); 134 } 135 } 136 return retval.elements(); 137 } 138 139 143 public class PropertyPreferenceWrapper implements Preferences 144 { 145 146 private final Properties props; 147 148 PropertyPreferenceWrapper(Properties props) 149 { 150 this.props = props; 151 } 152 153 public String get(String key) 154 { 155 return props.getProperty(key); 156 } 157 158 public String get(String key, String defaultVal) 159 { 160 return props.getProperty(key, defaultVal); 161 } 162 163 public Enumeration names() 164 { 165 return props.propertyNames(); 166 } 167 168 public String toString() 169 { 170 return super.toString() + props.toString(); 171 } 172 } 173 177 178 } 179 | Popular Tags |