[jboss-svn-commits] JBL Code SVN: r12771 - 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
Fri Jun 22 02:52:09 EDT 2007
Author: michael.neale at jboss.com
Date: 2007-06-22 02:52:08 -0400 (Fri, 22 Jun 2007)
New Revision: 12771
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/HttpClientImpl.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/IHttpClient.java
Removed:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/HttpClient.java
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/PackageProvider.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/URLScanner.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/URLScannerTest.java
Log:
JBRULES-752
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-22 03:59:59 UTC (rev 12770)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java 2007-06-22 06:52:08 UTC (rev 12771)
@@ -23,13 +23,10 @@
*/
public class FileScanner extends PackageProvider {
- private File[] files;
- private Map lastUpdated = new HashMap();
+ File[] files;
+ Map lastUpdated = new HashMap();
- public FileScanner() {
- }
-
/**
* This sets the list of files to be monitored.
* This takes a list of paths and files (not directories).
@@ -61,38 +58,12 @@
void updateRuleBase(RuleBase rb,
boolean removeExistingPackages) {
Package[] changes = getChangeSet();
- if ( changes == null ) return;
- for ( int i = 0; i < changes.length; i++ ) {
- Package p = changes[i];
- if ( removeExistingPackages ) {
- removePackage( p.getName(),
- rb );
- }
- try {
- rb.addPackage( p );
- } catch ( Exception e ) {
- throw new RuntimeDroolsException( e );
- }
- }
+ applyChanges( rb, removeExistingPackages, changes );
}
- /**
- * Remove the package from the rulebase if it exists in it.
- * If it does not, does nothing.
- */
- private void removePackage(String name,
- RuleBase rb) {
- Package[] ps = rb.getPackages();
- if ( ps == null ) return;
- for ( int i = 0; i < ps.length; i++ ) {
- Package p = ps[i];
- if ( p.getName().equals( name ) ) {
- rb.removePackage( name );
- return;
- }
- }
- }
+
+
/**
* Calculate a change set, based on last updated times.
* (keep a map of files).
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/HttpClient.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/HttpClient.java 2007-06-22 03:59:59 UTC (rev 12770)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/HttpClient.java 2007-06-22 06:52:08 UTC (rev 12771)
@@ -1,74 +0,0 @@
-package org.drools.agent;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.ProtocolException;
-import java.net.URL;
-import java.net.URLConnection;
-
-import org.drools.RuntimeDroolsException;
-import org.drools.common.DroolsObjectInputStream;
-import org.drools.rule.Package;
-
-public class HttpClient {
-
-
-
-
- public HttpClient(String uri) throws Exception {
- try {
-
- checkLastUpdated( uri );
- URL url = new URL(uri);
-
- URLConnection con = url.openConnection();
- HttpURLConnection httpCon = (HttpURLConnection) con;
- httpCon.setRequestMethod( "GET" );
- InputStream in = httpCon.getInputStream();
-
- DroolsObjectInputStream oin = new DroolsObjectInputStream(in);
- Package p = (Package) oin.readObject();
- in.close();
-
-
- System.err.println(p.getName());
-
- } catch ( IOException e ) {
- throw new RuntimeDroolsException(e);
- }
-
- }
-
- private void checkLastUpdated(String uri) throws MalformedURLException,
- IOException,
- ProtocolException {
- URL url = new URL(uri);
-
- URLConnection con = url.openConnection();
- HttpURLConnection httpCon = (HttpURLConnection) con;
- httpCon.setRequestMethod( "HEAD" );
-
-
-
- //if this is null, then its not cool
- System.err.println(httpCon.getHeaderField( "lastModified" ));
-
- //can check for '200 OK' to make sure its kosher.
- System.err.println(httpCon.getHeaderFields());
-
- String status = httpCon.getHeaderField( null );
-
-
- System.err.println(status);
-
- httpCon.disconnect();
- }
-
- public static void main(String[] args) throws Exception {
- HttpClient scan = new HttpClient("http://localhost:8888/org.drools.brms.JBRMS/package/com.billasurf.manufacturing.plant/SNAP");
-
- }
-
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/HttpClientImpl.java (from rev 12767, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/HttpClient.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/HttpClientImpl.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/HttpClientImpl.java 2007-06-22 06:52:08 UTC (rev 12771)
@@ -0,0 +1,80 @@
+package org.drools.agent;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.drools.common.DroolsObjectInputStream;
+import org.drools.rule.Package;
+
+public class HttpClientImpl implements IHttpClient {
+
+
+
+
+
+
+
+
+
+
+
+ public LastUpdatedPing checkLastUpdated(URL url) throws IOException {
+ URLConnection con = url.openConnection();
+ HttpURLConnection httpCon = (HttpURLConnection) con;
+ try {
+ httpCon.setRequestMethod( "HEAD" );
+
+ String lm = httpCon.getHeaderField( "lastModified" );
+ LastUpdatedPing ping = new LastUpdatedPing();
+
+ ping.responseMessage = httpCon.getHeaderFields().toString();
+
+ if (lm != null) {
+ ping.lastUpdated = Long.parseLong( lm );
+ }
+
+ return ping;
+ } finally {
+ httpCon.disconnect();
+ }
+
+ }
+
+ public Package fetchPackage(URL url) throws IOException {
+ URLConnection con = url.openConnection();
+ HttpURLConnection httpCon = (HttpURLConnection) con;
+ try {
+ httpCon.setRequestMethod( "GET" );
+ InputStream in = httpCon.getInputStream();
+
+ DroolsObjectInputStream oin = new DroolsObjectInputStream(in);
+ try {
+ return (Package) oin.readObject();
+ } catch ( ClassNotFoundException e ) {
+ e.printStackTrace();
+ return null;
+ }
+ } finally {
+ httpCon.disconnect();
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ HttpClientImpl cl = new HttpClientImpl();
+ URL url = new URL("http://localhost:8888/org.drools.brms.JBRMS/package/com.billasurf.manufacturing.plant/SNAP");
+
+
+ LastUpdatedPing ping = cl.checkLastUpdated( url );
+
+
+ Package p = cl.fetchPackage( url );
+
+
+ System.err.println(ping);
+ System.err.println( ping.isError() );
+ }
+
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/IHttpClient.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/IHttpClient.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/IHttpClient.java 2007-06-22 06:52:08 UTC (rev 12771)
@@ -0,0 +1,45 @@
+package org.drools.agent;
+
+import java.io.IOException;
+import java.net.URL;
+
+import org.drools.rule.Package;
+
+/**
+ * A nicely mockable Http client interface.
+ *
+ * IM IN YR HTTP MOCKIN UR CLEINT
+ *
+ * @author Michael Neale
+ *
+ */
+public interface IHttpClient {
+
+ public LastUpdatedPing checkLastUpdated(URL url) throws IOException;
+
+ public Package fetchPackage(URL url) throws IOException;
+
+
+}
+
+/**
+ * This is returned when pinging for changes.
+ *
+ * @author Michael Neale
+ */
+class LastUpdatedPing {
+ public long lastUpdated = -1;
+ public String responseMessage;
+
+ public boolean isError() {
+ if (lastUpdated == -1) return true;
+ if (responseMessage == null) return true;
+ if (responseMessage.indexOf( "200 OK" ) == -1) return true;
+ return false;
+ }
+
+ public String toString() {
+ return "Last updated: " + lastUpdated + "\n" + "Reponse header: " + responseMessage;
+ }
+
+}
Property changes on: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/IHttpClient.java
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: 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 2007-06-22 03:59:59 UTC (rev 12770)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/PackageProvider.java 2007-06-22 06:52:08 UTC (rev 12771)
@@ -3,6 +3,8 @@
import java.util.Properties;
import org.drools.RuleBase;
+import org.drools.RuntimeDroolsException;
+import org.drools.rule.Package;
/**
* All sources of packages must implement this.
@@ -24,4 +26,39 @@
*/
abstract void configure(Properties config);
+ /**
+ * Remove the package from the rulebase if it exists in it.
+ * If it does not, does nothing.
+ */
+ void removePackage(String name,
+ RuleBase rb) {
+ Package[] ps = rb.getPackages();
+ if ( ps == null ) return;
+ for ( int i = 0; i < ps.length; i++ ) {
+ Package p = ps[i];
+ if ( p.getName().equals( name ) ) {
+ rb.removePackage( name );
+ return;
+ }
+ }
+ }
+
+
+ void applyChanges(RuleBase rb, boolean removeExistingPackages, Package[] changes) {
+ if ( changes == null ) return;
+ for ( int i = 0; i < changes.length; i++ ) {
+ Package p = changes[i];
+ if ( removeExistingPackages ) {
+ removePackage( p.getName(),
+ rb );
+ }
+ try {
+ rb.addPackage( p );
+ } catch ( Exception e ) {
+ throw new RuntimeDroolsException( e );
+ }
+ }
+ }
+
+
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/URLScanner.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/URLScanner.java 2007-06-22 03:59:59 UTC (rev 12770)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/URLScanner.java 2007-06-22 06:52:08 UTC (rev 12771)
@@ -1,54 +1,159 @@
package org.drools.agent;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
import org.drools.RuleBase;
import org.drools.RuntimeDroolsException;
+import org.drools.rule.Package;
+public class URLScanner extends PackageProvider {
-public class URLScanner extends PackageProvider {
+ //this is the URLs we are managing
+ URL[] urls;
- private List uriList;
+ //this is only set if we are using a local cache - only fall back on this
+ //when URL connection is not available.
+ FileScanner localCacheFileScanner;
+
+ //this is used to access the remote resources
+ IHttpClient httpClient = new HttpClientImpl();
+
+ //a record of the last updated URL timestamps
+ Map lastUpdated = new HashMap();
+
private File localCacheDir;
- private URL[] urls;
-
+
void configure(Properties config) {
- uriList = RuleAgent.list( config.getProperty( RuleAgent.URLS ) );
+ List uriList = RuleAgent.list( config.getProperty( RuleAgent.URLS ) );
urls = new URL[uriList.size()];
- for (int i = 0; i < uriList.size(); i++ ) {
+ for ( int i = 0; i < uriList.size(); i++ ) {
String url = (String) uriList.get( i );
try {
- urls[i] = new URL(url);
+ urls[i] = new URL( url );
} catch ( MalformedURLException e ) {
- throw new RuntimeException("The URL " + url + " is not valid.", e);
+ throw new RuntimeException( "The URL " + url + " is not valid.",
+ e );
}
}
+
+ //if we have a local cache, check its all kosher
String localCache = config.getProperty( RuleAgent.LOCAL_URL_CACHE );
- if (localCache != null) {
- localCacheDir = new File(localCache);
- if (!localCacheDir.isDirectory()) {
- throw new RuntimeDroolsException("The local cache dir " + localCache + " is a file, not a directory.");
+ if ( localCache != null ) {
+ localCacheDir = new File( localCache );
+ if ( !localCacheDir.isDirectory() ) {
+ throw new RuntimeDroolsException( "The local cache dir " + localCache + " is a file, not a directory." );
}
+ this.localCacheFileScanner = new FileScanner();
+ this.localCacheFileScanner.setFiles( getFiles( urls, localCacheDir ) );
+ }
+ }
+ File[] getFiles(URL[] urls, File cacheDir) {
+ File[] fs = new File[urls.length];
+ for ( int i = 0; i < urls.length; i++ ) {
+ URL u = urls[i];
+ File f = getLocalCacheFileForURL( cacheDir, u );
+ fs[i] = f;
}
+ return fs;
}
-
- /**
- * Return the full url in string form.
- */
- static String getURL(URL u) {
- return u.getProtocol() + "://" + u.getHost() + ":" + u.getPort() + u.getPath();
+ private File getLocalCacheFileForURL(File cacheDir, URL u) {
+ File f;
+ try {
+ f = new File( cacheDir,
+ URLEncoder.encode( u.toExternalForm(), "UTF-8" ) );
+ } catch ( UnsupportedEncodingException e ) {
+ throw new RuntimeDroolsException( e );
+ }
+ return f;
}
+
+
void updateRuleBase(RuleBase rb, boolean removeExistingPackages) {
- URL url;
+ Package[] changes = null;
+ try {
+ changes = getChangeSet();
+ applyChanges( rb, removeExistingPackages, changes );
+ } catch ( IOException e ) {
+ if (this.localCacheFileScanner != null) {
+ localCacheFileScanner.updateRuleBase( rb, removeExistingPackages );
+ }
+ //TODO: add in logging
+ }
}
+
+ private Package[] getChangeSet() throws IOException {
+ if ( this.urls == null ) return new Package[0];
+ List list = new ArrayList();
+ for ( int i = 0; i < urls.length; i++ ) {
+ URL u = urls[i];
+ if ( hasChanged( u, this.lastUpdated) ) {
+ Package p = readPackage( u );
+ if ( p == null ) return null;
+ list.add( p );
+ if (localCacheDir != null) {
+ writeLocalCacheCopy(p, u, localCacheDir);
+ }
+ }
+ }
+ return (Package[]) list.toArray( new Package[list.size()] );
+ }
+
+ private void writeLocalCacheCopy(Package p, URL u, File localCacheDir) {
+ File local = getLocalCacheFileForURL( localCacheDir, u );
+ if (local.exists()) local.delete();
+ try {
+ ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(local));
+ out.writeObject( p );
+ out.flush();
+ out.close();
+ } catch (IOException e) {
+ //TODO: log this event
+ }
+
+ }
+ private Package readPackage(URL u) throws IOException {
+ return httpClient.fetchPackage( u );
+ }
+ private boolean hasChanged(URL u, Map updates) throws IOException {
+ LastUpdatedPing pong = httpClient.checkLastUpdated( u );
+ if (pong.isError()) {
+ throw new IOException("Was unable to reach server.");
+ //TODO: log stuff here.
+ }
+
+ String url = u.toExternalForm();
+ if ( !updates.containsKey( url ) ) {
+ updates.put( url,
+ new Long( pong.lastUpdated ) );
+ return true;
+ } else {
+ Long last = (Long) updates.get( url );
+ if ( last.intValue() < pong.lastUpdated ) {
+ updates.put( url,
+ new Long( pong.lastUpdated ) );
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+
}
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/URLScannerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/URLScannerTest.java 2007-06-22 03:59:59 UTC (rev 12770)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/URLScannerTest.java 2007-06-22 06:52:08 UTC (rev 12771)
@@ -3,14 +3,14 @@
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
-import java.net.URLConnection;
import java.net.URLEncoder;
+import java.util.Properties;
+import junit.framework.TestCase;
+
import org.drools.common.DroolsObjectInputStream;
import org.drools.rule.Package;
-import junit.framework.TestCase;
-
public class URLScannerTest extends TestCase {
public void testFileURLCache() throws Exception {
@@ -28,19 +28,74 @@
DroolsObjectInputStream in = new DroolsObjectInputStream(new FileInputStream(f));
Package p_ = (Package) in.readObject();
- assertEquals("x", p.getName());
+ assertEquals("x", p_.getName());
}
- public void testGetURL() throws Exception {
- String url = "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST";
- URL u = new URL(url);
- assertEquals(url, URLScanner.getURL( u ));
- //URLConnection con = u.openConnection();
- //con.connect();
+// public void testGetURL() throws Exception {
+// String url = "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST";
+// URL u = new URL(url);
+// assertEquals(url, URLScanner.getURL( u ));
+// //URLConnection con = u.openConnection();
+// //con.connect();
+// }
+
+ public void testGetFiles() throws Exception {
+
+ URL u1 = new URL("http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST");
+ URL u2 = new URL("http://localhost:8080/foo/bar.bar/packages/IMINYRURL/PROD");
+ URLScanner scan = new URLScanner();
+
+ File dir = RuleBaseAssemblerTest.getTempDirectory();
+ File[] result = scan.getFiles( new URL[] {u1, u2}, dir );
+
+ assertEquals(2, result.length);
+ assertEquals(dir.getPath(), result[0].getParent());
+
+ File f1 = result[0];
+ File f2 = result[1];
+ assertEquals("http%3A%2F%2Flocalhost%3A8080%2Ffoo%2Fbar.bar%2Fpackages%2FIMINYRURL%2FLATEST", f1.getName());
+ assertEquals("http%3A%2F%2Flocalhost%3A8080%2Ffoo%2Fbar.bar%2Fpackages%2FIMINYRURL%2FPROD", f2.getName());
+
}
+ public void testConfig() throws Exception {
+ URLScanner scan = new URLScanner();
+
+ File dir = RuleBaseAssemblerTest.getTempDirectory();
+
+ Properties config = new Properties();
+ config.setProperty( RuleAgent.LOCAL_URL_CACHE, dir.getPath() );
+ config.setProperty( RuleAgent.URLS, "http://goo.ber http://wee.waa" );
+
+ scan.configure( config );
+
+ assertNotNull(scan.lastUpdated);
+ assertEquals(2, scan.urls.length);
+ assertEquals("http://goo.ber", scan.urls[0].toExternalForm());
+ assertEquals("http://wee.waa", scan.urls[1].toExternalForm());
+ assertNotNull(scan.localCacheFileScanner);
+
+ assertEquals(2, scan.localCacheFileScanner.files.length);
+
+ assertEquals("http%3A%2F%2Fgoo.ber", scan.localCacheFileScanner.files[0].getName());
+ assertEquals("http%3A%2F%2Fwee.waa", scan.localCacheFileScanner.files[1].getName());
+
+
+
+ }
+ public void testLastUpdatedError() {
+ LastUpdatedPing ping = new LastUpdatedPing();
+ assertTrue(ping.isError());
+ ping.responseMessage = "ABC";
+ ping.lastUpdated = 42;
+ assertTrue(ping.isError());
+ ping.responseMessage = "200 OK";
+ assertFalse(ping.isError());
+ }
+
+
}
More information about the jboss-svn-commits
mailing list