KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > Configuration


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/Configuration.java,v 1.19 2004/07/28 09:34:29 ib Exp $
3  * $Revision: 1.19 $
4  * $Date: 2004/07/28 09:34:29 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.util;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import org.apache.slide.common.Domain;
33 import org.apache.slide.store.Store;
34
35 /**
36  * Provides default configuration for Slide components from the
37  * <tt>slide.properties</tt> configuration file. All slide features
38  * rely on the central configuration file.
39  * <p>
40  * The configuration file is loaded from the Java <tt>lib</tt>
41  * directory, the classpath and the Castor JAR. Properties set in the
42  * classpath file takes precedence over properties set in the Java library
43  * configuration file and properties set in the Slide JAR, allowing for
44  * each customization. All three files are named <tt>slide.properties</tt>.
45  * <p>
46  * For example, to change the domain init file in use, create a new
47  * configuration file in the current directory, instead of modifying
48  * the global one.
49  *
50  * @version $Revision: 1.19 $ $Date: 2004/07/28 09:34:29 $
51  */

52 public abstract class Configuration {
53     
54     
55     // --------------------------------------------------- Property Inner Class
56

57     
58     /**
59      * Names of properties used in the configuration file.
60      */

61     public static class Property {
62         
63         
64         // ---------------------------------------------------------- Constants
65

66         
67         /**
68          * Property specifying the domain XML initialization file to use.
69          * <pre>
70          * org.apache.slide.domain
71          * </pre>
72          */

73         public static final String JavaDoc DomainInitFilename =
74             "org.apache.slide.domain";
75         
76         
77         /**
78          * Property specifying that internal security checking is enabled.
79          * <pre>
80          * org.apache.slide.security
81          * </pre>
82          */

83         public static final String JavaDoc IntegratedSecurity =
84             "org.apache.slide.security";
85         
86         
87         /**
88          * Property specifying that internal locking checks are enabled.
89          * <pre>
90          * org.apache.slide.lock
91          * </pre>
92          */

93         public static final String JavaDoc IntegratedLocking = "org.apache.slide.lock";
94         
95         
96         /**
97          * Property specifying that version control is enabled.
98          * <pre>
99          * org.apache.slide.versioncontrol
100          * </pre>
101          */

102         public static final String JavaDoc VersionControl = "org.apache.slide.versioncontrol";
103         
104         
105         /**
106          * Property specifying that search is enabled.
107          * <pre>
108          * org.apache.slide.versioncontrol
109          * </pre>
110          */

111         public static final String JavaDoc Search = "org.apache.slide.search";
112         
113         
114         /**
115          * Property specifying that the binding is enabled.
116          * <pre>
117          * org.apache.slide.binding
118          * </pre>
119          */

120         public static final String JavaDoc Binding = "org.apache.slide.binding";
121
122
123         /**
124          * Property specifying if events are enabled.
125          * <pre>
126          * org.apache.slide.events
127          * </pre>
128          */

129         public static final String JavaDoc Events = "org.apache.slide.events";
130
131         /**
132          * Property specifying the encoding for URLs.
133          * <pre>
134          * org.apache.slide.urlEncoding
135          * </pre>
136          */

137         public static final String JavaDoc UrlEncoding = "org.apache.slide.urlEncoding";
138         
139         
140         /**
141          * Property specifying whether principal-identified locks are supported.
142          * <pre>
143          * org.apache.slide.principalIdentifiedLocks
144          * </pre>
145          */

146         public static final String JavaDoc PrincipalIdentifiedLocks = "org.apache.slide.principalIdentifiedLocks";
147
148
149         /**
150          * Property specifying whether to run in debug mode.
151          * <pre>
152          * org.apache.slide.debug
153          * </pre>
154          */

155         public static final String JavaDoc Debug = "org.apache.slide.debug";
156         
157         
158         /**
159          * The name of the configuration file.
160          * <pre>
161          * slide.properties
162          * </pre>
163          */

164         public static final String JavaDoc FileName = "slide.properties";
165         
166         
167         static final String JavaDoc ResourceName =
168             "/org/apache/slide/slide.properties";
169         
170     }
171     
172     
173     // -------------------------------------------------------------- Variables
174

175     
176     /**
177      * The default properties loaded from the configuration file.
178      */

179     private static Properties JavaDoc _default;
180     
181     
182     /**
183      * True if the default configuration specified debugging.
184      */

185     private static boolean _debug;
186     
187     
188     /**
189      * True if integrated security checks are performed.
190      */

191     private static boolean _security;
192     
193     
194     /**
195      * True if integrated locking is used.
196      */

197     private static boolean _locking;
198     
199     
200     /**
201      * True if version control is used.
202      */

203     private static boolean _versioncontrol;
204     
205     
206     /**
207      * True if search is enabled.
208      */

209     private static boolean _search;
210     
211     
212     /**
213      * True if bind is enabled.
214      */

215     private static boolean _binding;
216
217     /**
218      * True if events should be fired
219      */

220     private static boolean _events;
221
222     /**
223      * URL encoding.
224      */

225     private static String JavaDoc _urlEncoding;
226     
227     
228     /**
229      * Principal identified locks.
230      */

231     private static boolean _principalIdentifiedLocks;
232
233
234     // ------------------------------------------------------------- Properties
235

236     
237     /**
238      * Returns true if the default configuration specified debugging.
239      */

240     public static boolean debug() {
241         return _debug;
242     }
243     
244     
245     /**
246      * Returns true if integrated security checking is used.
247      */

248     public static boolean useIntegratedSecurity() {
249         return _security;
250     }
251     
252     
253     /**
254      * Returns true if integrated lock checking is used.
255      */

256     public static boolean useIntegratedLocking() {
257         return _locking;
258     }
259     
260     
261     /**
262      * Returns true if version control is used.
263      */

264     public static boolean useVersionControl() {
265         return _versioncontrol;
266     }
267     
268     
269     /**
270      * Returns true if search is enabled.
271      */

272     public static boolean useSearch () {
273         return _search;
274     }
275     
276     // TODO: move somewhere else?
277
public static boolean useBinding (Store store) {
278         return _binding && store.useBinding();
279     }
280     
281     /**
282      * TODO: dump?
283      * Returns true if binding is enabled.
284      */

285     public static boolean useGlobalBinding () {
286         return _binding;
287     }
288
289     /**
290      * Returns true if events are enabled by default
291      */

292     public static boolean fireEvents() {
293         return _events;
294     }
295
296     
297     /**
298      * Returns the used URL encoding.
299      */

300     public static String JavaDoc urlEncoding() {
301         return _urlEncoding;
302     }
303     
304     /**
305      * Returns true if Principal identified locks are enabled.
306      */

307     public static boolean usePrincipalIdentifiedLocks() {
308         return _principalIdentifiedLocks;
309     }
310
311     
312     // ------------------------------------------------------------ Initializer
313

314     
315     static {
316         getDefault();
317     }
318     
319     
320     // --------------------------------------------------------- Public Methods
321

322     
323     /**
324      * Returns the default configuration file. Changes to the returned
325      * properties set will affect all Castor functions relying on the
326      * default configuration.
327      *
328      * @return The default configuration
329      */

330     public static synchronized Properties JavaDoc getDefault() {
331         if (_default == null) {
332             load();
333         }
334         return _default;
335     }
336     
337     
338     // ------------------------------------------------------ Protected Methods
339

340     
341     /**
342      * Called by {@link #getDefault} to load the configuration the
343      * first time. Will not complain about inability to load
344      * configuration file from one of the default directories, but if
345      * it cannot find the JAR's configuration file, will throw a
346      * run time exception.
347      */

348     protected static void load() {
349         
350         File JavaDoc file;
351         InputStream JavaDoc is;
352         
353         // Get detault configuration from the Slide JAR.
354
_default = new Properties JavaDoc();
355         try {
356             _default.load(Configuration.class.getResourceAsStream
357                           (Property.ResourceName));
358         } catch (Exception JavaDoc except) {
359             // This should never happen
360
//throw new RuntimeException
361
// (Messages.format("conf.notDefaultConfigurationFile",
362
// Property.FileName));
363
}
364         
365         // Get overriding configuration from the Java
366
// library directory, ignore if not found.
367
file = new File JavaDoc(System.getProperty("java.home"), "lib");
368         file = new File JavaDoc(file, Property.FileName);
369         if (file.exists()) {
370             _default = new Properties JavaDoc(_default);
371             try {
372                 _default.load(new FileInputStream JavaDoc(file));
373                 Domain.info("Configuration found in java.home");
374             } catch (IOException JavaDoc except) {
375                 // Do nothing
376
}
377         }
378         
379         // Get overriding configuration from the classpath,
380
// ignore if not found.
381
try {
382             is = Configuration.class.getResourceAsStream("/"
383                                                          + Property.FileName);
384             if ( is != null ) {
385                 _default = new Properties JavaDoc(_default);
386                 _default.load(is);
387                 Domain.info("Configuration found in classpath");
388             }
389         } catch (Exception JavaDoc except) {
390             // Do nothing
391
}
392         
393         String JavaDoc prop;
394         
395         prop = _default.getProperty(Property.Debug, "false");
396         if (prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("on")) {
397             _debug = true;
398         }
399         
400         prop = _default.getProperty(Property.IntegratedSecurity, "true");
401         if (prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("on")) {
402             _security = true;
403         } else {
404             _security = false;
405         }
406         
407         prop = _default.getProperty(Property.IntegratedLocking, "true");
408         if (prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("on")) {
409             _locking = true;
410         } else {
411             _locking = false;
412         }
413         
414         prop = _default.getProperty(Property.VersionControl, "true");
415         if (prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("on")) {
416             _versioncontrol = true;
417         } else {
418             _versioncontrol = false;
419         }
420         
421         prop = _default.getProperty(Property.Search, "true");
422         if (prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("on")) {
423             _search = true;
424         } else {
425             _search = false;
426         }
427         
428         prop = _default.getProperty(Property.Binding, "true");
429         if (prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("on")) {
430             _binding = true;
431         } else {
432             _binding = false;
433         }
434
435         prop = _default.getProperty(Property.Events, "false");
436         if (prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("on")) {
437             _events = true;
438         } else {
439             _events = false;
440         }
441
442         prop = _default.getProperty(Property.PrincipalIdentifiedLocks, "false");
443         if (prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("on")) {
444             _principalIdentifiedLocks = true;
445         } else {
446             _principalIdentifiedLocks = false;
447         }
448
449         String JavaDoc defaultEncoding = new java.io.InputStreamReader JavaDoc(System.in).getEncoding();
450         _urlEncoding = _default.getProperty(Property.UrlEncoding, defaultEncoding);
451         
452     }
453     
454 }
455
Popular Tags