1 32 33 package com.knowgate.rules; 34 35 import java.util.Properties ; 36 import java.util.HashMap ; 37 import java.util.Date ; 38 import java.util.Iterator ; 39 import java.util.Enumeration ; 40 import java.text.ParseException ; 41 import java.text.SimpleDateFormat ; 42 43 import java.math.BigDecimal ; 44 45 import java.io.File ; 46 import java.io.FileInputStream ; 47 import java.io.FileOutputStream ; 48 import java.io.FileNotFoundException ; 49 import java.io.IOException ; 50 51 import java.sql.SQLException ; 52 53 import com.knowgate.dataobjs.DBBind; 54 import com.knowgate.jdc.JDCConnection; 55 import com.knowgate.debug.DebugFile; 56 57 62 public class RuleEngine { 63 private static HashMap oEngines = new HashMap (11); 64 65 private HashMap oProps = new HashMap (11); 66 private HashMap oPaths = new HashMap (11); 67 private HashMap oDbbs = new HashMap (11); 68 private HashMap oAsrt = new HashMap (29); 69 70 private String sDefaultProfileName; 71 private String sDefaultBindingName; 72 73 private RuleEngine() { 74 sDefaultProfileName = null; 75 sDefaultBindingName = null; 76 } 77 78 protected RuleEngine(String sPropertiesPath, String sProfile) 79 throws FileNotFoundException , IOException { 80 loadDefaultProperties(sProfile, sPropertiesPath); 81 setDefaultDataBaseBind(new DBBind(sProfile)); 82 } 83 84 protected RuleEngine(Properties oProperties, String sProfile) 85 throws FileNotFoundException , IOException { 86 87 if (DebugFile.trace) { 88 DebugFile.writeln("new RuleEngine([oProperties], "+sProfile+")"); 89 DebugFile.incIdent(); 90 Enumeration oKeys = oProperties.keys(); 91 while (oKeys.hasMoreElements()) { 92 String sKey = (String ) oKeys.nextElement(); 93 DebugFile.writeln(sKey+"="+oProperties.getProperty(sKey)); 94 } } 96 97 setDefaultProperties(oProperties, sProfile); 98 setDefaultDataBaseBind(new DBBind(sProfile)); 99 100 if (DebugFile.trace) DebugFile.decIdent(); 101 } 102 103 public static HashMap engines() { 104 return oEngines; 105 } 106 107 public static boolean existsEngine(String sEngineName) { 108 return oEngines.containsKey(sEngineName); 109 } 110 111 public static RuleEngine getEngine(String sEngineName, String sPropertiesPath, String sProfileName) 112 throws FileNotFoundException , IOException { 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 } 137 public static void closeEngine(String sEngineName) { 138 if (DebugFile.trace) { 139 DebugFile.writeln("Begin RuleEngine.closeEngine("+sEngineName+")"); 140 DebugFile.incIdent(); 141 } 142 143 if (existsEngine(sEngineName)) { 144 Iterator oKeys = oEngines.keySet().iterator(); 145 while (oKeys.hasNext()) { 146 String sKey = oKeys.next().toString(); 147 if (sKey.equals(sEngineName)) 148 ((DBBind) oEngines.get(sKey)).close(); 149 } } 152 if (DebugFile.trace) { 153 DebugFile.decIdent(); 154 DebugFile.writeln("End RuleEngine.closeEngine()"); 155 } 156 } 158 public static void closeAll() { 159 Iterator oKeys = oEngines.keySet().iterator(); 160 while (oKeys.hasNext()) { 161 ((DBBind) oEngines.get(oKeys.next().toString())).close(); 162 } } 165 public Properties getProperties(String sSetName) { 166 return (Properties ) oProps.get(sSetName); 167 } 168 169 public Properties getDefaultProperties() { 170 return (Properties ) oProps.get(sDefaultProfileName); 171 } 172 173 public String getPropertyStr(String sSetName, String sKey) { 174 return getProperties(sSetName).getProperty(sKey); 175 } 176 177 public String getPropertyStr(String sSetName, String sKey, String sDefault) { 178 return getProperties(sSetName).getProperty(sKey, sDefault); 179 } 180 181 public String getDefaultPropertyStr(String sKey, String sDefault) { 182 String sProp = getDefaultProperties().getProperty(sKey); 183 return sProp==null ? sDefault : sProp; 184 } 185 186 public String getDefaultPropertyStr(String sKey) { 187 return getDefaultPropertyStr(sKey,null); 188 } 189 190 public BigDecimal getPropertyDec(String sSetName, String sKey, BigDecimal dDefault) 191 throws NumberFormatException { 192 String sProp = getProperties(sSetName).getProperty(sKey); 193 if (null==sProp) 194 return dDefault; 195 else 196 return new BigDecimal (sProp); 197 } 198 199 public BigDecimal getPropertyDec(String sSetName, String sKey) 200 throws NumberFormatException { 201 return getPropertyDec(sSetName, sKey, null); 202 } 203 204 public BigDecimal getDefaultPropertyDec(String sKey) 205 throws NumberFormatException { 206 return getPropertyDec(sDefaultProfileName, sKey, null); 207 } 208 209 public BigDecimal getDefaultPropertyDec(String sKey, BigDecimal dDefault) 210 throws NumberFormatException { 211 return getPropertyDec(sDefaultProfileName, sKey, dDefault); 212 } 213 214 public Integer getPropertyInt(String sSetName, String sKey, Integer iDefault) 215 throws NumberFormatException { 216 String sProp = getProperties(sSetName).getProperty(sKey); 217 if (null==iDefault) 218 return null; 219 else 220 return new Integer (sProp); 221 } 222 223 public Integer getPropertyInt(String sSetName, String sKey) 224 throws NumberFormatException { 225 return getPropertyInt(sSetName, sKey); 226 } 227 228 public Integer getDefaultPropertyInt(String sKey) 229 throws NumberFormatException { 230 return getPropertyInt(sDefaultProfileName, sKey); 231 } 232 233 public Integer getDefaultPropertyInt(String sKey, Integer iDefault) 234 throws NumberFormatException { 235 return getPropertyInt(sDefaultProfileName, sKey, iDefault); 236 } 237 238 public Date getPropertyDate(String sSetName, String sKey, String sFormat, Date dtDefault) 239 throws ParseException { 240 String sProp = getProperties(sSetName).getProperty(sKey); 241 if (null==sProp) 242 return dtDefault; 243 else { 244 SimpleDateFormat oFmt = new SimpleDateFormat (sFormat); 245 return oFmt.parse(sProp); 246 } 247 } 248 249 public Date getPropertyDate(String sSetName, String sKey, String sFormat) 250 throws ParseException { 251 return getPropertyDate(sSetName, sKey, null); 252 } 253 254 public Date getDefaultPropertyDate(String sKey, String sFormat) 255 throws ParseException { 256 return getPropertyDate(sDefaultProfileName, sKey, null); 257 } 258 259 public Date getDefaultPropertyDate(String sKey, String sFormat, Date dtDefault) 260 throws ParseException { 261 return getPropertyDate(sDefaultProfileName, sKey, sFormat, dtDefault); 262 } 263 264 public void setProperties(String sSetName, Properties oPSet) { 265 if (oProps.containsKey(sSetName)) 266 oProps.remove(sSetName); 267 oProps.put(sSetName, oPSet); 268 } 269 270 public void setDefaultProperties(Properties oProps) { 271 setProperties(sDefaultProfileName, oProps); 272 } 273 274 public void setDefaultProperties(Properties oProps, String sProfileName) { 275 sDefaultProfileName = sProfileName; 276 if (oProps.containsKey(sProfileName)) oProps.remove(sProfileName); 277 setProperties(sDefaultProfileName, oProps); 278 } 279 280 public void loadProperties(String sSetName, String sPathFile) 281 throws FileNotFoundException , IOException { 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 oPSet = new Properties (); 291 File oFile = new File (sPathFile); 292 FileInputStream oIoStrm = new FileInputStream (oFile); 293 oPSet.load(oIoStrm); 294 oIoStrm.close(); 295 296 if (DebugFile.trace) { 297 Enumeration oKeys = oPSet.keys(); 298 while (oKeys.hasMoreElements()) { 299 String sKey = (String ) oKeys.nextElement(); 300 DebugFile.writeln(sKey+"="+oPSet.getProperty(sKey)); 301 } } 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 } 313 public void loadDefaultProperties(String sSetName, String sPathFile) 314 throws FileNotFoundException , IOException { 315 sDefaultProfileName = sSetName; 316 loadProperties(sSetName, sPathFile); 317 } 318 319 public void saveProperties(String sSetName) 320 throws IOException { 321 FileOutputStream oIoStrm = new FileOutputStream ((File )oPaths.get(sSetName)); 322 getProperties(sSetName).store(oIoStrm, "# hipergate Rule Engine "+new Date ().toString()); 323 oIoStrm.close(); 324 } 325 326 public void saveDefaultProperties() 327 throws IOException { 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 sProfileName) { 341 sDefaultBindingName = sProfileName; 342 } 343 344 public DBBind getDataBaseBind(String 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 sCaller) 353 throws SQLException { 354 JDCConnection oRetObj; 355 oRetObj = getDefaultDataBaseBind().getConnection(sCaller); 356 return oRetObj; 357 } 358 359 public void setAssert(String sAssertKey, boolean bTrueOrFalse) { 360 if (oAsrt.containsKey(sAssertKey)) 361 oAsrt.remove(sAssertKey); 362 oAsrt.put(sAssertKey, new Boolean (bTrueOrFalse)); 363 } 364 365 public boolean getAssert(String sAssertKey) { 366 Boolean bAssrt = (Boolean ) oAsrt.get(sAssertKey); 367 if (bAssrt==null) 368 return false; 369 else 370 return bAssrt.booleanValue(); 371 } 372 } 373 | Popular Tags |