[jboss-svn-commits] JBL Code SVN: r21553 - in labs/jbossrules/trunk/drools-api/src/main/java: oirg and 3 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Aug 14 21:18:47 EDT 2008
Author: mark.proctor at jboss.com
Date: 2008-08-14 21:18:44 -0400 (Thu, 14 Aug 2008)
New Revision: 21553
Added:
labs/jbossrules/trunk/drools-api/src/main/java/oirg/
labs/jbossrules/trunk/drools-api/src/main/java/oirg/drools/
labs/jbossrules/trunk/drools-api/src/main/java/oirg/drools/util/
labs/jbossrules/trunk/drools-api/src/main/java/oirg/drools/util/ChainedProperties.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/ProviderInitializationException.java
Modified:
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/KnowledgeSessionFactory.java
Log:
JBRULES-1734 Drools-API
-Added ability to load properties and a sample loadProvider that uses it.
Added: labs/jbossrules/trunk/drools-api/src/main/java/oirg/drools/util/ChainedProperties.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/oirg/drools/util/ChainedProperties.java (rev 0)
+++ labs/jbossrules/trunk/drools-api/src/main/java/oirg/drools/util/ChainedProperties.java 2008-08-15 01:18:44 UTC (rev 21553)
@@ -0,0 +1,268 @@
+/**
+ *
+ */
+package oirg.drools.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Serializable;
+import java.io.Externalizable;
+import java.io.ObjectOutput;
+import java.io.ObjectInput;
+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
+ implements
+ Externalizable {
+ private List props;
+ private List defaultProps;
+
+ public ChainedProperties() {
+
+ }
+
+ public ChainedProperties(String confFileName) {
+ this( null,
+ confFileName );
+ }
+
+ public ChainedProperties(ClassLoader classLoader,
+ String confFileName) {
+ this( classLoader,
+ confFileName,
+ true );
+ }
+
+ public ChainedProperties(ClassLoader classLoader,
+ String confFileName,
+ boolean populateDefaults) {
+ if ( classLoader == null ) {
+ classLoader = Thread.currentThread().getContextClassLoader();
+ if ( classLoader == null ) {
+ classLoader = this.getClass().getClassLoader();
+ }
+ }
+
+ this.props = new ArrayList();
+ this.defaultProps = new ArrayList();
+
+ // Properties added in precedence order
+
+ // System defined properties always get precedence
+ addProperties( System.getProperties() );
+
+ // System property defined properties file
+ loadProperties( System.getProperty( "drools." + confFileName ),
+ this.props );
+
+ // User home properties file
+ loadProperties( System.getProperty( "user.home" ) + "/drools." + confFileName,
+ this.props );
+
+ // Working directory properties file
+ loadProperties( "drools." + confFileName,
+ this.props );
+
+ // check META-INF directories for all known ClassLoaders
+ ClassLoader confClassLoader = classLoader;
+ if ( confClassLoader != null ) {
+ loadProperties( getResources( "META-INF/drools." + confFileName,
+ confClassLoader ),
+ this.props );
+ }
+
+ confClassLoader = getClass().getClassLoader();
+ if ( confClassLoader != null && confClassLoader != classLoader ) {
+ loadProperties( getResources( "META-INF/drools." + confFileName,
+ confClassLoader ),
+ this.props );
+ }
+
+ confClassLoader = Thread.currentThread().getContextClassLoader();
+ if ( confClassLoader != null && confClassLoader != classLoader ) {
+ loadProperties( getResources( "META-INF/drools." + confFileName,
+ confClassLoader ),
+ this.props );
+ }
+
+ confClassLoader = ClassLoader.getSystemClassLoader();
+ if ( confClassLoader != null && confClassLoader != classLoader ) {
+ loadProperties( getResources( "META-INF/drools." + confFileName,
+ confClassLoader ),
+ this.props );
+ }
+
+ if ( !populateDefaults ) {
+ return;
+ }
+
+ // load defaults
+ confClassLoader = classLoader;
+ if ( confClassLoader != null ) {
+ loadProperties( getResources( "META-INF/drools.default." + confFileName,
+ confClassLoader ),
+ this.defaultProps );
+ }
+
+ confClassLoader = getClass().getClassLoader();
+ if ( confClassLoader != null && confClassLoader != classLoader ) {
+ loadProperties( getResources( "META-INF/drools.default." + confFileName,
+ confClassLoader ),
+ this.defaultProps );
+ }
+
+ confClassLoader = Thread.currentThread().getContextClassLoader();
+ if ( confClassLoader != null && confClassLoader != classLoader ) {
+ loadProperties( getResources( "META-INF/drools.default." + confFileName,
+ confClassLoader ),
+ this.defaultProps );
+ }
+
+ confClassLoader = ClassLoader.getSystemClassLoader();
+ if ( confClassLoader != null && confClassLoader != classLoader ) {
+ loadProperties( getResources( "META-INF/drools.default." + confFileName,
+ confClassLoader ),
+ this.defaultProps );
+ }
+ }
+
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ props = (List)in.readObject();
+ defaultProps = (List)in.readObject();
+ }
+
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeObject(props);
+ out.writeObject(defaultProps);
+ }
+
+ private Enumeration getResources(String name,
+ ClassLoader classLoader) {
+ Enumeration enumeration = null;
+ try {
+ enumeration = classLoader.getResources( name );
+ } catch ( IOException e ) {
+ e.printStackTrace();
+ }
+ return enumeration;
+ }
+
+ 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,
+ boolean includeSubProperties) {
+ for ( Iterator it = this.props.iterator(); it.hasNext(); ) {
+ Properties props = (Properties) it.next();
+ mapStartsWith( map,
+ props,
+ startsWith,
+ includeSubProperties );
+ }
+
+ for ( Iterator it = this.defaultProps.iterator(); it.hasNext(); ) {
+ Properties props = (Properties) it.next();
+ mapStartsWith( map,
+ props,
+ startsWith,
+ includeSubProperties );
+ }
+ }
+
+ private void mapStartsWith(Map map,
+ Properties properties,
+ String startsWith,
+ boolean includeSubProperties) {
+ Enumeration enumeration = properties.propertyNames();
+ while ( enumeration.hasMoreElements() ) {
+ String key = (String) enumeration.nextElement();
+ if ( key.startsWith( startsWith ) ) {
+ if ( !includeSubProperties && key.substring( startsWith.length() + 1 ).indexOf( '.' ) > 0 ) {
+ // +1 to the length, as we do allow the direct property, just not ones below it
+ // This key has sub properties beyond the given startsWith, so skip
+ continue;
+ }
+ if ( !map.containsKey( key ) ) {
+ map.put( key,
+ properties.getProperty( key ) );
+ }
+
+ }
+ }
+ }
+
+ private void loadProperties(Enumeration enumeration,
+ List chain) {
+ if ( enumeration == null ) {
+ return;
+ }
+
+ while ( enumeration.hasMoreElements() ) {
+ URL url = (URL) enumeration.nextElement();
+ loadProperties( url,
+ chain );
+ }
+ }
+
+ 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 " + fileName + " properties value '" + file + "'" );
+ }
+ } else {
+ //throw new IllegalArgumentException( fileName + " is specified but cannot be found '" + file + "'" );
+ }
+ }
+ }
+
+ private void loadProperties(URL confURL,
+ List chain) {
+ if ( confURL == null ) {
+ return;
+ }
+ 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
Modified: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/KnowledgeSessionFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/KnowledgeSessionFactory.java 2008-08-15 00:49:01 UTC (rev 21552)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/KnowledgeSessionFactory.java 2008-08-15 01:18:44 UTC (rev 21553)
@@ -1,5 +1,7 @@
package org.drools;
+import oirg.drools.util.ChainedProperties;
+
public class KnowledgeSessionFactory {
private static KnowledgeSessionProvider provider;
@@ -8,6 +10,27 @@
}
public static StatefulKnowledgeSession newStatefulKnowledgeSession() {
+ if ( provider == null ) {
+ loadProvider();
+ }
return provider.newStatefulKnowledgeSession();
}
+
+ private static void loadProvider() {
+ try {
+ ChainedProperties properties = new ChainedProperties( "drools-providers.conf" );
+ String className = properties.getProperty( "KnowledgeSessionProvider", null );
+ if ( className != null || className.trim().length() > 0 ) {
+ Class<KnowledgeSessionProvider> cls = ( Class<KnowledgeSessionProvider> ) Class.forName( className );
+ }
+ } catch ( Exception e1 ) {
+ try {
+ // we didn't find anything in properties so lets try and us reflection
+ Class<KnowledgeSessionProvider> cls = ( Class<KnowledgeSessionProvider> ) Class.forName( "org.drools.KnowledgeSessionProviderImpl" );
+ provider = cls.newInstance();
+ } catch ( Exception e2 ) {
+ throw new ProviderInitializationException( "Provider was not set and the Factory was unable to load a provider from properties, nor could reflection find org.drools.KnowledgeSessionProviderImpl." );
+ }
+ }
+ }
}
Added: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/ProviderInitializationException.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/ProviderInitializationException.java (rev 0)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/ProviderInitializationException.java 2008-08-15 01:18:44 UTC (rev 21553)
@@ -0,0 +1,61 @@
+package org.drools;
+
+/*
+ * Copyright 2005 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * The factory was unable to initialize a provider.
+ *
+ * @see RuntimeException
+
+ */
+public class ProviderInitializationException extends RuntimeException {
+ /**
+ *
+ */
+ private static final long serialVersionUID = 400L;
+
+ /**
+ * @see java.lang.Exception#Exception()
+ */
+ public ProviderInitializationException() {
+ super();
+ }
+
+ /**
+ * @see java.lang.Exception#Exception(String message)
+ */
+ public ProviderInitializationException(final String message) {
+ super( message );
+ }
+
+ /**
+ * @see java.lang.Exception#Exception(String message, Throwable cause)
+ */
+ public ProviderInitializationException(final String message,
+ final Throwable cause) {
+ super( message,
+ cause );
+ }
+
+ /**
+ * @see java.lang.Exception#Exception(Throwable cause)
+ */
+ public ProviderInitializationException(final Throwable cause) {
+ super( cause );
+ }
+
+}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list