Source code: org/acs/damsel/srvr/Config.java
1 package org.acs.damsel.srvr;
2
3 import java.io.*;
4
5 import org.apache.log4j.*;
6 import org.exolab.castor.xml.*;
7
8 /**
9 * <p>Title: Config</p>
10 * <p>Description: Config is a singleton class that stores the configuration
11 * information for the application. It also provides a means to save the
12 * configuration to a disk. When the Config class is first created, it loads
13 * the saved configuration information from disk.</p>
14 * @version 1.0
15 * @todo add more config data (.css filenames, paths to other xml files, etc)
16 */
17 public class Config implements Serializable {
18 /*
19 * new config data goes here: (remember to create get/set methods!)
20 * note also that these are default values. normally, config data will be
21 * read in from the config.xml file when instance() is called for the first
22 * time.
23 */
24
25 private static String prefix = "../webapps/";
26 //private static String prefix = "C:/Documents and Settings/ACS/jbproject/damsel/";
27 private static String logPropertiesFileName = prefix + "logProperties.txt";
28 private static String uploadPrefix = prefix + "client/assets/";
29 private static String configFileName = "config.xml";
30 private static String assetPrefix = "assets/";
31 private static boolean allowRegistration;
32
33 // these control the database configuration.
34 // to use another database, change these values accordingly
35 private static String assetDBDriverClass = "com.mysql.jdbc.Driver";
36 private static String assetDBConnection = "jdbc:mysql://161.13.4.191/damsel";
37 private static String assetDBUserName = "damsel";
38 private static String assetDBPassword = "";
39
40 /* end config data */
41
42 private static Logger log = Logger.getLogger(Config.class);
43 private static Config instance = null;
44
45 /**
46 * This default constructor is only here because Castor's Unmarshaller requires
47 * it. Generally, one should use the instance() method.
48 */
49 public Config() {}
50
51 /**
52 * The instance() method is used instead of a constructor to get a handle on
53 * the Config object. If the Config object has not yet been initialized, it
54 * will load from the default config.xml file specified above.
55 * @return a handle on the Config object
56 */
57 public static synchronized Config instance() {
58 if (instance == null) {
59 instance = newConfigFromFile(new File(prefix + configFileName));
60 }
61 return instance;
62 }
63
64 /**
65 * The instance(File) method is like the instance() method except that it will
66 * load the configuration from the specified file if the Config object has yet
67 * to be initialized.
68 * @param f File
69 * @return a handle on the Config object
70 */
71 public static synchronized Config instance(File f) {
72 if(instance == null) {
73 instance = newConfigFromFile(f);
74 }
75 return instance;
76 }
77
78 /**
79 * The newConfigFromFile method is used instead of a constructor to load the
80 * configuration data from the file specified in configFileName. If loading
81 * fails for whatever reason, the default constructor is used as a failsafe.
82 * @param f File
83 * @return a handle on new instance of the Config object for File
84 */
85 private static synchronized Config newConfigFromFile(File f) {
86 BasicConfigurator.resetConfiguration();
87 PropertyConfigurator.configure(logPropertiesFileName);
88
89 FileReader reader = null;
90 Config newConfig = null;
91 try {
92 // Create a File to unmarshal from
93 reader = new FileReader(f);
94 // Unmarshal the Config object
95 newConfig = (Config) Unmarshaller.unmarshal(Config.class, reader);
96 }
97 catch (ValidationException ex) {
98 log.error("ValidationException while loading config from file " +
99 f.getAbsoluteFile());
100 log.error(ex.getMessage());
101 }
102 catch (MarshalException ex) {
103 log.error("MarshalException while loading config from file " +
104 f.getAbsoluteFile());
105 log.error(ex.getMessage());
106 }
107 catch (FileNotFoundException ex) {
108 log.error("FileNotFoundException while loading config from file " +
109 f.getAbsoluteFile());
110 log.error(ex.getMessage());
111 }
112 finally {
113 // be sure to close the writer
114 try {
115 if (reader != null)
116 reader.close();
117 }
118 catch (IOException e) {
119 log.error("IOException while closing file " + f.getAbsoluteFile());
120 }
121 }
122 if(newConfig == null) {
123 log.warn("Using default configuration specified in Config.java");
124 return new Config();
125 }
126 return newConfig;
127 }
128
129 /**
130 * The save method writes the current configuration to the XML file specified
131 * in configFileName.
132 * @param f File
133 * @throws IOException
134 */
135 public void save(File f) throws IOException {
136 FileWriter writer = null;
137 try {
138 // Create a File to marshal to
139 writer = new FileWriter(f);
140 // Marshal the SchemaMgr object
141 Marshaller.marshal(this, writer);
142 }
143 catch (ValidationException ex) {
144 log.error("ValidationException while saving config to file " +
145 f.getAbsoluteFile());
146 }
147 catch (MarshalException ex) {
148 log.error("MarshalException while saving config to file " +
149 f.getAbsoluteFile());
150 }
151 finally {
152 // be sure to close the writer
153 try {
154 if (writer != null)
155 writer.close();
156 }
157 catch (IOException e) {
158 log.error("IOException while closing file " + f.getAbsoluteFile());
159 }
160 }
161 }
162
163 /* Getters and Setters */
164 public String getConfigFileName() {
165 return configFileName;
166 }
167 public void setConfigFileName(String configFileName) {
168 this.configFileName = configFileName;
169 }
170 public String getPrefix() {
171 return prefix;
172 }
173 public void setPrefix(String prefix) {
174 this.prefix = prefix;
175 }
176 public String getAssetPrefix() {
177 return assetPrefix;
178 }
179 public void setAssetPrefix(String assetPrefix) {
180 this.assetPrefix = assetPrefix;
181 }
182
183 /**
184 * The invalidate() method sets the Config object instance to null. Because
185 * Config is a singleton and is used by many other parts of the system,
186 * use this only for unit-testing or if you are ABSOLUTELY SURE of what you're
187 * doing!
188 */
189 public synchronized void invalidate() {
190 instance = null;
191 }
192 public String getAssetDBConnection() {
193 return assetDBConnection;
194 }
195 public String getAssetDBDriverClass() {
196 return assetDBDriverClass;
197 }
198 public void setAssetDBConnection(String assetDBConnection) {
199 this.assetDBConnection = assetDBConnection;
200 }
201 public void setAssetDBDriverClass(String assetDBDriverClass) {
202 this.assetDBDriverClass = assetDBDriverClass;
203 }
204 public String getAssetDBUserName() {
205 return assetDBUserName;
206 }
207 public void setAssetDBUserName(String assetDBUserName) {
208 this.assetDBUserName = assetDBUserName;
209 }
210 public String getLogPropertiesFileName() {
211 return logPropertiesFileName;
212 }
213 public void setLogPropertiesFileName(String logPropertiesFileName) {
214 this.logPropertiesFileName = logPropertiesFileName;
215 }
216 public String getUploadPrefix() {
217 return uploadPrefix;
218 }
219 public void setUploadPrefix(String uploadPrefix) {
220 this.uploadPrefix = uploadPrefix;
221 }
222 public String getAssetDBPassword() {
223 return assetDBPassword;
224 }
225 public void setAssetDBPassword(String assetDBPassword) {
226 this.assetDBPassword = assetDBPassword;
227 }
228 public boolean isAllowRegistration() {
229 return allowRegistration;
230 }
231 public void setAllowRegistration(boolean allowRegistration) {
232 this.allowRegistration = allowRegistration;
233 }
234 }