KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > util > PropertyManager


1 /*
2  * $Id: PropertyManager.java,v 1.7 2002/07/15 02:15:03 skavish Exp $
3  *
4  * ===========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.util;
52
53 import org.openlaszlo.iv.flash.api.*;
54
55 import org.openlaszlo.iv.flash.cache.*;
56 import java.io.*;
57 import java.net.*;
58 import java.util.*;
59
60 /**
61  * Property manager.
62  * <P>
63  * Reads and writes properties from iv.property file.
64  * Caches most used properties as static variables
65  *
66  * @author Dmitry Skavish
67  */

68 public class PropertyManager {
69
70     /**
71      * Property org.openlaszlo.iv.flash.varCaseSensitive
72      */

73     public static boolean varCaseSensitive = true;
74     /**
75      * Property org.openlaszlo.iv.flash.symCaseSensitive
76      */

77     public static boolean symCaseSensitive = false;
78     /**
79      * Property org.openlaszlo.iv.flash.showErrorsInline
80      */

81     public static boolean showErrorsInline = true;
82     /**
83      * Property org.openlaszlo.iv.flash.textBoundsMMStyle
84      */

85     public static boolean textMMStyle = false;
86     /**
87      * Property org.openlaszlo.iv.flash.fontPath
88      */

89     public static String JavaDoc fontPath = "fonts/";
90     /**
91      * Property org.openlaszlo.iv.flash.defaultEncoding
92      */

93     public static String JavaDoc defaultEncoding = null;
94     /**
95      * Property org.openlaszlo.iv.flash.mxLibrarySymbolPrefix
96      */

97     public static String JavaDoc mxLibrarySymbolPrefix = "__";
98     /**
99      * Property org.openlaszlo.iv.flash.mxLibraryFontID
100      */

101     public static String JavaDoc mxLibraryFontID = "[jgen]";
102
103     private static Properties myProperties;
104
105     private static void loadCacheProperties( CacheSettings cs, String JavaDoc prefix ) {
106         cs.setMaxSize( getIntProperty( prefix+"CacheMaxSize", 0 ) );
107         cs.setDefaultExpire( (long) (getDoubleProperty( prefix+"CacheDefaultExpire", 0.0 )*1000) );
108         cs.setForce( getBoolProperty( prefix+"CacheForce", false ) );
109         cs.setRecycle( getBoolProperty( prefix+"CacheRecycle", false ) );
110         cs.setCheckModifiedSince( getBoolProperty( prefix+"CacheCheckModifiedSince", false ) );
111     }
112
113     private static void saveCacheProperties( CacheSettings cs, String JavaDoc prefix ) {
114         setProperty( prefix+"CacheMaxSize", cs.getMaxSize() );
115         setProperty( prefix+"CacheDefaultExpire", cs.getDefaultExpire()/1000.0 );
116         setProperty( prefix+"CacheForce", cs.isForce() );
117         setProperty( prefix+"CacheRecycle", cs.isRecycle() );
118         setProperty( prefix+"CacheCheckModifiedSince", cs.isCheckModifiedSince() );
119     }
120
121     /**
122      * Initalizes manager.
123      * <P>
124      * Reads and caches all the properties
125      *
126      * @param propFileName iv.properties file name - can be absolute name
127      */

128     public static void init( String JavaDoc propFileName ) {
129         if( propFileName == null ) propFileName = "iv.properties";
130         load(propFileName);
131         varCaseSensitive = getBoolProperty( "org.openlaszlo.iv.flash.varCaseSensitive", true );
132         symCaseSensitive = getBoolProperty( "org.openlaszlo.iv.flash.symCaseSensitive", false );
133         showErrorsInline = getBoolProperty( "org.openlaszlo.iv.flash.showErrorsInline", true );
134         textMMStyle = getBoolProperty( "org.openlaszlo.iv.flash.textBoundsMMStyle", false );
135         fontPath = getProperty( "org.openlaszlo.iv.flash.fontPath", "fonts/" );
136         defaultEncoding = getProperty( "org.openlaszlo.iv.flash.defaultEncoding" );
137         mxLibrarySymbolPrefix = getProperty( "org.openlaszlo.iv.flash.mxLibrarySymbolPrefix", "__" );
138         mxLibraryFontID = getProperty( "org.openlaszlo.iv.flash.mxLibraryFontID", "[jgen]" );
139         if( defaultEncoding != null ) {
140             defaultEncoding = defaultEncoding.trim();
141             if( defaultEncoding.length() == 0 || Util.isDefault(defaultEncoding) ) {
142                 defaultEncoding = null;
143             }
144         }
145     }
146
147     /**
148      * Initalizes manager.
149      * <P>
150      * Reads and caches all the properties
151      */

152     public static void init() {
153         init("iv.properties");
154     }
155
156     /**
157      * Loads iv.properties file and caches "cache" related properties.
158      */

159     public static void load( String JavaDoc propFileName ) {
160         myProperties = new Properties();
161         try {
162             myProperties.load( new FileInputStream( Util.getSysFile( propFileName ) ) );
163         } catch( Exception JavaDoc e ) {
164             try {
165                 myProperties.load( Util.getResource("/iv.properties").openStream() );
166             } catch( Exception JavaDoc e1 ) {
167                 Log.error(Resource.get(Resource.CANTLOADPROPERTIES), e1);
168                 myProperties = new Properties();
169             }
170         }
171         loadCacheProperties( getRequestCacheSettings(), "org.openlaszlo.iv.flash.request" );
172         loadCacheProperties( getFontCacheSettings(), "org.openlaszlo.iv.flash.font" );
173         loadCacheProperties( getMediaCacheSettings(), "org.openlaszlo.iv.flash.media" );
174         loadCacheProperties( getXMLCacheSettings(), "org.openlaszlo.iv.flash.xml" );
175     }
176
177     /**
178      * Saves properties to iv.properties file.
179      *
180      * @exception IOException
181      */

182     public static void save( String JavaDoc propFileName ) throws IOException {
183         saveCacheProperties( getRequestCacheSettings(), "org.openlaszlo.iv.flash.request" );
184         saveCacheProperties( getFontCacheSettings(), "org.openlaszlo.iv.flash.font" );
185         saveCacheProperties( getMediaCacheSettings(), "org.openlaszlo.iv.flash.media" );
186         saveCacheProperties( getXMLCacheSettings(), "org.openlaszlo.iv.flash.xml" );
187         setProperty( "org.openlaszlo.iv.flash.varCaseSensitive", varCaseSensitive );
188         setProperty( "org.openlaszlo.iv.flash.symCaseSensitive", symCaseSensitive );
189         setProperty( "org.openlaszlo.iv.flash.showErrorsInline", showErrorsInline );
190         setProperty( "org.openlaszlo.iv.flash.textBoundsMMStyle", textMMStyle );
191         setProperty( "org.openlaszlo.iv.flash.fontPath", fontPath );
192         if( defaultEncoding != null ) setProperty( "org.openlaszlo.iv.flash.defaultEncoding", defaultEncoding );
193         // has to be replaced with store
194
myProperties.save( new FileOutputStream( Util.getSysFile(propFileName) ), "" );
195     }
196
197     /**
198      * Sets property by name.
199      *
200      * @param name name of the property
201      * @param value value of the property
202      */

203     public static void setProperty( String JavaDoc name, String JavaDoc value ) {
204         myProperties.put(name, value);
205     }
206
207     /**
208      * Sets integer property by name.
209      *
210      * @param name name of the property
211      * @param value value of the property
212      */

213     public static void setProperty( String JavaDoc name, int value ) {
214         myProperties.put(name, new Integer JavaDoc(value).toString());
215     }
216
217     /**
218      * Sets boolean property by name.
219      *
220      * @param name name of the property
221      * @param value value of the property
222      */

223     public static void setProperty( String JavaDoc name, boolean value ) {
224         myProperties.put(name, new Boolean JavaDoc(value).toString());
225     }
226
227     /**
228      * Sets double property by name.
229      *
230      * @param name name of the property
231      * @param value value of the property
232      */

233     public static void setProperty( String JavaDoc name, double value ) {
234         myProperties.put(name, new Double JavaDoc(value).toString());
235     }
236
237     /**
238      * Gets property by name.
239      *
240      * @param name name of the property
241      * @param def default value to be returned if there is no such property
242      * @return value of the property or provided default value
243      */

244     public static String JavaDoc getProperty( String JavaDoc name, String JavaDoc def ) {
245         String JavaDoc value = System.getProperty(name);
246         if( value != null ) return value;
247         return myProperties.getProperty(name, def);
248     }
249
250     /**
251      * Gets property by name.
252      *
253      * @param name name of the property
254      * @return value of the property or null
255      */

256     public static String JavaDoc getProperty( String JavaDoc name ) {
257         String JavaDoc value = System.getProperty(name);
258         if( value != null ) return value;
259         return myProperties.getProperty(name);
260     }
261
262     /**
263      * Gets integer property by name.
264      *
265      * @param name name of the property
266      * @param def default value to be returned if there is no such property
267      * @return value of the property or provided default value
268      */

269     public static int getIntProperty( String JavaDoc property, int def ) {
270         return Util.toInt( getProperty(property), def );
271     }
272
273     /**
274      * Gets long property by name.
275      *
276      * @param name name of the property
277      * @param def default value to be returned if there is no such property
278      * @return value of the property or provided default value
279      */

280     public static long getLongProperty( String JavaDoc property, long def ) {
281         return Util.toLong( getProperty(property), def );
282     }
283
284     /**
285      * Gets boolean property by name.
286      *
287      * @param name name of the property
288      * @param def default value to be returned if there is no such property
289      * @return value of the property or provided default value
290      */

291     public static boolean getBoolProperty( String JavaDoc property, boolean def ) {
292         return Util.toBool( getProperty(property), def );
293     }
294
295     /**
296      * Gets double property by name.
297      *
298      * @param name name of the property
299      * @param def default value to be returned if there is no such property
300      * @return value of the property or provided default value
301      */

302     public static double getDoubleProperty( String JavaDoc property, double def ) {
303         return Util.toDouble( getProperty(property), def );
304     }
305
306     public static Object JavaDoc getObjectProperty( Object JavaDoc key ) {
307         return myProperties.get(key);
308     }
309
310     public static void setObjectProperty( Object JavaDoc key, Object JavaDoc value ) {
311         myProperties.put(key, value);
312     }
313
314     /**
315      * Returns request cache settings.
316      *
317      * @return request cache settings
318      */

319     public static CacheSettings getRequestCacheSettings() {
320         return RequestCache.getSettings();
321     }
322
323     /**
324      * Returns font cache settings.
325      *
326      * @return font cache settings
327      */

328     public static CacheSettings getFontCacheSettings() {
329         return FontCache.getSettings();
330     }
331
332     /**
333      * Returns media cache settings.
334      *
335      * @return media cache settings
336      */

337     public static CacheSettings getMediaCacheSettings() {
338         return MediaCache.getSettings();
339     }
340
341     /**
342      * Returns xml cache settings.
343      *
344      * @return xml cache settings
345      */

346     public static CacheSettings getXMLCacheSettings() {
347         return XMLCache.getSettings();
348     }
349
350 }
351
Popular Tags