KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > core > BaseConfigurationBackedConstants


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
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
20 package za.org.coefficient.core;
21
22 import org.apache.commons.beanutils.ConvertUtils;
23
24 import za.org.coefficient.util.common.InvokerFactory;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * This is a base class that if extended will save static fields in a
32  * database backed configuration.
33  *
34  */

35 public abstract class BaseConfigurationBackedConstants {
36     //~ Static fields/initializers =============================================
37

38     //protected static String propertiesFileName = null;
39
//protected static String className = null;
40

41     public static void init(String JavaDoc propertiesFileName, String JavaDoc className) {
42         try {
43             CoefficientConfiguration cc =
44                 (CoefficientConfiguration)InvokerFactory.getInvoker()
45                 .invokeMethodOnModule("Configuration", "getConfiguration",
46                                       new Object JavaDoc[]{className});
47
48             // read the properties file if present
49
java.util.Properties JavaDoc props = new java.util.Properties JavaDoc();
50             java.io.InputStream JavaDoc propsStream = null;
51
52             if(propertiesFileName != null) {
53                 try {
54                     // See if it is in the classpath
55
propsStream =
56                         BaseConfigurationBackedConstants
57                         .class.getResourceAsStream("/WEB-INF/"
58                                                    + propertiesFileName);
59                     // See if it is in the jar
60
if (null == propsStream) {
61                         propsStream =
62                             BaseConfigurationBackedConstants.class
63                             .getResourceAsStream("/" + propertiesFileName);
64                         if (null == propsStream) {
65                             propsStream =
66                                 BaseConfigurationBackedConstants.class
67                                 .getResourceAsStream(propertiesFileName);
68                         }
69                     }
70                     props.load(propsStream);
71                 } catch (Throwable JavaDoc t) {
72                 }
73             }
74
75             // update field values as appropriate
76
java.lang.reflect.Field JavaDoc[] fields = Class.forName(className)
77                 .getFields();
78             for (int i = 0; i < fields.length; i++) {
79                 String JavaDoc name = fields[i].getName();
80                 String JavaDoc value = props.getProperty(name);
81                 if (value == null) {
82                     value = cc.getProperty(name);
83                 }
84                 if (value != null) {
85                     value = value.trim();
86                     Object JavaDoc oVal = ConvertUtils.convert(value, fields[i].getType());
87                     fields[i].set(null, oVal);
88                     cc.addProperty(name, oVal.toString());
89                 } else {
90                     cc.addProperty(name, fields[i].get(null).toString());
91                 }
92             }
93
94             // Always store the currentValues
95
InvokerFactory.getInvoker()
96                 .invokeMethodOnModule("Configuration", "saveConfiguration",
97                                       new Object JavaDoc[] {cc});
98             
99         } catch (Throwable JavaDoc t) {
100             t.printStackTrace();
101             System.err.println("## BaseConfigurationBackedConstants init error: "
102                                + t);
103         }
104     }
105
106     public static void initFromConfig(CoefficientConfiguration cc,
107                                       String JavaDoc className) {
108         try {
109             BaseConfigurationBackedConstants conf =
110                 (BaseConfigurationBackedConstants)Class.forName(className)
111                 .newInstance();
112             // update field values as appropriate
113
java.lang.reflect.Field JavaDoc[] fields = Class.forName(className).getFields();
114                 //Constants.class.getFields();
115
for (int i = 0; i < fields.length; i++) {
116                 String JavaDoc name = fields[i].getName();
117                 String JavaDoc value = cc.getProperty(name);
118                 if (value != null) {
119                     value = value.trim();
120                     Object JavaDoc oVal = ConvertUtils.convert(value, fields[i].getType());
121                     fields[i].set(null, oVal);
122                 }
123             }
124             BaseConfigurationBackedConstants.hasRequiredFields();
125         } catch (Throwable JavaDoc t) {
126             System.err.println("## BaseConfigurationBackedConstants config init error: " + t);
127         }
128     }
129
130     public static boolean hasRequiredFields() {
131         return true;
132     }
133
134     public static List JavaDoc getPropertyFields(String JavaDoc className) {
135         try {
136             return Arrays.asList(Class.forName(className).getFields());
137         } catch (Exception JavaDoc ex) {
138             ex.printStackTrace();
139             return new ArrayList JavaDoc();
140         }
141     }
142 }
143
Popular Tags