001: /*
002: Copyright (C) 2006 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.rules;
034:
035: import java.util.Properties;
036: import java.util.HashMap;
037: import java.util.Date;
038: import java.util.Iterator;
039: import java.util.Enumeration;
040: import java.text.ParseException;
041: import java.text.SimpleDateFormat;
042:
043: import java.math.BigDecimal;
044:
045: import java.io.File;
046: import java.io.FileInputStream;
047: import java.io.FileOutputStream;
048: import java.io.FileNotFoundException;
049: import java.io.IOException;
050:
051: import java.sql.SQLException;
052:
053: import com.knowgate.dataobjs.DBBind;
054: import com.knowgate.jdc.JDCConnection;
055: import com.knowgate.debug.DebugFile;
056:
057: /**
058: * <p>Rule Engine</p>
059: * @author Sergio Montoro Ten
060: * @version 1.0
061: */
062: public class RuleEngine {
063: private static HashMap oEngines = new HashMap(11);
064:
065: private HashMap oProps = new HashMap(11);
066: private HashMap oPaths = new HashMap(11);
067: private HashMap oDbbs = new HashMap(11);
068: private HashMap oAsrt = new HashMap(29);
069:
070: private String sDefaultProfileName;
071: private String sDefaultBindingName;
072:
073: private RuleEngine() {
074: sDefaultProfileName = null;
075: sDefaultBindingName = null;
076: }
077:
078: protected RuleEngine(String sPropertiesPath, String sProfile)
079: throws FileNotFoundException, IOException {
080: loadDefaultProperties(sProfile, sPropertiesPath);
081: setDefaultDataBaseBind(new DBBind(sProfile));
082: }
083:
084: protected RuleEngine(Properties oProperties, String sProfile)
085: throws FileNotFoundException, IOException {
086:
087: if (DebugFile.trace) {
088: DebugFile.writeln("new RuleEngine([oProperties], "
089: + sProfile + ")");
090: DebugFile.incIdent();
091: Enumeration oKeys = oProperties.keys();
092: while (oKeys.hasMoreElements()) {
093: String sKey = (String) oKeys.nextElement();
094: DebugFile.writeln(sKey + "="
095: + oProperties.getProperty(sKey));
096: } // wend
097: }
098:
099: setDefaultProperties(oProperties, sProfile);
100: setDefaultDataBaseBind(new DBBind(sProfile));
101:
102: if (DebugFile.trace)
103: DebugFile.decIdent();
104: }
105:
106: public static HashMap engines() {
107: return oEngines;
108: }
109:
110: public static boolean existsEngine(String sEngineName) {
111: return oEngines.containsKey(sEngineName);
112: }
113:
114: public static RuleEngine getEngine(String sEngineName,
115: String sPropertiesPath, String sProfileName)
116: throws FileNotFoundException, IOException {
117:
118: if (DebugFile.trace) {
119: DebugFile.writeln("Begin RuleEngine.getEngine("
120: + sEngineName + "," + sPropertiesPath + ","
121: + sProfileName + ")");
122: DebugFile.incIdent();
123: }
124:
125: RuleEngine oEngine;
126: if (existsEngine(sEngineName)) {
127: if (DebugFile.trace)
128: DebugFile.writeln("getting existing engine");
129: oEngine = (RuleEngine) oEngines.get(sEngineName);
130: } else {
131: if (DebugFile.trace)
132: DebugFile.writeln("instantiating new engine");
133: oEngine = new RuleEngine(sPropertiesPath, sProfileName);
134: oEngines.put(oEngine, sEngineName);
135: }
136:
137: if (DebugFile.trace) {
138: DebugFile.decIdent();
139: DebugFile.writeln("End RuleEngine.getEngine()");
140: }
141:
142: return oEngine;
143: } // getEngine
144:
145: public static void closeEngine(String sEngineName) {
146: if (DebugFile.trace) {
147: DebugFile.writeln("Begin RuleEngine.closeEngine("
148: + sEngineName + ")");
149: DebugFile.incIdent();
150: }
151:
152: if (existsEngine(sEngineName)) {
153: Iterator oKeys = oEngines.keySet().iterator();
154: while (oKeys.hasNext()) {
155: String sKey = oKeys.next().toString();
156: if (sKey.equals(sEngineName))
157: ((DBBind) oEngines.get(sKey)).close();
158: } // wend
159: } // fi
160:
161: if (DebugFile.trace) {
162: DebugFile.decIdent();
163: DebugFile.writeln("End RuleEngine.closeEngine()");
164: }
165: } // closeEngine
166:
167: public static void closeAll() {
168: Iterator oKeys = oEngines.keySet().iterator();
169: while (oKeys.hasNext()) {
170: ((DBBind) oEngines.get(oKeys.next().toString())).close();
171: } // wend
172: } // closeAll
173:
174: public Properties getProperties(String sSetName) {
175: return (Properties) oProps.get(sSetName);
176: }
177:
178: public Properties getDefaultProperties() {
179: return (Properties) oProps.get(sDefaultProfileName);
180: }
181:
182: public String getPropertyStr(String sSetName, String sKey) {
183: return getProperties(sSetName).getProperty(sKey);
184: }
185:
186: public String getPropertyStr(String sSetName, String sKey,
187: String sDefault) {
188: return getProperties(sSetName).getProperty(sKey, sDefault);
189: }
190:
191: public String getDefaultPropertyStr(String sKey, String sDefault) {
192: String sProp = getDefaultProperties().getProperty(sKey);
193: return sProp == null ? sDefault : sProp;
194: }
195:
196: public String getDefaultPropertyStr(String sKey) {
197: return getDefaultPropertyStr(sKey, null);
198: }
199:
200: public BigDecimal getPropertyDec(String sSetName, String sKey,
201: BigDecimal dDefault) throws NumberFormatException {
202: String sProp = getProperties(sSetName).getProperty(sKey);
203: if (null == sProp)
204: return dDefault;
205: else
206: return new BigDecimal(sProp);
207: }
208:
209: public BigDecimal getPropertyDec(String sSetName, String sKey)
210: throws NumberFormatException {
211: return getPropertyDec(sSetName, sKey, null);
212: }
213:
214: public BigDecimal getDefaultPropertyDec(String sKey)
215: throws NumberFormatException {
216: return getPropertyDec(sDefaultProfileName, sKey, null);
217: }
218:
219: public BigDecimal getDefaultPropertyDec(String sKey,
220: BigDecimal dDefault) throws NumberFormatException {
221: return getPropertyDec(sDefaultProfileName, sKey, dDefault);
222: }
223:
224: public Integer getPropertyInt(String sSetName, String sKey,
225: Integer iDefault) throws NumberFormatException {
226: String sProp = getProperties(sSetName).getProperty(sKey);
227: if (null == iDefault)
228: return null;
229: else
230: return new Integer(sProp);
231: }
232:
233: public Integer getPropertyInt(String sSetName, String sKey)
234: throws NumberFormatException {
235: return getPropertyInt(sSetName, sKey);
236: }
237:
238: public Integer getDefaultPropertyInt(String sKey)
239: throws NumberFormatException {
240: return getPropertyInt(sDefaultProfileName, sKey);
241: }
242:
243: public Integer getDefaultPropertyInt(String sKey, Integer iDefault)
244: throws NumberFormatException {
245: return getPropertyInt(sDefaultProfileName, sKey, iDefault);
246: }
247:
248: public Date getPropertyDate(String sSetName, String sKey,
249: String sFormat, Date dtDefault) throws ParseException {
250: String sProp = getProperties(sSetName).getProperty(sKey);
251: if (null == sProp)
252: return dtDefault;
253: else {
254: SimpleDateFormat oFmt = new SimpleDateFormat(sFormat);
255: return oFmt.parse(sProp);
256: }
257: }
258:
259: public Date getPropertyDate(String sSetName, String sKey,
260: String sFormat) throws ParseException {
261: return getPropertyDate(sSetName, sKey, null);
262: }
263:
264: public Date getDefaultPropertyDate(String sKey, String sFormat)
265: throws ParseException {
266: return getPropertyDate(sDefaultProfileName, sKey, null);
267: }
268:
269: public Date getDefaultPropertyDate(String sKey, String sFormat,
270: Date dtDefault) throws ParseException {
271: return getPropertyDate(sDefaultProfileName, sKey, sFormat,
272: dtDefault);
273: }
274:
275: public void setProperties(String sSetName, Properties oPSet) {
276: if (oProps.containsKey(sSetName))
277: oProps.remove(sSetName);
278: oProps.put(sSetName, oPSet);
279: }
280:
281: public void setDefaultProperties(Properties oProps) {
282: setProperties(sDefaultProfileName, oProps);
283: }
284:
285: public void setDefaultProperties(Properties oProps,
286: String sProfileName) {
287: sDefaultProfileName = sProfileName;
288: if (oProps.containsKey(sProfileName))
289: oProps.remove(sProfileName);
290: setProperties(sDefaultProfileName, oProps);
291: }
292:
293: public void loadProperties(String sSetName, String sPathFile)
294: throws FileNotFoundException, IOException {
295:
296: if (DebugFile.trace) {
297: DebugFile.writeln("Begin RuleEngine.loadProperties("
298: + sSetName + "," + sPathFile + ")");
299: DebugFile.incIdent();
300: }
301:
302: if (oProps.containsKey(sSetName))
303: oProps.remove(sSetName);
304: Properties oPSet = new Properties();
305: File oFile = new File(sPathFile);
306: FileInputStream oIoStrm = new FileInputStream(oFile);
307: oPSet.load(oIoStrm);
308: oIoStrm.close();
309:
310: if (DebugFile.trace) {
311: Enumeration oKeys = oPSet.keys();
312: while (oKeys.hasMoreElements()) {
313: String sKey = (String) oKeys.nextElement();
314: DebugFile.writeln(sKey + "=" + oPSet.getProperty(sKey));
315: } // wend
316: } // fi
317:
318: oProps.put(sSetName, oPSet);
319: oPaths.put(sSetName, oFile);
320:
321: if (DebugFile.trace) {
322: DebugFile.decIdent();
323: DebugFile.writeln("End RuleEngine.loadProperties()");
324: }
325: } // loadProperties
326:
327: public void loadDefaultProperties(String sSetName, String sPathFile)
328: throws FileNotFoundException, IOException {
329: sDefaultProfileName = sSetName;
330: loadProperties(sSetName, sPathFile);
331: }
332:
333: public void saveProperties(String sSetName) throws IOException {
334: FileOutputStream oIoStrm = new FileOutputStream((File) oPaths
335: .get(sSetName));
336: getProperties(sSetName).store(oIoStrm,
337: "# hipergate Rule Engine " + new Date().toString());
338: oIoStrm.close();
339: }
340:
341: public void saveDefaultProperties() throws IOException {
342: saveProperties(sDefaultProfileName);
343: }
344:
345: public void setDataBaseBind(DBBind oDbb) {
346: oDbbs.put(oDbb.getProfileName(), oDbb);
347: }
348:
349: public void setDefaultDataBaseBind(DBBind oDbb) {
350: sDefaultBindingName = oDbb.getProfileName();
351: setDataBaseBind(oDbb);
352: }
353:
354: public void setDefaultDataBaseBind(String sProfileName) {
355: sDefaultBindingName = sProfileName;
356: }
357:
358: public DBBind getDataBaseBind(String sProfileName) {
359: return (DBBind) oDbbs.get(sProfileName);
360: }
361:
362: public DBBind getDefaultDataBaseBind() {
363: return (DBBind) oDbbs.get(sDefaultBindingName);
364: }
365:
366: public JDCConnection getConnection(String sCaller)
367: throws SQLException {
368: JDCConnection oRetObj;
369: oRetObj = getDefaultDataBaseBind().getConnection(sCaller);
370: return oRetObj;
371: }
372:
373: public void setAssert(String sAssertKey, boolean bTrueOrFalse) {
374: if (oAsrt.containsKey(sAssertKey))
375: oAsrt.remove(sAssertKey);
376: oAsrt.put(sAssertKey, new Boolean(bTrueOrFalse));
377: }
378:
379: public boolean getAssert(String sAssertKey) {
380: Boolean bAssrt = (Boolean) oAsrt.get(sAssertKey);
381: if (bAssrt == null)
382: return false;
383: else
384: return bAssrt.booleanValue();
385: }
386: }
|