1 22 23 package org.objectweb.perseus.jndi; 24 25 import java.io.FileInputStream; 26 import java.lang.reflect.Method; 27 import java.util.Enumeration; 28 import java.util.Properties; 29 30 import javax.naming.Context; 31 32 36 public class PropertiesLoader { 37 38 41 protected String fileName = null; 42 43 47 protected Context context = null; 48 49 53 protected Class contextImpl = null; 54 55 59 protected Properties properties = new Properties(); 60 61 public PropertiesLoader() { 62 } 63 64 70 public PropertiesLoader(Class _contextImpl) 71 throws ClassCastException, ClassNotFoundException, 72 InstantiationException, IllegalAccessException { 73 74 context = (Context) _contextImpl.newInstance(); 75 contextImpl = _contextImpl; 76 } 77 78 79 public synchronized Context getContext() { return context; } 80 public synchronized void setContext(Context ctx) { context=ctx; } 81 82 public synchronized String getFileName() { return fileName; } 83 public synchronized void setFileName(String _fileName) { fileName = _fileName; } 84 85 public synchronized Properties getProperties() { return properties; } 86 public synchronized void setProperties(Properties p) { properties=p; } 87 88 92 public synchronized void load(String _fileName) throws Exception { 93 fileName = _fileName; 94 reload(); 95 } 96 97 101 public synchronized void reload() throws Exception { 102 FileInputStream is = new FileInputStream(fileName); 104 properties.clear(); 105 properties.load(is); 106 107 if (contextImpl!=null) { 110 context = (Context) contextImpl.newInstance(); 111 Enumeration e = properties.propertyNames(); 112 String key = null; 113 while ( e.hasMoreElements() ) { 114 key = (String) e.nextElement(); 115 context.bind( key, properties.getProperty( key, "") ); 116 } 117 } 118 } 119 120 124 public synchronized void clear() throws Exception { 125 properties = null; 126 context = null; 127 fileName = null; 128 } 129 130 131 134 public synchronized void invokeSetters( Object o) throws Exception { 135 136 Enumeration enum = properties.propertyNames(); 137 while ( enum.hasMoreElements() ){ 138 String fieldName = (String) enum.nextElement(); 139 String methodName = "set"+fieldName; 140 Method[] m = o.getClass().getMethods(); 141 int i=0; 142 while (i<m.length 143 && (! m[i].getName().equals(methodName) 144 || m[i].getParameterTypes().length!=1 ) 145 ) { 146 i++; 147 } 148 if (i<m.length) { 149 Class [] paramtype = m[i].getParameterTypes(); 150 Object[] param = new Object[1]; 151 152 if (paramtype[0].equals(java.lang.Integer.TYPE) || 153 paramtype[0].equals(java.lang.Integer.class)){ 154 param[0] = new Integer(properties.getProperty(fieldName,"")); 155 156 } else if (paramtype[0].equals(java.lang.Boolean.TYPE) || 157 paramtype[0].equals(java.lang.Boolean.class) ){ 158 param[0] = new Boolean(properties.getProperty(fieldName,"")); 159 160 } else if (paramtype[0].equals(java.lang.Double.TYPE) || 161 paramtype[0].equals(java.lang.Double.class) ){ 162 param[0] = new Double(properties.getProperty(fieldName,"")); 163 164 } else if (paramtype[0].equals(java.lang.Byte.TYPE) || 165 paramtype[0].equals(java.lang.Byte.class) ){ 166 param[0] = new Byte(properties.getProperty(fieldName,"")); 167 168 } else if (paramtype[0].equals(java.lang.Short.TYPE) || 169 paramtype[0].equals(java.lang.Short.class)){ 170 param[0] = new Short(properties.getProperty(fieldName,"")); 171 172 } else if (paramtype[0].equals(java.lang.Long.TYPE) || 173 paramtype[0].equals(java.lang.Long.class) ){ 174 param[0] = new Long(properties.getProperty(fieldName,"")); 175 176 } else if (paramtype[0].equals(java.lang.Float.TYPE) || 177 paramtype[0].equals(java.lang.Float.class) ){ 178 param[0] = new Float(properties.getProperty(fieldName,"")); 179 180 } else if (paramtype[0].equals(java.lang.String.class)){ 181 param[0] = properties.getProperty(fieldName,""); 182 183 } else { 184 throw new Exception("incorrect type for setter method "); 185 } 186 m[i].invoke(o,param); 189 } else { 190 throw new Exception("No method "+methodName+" provided by "+o); 191 } 192 } 193 } 194 } 195 | Popular Tags |