users@glassfish.java.net

ObjectFactory in context.xml?

From: <glassfish_at_javadesktop.org>
Date: Tue, 31 Aug 2010 15:07:42 PDT

I'm using Glassfish v3 and cannot seem to get any Resource that is created by an ObjectFactory to work. I've had no problems with this in Tomcat; I've also not had any problems getting this to work with global "custom resources" by using the asadmin tool.

META-INF/context.xml :
<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Environment name="foo" type="java.lang.String" value="foo"/>
  <Resource name="bar"
                   auth="Container"
                   type="java.lang.String"
                   factory="com.foo.bar.StringObjectFactory"/>
</Context>

StringObjectFactory.java :

package com.foo.bar;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.spi.ObjectFactory;

public class StringObjectFactory implements ObjectFactory {

&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;public Object getObjectInstance(Object arg0, Name arg1, Context arg2, Hashtable<?, ?> arg3) throws Exception {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "bar";
&nbsp;&nbsp;&nbsp;&nbsp;}

}

In the doGet() method of the servlet, I do:

Context cntx = new InitialContext();
cntx.lookup("java:comp/env/bar");

This what I see in the logs:
[#|2010-08-31T17:00:50.689-0400|WARNING|glassfish3.0.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=23;_ThreadName=Thread-1;|StandardWrapperValve[TestServlet]: PWC1406: Servlet.service() for servlet TestServlet threw exception
javax.naming.NameNotFoundException: bar not found
        at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:197)
        at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:168)
        at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:58)
        at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:101)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at com.sun.enterprise.naming.util.JndiNamingObjectFactory.create(JndiNamingObjectFactory.java:87)
        at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:688)
        at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:657)
        at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:148)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:428)
....

Note that cntx.lookup("java:comp/env/foo") works fine.

It seems that Glassfish does not want to invoke ObjectFactories specified in context.xml - or is there something else I need to set or tweak?
[Message sent by forum member 'decostae']

http://forums.java.net/jive/thread.jspa?messageID=481524