Hi all,
I'm working on drools spring and I've come upon some issues in
SessionConfiguration that are stopping/confusing me.
Here are my lists of doubts/concerns about SessionConfiguration:
1) It stores 2 properties using Option instances variables and the rest
inside a ChainedProperties class.
2) There is no way no way to know what props are in the
ChainedProperties from outside the SessionConfiguration.
3) When the SessionConfiguration is immutable, it's still possible to
add more Properties using the addProperties method.
4) The ChainedProperties class is add only, in other words it isn't
possible change props values. Moreover it's also possible to have two
props with different values.
I'm attaching a patch that fix some these issues. I've updated
SessionConfiguration (g|s)etProperty(String name) method and added the
ability to ChainedProperties update existing props.
WDYT?
--
Bauna
Index: src/main/java/org/drools/SessionConfiguration.java
===================================================================
--- src/main/java/org/drools/SessionConfiguration.java (revision 30510)
+++ src/main/java/org/drools/SessionConfiguration.java (working copy)
@@ -159,6 +159,8 @@
} else if ( name.equals( "drools.clockType" ) ) {
setClockType( ClockType.resolveClockType( StringUtils.isEmpty(
value ) ? "realtime" : value ) );
}
+
+ chainedProperties.setProperty(name, value);
}
public String getProperty(String name) {
@@ -173,7 +175,7 @@
return this.clockType.toExternalForm();
}
- return null;
+ return chainedProperties.getProperty(name, null);
}
/**
Index: src/main/java/org/drools/util/ChainedProperties.java
===================================================================
--- src/main/java/org/drools/util/ChainedProperties.java (revision 30510)
+++ src/main/java/org/drools/util/ChainedProperties.java (working copy)
@@ -17,11 +17,9 @@
import java.util.Map;
import java.util.Properties;
-public class ChainedProperties
- implements
- Externalizable {
- private List props;
- private List defaultProps;
+public class ChainedProperties implements Externalizable {
+ private List<Properties> props;
+ private List<Properties> defaultProps;
public ChainedProperties() {
@@ -49,8 +47,8 @@
}
}
- this.props = new ArrayList();
- this.defaultProps = new ArrayList();
+ this.props = new ArrayList<Properties>();
+ this.defaultProps = new ArrayList<Properties>();
// Properties added in precedence order
@@ -132,9 +130,10 @@
}
}
- public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
- props = (List)in.readObject();
- defaultProps = (List)in.readObject();
+ @SuppressWarnings("unchecked")
+ public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
+ props = (List<Properties>)in.readObject();
+ defaultProps = (List<Properties>)in.readObject();
}
public void writeExternal(ObjectOutput out) throws IOException {
@@ -142,15 +141,14 @@
out.writeObject(defaultProps);
}
- private Enumeration getResources(String name,
+ private Enumeration<URL> getResources(String name,
ClassLoader classLoader) {
- Enumeration enumeration = null;
try {
- enumeration = classLoader.getResources( name );
+ return classLoader.getResources( name );
} catch ( IOException e ) {
e.printStackTrace();
+ return null;
}
- return enumeration;
}
public void addProperties(Properties properties) {
@@ -160,38 +158,40 @@
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;
- }
- }
- }
+ for (Iterator<Properties> it = this.props.iterator(); it.hasNext() &&
value == null;) {
+ value = it.next().getProperty(key);
+ }
+ for (Iterator<Properties> it = this.defaultProps.iterator();
it.hasNext() && value == null;) {
+ value = it.next().getProperty(key);
+ }
return (value != null) ? value : defaultValue;
}
- public void mapStartsWith(Map map,
+ public void setProperty(String key, String value) {
+ for (Properties prop : props) {
+ if (prop.containsKey(key)) {
+ prop.setProperty(key, value);
+ }
+ }
+ }
+
+ public String getProperty(String key) {
+ return getProperty(key, null);
+ }
+
+ public void mapStartsWith(Map<String, Object> map,
String startsWith,
boolean includeSubProperties) {
- for ( Iterator it = this.props.iterator(); it.hasNext(); ) {
- Properties props = (Properties) it.next();
+ for ( Iterator<Properties> it = this.props.iterator(); it.hasNext(); )
{
+ Properties props = it.next();
mapStartsWith( map,
props,
startsWith,
includeSubProperties );
}
- for ( Iterator it = this.defaultProps.iterator(); it.hasNext(); ) {
- Properties props = (Properties) it.next();
+ for ( Iterator<Properties> it = this.defaultProps.iterator();
it.hasNext(); ) {
+ Properties props = it.next();
mapStartsWith( map,
props,
startsWith,
@@ -199,13 +199,13 @@
}
}
- private void mapStartsWith(Map map,
+ private void mapStartsWith(Map<String, Object> map,
Properties properties,
String startsWith,
boolean includeSubProperties) {
- Enumeration enumeration = properties.propertyNames();
- while ( enumeration.hasMoreElements() ) {
- String key = (String) enumeration.nextElement();
+
+ for (Enumeration<?> enumeration = properties.propertyNames();
enumeration.hasMoreElements(); ) {
+ String key = enumeration.nextElement().toString();
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
@@ -221,24 +221,24 @@
}
}
- private void loadProperties(Enumeration enumeration,
- List chain) {
+ private void loadProperties(Enumeration<URL> enumeration,
+ List<Properties> chain) {
if ( enumeration == null ) {
return;
}
while ( enumeration.hasMoreElements() ) {
- URL url = (URL) enumeration.nextElement();
+ URL url = enumeration.nextElement();
loadProperties( url,
chain );
}
}
private void loadProperties(String fileName,
- List chain) {
+ List<Properties> chain) {
if ( fileName != null ) {
File file = new File( fileName );
- if ( file != null && file.exists() ) {
+ if ( file.exists() ) {
try {
loadProperties( file.toURL(),
chain );
@@ -252,7 +252,7 @@
}
private void loadProperties(URL confURL,
- List chain) {
+ List<Properties> chain) {
if ( confURL == null ) {
return;
}
@@ -261,6 +261,7 @@
properties.load( confURL.openStream() );
chain.add( properties );
} catch ( IOException e ) {
+ e.printStackTrace();
//throw new IllegalArgumentException( "Invalid URL to properties
file '" + confURL.toExternalForm() + "'" );
}
}
_______________________________________________
rules-dev mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/rules-dev