KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > jndi > PropertiesLoader


1 /**
2  * Copyright (C) 2001-2004
3  * - France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors: P. Dechamboux, S. Chassande-Barrioz
20  *
21  */

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 /**
33  * This class permits to load a properties file. The values are
34  * reachable via to type: Context or Properties
35  */

36 public class PropertiesLoader {
37
38     /**
39      * This field is the name of the properties file.
40      */

41     protected String fileName = null;
42
43     /**
44      * This field is the reference to a context implementation
45      * which contains the values load from the properties file.
46      */

47     protected Context context = null;
48     
49     /**
50      * This field is the class which must be used as implementation
51      * of the Context interface.
52      */

53     protected Class contextImpl = null;
54
55     /**
56      * This field is the reference to a Properties implementation
57      * which contains the values load from the properties file.
58      */

59     protected Properties properties = new Properties();
60
61     public PropertiesLoader() {
62     }
63
64     /**
65      * This contructor permits to specify which implementation of the
66      * Context interface must be used.
67      * This constructor throws an exception when the specified class
68      * isn't reachable or doesn't implement the Context interface.
69      */

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     /**
89      * This method load the values of the properties file which
90      * the name is the parameter value.
91      */

92     public synchronized void load(String _fileName) throws Exception {
93         fileName = _fileName;
94         reload();
95     }
96     
97     /**
98      * This method cleans all values of the current object, and reload
99      * the new values from the current file(fileName field).
100      */

101     public synchronized void reload() throws Exception {
102         // Load file in properties field
103
FileInputStream is = new FileInputStream(fileName);
104         properties.clear();
105         properties.load(is);
106         
107         // Full the context field if a context implementation class
108
// is specified
109
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     /**
121      * This method clean the current object. No value is accessible
122      * until the next load call.
123      */

124     public synchronized void clear() throws Exception {
125         properties = null;
126         context = null;
127         fileName = null;
128     }
129
130
131     /**
132      *
133      */

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                 // invocation of the corresponding setter method on
187
// ManagedConnectionFactory instance
188
m[i].invoke(o,param);
189             } else {
190                 throw new Exception("No method "+methodName+" provided by "+o);
191             }
192         }
193     }
194 }
195
Popular Tags