dev@glassfish.java.net

[patch] Factory class for custom JNDI resource

From: Cheng Fang <Cheng.Fang_at_Sun.COM>
Date: Fri, 09 Mar 2007 14:05:50 -0500

This is a follow-up to my previous email wrt custom resource in
glassfish. When I use org..apache.naming.factory.BeanFactory in
appserv-rt.jar as the factory class for a custom resource, the lookup
always returns null (the lookup succeeded with a null return value).

The reason is, its getObjectInstance method creates instances only if
the first parameter is instanceof org.apache.naming.ResourceRef (a
subclass of javax.naming.Reference). But in glassfish, this parameter
is of type javax.naming.Reference.

The following change seems to fix the problem:

Index: BeanFactory.java
===================================================================
RCS file:
/cvs/glassfish/appserv-webtier/src/java/org/apache/naming/factory/BeanFactory.java,v
retrieving revision 1.3
diff -w -u -b -r1.3 BeanFactory.java
--- BeanFactory.java 6 Nov 2006 21:14:08 -0000 1.3
+++ BeanFactory.java 9 Mar 2007 18:09:56 -0000
@@ -121,7 +121,7 @@
                                     Hashtable environment)
         throws NamingException {

- if (obj instanceof ResourceRef) {
+ if (obj instanceof Reference) {

I don't see any operations within this method that are specific to
o.a.n.ResourceRef. Since resources can be injected and looked up in all
JavaEE component classes, it seems a little odd to limit the reference
type to o.a.n.ResourceRef, but I'm not sure about the full implication
of this change. Could someone review it? Any suggestions are welcome.

I'm writing a series of blog posts on using custom resource in
Glassfish. I like to point to a built-in factory rather an a hacked one.
http://blogs.sun.com/chengfang/entry/how_to_parameterize_and_configure
http://blogs.sun.com/chengfang/entry/how_to_inject_and_look
http://blogs.sun.com/chengfang/entry/how_to_create_custom_resources

Thanks,
-cheng

-------- Original Message --------
Subject: Factory class for custom JNDI resource
Date: Wed, 06 Dec 2006 08:31:46 -0500
From: Cheng Fang <cheng.fang_at_sun.com>
To: dev_at_glassfish.dev.java.net



Is there a ready-to-use factory class for custom JNDI resource in
Glassfish? Tomcat has one: org.apache.naming.BeanFactory that can be
used to create any custom resource conforming to JavaBeans.

I noticed this BeanFactory class is available in appserv-rt.jar. Is it
supported for this purpose in Glassfish? Is it supported in web
container, ejb container, and appclient container?

If not, then users will need to create their own factory class. It
seems to be a usability issue.

I was able to create such a resource using this BeanFactory, but at
runtime, the lookup always returns a null. It means the lookup succeeds
and the value bound to the name is null.

Any suggestions before I go about creating my own factory? Thanks.

-cheng