JavaLoader Factory CFC, for ColdSpring, on GitHub

119 views
Skip to first unread message

Jamie Krug

unread,
Mar 31, 2011, 3:42:38 PM3/31/11
to javaloa...@googlegroups.com
I put together a little JavaLoaderFactory.cfc to simplify instantiating JavaLoader in the server scope and making it available as a ColdSpring bean:

At its most basic/default usage it provides a factory method for ColdSpring, which accepts arguments identical to JavaLoader:init(). It creates (if needed) and returns a JavaLoader instance with a sensible server scope key that is unique to the instance (a hash of the JavaLoader:init() arguments).

The factory method has one additional optional argument, loadRelativePaths, which allows you to specify an array of _relative_ loadPaths (so you needn't expand them first).

You also have the option of specifying the server scope key, though I would not recommend it.

Check out the README file for a few more details and quick examples.

Please let me know if you find this at all useful or have any feedback.

Thanks,
Jamie

Mark Mandel

unread,
Mar 31, 2011, 5:57:50 PM3/31/11
to javaloa...@googlegroups.com

Took the thought out of my head jamie! Nice one :)

I was totally going to build exactly the same thing :)

I'll check it out, and will probably look at pulling it into Alpha 2 of CS2, as I've locked down features of Alpha 1.

Mark

Sent from my mobile device.

Mark Mandel

unread,
Mar 31, 2011, 8:46:12 PM3/31/11
to javaloa...@googlegroups.com
Jamie, as an interesting question - how come you didn't make it a FactoryBean, so that only the Javaloader object found in the server scope is exposed?

I.e. in CS2 it's:
http://coldspring.sourceforge.net/ber/docs/api/coldspring/coldspring/beans/factory/FactoryBean.html

Although is CS1.x has an inheritance hierarchy. Or did you not want it tied to ColdSpring (which I can understand as well)?

Mark
--
E: mark....@gmail.com
T: http://www.twitter.com/neurotic
W: www.compoundtheory.com

cf.Objective(ANZ) - Nov 17, 18 - Melbourne Australia
http://www.cfobjective.com.au

Hands-on ColdFusion ORM Training
www.ColdFusionOrmTraining.com

Jamie Krug

unread,
Apr 1, 2011, 10:10:01 AM4/1/11
to javaloa...@googlegroups.com
Ah, very interesting. I'm familiar with ColdSpring's ProxyFactoryBean and RemoteFactoryBean, but hadn't even thought to look at similar construct here, hence my non-ColdSpring-specific factory. That said, it seems silly not to do it, because ColdSpring is where I wanted JavaLoader to begin with. The CS2 FactoryBean interface makes perfect sense, but I'm not sure about CS1.x. You mention an inheritance hierarchy--I presume this just means you need to extend FactoryBean in 1.2, because it did not depend upon interfaces (which didn't exist in CFML at the time), but rather an "abstract" CFC, by convention?

In any case, is the behavior basically the same in both CS 1.2 and CS 2 alpha? If I understand correctly, but bean definition is completely normal, but ColdSpring knows to return the result of getObject() instead of the factory bean itself, as long as the bean class extends/implements FactoryBean?

I'll whip up a little prototype revision now. Just for style preference, do you think it would make sense to set properties in the bean definition to match JavaLoader's init args, or just have one map property hold those?

Thanks,
Jamie

Jamie Krug

unread,
Apr 1, 2011, 2:08:43 PM4/1/11
to javaloa...@googlegroups.com
How about something like this?

This works for ColdSpring 1.2, and it should work with ColdSpring 2.0 by merely using a component "implements" attribute in place of the "extends" attribute.

It seems that the named lock in getObject() is not likely necessary, but I see no harm either.

Best,
Jamie

Mark Mandel

unread,
Apr 5, 2011, 9:32:15 PM4/5/11
to javaloa...@googlegroups.com
Works nicely :)

Mark

Jamie Krug

unread,
Apr 5, 2011, 9:37:01 PM4/5/11
to javaloa...@googlegroups.com
Cool, it's working nicely for me in a ColdSpring 1.2 app. I'll clean up a CS2 version for you if you let me know how you'd like a patch. I assume I'd reference the coldspring.util.javaloader.JavaLoader CFC by default in CS2? I can also throw in hints to fit in with the CS2 style for ColdDocs. I forget--CF9+ for CS2 or CF8+?

Jamie

Mark Mandel

unread,
Apr 5, 2011, 9:43:44 PM4/5/11
to javaloa...@googlegroups.com
CS2 is CF8+

For providing patches - see:
http://sourceforge.net/apps/trac/coldspring/wiki/Collaboration

But that all sounds good. Cool stuff.

Mark

Jamie Krug

unread,
Apr 6, 2011, 9:18:36 AM4/6/11
to javaloa...@googlegroups.com
Great, sorry, totally missed that wiki page. I'll send a pull request soon. Cheers.

Jamie Jackson

unread,
Mar 11, 2013, 9:37:40 AM3/11/13
to javaloa...@googlegroups.com
It seems like it would be normal to manage Java singletons in ColdSpring as well, and I would think something like the following would be common practice.

I'm just looking for a sanity check/thoughts/suggestions, before I go forward full-steam.

CS snippet:
<bean id="javaLoaderFactory" class="lib.JavaLoaderFactory.JavaLoaderFactory" />
<bean id="javaLoader" factory-bean="javaLoaderFactory" factory-method="getJavaLoader">
<constructor-arg name="loadRelativePaths">
<list>
<value>/lib/pdfgen-1.1-jar-with-dependencies.jar</value>
</list>
</constructor-arg>
</bean>
<bean id="javaLoaderObjectFactory" class="lib.JavaLoaderFactory.JavaLoaderObjectFactory" autowire="byName" />
<bean id="pdfGenerator" factory-bean="javaLoaderObjectFactory" factory-method="create" autowire="byName">
<constructor-arg name="objectToCreate">
<value>com.icf.onecpd.pdfgen.PdfGenerator</value>
</constructor-arg>
</bean>

JavaLoaderObjectFactory:
component
accessors=true {
property javaLoader;
function init(javaLoader) {
setJavaLoader(arguments.javaLoader);
}
public any function create(string objectToCreate) {
return getJavaLoader().create(arguments.objectToCreate);
}
}

Thanks,
Jamie 2 (Electric Boogaloo)

Jamie Jackson

unread,
Mar 11, 2013, 9:53:03 AM3/11/13
to javaloa...@googlegroups.com
Whoops, JavaLoader is a factory already, so forget my spurious factory component, and consider this, instead:

<bean id="pdfGenerator" factory-bean="javaLoader" factory-method="create" autowire="byName">
<constructor-arg name="className">
<value>com.icf.onecpd.pdfgen.PdfGenerator</value>
</constructor-arg>
</bean>

Chris Blackwell

unread,
Mar 11, 2013, 10:29:33 AM3/11/13
to javaloa...@googlegroups.com
Yep, nothing wrong with that.

--
You received this message because you are subscribed to the Google Groups "javaloader-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javaloader-de...@googlegroups.com.
To post to this group, send email to javaloa...@googlegroups.com.
Visit this group at http://groups.google.com/group/javaloader-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jamie Jackson

unread,
Mar 11, 2013, 12:02:04 PM3/11/13
to javaloa...@googlegroups.com
Actually maybe my javaLoader factory is a good idea.

Here's what I want to do: I want to set properties on my JavaLoader-loaded object in ColdSpring:

<bean id="javaLoaderFactory" class="lib.JavaLoaderFactory.JavaLoaderFactory">
<constructor-arg name="javaLoaderDotPath">
<value>lib.javaloader.JavaLoader</value>
</constructor-arg>
</bean>
<bean id="javaLoader" factory-bean="javaLoaderFactory" factory-method="getJavaLoader">
<constructor-arg name="loadRelativePaths">
<list>
<value>/plugins/incomecalculator/lib/pdfgen-1.1-jar-with-dependencies.jar</value>
</list>
</constructor-arg>
</bean>
<bean id="javaLoaderObjectFactory" class="lib.JavaLoaderFactory.JavaLoaderObjectFactory" autowire="byName">
<constructor-arg name="javaLoader"><ref bean="javaLoader" /></constructor-arg>
</bean>
<bean id="pdfGenerator" factory-bean="javaLoaderObjectFactory" factory-method="create" autowire="byName">
<constructor-arg name="objectToCreate">
<value>com.icf.onecpd.pdfgen.PdfGenerator</value>
</constructor-arg>
<constructor-arg name="properties" >
<map>
<entry key="templatesPath"><value>/incomecalculator/public/pdf_templates/</value></entry>
</map>
</constructor-arg>
</bean>

javaLoaderObjectFactory:
component
accessors=true {
property javaLoader;
function init(javaLoader) {
setJavaLoader(arguments.javaLoader);
}
public any function create(string objectToCreate, struct properties = {}) {
var obj = getJavaLoader().create(arguments.objectToCreate);
return populate(obj, arguments.properties);
}
public any function populate(object, required struct memento){
var bu = createObject("java", "org.apache.commons.beanutils.BeanUtils");
bu.populate(object, memento);
// writeDump(var=memento.getClass(), abort=true); //object.getTemplatesPath(),
return object;
}
}

But, my Java is rusty, so I'm not sure how to populate the Java object generically.

I'd gues it's common thing to want to set some properties of a JavaLoader-loaded object in ColdSpring. Is there some more elegant/standard way to do it? Am I on the right track?

Thanks,
Jamie

On Monday, March 11, 2013 9:37:40 AM UTC-4, Jamie Jackson wrote:

Jamie Krug

unread,
Mar 11, 2013, 11:13:05 PM3/11/13
to javaloa...@googlegroups.com
On Mon, Mar 11, 2013 at 9:37 AM, Jamie Jackson <jamie...@gmail.com> wrote:
It seems like it would be normal to manage Java singletons in ColdSpring as well, and I would think something like the following would be common practice.

Certainly. I'm using DI/1 in an app right now, and I do just that -- use JavaLoader to grab an instance of the Java object and just add it as a (singleton) bean to the bean factory.

Jamie Jackson

unread,
Mar 12, 2013, 3:23:42 PM3/12/13
to javaloa...@googlegroups.com, ja...@thekrugs.com
Do you have any tricks for creating a generic object factory (which will set properties on the object)?

I tried to implement a "populate" function using org.apache.commons.beanutils.BeanUtils.populate(), but it didn't have any effect on the object:
component
accessors=true {
property javaLoader;
function init(javaLoader) {
setJavaLoader(arguments.javaLoader);
}
public any function create(string objectToCreate, struct properties = {}) {
var obj = getJavaLoader().create(arguments.objectToCreate);
return populate(obj, arguments.properties);
}
public any function populate(object, required struct memento){
var bu = createObject("java", "org.apache.commons.beanutils.BeanUtils");
bu.populate(object, memento);
// writeDump(var=memento.getClass(), abort=true); //object.getTemplatesPath(),
return object;
}
}

Jamie Krug

unread,
Mar 14, 2013, 10:28:40 AM3/14/13
to javaloa...@googlegroups.com
Sorry, nothing off the top of my head. That BeanUtils options seems to make sense, but I haven't used it recently. It seems that it should work if your object has getters/setters for properties.


--
Reply all
Reply to author
Forward
0 new messages