From: <jbo...@li...> - 2006-04-20 07:15:13
|
Author: mic...@jb... Date: 2006-04-20 03:15:02 -0400 (Thu, 20 Apr 2006) New Revision: 3839 Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderConfigurationTest.java Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java Log: added drools.compiler system property option Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java 2006-04-20 06:25:34 UTC (rev 3838) +++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java 2006-04-20 07:15:02 UTC (rev 3839) @@ -19,12 +19,25 @@ import org.apache.commons.jci.compilers.JavaCompilerFactory; import org.drools.RuntimeDroolsException; +/** + * This class configures the package compiler. + * There are options to use various flavours of runtime compilers. + * Apache JCI is used as the interface to all the runtime compilers. + * You may also use this class to override the class loader defaults that are otherwise used. + * Normally you will not need to look at this class, unless you want to override the defaults. + * + * You can also use the system property "drools.compiler" to set the desired compiler. + * The valid values are "ECLIPSE" and "JANINO" only. + */ public class PackageBuilderConfiguration { - public static final int ECLIPSE = 0; - public static final int JANINO = 1; + public static final int ECLIPSE = JavaCompilerFactory.ECLIPSE; + public static final int JANINO = JavaCompilerFactory.JANINO; + + /** This will be only setup once. It tries to look for a system property */ + private static final int CONFIGURED_COMPILER = getDefaultCompiler(); + + private int compiler = CONFIGURED_COMPILER; - private int compiler = JavaCompilerFactory.ECLIPSE; - private ClassLoader classLoader; public PackageBuilderConfiguration() { @@ -39,6 +52,10 @@ return this.compiler; } + /** + * Set the compiler to be used when building the rules semantic code blocks. + * This overrides the default, and even what was set as a system property. + */ public void setCompiler(int compiler) { switch ( compiler ) { case PackageBuilderConfiguration.ECLIPSE : @@ -56,9 +73,32 @@ return this.classLoader; } + /** Use this to override the classloader that will be used for the rules. */ public void setClassLoader(ClassLoader classLoader) { if ( classLoader != null ) { this.classLoader = classLoader; } } + + /** + * This will attempt to read the System property to work out what default to set. + * This should only be done once when the class is loaded. After that point, you will have + * to programmatically override it. + */ + static int getDefaultCompiler() { + try { + String prop = System.getProperty( "drools.compiler", "ECLIPSE" ); + if (prop.equals( "ECLIPSE".intern() )) { + return ECLIPSE; + } else if (prop.equals( "JANINO" )) { + return JANINO; + } else { + System.err.println("Drools config: unable to use the drools.compiler property. Using default. It was set to:" + prop); + return ECLIPSE; + } + } catch (SecurityException e) { + System.err.println("Drools config: unable to read the drools.compiler property. Using default."); + return ECLIPSE; + } + } } \ No newline at end of file Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderConfigurationTest.java =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderConfigurationTest.java 2006-04-20 06:25:34 UTC (rev 3838) +++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderConfigurationTest.java 2006-04-20 07:15:02 UTC (rev 3839) @@ -0,0 +1,32 @@ +package org.drools.compiler; + +import junit.framework.TestCase; + +public class PackageBuilderConfigurationTest extends TestCase { + + public void testSystemProperty() { + PackageBuilderConfiguration cfg = new PackageBuilderConfiguration(); + assertEquals(cfg.getCompiler(), PackageBuilderConfiguration.getDefaultCompiler()); + + System.setProperty( "drools.compiler", "JANINO" ); + assertEquals(PackageBuilderConfiguration.JANINO, PackageBuilderConfiguration.getDefaultCompiler()); + + PackageBuilderConfiguration cfg2 = new PackageBuilderConfiguration(); + assertEquals(cfg.getCompiler(), cfg2.getCompiler()); + + System.setProperty( "drools.compiler", "ECLIPSE" ); + assertEquals(PackageBuilderConfiguration.ECLIPSE, PackageBuilderConfiguration.getDefaultCompiler()); + + cfg2.setCompiler( PackageBuilderConfiguration.ECLIPSE ); + assertEquals(PackageBuilderConfiguration.ECLIPSE, cfg2.getCompiler()); + + cfg2.setCompiler( PackageBuilderConfiguration.JANINO ); + assertEquals(PackageBuilderConfiguration.JANINO, cfg2.getCompiler()); + + PackageBuilderConfiguration cfg3 = new PackageBuilderConfiguration(); + + assertEquals(cfg.getCompiler(), cfg3.getCompiler()); + + } + +} Property changes on: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderConfigurationTest.java ___________________________________________________________________ Name: svn:eol-style + native |