[jboss-svn-commits] JBL Code SVN: r12250 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/util and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed May 30 23:36:43 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-05-30 23:36:42 -0400 (Wed, 30 May 2007)
New Revision: 12250

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ChainedProperties.java
   labs/jbossrules/trunk/drools-core/src/main/resources/META-INF/
   labs/jbossrules/trunk/drools-core/src/main/resources/META-INF/drools.default.rulebase.conf
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/RuleBaseConfigurationTest.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
Log:
JBRULES-713 Make Dialects Pluggeable
-Default dialects are no longer hard coded and the DialectRegistry is now built by the configuration object.

JBRULES-895
-The properties handling has been extended to support chaining.

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java	2007-05-31 00:21:38 UTC (rev 12249)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java	2007-05-31 03:36:42 UTC (rev 12250)
@@ -17,8 +17,10 @@
 package org.drools;
 
 import java.io.Serializable;
+import java.util.Properties;
 
 import org.drools.concurrent.ExecutorService;
+import org.drools.util.ChainedProperties;
 
 /**
  * RuleBaseConfiguration
@@ -57,6 +59,8 @@
     Serializable {
     private static final long serialVersionUID = 320L;
 
+    private ChainedProperties chainedProperties;
+
     private boolean           immutable;
 
     private boolean           maintainTms;
@@ -72,42 +76,56 @@
     private LogicalOverride   logicalOverride;
     private ExecutorService   executorService;
 
+    public RuleBaseConfiguration(Properties properties) {
+        init(properties);
+    }
+    
     public RuleBaseConfiguration() {
+        init(null);
+    }
+    
+    private void init(Properties properties) {
         this.immutable = false;
 
-        setMaintainTms( Boolean.valueOf( System.getProperty( "drools.maintainTms",
-                                                             "true" ) ).booleanValue() );
+        this.chainedProperties = new ChainedProperties( "rulebase.conf" );
+        
+        if ( properties != null ) {
+            this.chainedProperties.addProperties( properties );
+        }
 
-        setRemoveIdentities( Boolean.valueOf( System.getProperty( "drools.removeIdentities",
-                                                                  "false" ) ).booleanValue() );
+        setMaintainTms( Boolean.valueOf( this.chainedProperties.getProperty( "drools.maintainTms",
+                                                                             "true" ) ).booleanValue() );
 
-        setAlphaMemory( Boolean.valueOf( System.getProperty( "drools.alphaMemory",
-                                                             "false" ) ).booleanValue() );
+        setRemoveIdentities( Boolean.valueOf( this.chainedProperties.getProperty( "drools.removeIdentities",
+                                                                                  "false" ) ).booleanValue() );
 
-        setShareAlphaNodes( Boolean.valueOf( System.getProperty( "drools.shareAlphaNodes",
-                                                                 "true" ) ).booleanValue() );
+        setAlphaMemory( Boolean.valueOf( this.chainedProperties.getProperty( "drools.alphaMemory",
+                                                                             "false" ) ).booleanValue() );
 
-        setShareBetaNodes( Boolean.valueOf( System.getProperty( "drools.shareBetaNodes",
-                                                                "true" ) ).booleanValue() );
+        setShareAlphaNodes( Boolean.valueOf( this.chainedProperties.getProperty( "drools.shareAlphaNodes",
+                                                                                 "true" ) ).booleanValue() );
 
-        setAlphaNodeHashingThreshold( Integer.parseInt( System.getProperty( "drools.alphaNodeHashingThreshold",
-                                                                            "3" ) ) );
+        setShareBetaNodes( Boolean.valueOf( this.chainedProperties.getProperty( "drools.shareBetaNodes",
+                                                                                "true" ) ).booleanValue() );
 
-        setCompositeKeyDepth( Integer.parseInt( System.getProperty( "drools.compositeKeyDepth",
-                                                                    "3" ) ) );
+        setAlphaNodeHashingThreshold( Integer.parseInt( this.chainedProperties.getProperty( "drools.alphaNodeHashingThreshold",
+                                                                                            "3" ) ) );
 
-        setIndexLeftBetaMemory( Boolean.valueOf( System.getProperty( "drools.indexLeftBetaMemory",
-                                                                     "true" ) ).booleanValue() );
-        setIndexRightBetaMemory( Boolean.valueOf( System.getProperty( "drools.indexRightBetaMemory",
-                                                                      "true" ) ).booleanValue() );
+        setCompositeKeyDepth( Integer.parseInt( this.chainedProperties.getProperty( "drools.compositeKeyDepth",
+                                                                                    "3" ) ) );
 
-        setAssertBehaviour( AssertBehaviour.determineAssertBehaviour( System.getProperty( "drools.assertBehaviour",
-                                                                                          "IDENTITY" ) ) );
-        setLogicalOverride( LogicalOverride.determineLogicalOverride( System.getProperty( "drools.logicalOverride",
-                                                                                          "DISCARD" ) ) );
+        setIndexLeftBetaMemory( Boolean.valueOf( this.chainedProperties.getProperty( "drools.indexLeftBetaMemory",
+                                                                                     "true" ) ).booleanValue() );
+        setIndexRightBetaMemory( Boolean.valueOf( this.chainedProperties.getProperty( "drools.indexRightBetaMemory",
+                                                                                      "true" ) ).booleanValue() );
 
-        setExecutorService( RuleBaseConfiguration.determineExecutorService( System.getProperty( "drools.executorService",
-                                                                                                "org.drools.concurrent.DefaultExecutorService" ) ) );
+        setAssertBehaviour( AssertBehaviour.determineAssertBehaviour( this.chainedProperties.getProperty( "drools.assertBehaviour",
+                                                                                                          "IDENTITY" ) ) );
+        setLogicalOverride( LogicalOverride.determineLogicalOverride( this.chainedProperties.getProperty( "drools.logicalOverride",
+                                                                                                          "DISCARD" ) ) );
+
+        setExecutorService( RuleBaseConfiguration.determineExecutorService( this.chainedProperties.getProperty( "drools.executorService",
+                                                                                                                "org.drools.concurrent.DefaultExecutorService" ) ) );        
     }
 
     /**

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ChainedProperties.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ChainedProperties.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ChainedProperties.java	2007-05-31 03:36:42 UTC (rev 12250)
@@ -0,0 +1,184 @@
+/**
+ * 
+ */
+package org.drools.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+public class ChainedProperties {
+    private final List props;
+    private final List defaultProps;
+    
+    public ChainedProperties(String confFileName) {
+        this( Thread.currentThread().getContextClassLoader(),
+              confFileName );
+    }
+    
+    public ChainedProperties(ClassLoader classLoader, String confFileName) {
+        this.props = new ArrayList();
+        this.defaultProps = new ArrayList();
+        
+        // Properties added in precedence order
+        //this.chainedProperties = new ChainedProperties();
+
+        // System defined properties always get precedence
+        addProperties( System.getProperties() );
+
+        // System property defined properties file
+        loadProperties( System.getProperty( "drools.packagebuilder.conf" ), this.props );
+
+        // User home properties file
+        loadProperties( System.getProperty( "user.home" ) + "/drools.packagebuilder.conf", this.props );
+
+        // Working directory properties file
+        loadProperties( "drools.packagebuilder.conf", this.props );
+
+        // check META-INF directories for all known ClassLoaders
+        ClassLoader confClassLoader = classLoader;
+        if ( confClassLoader != null ) {
+            loadProperties( confClassLoader.getResource( "META-INF/drools." + confFileName), this.props );
+        }
+
+        confClassLoader = getClass().getClassLoader();
+        if ( confClassLoader != null && confClassLoader != classLoader ) {
+            loadProperties( confClassLoader.getResource( "META-INF/drools." + confFileName ), this.props );
+        }
+
+        confClassLoader = Thread.currentThread().getContextClassLoader();
+        if ( confClassLoader != null && confClassLoader != classLoader ) {
+            loadProperties( confClassLoader.getResource( "META-INF/drools." + confFileName ), this.props );
+        }
+
+        confClassLoader = ClassLoader.getSystemClassLoader();
+        if ( confClassLoader != null && confClassLoader != classLoader ) {
+            loadProperties( confClassLoader.getResource( "META-INF/drools." + confFileName ), this.props );
+        }
+
+        // load default, only use the first one as there should only be one
+        confClassLoader = classLoader;
+        URL defaultURL = null;
+        if ( confClassLoader != null ) {
+            defaultURL = confClassLoader.getResource( "META-INF/drools.default." + confFileName );
+        }
+
+        if ( defaultURL == null ) {
+            confClassLoader = getClass().getClassLoader();
+            if ( confClassLoader != null && confClassLoader != classLoader ) {
+                defaultURL = confClassLoader.getResource( "META-INF/drools.default." + confFileName );
+            }
+        }
+
+        if ( defaultURL == null ) {
+            confClassLoader = Thread.currentThread().getContextClassLoader();
+            if ( confClassLoader != null && confClassLoader != classLoader ) {
+                defaultURL = confClassLoader.getResource( "META-INF/drools.default." + confFileName );
+            }
+        }
+
+        if ( defaultURL == null ) {
+            confClassLoader = ClassLoader.getSystemClassLoader();
+            if ( confClassLoader != null && confClassLoader != classLoader ) {
+                defaultURL = confClassLoader.getResource( "META-INF/drools.default.packagebuilder.conf" );
+            }
+        }
+
+        if ( defaultURL != null ) {
+            loadProperties( defaultURL, this.defaultProps );
+        }
+    }
+    
+    public void addProperties(Properties properties) {
+        this.props.add( properties );
+    }  
+
+    public String getProperty(String key,
+                              String defaultValue) {
+        String value = null;
+        for ( Iterator it = this.props.iterator(); it.hasNext(); ) {
+            Properties props = (Properties) it.next();
+            value = props.getProperty( key );
+            if ( value != null ) {
+                break;
+            }
+        }
+        if ( value == null ) {
+            for ( Iterator it = this.defaultProps.iterator(); it.hasNext(); ) {
+                Properties props = (Properties) it.next();
+                value = props.getProperty( key );
+                if ( value != null ) {
+                    break;
+                }
+            }
+        }
+        return (value != null) ? value : defaultValue;
+    }
+
+    public void mapStartsWith(Map map,
+                              String startsWith) {
+        for ( Iterator it = this.props.iterator(); it.hasNext(); ) {
+            Properties props = (Properties) it.next();
+            mapStartsWith( map,
+                           props,
+                           startsWith );
+        }
+        
+        for ( Iterator it = this.defaultProps.iterator(); it.hasNext(); ) {
+            Properties props = (Properties) it.next();
+            mapStartsWith( map,
+                           props,
+                           startsWith );
+        }        
+    }
+
+    private void mapStartsWith(Map map,
+                               Properties properties,
+                               String startsWith) {
+        Enumeration enumeration = properties.propertyNames();
+        while ( enumeration.hasMoreElements() ) {
+            String key = (String) enumeration.nextElement();
+            if ( key.startsWith( startsWith ) ) {
+                if ( !map.containsKey( key ) ) {
+                    map.put( key,
+                             properties.getProperty( key ) );
+                }
+
+            }
+        }
+    }
+        
+    private void loadProperties(String fileName, List chain) {
+        if ( fileName != null ) {
+            File file = new File( fileName );
+            if ( file != null && file.exists() ) {
+                try {
+                    loadProperties( file.toURL(), chain );
+                } catch ( MalformedURLException e ) {
+                    throw new IllegalArgumentException( "file.toURL failed for drools.packagebuilder.conf properties value '" + file + "'" );
+                }
+            } else {
+                //throw new IllegalArgumentException( "drools.packagebuilder.conf is specified but cannot be found '" + file + "'" );
+            }
+        }
+    }
+
+    private void loadProperties(URL confURL, List chain) {
+        if ( confURL != null ) {
+            Properties properties = new Properties();
+            try {
+                properties.load( confURL.openStream() );
+                chain.add( properties );
+            } catch ( IOException e ) {
+                //throw new IllegalArgumentException( "Invalid URL to properties file '" + confURL.toExternalForm() + "'" );
+            }
+        }
+    }    
+}
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-core/src/main/resources/META-INF/drools.default.rulebase.conf
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/resources/META-INF/drools.default.rulebase.conf	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/resources/META-INF/drools.default.rulebase.conf	2007-05-31 03:36:42 UTC (rev 12250)
@@ -0,0 +1,12 @@
+drools.maintainTms = true
+drools.removeIdentities = false
+drools.alphaMemory = false
+drools.shareAlphaNodes = true
+drools.shareBetaNodes = true
+drools.alphaNodeHashingThreshold = 3
+drools.compositeKeyDepth = 3
+drools.indexLeftBetaMemory = true
+drools.indexRightBetaMemory = true
+drools.assertBehaviour = IDENTITY
+drools.logicalOverride = DISCARD
+drools.executorService = org.drools.concurrent.DefaultExecutorService
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/RuleBaseConfigurationTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/RuleBaseConfigurationTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/RuleBaseConfigurationTest.java	2007-05-31 03:36:42 UTC (rev 12250)
@@ -0,0 +1,37 @@
+package org.drools;
+
+import java.util.Properties;
+
+import org.drools.RuleBaseConfiguration.AssertBehaviour;
+
+import junit.framework.TestCase;
+
+public class RuleBaseConfigurationTest extends TestCase {
+
+    public void testSystemProperties() {
+        RuleBaseConfiguration cfg = new RuleBaseConfiguration();
+        assertEquals( AssertBehaviour.IDENTITY,
+                      cfg.getAssertBehaviour() );
+
+        System.setProperty( "drools.assertBehaviour",
+                            "EQUALITY" );
+        cfg = new RuleBaseConfiguration();
+        assertEquals( AssertBehaviour.EQUALITY,
+                      cfg.getAssertBehaviour() );
+    }
+
+    public void testProgrammaticPropertiesFile() {
+        RuleBaseConfiguration cfg = new RuleBaseConfiguration();
+        assertEquals( true,
+                      cfg.isIndexLeftBetaMemory() );
+
+        Properties properties = new Properties();
+        properties.setProperty( "drools.indexLeftBetaMemory",
+                                "false" );
+        cfg = new RuleBaseConfiguration( properties );
+
+        assertEquals( false,
+                      cfg.isIndexLeftBetaMemory() );
+    }
+
+}




More information about the jboss-svn-commits mailing list