[jboss-svn-commits] JBL Code SVN: r12726 - in labs/jbossrules/trunk/drools-core/src: test/java/org/drools/agent and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Jun 20 23:52:07 EDT 2007
Author: michael.neale at jboss.com
Date: 2007-06-20 23:52:07 -0400 (Wed, 20 Jun 2007)
New Revision: 12726
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/PackageProvider.java
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/DirectoryScanner.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/FileScannerTest.java
Log:
JBRULES-752 Rule agent
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/DirectoryScanner.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/DirectoryScanner.java 2007-06-21 02:51:28 UTC (rev 12725)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/DirectoryScanner.java 2007-06-21 03:52:07 UTC (rev 12726)
@@ -1,5 +1,24 @@
package org.drools.agent;
-public class DirectoryScanner {
+import java.util.List;
+import org.drools.RuleBase;
+
+public class DirectoryScanner extends PackageProvider {
+
+ private List dir;
+
+ void configure(List configList) {
+ this.dir = configList;
+ //now check to see whats in them dir...
+ if (configList.size() > 1) {
+ throw new IllegalArgumentException("You can only monitor one directory at a time this way.");
+ }
+
+ }
+
+ void updateRuleBase(RuleBase rb, boolean removeExistingPackages) {
+
+ }
+
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java 2007-06-21 02:51:28 UTC (rev 12725)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java 2007-06-21 03:52:07 UTC (rev 12726)
@@ -20,7 +20,7 @@
* @author Michael Neale
*
*/
-public class FileScanner {
+public class FileScanner extends PackageProvider {
private File[] files;
private Map lastUpdated = new HashMap();
@@ -33,10 +33,10 @@
* This sets the list of files to be monitored.
* This takes a list of paths and files (not directories).
*/
- public void setFiles(String[] paths) {
- files = new File[paths.length];
- for ( int i = 0; i < paths.length; i++ ) {
- File file = new File( paths[i] );
+ void configure(List paths) {
+ files = new File[paths.size()];
+ for ( int i = 0; i < paths.size(); i++ ) {
+ File file = new File( (String) paths.get( i ) );
if ( !file.exists() ) {
throw new IllegalArgumentException( "The file " + file.getName() + " does not exist." );
}
@@ -152,4 +152,6 @@
return buf.toString();
}
+
+
}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/PackageProvider.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/PackageProvider.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/PackageProvider.java 2007-06-21 03:52:07 UTC (rev 12726)
@@ -0,0 +1,30 @@
+package org.drools.agent;
+
+import java.util.List;
+
+import org.drools.RuleBase;
+
+/**
+ * All sources of packages must implement this.
+ * @author Michael Neale
+ *
+ */
+public abstract class PackageProvider {
+
+ /**
+ * Perform the scan, adding in any packages changed to the rulebase.
+ * If there was an error reading the packages, this will not fail, it will
+ * just do nothing (as there may be a temporary IO issue).
+ */
+ abstract void updateRuleBase(RuleBase rb, boolean removeExistingPackages);
+
+
+ /**
+ * This will be passed a list of configuration strings which were extracted from the config property.
+ */
+ abstract void configure(List configList);
+
+
+
+
+}
Property changes on: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/PackageProvider.java
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java 2007-06-21 02:51:28 UTC (rev 12725)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleBaseAgent.java 2007-06-21 03:52:07 UTC (rev 12726)
@@ -5,7 +5,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Timer;
@@ -13,6 +16,7 @@
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
+import org.drools.RuntimeDroolsException;
import org.drools.util.BinaryRuleBaseLoader;
/**
@@ -37,61 +41,104 @@
* Following are property keys to be used in the property
* config file.
*/
- public static final String NEW_INSTANCE = "newInstance";
- public static final String FILES = "file";
+ public static final String NEW_INSTANCE = "newInstance";
+ public static final String FILES = "file";
//public static final String DIRECTORIES = "dir";
//public static final String URIS = "uri";
- public static final String POLL_INTERVAL = "poll";
+ public static final String POLL_INTERVAL = "poll";
/**
+ * Here is where we have a map of providers to the key that appears on the configuration.
+ */
+ public static Map PACKAGE_PROVIDERS = new HashMap() {
+ {
+ put( FILES, FileScanner.class );
+ }
+ };
+
+ /**
* This is true if the rulebase is created anew each time.
*/
private boolean newInstance;
-
+
/**
* The rule base that is being managed.
*/
private RuleBase ruleBase;
- private Timer timer;
- public RuleBaseAgent(Properties config) {
+ /**
+ * The timer that is used to monitor for changes and deal with them.
+ */
+ private Timer timer;
+
+ public RuleBaseAgent(
+ Properties config) {
boolean newInstance = Boolean.parseBoolean( config.getProperty( NEW_INSTANCE, "false" ) );
- int secondsToRefresh = Integer.parseInt( config.getProperty( POLL_INTERVAL , "-1") );
- List files = list( config.getProperty( FILES ) );
-
- init( newInstance, files, secondsToRefresh );
+ int secondsToRefresh = Integer.parseInt( config.getProperty( POLL_INTERVAL, "-1" ) );
+
+ List providers = new ArrayList();
+
+ for ( Iterator iter = config.keySet().iterator(); iter.hasNext(); ) {
+ String key = (String) iter.next();
+ if ( key.equals( POLL_INTERVAL ) || key.equals( NEW_INSTANCE ) ) {
+ //already dealt with these
+ } else {
+ providers.add( getProvider( key, config.getProperty( key ) ) );
+ }
+ }
+
+ init( newInstance, providers, secondsToRefresh );
}
- synchronized void init(boolean newInstance, List files, int secondsToRefresh) {
+ /**
+ * Return a configured provider ready to go.
+ */
+ private PackageProvider getProvider(String key, String property) {
+ Class clz = (Class) PACKAGE_PROVIDERS.get( key );
+ try {
+ PackageProvider prov = (PackageProvider) clz.newInstance();
+ prov.configure( list( property ) );
+ return prov;
+ } catch ( InstantiationException e ) {
+ throw new RuntimeDroolsException( "Unable to load up a package provider for " + key,
+ e );
+ } catch ( IllegalAccessException e ) {
+ throw new RuntimeDroolsException( "Unable to load up a package provider for " + key,
+ e );
+ }
+ }
+
+ synchronized void init(boolean newInstance, final List providers, int secondsToRefresh) {
this.newInstance = newInstance;
- final FileScanner fileScan = new FileScanner();
- fileScan.setFiles( (String[]) files.toArray( new String[files.size()] ) );
-
- //run it the first time
- updateRuleBase( fileScan );
-
-
- if (secondsToRefresh != -1) {
+
+ //run it the first time for each.
+ for ( Iterator iter = providers.iterator(); iter.hasNext(); ) {
+ PackageProvider prov = (PackageProvider) iter.next();
+ updateRuleBase( prov );
+ }
+
+ if ( secondsToRefresh != -1 ) {
int interval = secondsToRefresh * 1000;
//now schedule it for polling
- timer = new Timer();
+ timer = new Timer( true );
timer.schedule( new TimerTask() {
public void run() {
- updateRuleBase( fileScan );
+ for ( Iterator iter = providers.iterator(); iter.hasNext(); ) {
+ PackageProvider prov = (PackageProvider) iter.next();
+ updateRuleBase( prov );
+ }
}
}, interval, interval );
}
}
-
- private synchronized void updateRuleBase(FileScanner fileScan) {
- System.err.println("SCANNING FOR CHANGE " + fileScan.toString());
-
- if (this.newInstance || this.ruleBase == null) {
+ private synchronized void updateRuleBase(PackageProvider prov) {
+ System.err.println( "SCANNING FOR CHANGE " + prov.toString() );
+ if ( this.newInstance || this.ruleBase == null ) {
ruleBase = RuleBaseFactory.newRuleBase();
}
- fileScan.updateRuleBase( this.ruleBase, !this.newInstance );
+ prov.updateRuleBase( this.ruleBase, !this.newInstance );
}
/**
@@ -99,7 +146,7 @@
* @param property
* @return
*/
- List list(String property) {
+ static List list(String property) {
if ( property == null ) return Collections.EMPTY_LIST;
StringTokenizer st = new StringTokenizer( property,
"\n\r\t " );
@@ -109,14 +156,14 @@
}
return list;
}
-
+
/**
* Return a current rulebase.
* Depending on the configuration, this may be a new object each time
* the rules are updated.
*
*/
- public RuleBase getRuleBase() {
+ public synchronized RuleBase getRuleBase() {
return this.ruleBase;
}
@@ -127,16 +174,16 @@
* Stop the polling (if it is happening)
*/
public void stopPolling() {
- if (this.timer != null) timer.cancel();
+ if ( this.timer != null ) timer.cancel();
timer = null;
}
-
+
boolean isNewInstance() {
return newInstance;
}
-
+
boolean isPolling() {
return this.timer != null;
}
-
+
}
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/FileScannerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/FileScannerTest.java 2007-06-21 02:51:28 UTC (rev 12725)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/FileScannerTest.java 2007-06-21 03:52:07 UTC (rev 12726)
@@ -1,6 +1,8 @@
package org.drools.agent;
import java.io.File;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -42,7 +44,7 @@
RuleBaseAssemblerTest.writePackage( p2, p2f);
FileScanner scan = new FileScanner();
- scan.setFiles( new String[] {p1f.getPath(), p2f.getPath()} );
+ scan.configure( Arrays.asList( new String[] {p1f.getPath(), p2f.getPath()}) );
RuleBase rb = RuleBaseFactory.newRuleBase();
scan.updateRuleBase( rb, true );
More information about the jboss-svn-commits
mailing list