001: /*
002: * $Id: PropertyManager.java,v 1.7 2002/07/15 02:15:03 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.util;
052:
053: import org.openlaszlo.iv.flash.api.*;
054:
055: import org.openlaszlo.iv.flash.cache.*;
056: import java.io.*;
057: import java.net.*;
058: import java.util.*;
059:
060: /**
061: * Property manager.
062: * <P>
063: * Reads and writes properties from iv.property file.
064: * Caches most used properties as static variables
065: *
066: * @author Dmitry Skavish
067: */
068: public class PropertyManager {
069:
070: /**
071: * Property org.openlaszlo.iv.flash.varCaseSensitive
072: */
073: public static boolean varCaseSensitive = true;
074: /**
075: * Property org.openlaszlo.iv.flash.symCaseSensitive
076: */
077: public static boolean symCaseSensitive = false;
078: /**
079: * Property org.openlaszlo.iv.flash.showErrorsInline
080: */
081: public static boolean showErrorsInline = true;
082: /**
083: * Property org.openlaszlo.iv.flash.textBoundsMMStyle
084: */
085: public static boolean textMMStyle = false;
086: /**
087: * Property org.openlaszlo.iv.flash.fontPath
088: */
089: public static String fontPath = "fonts/";
090: /**
091: * Property org.openlaszlo.iv.flash.defaultEncoding
092: */
093: public static String defaultEncoding = null;
094: /**
095: * Property org.openlaszlo.iv.flash.mxLibrarySymbolPrefix
096: */
097: public static String mxLibrarySymbolPrefix = "__";
098: /**
099: * Property org.openlaszlo.iv.flash.mxLibraryFontID
100: */
101: public static String mxLibraryFontID = "[jgen]";
102:
103: private static Properties myProperties;
104:
105: private static void loadCacheProperties(CacheSettings cs,
106: String prefix) {
107: cs.setMaxSize(getIntProperty(prefix + "CacheMaxSize", 0));
108: cs.setDefaultExpire((long) (getDoubleProperty(prefix
109: + "CacheDefaultExpire", 0.0) * 1000));
110: cs.setForce(getBoolProperty(prefix + "CacheForce", false));
111: cs.setRecycle(getBoolProperty(prefix + "CacheRecycle", false));
112: cs.setCheckModifiedSince(getBoolProperty(prefix
113: + "CacheCheckModifiedSince", false));
114: }
115:
116: private static void saveCacheProperties(CacheSettings cs,
117: String prefix) {
118: setProperty(prefix + "CacheMaxSize", cs.getMaxSize());
119: setProperty(prefix + "CacheDefaultExpire", cs
120: .getDefaultExpire() / 1000.0);
121: setProperty(prefix + "CacheForce", cs.isForce());
122: setProperty(prefix + "CacheRecycle", cs.isRecycle());
123: setProperty(prefix + "CacheCheckModifiedSince", cs
124: .isCheckModifiedSince());
125: }
126:
127: /**
128: * Initalizes manager.
129: * <P>
130: * Reads and caches all the properties
131: *
132: * @param propFileName iv.properties file name - can be absolute name
133: */
134: public static void init(String propFileName) {
135: if (propFileName == null)
136: propFileName = "iv.properties";
137: load(propFileName);
138: varCaseSensitive = getBoolProperty(
139: "org.openlaszlo.iv.flash.varCaseSensitive", true);
140: symCaseSensitive = getBoolProperty(
141: "org.openlaszlo.iv.flash.symCaseSensitive", false);
142: showErrorsInline = getBoolProperty(
143: "org.openlaszlo.iv.flash.showErrorsInline", true);
144: textMMStyle = getBoolProperty(
145: "org.openlaszlo.iv.flash.textBoundsMMStyle", false);
146: fontPath = getProperty("org.openlaszlo.iv.flash.fontPath",
147: "fonts/");
148: defaultEncoding = getProperty("org.openlaszlo.iv.flash.defaultEncoding");
149: mxLibrarySymbolPrefix = getProperty(
150: "org.openlaszlo.iv.flash.mxLibrarySymbolPrefix", "__");
151: mxLibraryFontID = getProperty(
152: "org.openlaszlo.iv.flash.mxLibraryFontID", "[jgen]");
153: if (defaultEncoding != null) {
154: defaultEncoding = defaultEncoding.trim();
155: if (defaultEncoding.length() == 0
156: || Util.isDefault(defaultEncoding)) {
157: defaultEncoding = null;
158: }
159: }
160: }
161:
162: /**
163: * Initalizes manager.
164: * <P>
165: * Reads and caches all the properties
166: */
167: public static void init() {
168: init("iv.properties");
169: }
170:
171: /**
172: * Loads iv.properties file and caches "cache" related properties.
173: */
174: public static void load(String propFileName) {
175: myProperties = new Properties();
176: try {
177: myProperties.load(new FileInputStream(Util
178: .getSysFile(propFileName)));
179: } catch (Exception e) {
180: try {
181: myProperties.load(Util.getResource("/iv.properties")
182: .openStream());
183: } catch (Exception e1) {
184: Log
185: .error(Resource
186: .get(Resource.CANTLOADPROPERTIES), e1);
187: myProperties = new Properties();
188: }
189: }
190: loadCacheProperties(getRequestCacheSettings(),
191: "org.openlaszlo.iv.flash.request");
192: loadCacheProperties(getFontCacheSettings(),
193: "org.openlaszlo.iv.flash.font");
194: loadCacheProperties(getMediaCacheSettings(),
195: "org.openlaszlo.iv.flash.media");
196: loadCacheProperties(getXMLCacheSettings(),
197: "org.openlaszlo.iv.flash.xml");
198: }
199:
200: /**
201: * Saves properties to iv.properties file.
202: *
203: * @exception IOException
204: */
205: public static void save(String propFileName) throws IOException {
206: saveCacheProperties(getRequestCacheSettings(),
207: "org.openlaszlo.iv.flash.request");
208: saveCacheProperties(getFontCacheSettings(),
209: "org.openlaszlo.iv.flash.font");
210: saveCacheProperties(getMediaCacheSettings(),
211: "org.openlaszlo.iv.flash.media");
212: saveCacheProperties(getXMLCacheSettings(),
213: "org.openlaszlo.iv.flash.xml");
214: setProperty("org.openlaszlo.iv.flash.varCaseSensitive",
215: varCaseSensitive);
216: setProperty("org.openlaszlo.iv.flash.symCaseSensitive",
217: symCaseSensitive);
218: setProperty("org.openlaszlo.iv.flash.showErrorsInline",
219: showErrorsInline);
220: setProperty("org.openlaszlo.iv.flash.textBoundsMMStyle",
221: textMMStyle);
222: setProperty("org.openlaszlo.iv.flash.fontPath", fontPath);
223: if (defaultEncoding != null)
224: setProperty("org.openlaszlo.iv.flash.defaultEncoding",
225: defaultEncoding);
226: // has to be replaced with store
227: myProperties.save(new FileOutputStream(Util
228: .getSysFile(propFileName)), "");
229: }
230:
231: /**
232: * Sets property by name.
233: *
234: * @param name name of the property
235: * @param value value of the property
236: */
237: public static void setProperty(String name, String value) {
238: myProperties.put(name, value);
239: }
240:
241: /**
242: * Sets integer property by name.
243: *
244: * @param name name of the property
245: * @param value value of the property
246: */
247: public static void setProperty(String name, int value) {
248: myProperties.put(name, new Integer(value).toString());
249: }
250:
251: /**
252: * Sets boolean property by name.
253: *
254: * @param name name of the property
255: * @param value value of the property
256: */
257: public static void setProperty(String name, boolean value) {
258: myProperties.put(name, new Boolean(value).toString());
259: }
260:
261: /**
262: * Sets double property by name.
263: *
264: * @param name name of the property
265: * @param value value of the property
266: */
267: public static void setProperty(String name, double value) {
268: myProperties.put(name, new Double(value).toString());
269: }
270:
271: /**
272: * Gets property by name.
273: *
274: * @param name name of the property
275: * @param def default value to be returned if there is no such property
276: * @return value of the property or provided default value
277: */
278: public static String getProperty(String name, String def) {
279: String value = System.getProperty(name);
280: if (value != null)
281: return value;
282: return myProperties.getProperty(name, def);
283: }
284:
285: /**
286: * Gets property by name.
287: *
288: * @param name name of the property
289: * @return value of the property or null
290: */
291: public static String getProperty(String name) {
292: String value = System.getProperty(name);
293: if (value != null)
294: return value;
295: return myProperties.getProperty(name);
296: }
297:
298: /**
299: * Gets integer property by name.
300: *
301: * @param name name of the property
302: * @param def default value to be returned if there is no such property
303: * @return value of the property or provided default value
304: */
305: public static int getIntProperty(String property, int def) {
306: return Util.toInt(getProperty(property), def);
307: }
308:
309: /**
310: * Gets long property by name.
311: *
312: * @param name name of the property
313: * @param def default value to be returned if there is no such property
314: * @return value of the property or provided default value
315: */
316: public static long getLongProperty(String property, long def) {
317: return Util.toLong(getProperty(property), def);
318: }
319:
320: /**
321: * Gets boolean property by name.
322: *
323: * @param name name of the property
324: * @param def default value to be returned if there is no such property
325: * @return value of the property or provided default value
326: */
327: public static boolean getBoolProperty(String property, boolean def) {
328: return Util.toBool(getProperty(property), def);
329: }
330:
331: /**
332: * Gets double property by name.
333: *
334: * @param name name of the property
335: * @param def default value to be returned if there is no such property
336: * @return value of the property or provided default value
337: */
338: public static double getDoubleProperty(String property, double def) {
339: return Util.toDouble(getProperty(property), def);
340: }
341:
342: public static Object getObjectProperty(Object key) {
343: return myProperties.get(key);
344: }
345:
346: public static void setObjectProperty(Object key, Object value) {
347: myProperties.put(key, value);
348: }
349:
350: /**
351: * Returns request cache settings.
352: *
353: * @return request cache settings
354: */
355: public static CacheSettings getRequestCacheSettings() {
356: return RequestCache.getSettings();
357: }
358:
359: /**
360: * Returns font cache settings.
361: *
362: * @return font cache settings
363: */
364: public static CacheSettings getFontCacheSettings() {
365: return FontCache.getSettings();
366: }
367:
368: /**
369: * Returns media cache settings.
370: *
371: * @return media cache settings
372: */
373: public static CacheSettings getMediaCacheSettings() {
374: return MediaCache.getSettings();
375: }
376:
377: /**
378: * Returns xml cache settings.
379: *
380: * @return xml cache settings
381: */
382: public static CacheSettings getXMLCacheSettings() {
383: return XMLCache.getSettings();
384: }
385:
386: }
|