[jboss-svn-commits] JBL Code SVN: r20446 - labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jun 12 09:26:55 EDT 2008


Author: beve
Date: 2008-06-12 09:26:55 -0400 (Thu, 12 Jun 2008)
New Revision: 20446

Modified:
   labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleService.java
Log:
Just cleaning up duplicate code.


Modified: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleService.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleService.java	2008-06-12 13:23:37 UTC (rev 20445)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleService.java	2008-06-12 13:26:55 UTC (rev 20446)
@@ -101,34 +101,26 @@
 	 * 
 	 * @return Message with updated objects.
 	 */
-	public Message executeStatelessRules(String ruleSet, String dsl,
-			boolean ruleReload, Message message, List<Object> objectList)
-			throws RuleServiceException
-
-	{
+	public Message executeStatelessRules(
+			final String ruleSet, 
+			final String dsl,
+			final boolean ruleReload, 
+			Message message, 
+			final List<Object> objectList) throws RuleServiceException {
+		
 		long startTime = System.nanoTime();
 
 		try {
-			RuleBase ruleBase = getRuleBaseForFileBasedRules(ruleSet, dsl,
-					ruleReload);
+			RuleBase ruleBase = getRuleBaseForFileBasedRules(ruleSet, dsl, ruleReload);
 			message = executeStatelessRules(ruleBase, message, objectList);
 
-			long procTime = System.nanoTime() - startTime;
-			if (rulesCounter != null) {
-				rulesCounter.update(procTime, ruleSet,
-						JBRulesCounter.RULES_SUCCEED);
-			}
+			updateJBRulesCounter( startTime, ruleSet, JBRulesCounter.RULES_SUCCEED );
 
 			return message;
 
 		} catch (RuleServiceException ree) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleSet,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not parse the rules. "
-					+ ree.getMessage(), ree);
+			updateJBRulesCounter( startTime, ruleSet, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not parse the rules. " + ree.getMessage(), ree);
 		}
 	}
 
@@ -147,32 +139,25 @@
 	 * 
 	 * @return Message with updated objects.
 	 */
-	public Message executeStatelessRulesFromDecisionTable(String decisionTable,
-			boolean ruleReload, Message message, List<Object> objectList)
-			throws RuleServiceException {
+	public Message executeStatelessRulesFromDecisionTable(
+			final String decisionTable,
+			final boolean ruleReload, 
+			Message message, 
+			final List<Object> objectList) throws RuleServiceException {
+		
 		long startTime = System.nanoTime();
 
 		try {
-			RuleBase ruleBase = getRuleBaseForDecisionTable(decisionTable,
-					ruleReload);
+			RuleBase ruleBase = getRuleBaseForDecisionTable(decisionTable, ruleReload);
 			message = executeStatelessRules(ruleBase, message, objectList);
 
-			long procTime = System.nanoTime() - startTime;
-			if (rulesCounter != null) {
-				rulesCounter.update(procTime, decisionTable,
-						JBRulesCounter.RULES_SUCCEED);
-			}
+			updateJBRulesCounter( startTime, decisionTable, JBRulesCounter.RULES_SUCCEED );
 
 			return message;
 
 		} catch (RuleServiceException ree) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, decisionTable,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not parse the rules. "
-					+ ree.getMessage(), ree);
+			updateJBRulesCounter( startTime, decisionTable, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not parse the rules. " + ree.getMessage(), ree);
 		}
 	}
 
@@ -190,55 +175,34 @@
 	 * @return Message with updated objects.
 	 */
 	public Message executeStatelessRulesFromRuleAgent(
-			String ruleAgentProperties, Message message, List<Object> objectList)
-			throws RuleServiceException {
+			final String ruleAgentProperties, 
+			Message message, 
+			final List<Object> objectList) throws RuleServiceException {
+		
 		long startTime = System.nanoTime();
 		try {
 			DroolsRuleBaseHelper rbHelper = new DroolsRuleBaseHelper();
-			RuleBase ruleBase = rbHelper
-					.loadRuleBaseFromRuleAgent(ruleAgentProperties);
-			final Map<String, RuleBase> ruleBases = lifecycleRuleBases
-					.getLifecycleResource();
+			RuleBase ruleBase = rbHelper .loadRuleBaseFromRuleAgent(ruleAgentProperties);
+			final Map<String, RuleBase> ruleBases = lifecycleRuleBases.getLifecycleResource();
 			if (ruleBase != null) {
 				ruleBases.put(ruleAgentProperties, ruleBase);
 
 				message = executeStatelessRules(ruleBase, message, objectList);
 
-				long procTime = System.nanoTime() - startTime;
-				if (rulesCounter != null) {
-					rulesCounter.update(procTime, ruleAgentProperties,
-							JBRulesCounter.RULES_SUCCEED);
-				}
+    			updateJBRulesCounter( startTime, ruleAgentProperties, JBRulesCounter.RULES_SUCCEED );
 			}
 
 			return message;
 
 		} catch (IOException ioe) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleAgentProperties,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException(
-					"Could not read the ruleAgentProperties. "
-							+ ioe.getMessage(), ioe);
+			updateJBRulesCounter( startTime, ruleAgentProperties, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException( "Could not read the ruleAgentProperties. " + ioe.getMessage(), ioe);
 		} catch (RuleServiceException ree) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleAgentProperties,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not parse the rules. "
-					+ ree.getMessage(), ree);
+			updateJBRulesCounter( startTime, ruleAgentProperties, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not parse the rules. " + ree.getMessage(), ree);
 		} catch (Exception e) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleAgentProperties,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException(
-					"RuleAgent could not get the RuleBase. " + e.getMessage(),
-					e);
+			updateJBRulesCounter( startTime, ruleAgentProperties, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException( "RuleAgent could not get the RuleBase. " + e.getMessage(), e);
 		}
 
 	}
@@ -263,33 +227,27 @@
 	 * 
 	 * @return Message with updated objects.
 	 */
-	public Message executeStatefulRules(String ruleSet, String dsl,
-			boolean ruleReload, boolean dispose, Message message,
-			List<Object> objectList) throws RuleServiceException {
+	public Message executeStatefulRules(
+			final String ruleSet, 
+			final String dsl,
+			final boolean ruleReload, 
+			final boolean dispose, 
+			Message message,
+			final List<Object> objectList) throws RuleServiceException {
+		
 		long startTime = System.nanoTime();
 
 		try {
-			RuleBase ruleBase = getRuleBaseForFileBasedRules(ruleSet, dsl,
-					ruleReload);
-			message = executeStatefulRules(ruleBase, dispose, message,
-					objectList);
+			RuleBase ruleBase = getRuleBaseForFileBasedRules(ruleSet, dsl, ruleReload);
+			message = executeStatefulRules(ruleBase, dispose, message, objectList);
 
-			long procTime = System.nanoTime() - startTime;
-			if (rulesCounter != null) {
-				rulesCounter.update(procTime, ruleSet,
-						JBRulesCounter.RULES_SUCCEED);
-			}
+			updateJBRulesCounter( startTime, ruleSet, JBRulesCounter.RULES_SUCCEED);
 
 			return message;
 
 		} catch (RuleServiceException ree) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleSet,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not parse the rules. "
-					+ ree.getMessage(), ree);
+			updateJBRulesCounter( startTime, ruleSet, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not parse the rules. " + ree.getMessage(), ree);
 		}
 	}
 
@@ -313,28 +271,20 @@
 	public Message executeStatefulRulesFromDecisionTable(String decisionTable,
 			boolean ruleReload, boolean dispose, Message message,
 			List<Object> objectList) throws RuleServiceException {
+		
 		long startTime = System.nanoTime();
 
 		try {
 			RuleBase ruleBase = getRuleBaseForDecisionTable(decisionTable, ruleReload);
 			message = executeStatefulRules(ruleBase, dispose, message, objectList);
 
-			long procTime = System.nanoTime() - startTime;
-			if (rulesCounter != null) {
-				rulesCounter.update(procTime, decisionTable,
-						JBRulesCounter.RULES_SUCCEED);
-			}
+			updateJBRulesCounter( startTime, decisionTable, JBRulesCounter.RULES_SUCCEED );
 
 			return message;
 
 		} catch (RuleServiceException ree) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, decisionTable,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not parse the rules. "
-					+ ree.getMessage(), ree);
+			updateJBRulesCounter( startTime, decisionTable, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not parse the rules. " + ree.getMessage(), ree);
 		}
 	}
 
@@ -356,55 +306,32 @@
 	public Message executeStatefulRulesFromRuleAgent(
 			String ruleAgentProperties, boolean dispose, Message message,
 			List<Object> objectList) throws RuleServiceException {
+		
 		long startTime = System.nanoTime();
 		try {
 			DroolsRuleBaseHelper rbHelper = new DroolsRuleBaseHelper();
-			RuleBase ruleBase = rbHelper
-					.loadRuleBaseFromRuleAgent(ruleAgentProperties);
+			RuleBase ruleBase = rbHelper.loadRuleBaseFromRuleAgent(ruleAgentProperties);
 
-			final Map<String, RuleBase> ruleBases = lifecycleRuleBases
-					.getLifecycleResource();
+			final Map<String, RuleBase> ruleBases = lifecycleRuleBases.getLifecycleResource();
 			if (ruleBase != null) {
 				ruleBases.put(ruleAgentProperties, ruleBase);
 
-				message = executeStatefulRules(ruleBase, dispose, message,
-						objectList);
+				message = executeStatefulRules(ruleBase, dispose, message, objectList);
 
-				long procTime = System.nanoTime() - startTime;
-				if (rulesCounter != null) {
-					rulesCounter.update(procTime, ruleAgentProperties,
-							JBRulesCounter.RULES_SUCCEED);
-				}
+    			updateJBRulesCounter( startTime, ruleAgentProperties, JBRulesCounter.RULES_SUCCEED );
 			}
 
 			return message;
 
 		} catch (IOException ioe) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleAgentProperties,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException(
-					"Could not read the ruleAgentProperties. "
-							+ ioe.getMessage(), ioe);
+			updateJBRulesCounter( startTime, ruleAgentProperties, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException( "Could not read the ruleAgentProperties. " + ioe.getMessage(), ioe);
 		} catch (RuleServiceException ree) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleAgentProperties,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not parse the rules. "
-					+ ree.getMessage(), ree);
+			updateJBRulesCounter( startTime, ruleAgentProperties, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not parse the rules. " + ree.getMessage(), ree);
 		} catch (Exception e) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleAgentProperties,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException(
-					"RuleAgent could not get the RuleBase. " + e.getMessage(),
-					e);
+			updateJBRulesCounter( startTime, ruleAgentProperties, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException( "RuleAgent could not get the RuleBase. " + e.getMessage(), e);
 		}
 
 	}
@@ -424,21 +351,21 @@
 	 * 
 	 * @return Message with updated objects.
 	 */
-	public Message continueStatefulRulesExecution(String rules,
-			boolean dispose, Message message, List<Object> objectList)
-			throws RuleServiceException {
+	public Message continueStatefulRulesExecution(
+			final String rules,
+			final boolean dispose, 
+			Message message, 
+			final List<Object> objectList) throws RuleServiceException {
+		
 		long startTime = System.nanoTime();
 		try {
-			final Map<String, RuleBase> ruleBases = lifecycleRuleBases
-					.getLifecycleResource();
+			final Map<String, RuleBase> ruleBases = lifecycleRuleBases.getLifecycleResource();
 
 			RuleBase ruleBase = ruleBases.get(rules);
 			StatefulSession[] workingMemories = ruleBase.getStatefulSessions();
 			// there should only be one created from this ruleBase?
 			StatefulSession workingMemory = workingMemories[0];
-			logger
-					.log(Level.DEBUG, "Executing rules with RuleBase="
-							+ ruleBase);
+			logger.log(Level.DEBUG, "Executing rules with RuleBase=" + ruleBase);
 
 			workingMemory.setGlobal(MESSAGE, message);
 
@@ -458,24 +385,17 @@
 				workingMemory.dispose();
 			}
 
-			long procTime = System.nanoTime() - startTime;
-			if (rulesCounter != null) {
-				rulesCounter.update(procTime, rules,
-						JBRulesCounter.RULES_SUCCEED);
-			}
+			updateJBRulesCounter( startTime, rules, JBRulesCounter.RULES_SUCCEED );
 
 			return message;
 
 		} catch (Exception e) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, rules,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException(
-					"Could not continue rule execution. " + e.getMessage(), e);
+			updateJBRulesCounter( startTime, rules, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException( "Could not continue rule execution. " + e.getMessage(), e);
 		}
 	}
+	
+	
 
 	/**
 	 * Determine if file based rules need reloading and return the rulebase
@@ -491,8 +411,11 @@
 	 * 
 	 * @return Message with updated objects.
 	 */
-	protected RuleBase getRuleBaseForFileBasedRules(String ruleSet, String dsl,
-			boolean ruleReload) throws RuleServiceException {
+	protected RuleBase getRuleBaseForFileBasedRules(
+			final String ruleSet, 
+			final String dsl,
+			final boolean ruleReload) throws RuleServiceException {
+		
 		long startTime = System.nanoTime();
 		try {
 			DroolsRuleBaseHelper rbHelper = new DroolsRuleBaseHelper();
@@ -500,19 +423,16 @@
 			String newRuleSet = null;
 			boolean isRulesChanged = false;
 
-			final Map<String, String> ruleSets = lifecycleRuleSets
-					.getLifecycleResource();
+			final Map<String, String> ruleSets = lifecycleRuleSets.getLifecycleResource();
 			if (ruleReload) {
 				String currentRuleSet = ruleSets.get(ruleSet);
 				newRuleSet = rbHelper.getRulesAsString(ruleSet, dsl);
-				if (currentRuleSet == null
-						|| !currentRuleSet.equals(newRuleSet)) {
+				if (currentRuleSet == null || !currentRuleSet.equals(newRuleSet)) {
 					isRulesChanged = true;
 				}
 
 			}
-			final Map<String, RuleBase> ruleBases = lifecycleRuleBases
-					.getLifecycleResource();
+			final Map<String, RuleBase> ruleBases = lifecycleRuleBases.getLifecycleResource();
 			RuleBase ruleBase = ruleBases.get(ruleSet);
 			if (ruleBase == null || isRulesChanged) {
 				logger.log(Level.DEBUG, "Reading ruleSet from file=" + ruleSet);
@@ -535,37 +455,17 @@
 			return ruleBase;
 
 		} catch (final LifecycleResourceException lre) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleSet,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not load lifecycle data. "
-					+ lre.getMessage(), lre);
+			updateJBRulesCounter( startTime, ruleSet, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not load lifecycle data. " + lre.getMessage(), lre);
 		} catch (IOException ioe) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleSet,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not read the rules. "
-					+ ioe.getMessage(), ioe);
+			updateJBRulesCounter( startTime, ruleSet, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not read the rules. " + ioe.getMessage(), ioe);
 		} catch (DroolsParserException dpe) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleSet,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not parse the rules. "
-					+ dpe.getMessage(), dpe);
+			updateJBRulesCounter( startTime, ruleSet, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not parse the rules. " + dpe.getMessage(), dpe);
 		} catch (RuleServiceException ree) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, ruleSet,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not parse the rules. "
-					+ ree.getMessage(), ree);
+			updateJBRulesCounter( startTime, ruleSet, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not parse the rules. " + ree.getMessage(), ree);
 		}
 
 	}
@@ -584,8 +484,10 @@
 	 * 
 	 * @return Message with updated objects.
 	 */
-	protected RuleBase getRuleBaseForDecisionTable(String decisionTable,
-			boolean ruleReload) throws RuleServiceException {
+	protected RuleBase getRuleBaseForDecisionTable(
+			final String decisionTable,
+			final boolean ruleReload) throws RuleServiceException {
+		
 		long startTime = System.nanoTime();
 		try {
 			DroolsRuleBaseHelper rbHelper = new DroolsRuleBaseHelper();
@@ -594,8 +496,7 @@
 				String drl = rbHelper.getSpreadsheetRules(decisionTable);
 				ruleBase = getRuleBaseForFileBasedRules(drl, null, ruleReload);
 			} else {
-				ruleBase = rbHelper
-						.createRuleBaseFromDecisionTable(decisionTable);
+				ruleBase = rbHelper.createRuleBaseFromDecisionTable(decisionTable);
 			}
 			// this should really be a RULES_COMPILED status, and processing time should be compilationTime
 			// long procTime = System.nanoTime() - startTime;
@@ -605,29 +506,14 @@
 			return ruleBase;
 
 		} catch (IOException ioe) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, decisionTable,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not read the rules. "
-					+ ioe.getMessage(), ioe);
+			updateJBRulesCounter( startTime, decisionTable, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not read the rules. " + ioe.getMessage(), ioe);
 		} catch (DroolsParserException dpe) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, decisionTable,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not parse the rules. "
-					+ dpe.getMessage(), dpe);
+			updateJBRulesCounter( startTime, decisionTable, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not parse the rules. " + dpe.getMessage(), dpe);
 		} catch (RuleServiceException ree) {
-			if (rulesCounter != null) {
-				long procTime = System.nanoTime() - startTime;
-				rulesCounter.update(procTime, decisionTable,
-						JBRulesCounter.RULES_FAILED);
-			}
-			throw new RuleServiceException("Could not parse the rules. "
-					+ ree.getMessage(), ree);
+			updateJBRulesCounter( startTime, decisionTable, JBRulesCounter.RULES_FAILED );
+			throw new RuleServiceException("Could not parse the rules. " + ree.getMessage(), ree);
 		}
 	}
 
@@ -825,4 +711,12 @@
 			}
 		}
 	}
+	
+	private void updateJBRulesCounter( final long startTime , final String rules, final String result  )
+	{
+		if (rulesCounter != null) {
+			long procTime = System.nanoTime() - startTime;
+			rulesCounter.update(procTime, rules, result );
+		}
+	}
 }




More information about the jboss-svn-commits mailing list