users@glassfish.java.net

Re: ObjectFactory in context.xml?

From: Amy Roh <amy.roh_at_oracle.com>
Date: Tue, 31 Aug 2010 18:58:10 -0700

glassfish_at_javadesktop.org wrote:
> 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?
>

You still need to create a custom resource [1] for GlassFish to be aware
of this resource. The context.xml definition [2] is equivalent to the
web.xml definition [3] but still needs the custom resource to be created
for the server.

[1] asadmin create-custom-resource --restype
javax.naming.spi.ObjectFactory --factoryclass
com.foo.bar.StringObjectFactory bar

For example,

[2]
<Context ...>
  ...
  <Resource name="jdbc/EmployeeDB" auth="Container"
            type="javax.sql.DataSource"
     description="Employees Database for HR Applications"/>
  ...
</Context>

[3]
<resource-ref>
  <description>Employees Database for HR Applications</description>
  <res-ref-name>jdbc/EmployeeDB</res-ref-name>
  <res-ref-type>javax.sql.DataSource</res-ref-type>
  <res-auth>Container</res-auth>
</resource-ref>




> [Message sent by forum member 'decostae']
>
> http://forums.java.net/jive/thread.jspa?messageID=481524
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>