KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > rules > RuleEngine


1 /*
2   Copyright (C) 2006 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.rules;
34
35 import java.util.Properties JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Date JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Enumeration JavaDoc;
40 import java.text.ParseException JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42
43 import java.math.BigDecimal JavaDoc;
44
45 import java.io.File JavaDoc;
46 import java.io.FileInputStream JavaDoc;
47 import java.io.FileOutputStream JavaDoc;
48 import java.io.FileNotFoundException JavaDoc;
49 import java.io.IOException JavaDoc;
50
51 import java.sql.SQLException JavaDoc;
52
53 import com.knowgate.dataobjs.DBBind;
54 import com.knowgate.jdc.JDCConnection;
55 import com.knowgate.debug.DebugFile;
56
57 /**
58  * <p>Rule Engine</p>
59  * @author Sergio Montoro Ten
60  * @version 1.0
61  */

62 public class RuleEngine {
63   private static HashMap JavaDoc oEngines = new HashMap JavaDoc(11);
64
65   private HashMap JavaDoc oProps = new HashMap JavaDoc(11);
66   private HashMap JavaDoc oPaths = new HashMap JavaDoc(11);
67   private HashMap JavaDoc oDbbs = new HashMap JavaDoc(11);
68   private HashMap JavaDoc oAsrt = new HashMap JavaDoc(29);
69
70   private String JavaDoc sDefaultProfileName;
71   private String JavaDoc sDefaultBindingName;
72
73   private RuleEngine() {
74     sDefaultProfileName = null;
75     sDefaultBindingName = null;
76   }
77
78   protected RuleEngine(String JavaDoc sPropertiesPath, String JavaDoc sProfile)
79     throws FileNotFoundException JavaDoc, IOException JavaDoc {
80     loadDefaultProperties(sProfile, sPropertiesPath);
81     setDefaultDataBaseBind(new DBBind(sProfile));
82   }
83
84   protected RuleEngine(Properties JavaDoc oProperties, String JavaDoc sProfile)
85     throws FileNotFoundException JavaDoc, IOException JavaDoc {
86
87     if (DebugFile.trace) {
88       DebugFile.writeln("new RuleEngine([oProperties], "+sProfile+")");
89       DebugFile.incIdent();
90       Enumeration JavaDoc oKeys = oProperties.keys();
91       while (oKeys.hasMoreElements()) {
92         String JavaDoc sKey = (String JavaDoc) oKeys.nextElement();
93         DebugFile.writeln(sKey+"="+oProperties.getProperty(sKey));
94       } // wend
95
}
96
97     setDefaultProperties(oProperties, sProfile);
98     setDefaultDataBaseBind(new DBBind(sProfile));
99
100     if (DebugFile.trace) DebugFile.decIdent();
101   }
102
103   public static HashMap JavaDoc engines() {
104     return oEngines;
105   }
106
107   public static boolean existsEngine(String JavaDoc sEngineName) {
108     return oEngines.containsKey(sEngineName);
109   }
110
111   public static RuleEngine getEngine(String JavaDoc sEngineName, String JavaDoc sPropertiesPath, String JavaDoc sProfileName)
112     throws FileNotFoundException JavaDoc, IOException JavaDoc {
113
114   if (DebugFile.trace) {
115     DebugFile.writeln("Begin RuleEngine.getEngine("+sEngineName+","+sPropertiesPath+","+sProfileName+")");
116     DebugFile.incIdent();
117   }
118
119     RuleEngine oEngine;
120     if (existsEngine(sEngineName)) {
121       if (DebugFile.trace) DebugFile.writeln("getting existing engine");
122       oEngine = (RuleEngine) oEngines.get(sEngineName);
123     } else {
124       if (DebugFile.trace) DebugFile.writeln("instantiating new engine");
125       oEngine = new RuleEngine(sPropertiesPath, sProfileName);
126       oEngines.put(oEngine, sEngineName);
127     }
128
129     if (DebugFile.trace) {
130       DebugFile.decIdent();
131       DebugFile.writeln("End RuleEngine.getEngine()");
132     }
133
134     return oEngine;
135   } // getEngine
136

137   public static void closeEngine(String JavaDoc sEngineName) {
138     if (DebugFile.trace) {
139       DebugFile.writeln("Begin RuleEngine.closeEngine("+sEngineName+")");
140       DebugFile.incIdent();
141     }
142
143     if (existsEngine(sEngineName)) {
144       Iterator JavaDoc oKeys = oEngines.keySet().iterator();
145       while (oKeys.hasNext()) {
146         String JavaDoc sKey = oKeys.next().toString();
147         if (sKey.equals(sEngineName))
148           ((DBBind) oEngines.get(sKey)).close();
149       } // wend
150
} // fi
151

152     if (DebugFile.trace) {
153       DebugFile.decIdent();
154       DebugFile.writeln("End RuleEngine.closeEngine()");
155     }
156   } // closeEngine
157

158   public static void closeAll() {
159       Iterator JavaDoc oKeys = oEngines.keySet().iterator();
160       while (oKeys.hasNext()) {
161         ((DBBind) oEngines.get(oKeys.next().toString())).close();
162       } // wend
163
} // closeAll
164

165   public Properties JavaDoc getProperties(String JavaDoc sSetName) {
166     return (Properties JavaDoc) oProps.get(sSetName);
167   }
168
169   public Properties JavaDoc getDefaultProperties() {
170     return (Properties JavaDoc) oProps.get(sDefaultProfileName);
171   }
172
173   public String JavaDoc getPropertyStr(String JavaDoc sSetName, String JavaDoc sKey) {
174     return getProperties(sSetName).getProperty(sKey);
175   }
176
177   public String JavaDoc getPropertyStr(String JavaDoc sSetName, String JavaDoc sKey, String JavaDoc sDefault) {
178     return getProperties(sSetName).getProperty(sKey, sDefault);
179   }
180
181   public String JavaDoc getDefaultPropertyStr(String JavaDoc sKey, String JavaDoc sDefault) {
182     String JavaDoc sProp = getDefaultProperties().getProperty(sKey);
183     return sProp==null ? sDefault : sProp;
184   }
185
186   public String JavaDoc getDefaultPropertyStr(String JavaDoc sKey) {
187     return getDefaultPropertyStr(sKey,null);
188   }
189
190   public BigDecimal JavaDoc getPropertyDec(String JavaDoc sSetName, String JavaDoc sKey, BigDecimal JavaDoc dDefault)
191     throws NumberFormatException JavaDoc {
192     String JavaDoc sProp = getProperties(sSetName).getProperty(sKey);
193     if (null==sProp)
194       return dDefault;
195     else
196       return new BigDecimal JavaDoc(sProp);
197   }
198
199   public BigDecimal JavaDoc getPropertyDec(String JavaDoc sSetName, String JavaDoc sKey)
200       throws NumberFormatException JavaDoc {
201     return getPropertyDec(sSetName, sKey, null);
202   }
203
204   public BigDecimal JavaDoc getDefaultPropertyDec(String JavaDoc sKey)
205       throws NumberFormatException JavaDoc {
206     return getPropertyDec(sDefaultProfileName, sKey, null);
207   }
208
209   public BigDecimal JavaDoc getDefaultPropertyDec(String JavaDoc sKey, BigDecimal JavaDoc dDefault)
210       throws NumberFormatException JavaDoc {
211     return getPropertyDec(sDefaultProfileName, sKey, dDefault);
212   }
213
214   public Integer JavaDoc getPropertyInt(String JavaDoc sSetName, String JavaDoc sKey, Integer JavaDoc iDefault)
215     throws NumberFormatException JavaDoc {
216     String JavaDoc sProp = getProperties(sSetName).getProperty(sKey);
217     if (null==iDefault)
218       return null;
219     else
220       return new Integer JavaDoc(sProp);
221   }
222
223   public Integer JavaDoc getPropertyInt(String JavaDoc sSetName, String JavaDoc sKey)
224     throws NumberFormatException JavaDoc {
225     return getPropertyInt(sSetName, sKey);
226   }
227
228   public Integer JavaDoc getDefaultPropertyInt(String JavaDoc sKey)
229     throws NumberFormatException JavaDoc {
230     return getPropertyInt(sDefaultProfileName, sKey);
231   }
232
233   public Integer JavaDoc getDefaultPropertyInt(String JavaDoc sKey, Integer JavaDoc iDefault)
234     throws NumberFormatException JavaDoc {
235     return getPropertyInt(sDefaultProfileName, sKey, iDefault);
236   }
237
238   public Date JavaDoc getPropertyDate(String JavaDoc sSetName, String JavaDoc sKey, String JavaDoc sFormat, Date JavaDoc dtDefault)
239     throws ParseException JavaDoc {
240     String JavaDoc sProp = getProperties(sSetName).getProperty(sKey);
241     if (null==sProp)
242       return dtDefault;
243     else {
244       SimpleDateFormat JavaDoc oFmt = new SimpleDateFormat JavaDoc(sFormat);
245       return oFmt.parse(sProp);
246     }
247   }
248
249   public Date JavaDoc getPropertyDate(String JavaDoc sSetName, String JavaDoc sKey, String JavaDoc sFormat)
250     throws ParseException JavaDoc {
251     return getPropertyDate(sSetName, sKey, null);
252   }
253
254   public Date JavaDoc getDefaultPropertyDate(String JavaDoc sKey, String JavaDoc sFormat)
255     throws ParseException JavaDoc {
256     return getPropertyDate(sDefaultProfileName, sKey, null);
257   }
258
259   public Date JavaDoc getDefaultPropertyDate(String JavaDoc sKey, String JavaDoc sFormat, Date JavaDoc dtDefault)
260     throws ParseException JavaDoc {
261     return getPropertyDate(sDefaultProfileName, sKey, sFormat, dtDefault);
262   }
263
264   public void setProperties(String JavaDoc sSetName, Properties JavaDoc oPSet) {
265     if (oProps.containsKey(sSetName))
266       oProps.remove(sSetName);
267     oProps.put(sSetName, oPSet);
268   }
269
270   public void setDefaultProperties(Properties JavaDoc oProps) {
271     setProperties(sDefaultProfileName, oProps);
272   }
273
274   public void setDefaultProperties(Properties JavaDoc oProps, String JavaDoc sProfileName) {
275     sDefaultProfileName = sProfileName;
276     if (oProps.containsKey(sProfileName)) oProps.remove(sProfileName);
277     setProperties(sDefaultProfileName, oProps);
278   }
279
280   public void loadProperties(String JavaDoc sSetName, String JavaDoc sPathFile)
281     throws FileNotFoundException JavaDoc, IOException JavaDoc {
282
283     if (DebugFile.trace) {
284       DebugFile.writeln("Begin RuleEngine.loadProperties("+sSetName+","+sPathFile+")");
285       DebugFile.incIdent();
286     }
287
288     if (oProps.containsKey(sSetName))
289       oProps.remove(sSetName);
290     Properties JavaDoc oPSet = new Properties JavaDoc();
291     File JavaDoc oFile = new File JavaDoc(sPathFile);
292     FileInputStream JavaDoc oIoStrm = new FileInputStream JavaDoc(oFile);
293     oPSet.load(oIoStrm);
294     oIoStrm.close();
295
296     if (DebugFile.trace) {
297       Enumeration JavaDoc oKeys = oPSet.keys();
298       while (oKeys.hasMoreElements()) {
299         String JavaDoc sKey = (String JavaDoc) oKeys.nextElement();
300         DebugFile.writeln(sKey+"="+oPSet.getProperty(sKey));
301       } // wend
302
} // fi
303

304     oProps.put(sSetName, oPSet);
305     oPaths.put(sSetName, oFile);
306
307     if (DebugFile.trace) {
308       DebugFile.decIdent();
309       DebugFile.writeln("End RuleEngine.loadProperties()");
310     }
311   } // loadProperties
312

313   public void loadDefaultProperties(String JavaDoc sSetName, String JavaDoc sPathFile)
314     throws FileNotFoundException JavaDoc, IOException JavaDoc {
315     sDefaultProfileName = sSetName;
316     loadProperties(sSetName, sPathFile);
317   }
318
319   public void saveProperties(String JavaDoc sSetName)
320     throws IOException JavaDoc {
321     FileOutputStream JavaDoc oIoStrm = new FileOutputStream JavaDoc((File JavaDoc)oPaths.get(sSetName));
322     getProperties(sSetName).store(oIoStrm, "# hipergate Rule Engine "+new Date JavaDoc().toString());
323     oIoStrm.close();
324   }
325
326   public void saveDefaultProperties()
327     throws IOException JavaDoc {
328     saveProperties(sDefaultProfileName);
329   }
330
331   public void setDataBaseBind(DBBind oDbb) {
332     oDbbs.put(oDbb.getProfileName(), oDbb);
333   }
334
335   public void setDefaultDataBaseBind(DBBind oDbb) {
336     sDefaultBindingName = oDbb.getProfileName();
337     setDataBaseBind(oDbb);
338   }
339
340   public void setDefaultDataBaseBind(String JavaDoc sProfileName) {
341     sDefaultBindingName = sProfileName;
342   }
343
344   public DBBind getDataBaseBind(String JavaDoc sProfileName) {
345     return (DBBind) oDbbs.get(sProfileName);
346   }
347
348   public DBBind getDefaultDataBaseBind() {
349     return (DBBind) oDbbs.get(sDefaultBindingName);
350   }
351
352   public JDCConnection getConnection(String JavaDoc sCaller)
353     throws SQLException JavaDoc {
354     JDCConnection oRetObj;
355     oRetObj = getDefaultDataBaseBind().getConnection(sCaller);
356     return oRetObj;
357   }
358
359   public void setAssert(String JavaDoc sAssertKey, boolean bTrueOrFalse) {
360     if (oAsrt.containsKey(sAssertKey))
361       oAsrt.remove(sAssertKey);
362     oAsrt.put(sAssertKey, new Boolean JavaDoc(bTrueOrFalse));
363   }
364
365   public boolean getAssert(String JavaDoc sAssertKey) {
366     Boolean JavaDoc bAssrt = (Boolean JavaDoc) oAsrt.get(sAssertKey);
367     if (bAssrt==null)
368       return false;
369     else
370       return bAssrt.booleanValue();
371   }
372 }
373
Popular Tags