org.deduced.dynamic
Class AbstractDynamicExecutionCompiler

java.lang.Object
  extended by org.deduced.dynamic.AbstractDynamicExecutionCompiler
All Implemented Interfaces:
DynamicExecutionCompiler
Direct Known Subclasses:
ActionExecutionCompiler, DeductionRuleExecutionCompiler

public abstract class AbstractDynamicExecutionCompiler
extends java.lang.Object
implements DynamicExecutionCompiler

DynamicExecutionCompiler is used to compile the code in a compiled model and associate the java class that goes with it.

Author:
Steve McDuff

Nested Class Summary
static class AbstractDynamicExecutionCompiler.DynamicFileNameGenerator
          DynamicFileNameFinder
static interface AbstractDynamicExecutionCompiler.FileNameGenerator
          FileNameGenerator
static class AbstractDynamicExecutionCompiler.LocalClassLoader
          LocalClassLoader is used to load locally compiled files.
 
Field Summary
private static boolean AUTOMATICALLY_DELETING_GENERATED_CLASS_FILES
          option to automatically delete the compiled class file after they are loaded
private static boolean AUTOMATICALLY_DELETING_GENERATED_JAVA_FILES
          option to automatically delete generated JAVA files after compilation
private static boolean AUTOMATICALLY_DELETING_GENERATED_JAVA_FILES_IF_COMPILE_FAILS
          option to automatically delete java files when compilation fails
static java.lang.String CLASS_FILE_EXTENSION
          class file extension
private static int CLASS_NAME_STRING_TRIM_LENGTH
          The number of characters to use in a type name when generating dynamic code
private  java.util.IdentityHashMap<PropertyCollection<?,?>,DeductionRuleAnalyzer<java.lang.Object>> compiledModelToAnalyzerMap
          map that associates compiled models to the analyzer that executes them
private static java.lang.String[] COMPILER_ARGUMENTS
          additional compiler arguments
private static int COUNTER
          counter used to increment the dynamic class name
static java.lang.String DEDUCED_DYNAMIC_CLASS_NAME
          dynamic class name prefix
static java.lang.String DYNAMIC_CODE_PACKAGE_NAME
          default dynamic code package name
static AbstractDynamicExecutionCompiler.FileNameGenerator FILE_NAME_GENERATOR
          File name finder
private static int FIND_AVAILABLE_FILE_MAX_ATTEMPT_COUNT
          The maximum number of attempts at finding an available file
static java.lang.String JAVA_FILE_EXTENSION
          java file extension
private static AbstractDynamicExecutionCompiler.LocalClassLoader LOADER
          class loader used to load dynamically generated classes
private static java.util.logging.Logger LOGGER
          logger
private static java.lang.String OUTPUT_FOLDER_NAME
          dynamic code output folder name
 
Constructor Summary
AbstractDynamicExecutionCompiler()
           
 
Method Summary
 boolean attemptToLoadClassFromCache(java.lang.String packageName, java.lang.String folderName, java.lang.String className, CompileResult result)
          attempt To Load Class From Cache
abstract  void buildClassContent(PropertyCollection compiledModel, java.lang.String className, java.lang.String packageName, java.lang.StringBuilder builder)
          build a Class Content based on the compiled model
static void compileClass(java.lang.String folderName, java.lang.String className, java.lang.String javaFileName, java.lang.String classFileName, java.lang.String packageName, CompileResult result)
          compile a class
static void compileFile(java.io.File outFile, CompileResult result)
          compile a file
 DeductionRuleAnalyzer<java.lang.Object> createAnalyzerFromCompiledModel(PropertyCollection compiledModel)
          create Analyzer From Compiled Model
static void deleteAllGeneratedClassFiles()
          delete All Generated Class Files
static void deleteAllGeneratedFiles()
          delete All Generated Files
static void deleteAllGeneratedJavaFiles()
          delete All Generated Java Files
 void deleteClassCachedFiles(DeductionRuleAnalyzer<java.lang.Object> deleteAnalyzer)
          delete Class Cached Files if they exist
static void deleteFileAssertIfFailed(java.io.File fileToDelete)
          delete File Assert If Failed
static void deleteInnerClasses(java.lang.String folderName, java.lang.String className)
          delete all generated Inner Classes for a specific class
 void disableCompiledModel(PropertyCollection compiledModel)
          disable Compiled Model
static boolean doesClassExist(java.lang.String className)
          does Class Exist
static java.lang.String filterIllegalJavaCharacters(java.lang.String str)
          filter Illegal Java Characters from a string
 DeductionRuleAnalyzer<java.lang.Object> getAnalyzerFromCompiledModel(PropertyCollection compiledModel)
          (non-JSDoc)
 void getClassFromCompiledModel(PropertyCollection compiledModel, CompileResult result)
          get Class From Compiled Model
 java.lang.String getClassNameForCompiledModel(java.lang.String packageName, PropertyCollection compiledModel)
          get Class Name For Compiled Model
static java.lang.String getClassNameFromCompiledModel(PropertyCollection compiledModel)
          get Class Name From compiled Model
private static java.lang.String[] getCompileFileCompilerArgument(java.lang.String fileToCompilePath)
          get Compile File Compiler Argument
static java.lang.String[] getCompilerArguments()
          get Compiler Arguments
static AbstractDynamicExecutionCompiler.LocalClassLoader getDynamicClassLoader()
          get dynamic Class Loader
static java.util.List<java.io.File> getInnerClassList(java.lang.String folderName, java.lang.String className)
          get the list of inner class files generated by a class
static java.lang.String getNextAvailableClassName(java.lang.String packageName, java.lang.String className)
          get Next Available Class Name by starting with a class name and verifying if it exists.
static java.lang.String getNextAvailableDynamicFileName()
          get Next Available Dynamic File Name will test if a file name is already in use before returning it.
static java.lang.String getNextAvailableFileName(AbstractDynamicExecutionCompiler.FileNameGenerator generator)
          get Next Available File Name
static java.lang.String getNextDynamicFileName()
          get a dynamic file name used to store dynamic class names
static java.lang.String getOutputFolderName()
          get Output Folder Name
private  java.lang.String getOutputFolderName(java.lang.String packageName)
          get Output Folder Name
static boolean isAutomaticallyDeletingGeneratedClassFiles()
           
static boolean isAutomaticallyDeletingGeneratedJavaFiles()
           
static boolean isAutomaticallyDeletingGeneratedJavaFilesIfCompileFails()
           
static boolean isClassName(java.lang.String codeClassName)
          test if the code points to a class name or not.
static boolean isCompiledModelEnabled(PropertyCollection compiledModel)
          is Compiled Model Enabled
static boolean isValidMethodNameCharacter(char value)
          test if a character is valid for a java method name
static java.lang.Class<?> loadClassAndInnerClass(java.lang.String folderName, java.lang.String className, java.lang.String packageName)
          load a Class And all it's Inner Classes
private static void loadInnerClasses(java.lang.String folderName, java.lang.String className, java.lang.String packageName)
          load all the compiled inner classes in the class loader
 void logAnalyzerCreationResult(PropertyCollection compiledModel, CompileResult result)
          log Rule Creation Result
 void resetAnalyzerFromCompiledModel(PropertyCollection<?,?> compiledModel)
          (non-JSDoc)
static void setAutomaticallyDeletingGeneratedClassFiles(boolean setAutomaticallyDeletingGeneratedClassFiles)
           
static void setAutomaticallyDeletingGeneratedJavaFiles(boolean setAutomaticallyDeletingGeneratedJavaFiles)
           
static void setAutomaticallyDeletingGeneratedJavaFilesIfCompileFails(boolean setAutomaticallyDeletingGeneratedJavaFilesIfCompileFails)
           
static void setCompilerArguments(java.lang.String[] newCompilerArguments)
          set Compiler Arguments
static void setDynamicClassLoader(AbstractDynamicExecutionCompiler.LocalClassLoader loader)
          set Dynamic Class Loader
static void setOutputFolderName(java.lang.String newOutputFolderName)
          set Output Folder Name
 void validateCompilationModelForCompilation(PropertyCollection compiledModel, CompileResult result)
          validate Compilation Model For Compilation
static void validateCompilationModelIsEnabled(PropertyCollection compiledModel)
          validate Compilation Model Is Enabled
private  void validateCreatedAnalyzerClass(java.lang.Object newInstance)
          validate Created Analyzer Class
static java.io.File writeJavaClass(java.lang.String fileName, java.lang.String classContent)
          write a Java Class
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_NAME_STRING_TRIM_LENGTH

private static final int CLASS_NAME_STRING_TRIM_LENGTH
The number of characters to use in a type name when generating dynamic code

See Also:
Constant Field Values

DYNAMIC_CODE_PACKAGE_NAME

public static final java.lang.String DYNAMIC_CODE_PACKAGE_NAME
default dynamic code package name

See Also:
Constant Field Values

LOGGER

private static final java.util.logging.Logger LOGGER
logger


FIND_AVAILABLE_FILE_MAX_ATTEMPT_COUNT

private static final int FIND_AVAILABLE_FILE_MAX_ATTEMPT_COUNT
The maximum number of attempts at finding an available file

See Also:
Constant Field Values

DEDUCED_DYNAMIC_CLASS_NAME

public static final java.lang.String DEDUCED_DYNAMIC_CLASS_NAME
dynamic class name prefix

See Also:
Constant Field Values

CLASS_FILE_EXTENSION

public static final java.lang.String CLASS_FILE_EXTENSION
class file extension

See Also:
Constant Field Values

JAVA_FILE_EXTENSION

public static final java.lang.String JAVA_FILE_EXTENSION
java file extension

See Also:
Constant Field Values

LOADER

private static AbstractDynamicExecutionCompiler.LocalClassLoader LOADER
class loader used to load dynamically generated classes


COMPILER_ARGUMENTS

private static java.lang.String[] COMPILER_ARGUMENTS
additional compiler arguments


OUTPUT_FOLDER_NAME

private static java.lang.String OUTPUT_FOLDER_NAME
dynamic code output folder name


AUTOMATICALLY_DELETING_GENERATED_JAVA_FILES

private static boolean AUTOMATICALLY_DELETING_GENERATED_JAVA_FILES
option to automatically delete generated JAVA files after compilation


AUTOMATICALLY_DELETING_GENERATED_CLASS_FILES

private static boolean AUTOMATICALLY_DELETING_GENERATED_CLASS_FILES
option to automatically delete the compiled class file after they are loaded


AUTOMATICALLY_DELETING_GENERATED_JAVA_FILES_IF_COMPILE_FAILS

private static boolean AUTOMATICALLY_DELETING_GENERATED_JAVA_FILES_IF_COMPILE_FAILS
option to automatically delete java files when compilation fails


COUNTER

private static int COUNTER
counter used to increment the dynamic class name


compiledModelToAnalyzerMap

private java.util.IdentityHashMap<PropertyCollection<?,?>,DeductionRuleAnalyzer<java.lang.Object>> compiledModelToAnalyzerMap
map that associates compiled models to the analyzer that executes them


FILE_NAME_GENERATOR

public static final AbstractDynamicExecutionCompiler.FileNameGenerator FILE_NAME_GENERATOR
File name finder

Constructor Detail

AbstractDynamicExecutionCompiler

public AbstractDynamicExecutionCompiler()
Method Detail

getOutputFolderName

public static java.lang.String getOutputFolderName()
get Output Folder Name

Returns:
the output folder name

setOutputFolderName

public static void setOutputFolderName(java.lang.String newOutputFolderName)
set Output Folder Name

Parameters:
newOutputFolderName - the new output folder name

deleteAllGeneratedFiles

public static void deleteAllGeneratedFiles()
delete All Generated Files


deleteAllGeneratedJavaFiles

public static void deleteAllGeneratedJavaFiles()
delete All Generated Java Files


deleteAllGeneratedClassFiles

public static void deleteAllGeneratedClassFiles()
delete All Generated Class Files


getCompilerArguments

public static java.lang.String[] getCompilerArguments()
get Compiler Arguments

Returns:
the arguments passed to the java compiler

setCompilerArguments

public static void setCompilerArguments(java.lang.String[] newCompilerArguments)
set Compiler Arguments

Parameters:
newCompilerArguments - the arguments to pass to the java compiler

getNextDynamicFileName

public static java.lang.String getNextDynamicFileName()
get a dynamic file name used to store dynamic class names

Returns:
a unique dynamic file name

getDynamicClassLoader

public static AbstractDynamicExecutionCompiler.LocalClassLoader getDynamicClassLoader()
get dynamic Class Loader

Returns:
the class loader

setDynamicClassLoader

public static void setDynamicClassLoader(AbstractDynamicExecutionCompiler.LocalClassLoader loader)
set Dynamic Class Loader

Parameters:
loader - the new dynamic class loader

resetAnalyzerFromCompiledModel

public void resetAnalyzerFromCompiledModel(PropertyCollection<?,?> compiledModel)
(non-JSDoc)

Specified by:
resetAnalyzerFromCompiledModel in interface DynamicExecutionCompiler
Parameters:
compiledModel - the compiled model to reset
See Also:
DynamicExecutionCompiler.resetAnalyzerFromCompiledModel(org.deduced.PropertyCollection)

deleteClassCachedFiles

public void deleteClassCachedFiles(DeductionRuleAnalyzer<java.lang.Object> deleteAnalyzer)
delete Class Cached Files if they exist

Parameters:
deleteAnalyzer - the analyzer to delete

getAnalyzerFromCompiledModel

public DeductionRuleAnalyzer<java.lang.Object> getAnalyzerFromCompiledModel(PropertyCollection compiledModel)
                                                                     throws java.lang.IllegalStateException,
                                                                            CompilationFailedException
(non-JSDoc)

Specified by:
getAnalyzerFromCompiledModel in interface DynamicExecutionCompiler
Parameters:
compiledModel - the compiled model
Returns:
the execution analyzer
Throws:
java.lang.IllegalStateException - if the compilation model isn't enabled.
CompilationFailedException - if the compilation operation fails
See Also:
DynamicExecutionCompiler.getAnalyzerFromCompiledModel(org.deduced.PropertyCollection)

createAnalyzerFromCompiledModel

public DeductionRuleAnalyzer<java.lang.Object> createAnalyzerFromCompiledModel(PropertyCollection compiledModel)
                                                                        throws CompilationFailedException,
                                                                               java.lang.IllegalStateException
create Analyzer From Compiled Model

Parameters:
compiledModel - the compiled model to compile
Returns:
the resulting analyzer
Throws:
CompilationFailedException - if the compilation operation fails.
java.lang.IllegalStateException - if the compilation model is disabled

validateCompilationModelIsEnabled

public static void validateCompilationModelIsEnabled(PropertyCollection compiledModel)
                                              throws java.lang.IllegalStateException
validate Compilation Model Is Enabled

Parameters:
compiledModel - the compiled model to validate
Throws:
java.lang.IllegalStateException - if the model isn't enabled.

validateCompilationModelForCompilation

public void validateCompilationModelForCompilation(PropertyCollection compiledModel,
                                                   CompileResult result)
                                            throws java.lang.IllegalArgumentException
validate Compilation Model For Compilation

Parameters:
compiledModel - the compiled model
result - the compilation result to fill
Throws:
java.lang.IllegalArgumentException - when the compilation model is invalid

isCompiledModelEnabled

public static boolean isCompiledModelEnabled(PropertyCollection compiledModel)
is Compiled Model Enabled

Parameters:
compiledModel - the compiled model
Returns:
true if the model is enabled

getClassFromCompiledModel

public void getClassFromCompiledModel(PropertyCollection compiledModel,
                                      CompileResult result)
                               throws CompilationFailedException,
                                      java.io.IOException
get Class From Compiled Model

Parameters:
compiledModel - the compiled model
result - the compilation result
Throws:
CompilationFailedException - if the class compilation failed
java.io.IOException - if the folder creation failed

getOutputFolderName

private java.lang.String getOutputFolderName(java.lang.String packageName)
get Output Folder Name

Parameters:
packageName - the package name to output to
Returns:
the full folder name

attemptToLoadClassFromCache

public boolean attemptToLoadClassFromCache(java.lang.String packageName,
                                           java.lang.String folderName,
                                           java.lang.String className,
                                           CompileResult result)
attempt To Load Class From Cache

Parameters:
packageName - package name
folderName - folder where the class is stored
className - name of the class
result - class compilation result
Returns:
true if cache loading was successful

getClassNameForCompiledModel

public java.lang.String getClassNameForCompiledModel(java.lang.String packageName,
                                                     PropertyCollection compiledModel)
get Class Name For Compiled Model

Parameters:
packageName - the package name storing the class
compiledModel - the compiled model to use
Returns:
the class name associated with the model

filterIllegalJavaCharacters

public static java.lang.String filterIllegalJavaCharacters(java.lang.String str)
filter Illegal Java Characters from a string

Parameters:
str - the string to filter
Returns:
the filtered value

getNextAvailableClassName

public static java.lang.String getNextAvailableClassName(java.lang.String packageName,
                                                         java.lang.String className)
get Next Available Class Name by starting with a class name and verifying if it exists. If it does exist, a new unique class name is generated.

Parameters:
packageName - package name
className - the class name to verify
Returns:
the new class name

doesClassExist

public static boolean doesClassExist(java.lang.String className)
does Class Exist

Parameters:
className - the class name to find
Returns:
true if the class exists

getClassNameFromCompiledModel

public static java.lang.String getClassNameFromCompiledModel(PropertyCollection compiledModel)
get Class Name From compiled Model

Parameters:
compiledModel - the compiled model
Returns:
the java class name to use

buildClassContent

public abstract void buildClassContent(PropertyCollection compiledModel,
                                       java.lang.String className,
                                       java.lang.String packageName,
                                       java.lang.StringBuilder builder)
build a Class Content based on the compiled model

Parameters:
compiledModel - the compiled model
className - the class name to use
packageName - the package name to use
builder - the string builder containing the class content

isClassName

public static boolean isClassName(java.lang.String codeClassName)
test if the code points to a class name or not.

Parameters:
codeClassName - the class name to test
Returns:
true if the code points to a class name.

compileClass

public static void compileClass(java.lang.String folderName,
                                java.lang.String className,
                                java.lang.String javaFileName,
                                java.lang.String classFileName,
                                java.lang.String packageName,
                                CompileResult result)
                         throws CompilationFailedException
compile a class

Parameters:
folderName - where the class and java files are stored
className - the name of the class to compile
javaFileName - name of the java file to use
classFileName - name of the class file to use
packageName - name of the package for the class
result - the compilation result to fill while compiling
Throws:
CompilationFailedException - any error detected while compiling the class

loadClassAndInnerClass

public static java.lang.Class<?> loadClassAndInnerClass(java.lang.String folderName,
                                                        java.lang.String className,
                                                        java.lang.String packageName)
                                                 throws java.lang.Exception
load a Class And all it's Inner Classes

Parameters:
folderName - the folder where the class is stored
className - the class name to load
packageName - the package name of the class
Returns:
the main loaded class
Throws:
java.lang.Exception - if any error occurs.

loadInnerClasses

private static void loadInnerClasses(java.lang.String folderName,
                                     java.lang.String className,
                                     java.lang.String packageName)
                              throws java.lang.Exception
load all the compiled inner classes in the class loader

Parameters:
folderName - folder where the classes are stored
className - the class name that was generated
packageName - the package used to generate the class
Throws:
java.lang.Exception - any unexpected

compileFile

public static void compileFile(java.io.File outFile,
                               CompileResult result)
                        throws CompilationFailedException
compile a file

Parameters:
outFile - file to compile
result - the compilation result to fill
Throws:
CompilationFailedException - any exception generated while running the compiler

getCompileFileCompilerArgument

private static java.lang.String[] getCompileFileCompilerArgument(java.lang.String fileToCompilePath)
get Compile File Compiler Argument

Parameters:
fileToCompilePath - the file to compile
Returns:
the complete compiler arguments

writeJavaClass

public static java.io.File writeJavaClass(java.lang.String fileName,
                                          java.lang.String classContent)
                                   throws java.io.IOException
write a Java Class

Parameters:
fileName - the file name to use
classContent - the class content
Returns:
the written file
Throws:
java.io.IOException - if writing the file fails

deleteInnerClasses

public static void deleteInnerClasses(java.lang.String folderName,
                                      java.lang.String className)
delete all generated Inner Classes for a specific class

Parameters:
folderName - folder storing the class
className - the class name

deleteFileAssertIfFailed

public static void deleteFileAssertIfFailed(java.io.File fileToDelete)
delete File Assert If Failed

Parameters:
fileToDelete - file to delete

getInnerClassList

public static java.util.List<java.io.File> getInnerClassList(java.lang.String folderName,
                                                             java.lang.String className)
get the list of inner class files generated by a class

Parameters:
folderName - name of the folder storing the classes
className - the class name
Returns:
the list of inner class files

isValidMethodNameCharacter

public static boolean isValidMethodNameCharacter(char value)
test if a character is valid for a java method name

Parameters:
value - the character to test
Returns:
true if the character is valid

getNextAvailableFileName

public static java.lang.String getNextAvailableFileName(AbstractDynamicExecutionCompiler.FileNameGenerator generator)
get Next Available File Name

Parameters:
generator - name finder
Returns:
the next available file name using the specified file name generator

getNextAvailableDynamicFileName

public static java.lang.String getNextAvailableDynamicFileName()
get Next Available Dynamic File Name will test if a file name is already in use before returning it.

Returns:
the next available dynamic file name

setAutomaticallyDeletingGeneratedJavaFiles

public static void setAutomaticallyDeletingGeneratedJavaFiles(boolean setAutomaticallyDeletingGeneratedJavaFiles)
Parameters:
setAutomaticallyDeletingGeneratedJavaFiles - the Automatically Deleting Generated Java Files flag

isAutomaticallyDeletingGeneratedJavaFiles

public static boolean isAutomaticallyDeletingGeneratedJavaFiles()
Returns:
the Automatically Deleting Generated Java Files flag

setAutomaticallyDeletingGeneratedClassFiles

public static void setAutomaticallyDeletingGeneratedClassFiles(boolean setAutomaticallyDeletingGeneratedClassFiles)
Parameters:
setAutomaticallyDeletingGeneratedClassFiles - the Automatically Deleting Generated Class Files flag

isAutomaticallyDeletingGeneratedClassFiles

public static boolean isAutomaticallyDeletingGeneratedClassFiles()
Returns:
the Automatically Deleting Generated Class Files flag

setAutomaticallyDeletingGeneratedJavaFilesIfCompileFails

public static void setAutomaticallyDeletingGeneratedJavaFilesIfCompileFails(boolean setAutomaticallyDeletingGeneratedJavaFilesIfCompileFails)
Parameters:
setAutomaticallyDeletingGeneratedJavaFilesIfCompileFails - the Automatically Deleting Generated Java Files if compile fails flag

isAutomaticallyDeletingGeneratedJavaFilesIfCompileFails

public static boolean isAutomaticallyDeletingGeneratedJavaFilesIfCompileFails()
Returns:
the Automatically Deleting Generated Java Files if compile fails flag

logAnalyzerCreationResult

public void logAnalyzerCreationResult(PropertyCollection compiledModel,
                                      CompileResult result)
log Rule Creation Result

Parameters:
compiledModel - the rule we tried to create
result - the compilation result

disableCompiledModel

public void disableCompiledModel(PropertyCollection compiledModel)
disable Compiled Model

Parameters:
compiledModel - the model to disable

validateCreatedAnalyzerClass

private void validateCreatedAnalyzerClass(java.lang.Object newInstance)
                                   throws java.lang.IllegalArgumentException
validate Created Analyzer Class

Parameters:
newInstance - the potential analyzer
Throws:
java.lang.IllegalArgumentException - if the object isn't an instance of DeductionRuleAnalyzer