Skip to content

Commit 36c4295

Browse files
committed
Rework 2.0 as generic Box module
1 parent f1dcf97 commit 36c4295

File tree

6 files changed

+82
-131
lines changed

6 files changed

+82
-131
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ workbench/*
1717
build/*
1818

1919
# Engines
20-
.engine/**
20+
.engine/**
21+
.project

.module.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
project.name=cbjavaloader
2-
project.version=1.7.0
2+
project.version=2.0.0
33
module.name=cbjavaloader

box.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"cbjavaloader Builder",
3-
"version":"1.7.0",
3+
"version":"2.0.0",
44
"slug":"cbjavaloader-shell",
55
"private":false,
66
"defaultPort":0,
@@ -12,9 +12,9 @@
1212
"testbox":"^2.6.0+156"
1313
},
1414
"installPaths":{
15-
"workbench":"workbench/",
16-
"coldbox":"coldbox/",
17-
"testbox":"testbox/"
15+
"workbench":"workbench",
16+
"coldbox":"coldbox",
17+
"testbox":"testbox"
1818
},
1919
"testbox":{
2020
"runner":"http://localhost:49616"

modules/cbjavaloader/ModuleConfig.cfc

Lines changed: 49 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,75 @@ component {
1111
this.webURL = "https://www.ortussolutions.com";
1212
this.description = "A JavaLoader Module for ColdBox";
1313
this.version = "@build.version@[email protected]@";
14-
// If true, looks for views in the parent first, if not found, then in the module. Else vice-versa
15-
this.viewParentLookup = true;
16-
// If true, looks for layouts in the parent first, if not found, then in module. Else vice-versa
17-
this.layoutParentLookup = true;
18-
// CF Mapping
1914
this.cfmapping = "cbjavaloader";
2015

2116
/**
2217
* Configure module
2318
*/
2419
function configure(){
20+
21+
settings = {
22+
// The array paths to load
23+
loadPaths = [],
24+
// Load ColdFusion classes with loader
25+
loadColdFusionClassPath = false,
26+
// Attach a custom class loader as a parent
27+
parentClassLoader = "",
28+
// Directories that contain Java source code that are to be dynamically compiled
29+
sourceDirectories = [],
30+
// the directory to build the .jar file for dynamic compilation in, defaults to ./tmp
31+
compileDirectory = variables.modulePath & "models/javaloader/tmp",
32+
// Whether or not the source is trusted, i.e. it is going to change? Defaults to false, so changes will be recompiled and loaded
33+
trustedSource = false
34+
};
35+
2536
// Register Custom DSL, don't map it because it is too late, mapping DSLs are only good by the parent app
26-
controller.getWireBox()
37+
wireBox
2738
.registerDSL( namespace="javaloader", path="#moduleMapping#.models.JavaLoaderDSL" );
2839
}
2940

3041
/**
3142
* Fired when the module is registered and activated.
3243
*/
3344
function onLoad(){
34-
var settings = controller.getConfigSettings();
35-
// parse parent settings
36-
parseParentSettings();
3745

3846
// Bind Core JavaLoader
3947
binder.map( "jl@cbjavaloader" )
40-
.to( "#moduleMapping#.models.javaloader.JavaLoader" )
41-
.initArg( name="loadPaths", value=settings.modules.cbjavaloader.settings.loadPaths )
42-
.initArg( name="loadColdFusionClassPath", value=settings.modules.cbjavaloader.settings.loadColdFusionClassPath )
43-
.initArg( name="parentClassLoader", value=settings.modules.cbjavaloader.settings.parentClassLoader )
44-
.initArg( name="sourceDirectories", value=settings.modules.cbjavaloader.settings.sourceDirectories )
45-
.initArg( name="compileDirectory", value=settings.modules.cbjavaloader.settings.compileDirectory )
46-
.initArg( name="trustedSource", value=settings.modules.cbjavaloader.settings.trustedSource );
48+
.to( "#moduleMapping#.models.javaloader.JavaLoader" );
49+
50+
// Duplicating so our final change won't affect the main module settings
51+
var finalSettings = duplicate( settings );
4752

48-
// Load JavaLoader and class loading
49-
wirebox.getInstance( "loader@cbjavaloader" ).setup();
50-
}
53+
// Start with empty array
54+
finalSettings.loadPaths = [];
5155

52-
/**
53-
* Fired when the module is unregistered and unloaded
54-
*/
55-
function onUnload(){
56+
// Grab module settings
57+
var moduleSettingsLoadPath = settings.loadPaths;
58+
59+
// Force array
60+
if( isSimpleValue( moduleSettingsLoadPath ) ) {
61+
moduleSettingsLoadPath = moduleSettingsLoadPath.listToArray();
62+
}
63+
64+
// Loop over settings, adding files and expanding directories
65+
for( var thisLocation in moduleSettingsLoadPath ){
66+
if( directoryExists( thisLocation ) ) {
67+
finalSettings.loadPaths.addAll( getJars( thisLocation ) );
68+
} else if ( fileExists( thisLocation ) ) {
69+
finalSettings.loadPaths.append( thisLocation );
70+
} else {
71+
throw( "Javalaoder cannot load #thisLocation# as it is not a valid path or file" );
72+
}
73+
}
5674

75+
// Dynamic Proxy
76+
arrayPrepend(
77+
finalSettings.loadPaths,
78+
variables.modulePath & "/models/javaloader/support/cfcdynamicproxy/lib/cfcdynamicproxy.jar"
79+
);
80+
81+
// Load JavaLoader and class loading
82+
wirebox.getInstance( "loader@cbjavaloader" ).setup( finalSettings );
5783
}
5884

5985
/**
@@ -73,73 +99,4 @@ component {
7399
);
74100
}
75101

76-
/**
77-
* parse parent settings
78-
*/
79-
private function parseParentSettings(){
80-
var oConfig = controller.getSetting( "ColdBoxConfig" );
81-
var configStruct = controller.getConfigSettings();
82-
var javaLoaderDSL = oConfig.getPropertyMixin( "javaloader", "variables", structnew() );
83-
84-
// Default Configurations
85-
configStruct.modules.cbjavaloader.settings = {
86-
// The array paths to load
87-
loadPaths = variables.modulePath & "lib",
88-
// Load ColdFusion classes with loader
89-
loadColdFusionClassPath = false,
90-
// Attach a custom class loader as a parent
91-
parentClassLoader = "",
92-
// Directories that contain Java source code that are to be dynamically compiled
93-
sourceDirectories = [],
94-
// the directory to build the .jar file for dynamic compilation in, defaults to ./tmp
95-
compileDirectory = variables.modulePath & "model/javaloader/tmp",
96-
// Whether or not the source is trusted, i.e. it is going to change? Defaults to false, so changes will be recompiled and loaded
97-
trustedSource = false
98-
};
99-
100-
// Default load paths, empty array
101-
if( !structKeyExists( javaLoaderDSL, "loadPaths" ) ){
102-
javaLoaderDSL.loadPaths = [];
103-
}
104-
105-
// Array of locations
106-
if( isArray( javaLoaderDSL.loadPaths ) ){
107-
var aJarPaths = [];
108-
for( var thisLocation in javaLoaderDSL.loadPaths ){
109-
if( directoryExists( thisLocation ) ) {
110-
aJarPaths.addAll( getJars( thisLocation ) );
111-
} else if ( !fileExists( thisLocation ) ) {
112-
throw( "Cannot load #thisLocation# as it is not a valid path or file" );
113-
}
114-
}
115-
javaLoaderDSL.loadPaths = aJarPaths;
116-
}
117-
118-
// Single directory? Get all Jars in it
119-
if( isSimpleValue( javaLoaderDSL.loadPaths ) and directoryExists( javaLoaderDSL.loadPaths ) ){
120-
javaLoaderDSL.loadPaths = getJars( javaLoaderDSL.loadPaths );
121-
}
122-
123-
// Single Jar?
124-
if( isSimpleValue( javaLoaderDSL.loadPaths ) and fileExists( javaLoaderDSL.loadPaths ) ){
125-
javaLoaderDSL.loadPaths = [ javaLoaderDSL.loadPaths ];
126-
}
127-
128-
// If simple value and no length
129-
if( isSimpleValue( javaLoaderDSL.loadPaths ) and !len( javaLoaderDSL.loadPaths ) ){
130-
javaLoaderDSL.loadPaths = [];
131-
}
132-
133-
// Now that we have figured out the user's settings, let's incorporate ours
134-
135-
// Dynamic Proxy
136-
arrayPrepend(
137-
javaLoaderDSL.loadPaths,
138-
variables.modulePath & "/models/javaloader/support/cfcdynamicproxy/lib/cfcdynamicproxy.jar"
139-
);
140-
141-
// incorporate settings
142-
structAppend( configStruct.modules.cbjavaloader.settings, javaLoaderDSL, true );
143-
}
144-
145102
}

modules/cbjavaloader/models/Loader.cfc

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
* Loads External Java Classes, while providing access to ColdFusion classes by interfacing with JavaLoader
66
* it Stores a reference in server scope to avoid leakage.
77
*/
8-
component accessors="true" singleton{
9-
10-
/**
11-
* Module Settings
12-
*/
13-
property name="moduleSettings" inject="coldbox:moduleSettings:cbjavaloader";
8+
component accessors="true" singleton {
9+
property name="wirebox" inject="wirebox";
1410

1511
/**
1612
* ID key saved in server scope to avoid leakage
@@ -19,34 +15,28 @@ component accessors="true" singleton{
1915

2016
/**
2117
* Constructor
22-
* @coldbox.inject coldbox
2318
*/
24-
function init( required coldbox ){
19+
function init(){
2520
// setup a static ID key according to coldbox app
26-
variables.staticIDKey = "cbox-javaloader-#arguments.coldbox.getAppHash()#";
27-
// setup wirebox reference
28-
variables.wirebox = arguments.coldbox.getWireBox();
21+
variables.staticIDKey = "cbox-javaloader-#hash( getCurrentTemplatePath() )#";
2922
return this;
3023
}
3124

3225
/**
3326
* Setup class loading
34-
* @loadPaths.hint An array of directories to classload
35-
* @loadColdFusionClassPath.hint Loads the CF library class loader as well
36-
* @parentClassLoader.hint The parent java.lang.ClassLoader to attach the network class loader to.
3727
*/
38-
function setup(){
28+
function setup( required struct moduleSettings ){
3929
// verify we have it loaded
4030
if( not isJavaLoaderInScope() ){
4131
lock name="#variables.staticIDKey#" throwontimeout="true" timeout="30" type="exclusive"{
4232
if( not isJavaLoaderInScope() ){
43-
setJavaLoaderInScope( variables.wirebox.getInstance( "jl@cbjavaloader" ) );
33+
setJavaLoaderInScope( variables.wirebox.getInstance( name="jl@cbjavaloader", initArguments=moduleSettings ) );
4434
}
4535
}
4636
} else {
4737
// reconfigure it, maybe settings changed
4838
lock name="#variables.staticIDKey#" throwontimeout="true" timeout="30" type="exclusive"{
49-
getJavaLoaderFromScope().init( argumentCollection=variables.moduleSettings );
39+
getJavaLoaderFromScope().init( argumentCollection=moduleSettings );
5040
}
5141
}
5242
}

readme.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Just drop into your **modules** folder or use the box-cli to install
2626

2727
`box install cbjavaloader`
2828

29-
The module has a default folder called `lib` where any jars you drop there will be class loaded automatically. However, we recommed using the `loadpaths` setting for selecting an array of locations to class load, so when the module updates you won't loose those files.
29+
The module has a default folder called `lib` where any jars you drop there will be class loaded automatically. However, we recommend using the `loadpaths` setting for selecting an array of locations to class load, so when the module updates you won't lose those files.
3030

3131
## Models
3232
The module registers the following mapping in WireBox: `loader@cbjavaloader`. Which is the class you will use to class load, append paths and much more. Check out the included API Docs for much more information. The main methods of importance of the java loader are:
@@ -46,24 +46,27 @@ property name="buffer" inject="javaloader:org.class.path.StringBuffer";
4646

4747
## Settings
4848

49-
Here are the module settings you can place in your `ColdBox.cfc` under an `javaloader` structure:
49+
Here are the module settings you can place in your `ColdBox.cfc` under an `moduleSettings.cbJavaLoader` structure:
5050

5151
```js
52-
// JavaLoader settings
53-
javaloader = {
54-
// A single path, and array of paths or a single Jar
55-
loadPaths = [],
56-
// Load ColdFusion classes with loader
57-
loadColdFusionClassPath = false,
58-
// Attach a custom class loader as a parent
59-
parentClassLoader = "",
60-
// Directories that contain Java source code that are to be dynamically compiled
61-
sourceDirectories = [],
62-
// the directory to build the .jar file for dynamic compilation in, defaults to ./tmp
63-
compileDirectory = getDirectoryFromPath( getCurrentTemplatePath() ) & "model/javaloader/tmp",
64-
// Whether or not the source is trusted, i.e. it is going to change? Defaults to false, so changes will be recompiled and loaded
65-
trustedSource = false
52+
53+
moduleSettings = {
54+
cbJavaLoader = {
55+
// A single path, and array of paths or a single Jar
56+
loadPaths = [],
57+
// Load ColdFusion classes with loader
58+
loadColdFusionClassPath = false,
59+
// Attach a custom class loader as a parent
60+
parentClassLoader = "",
61+
// Directories that contain Java source code that are to be dynamically compiled
62+
sourceDirectories = [],
63+
// the directory to build the .jar file for dynamic compilation in, defaults to ./tmp
64+
compileDirectory = "models/javaloader/tmp",
65+
// Whether or not the source is trusted, i.e. it is going to change? Defaults to false, so changes will be recompiled and loaded
66+
trustedSource = false
67+
}
6668
};
69+
6770
```
6871

6972
Below is a simple example:

0 commit comments

Comments
 (0)