You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Derek Potts <de...@helloworld.sh> on 2009/03/08 21:31:57 UTC

Resource with custom factory

Hello,

I am having trouble setting up a resource for my web app. First, some  
background...

I am using Tomcat 5.5 and OpenJPA 1.2.0. Everything is working fine in the  
unmanaged fashion:
EntityManagerFactory emf = Persistence.createEntityManagerFactory(null);
EntityManager em = emf.createEntityManager();

Inspired by the OpenJPA manual at  
http://openjpa.apache.org/builds/1.2.0/apache-openjpa-1.2.0/docs/manual/jpa_overview_emfactory.html,
	JPA allows you to create and configure an EntityManagerFactory,
	then store it in a Java Naming and Directory Interface (JNDI)
	tree for later retrieval and use.
I would like to do something like this:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
EntityManagerFactory emf = (EntityManagerFactory) envCtx.lookup("emf");

I started off by reading  
http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html. From  
what I can see, I need to create a resource with a custom factory. I also  
think the resource can be a resource-env-ref since it does not need  
authentication.

So I put this into my web.xml:
<resource-env-ref>
	<resource-env-ref-name>emf</resource-env-ref-name>
	<resource-env-ref-type>org.apache.openjpa.persistence.OpenJPAEntityManagerFactory</resource-env-ref-type>
</resource-env-ref>

With that,
EntityManagerFactory emf = (EntityManagerFactory) envCtx.lookup("emf");
throws a NamingException (as I would expect, since I had not yet  
configured the resource itself), "Cannot create resource instance",  
org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:113)

So then I added a META-INF/context.xml like this:
<context>
	<resource
		name="emf" auth="Container"
		type="org.apache.openjpa.persistence.OpenJPAEntityManagerFactory"
		factory="sh.helloworld.testing.EMFFactory"
	/>
</context>

EMFFactory is a class I wrote that implements  
javax.naming.spi.ObjectFactory, which is then placed in my WEB-INF/classes  
path.

Unfortunately, I still receive the same NamingException, thrown by  
ResourceEnvFactory. I was hoping to at least get an exception from my  
custom factory. The fact that tomcat is still calling the  
ResourceEnvFactory is why I thought that my question is for the Tomcat  
mailing list and not the OpenJPA list.

Thanks in advance for any help!

Derek

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Resource with custom factory

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Derek,

On 3/8/2009 4:31 PM, Derek Potts wrote:
> JPA allows you to create and configure an EntityManagerFactory, then
> store it in a Java Naming and Directory Interface (JNDI) tree for
> later retrieval and use.
>
> I would like to do something like this:
> Context initCtx = new InitialContext();
> Context envCtx = (Context) initCtx.lookup("java:comp/env");
> EntityManagerFactory emf = (EntityManagerFactory) envCtx.lookup("emf");

Sounds reasonable.

> I started off by reading
> http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html.

That's a good place to start. Pay special attention to this section:
http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html#Adding%20Custom%20Resource%20Factories

> Unfortunately, I still receive the same NamingException, thrown by
> ResourceEnvFactory. I was hoping to at least get an exception from my
> custom factory. The fact that tomcat is still calling the
> ResourceEnvFactory is why I thought that my question is for the Tomcat
> mailing list and not the OpenJPA list.

Yeah, this is almost definintely a Tomcat question, not an OpenJPA
question (unless you want to ask if someone has ever done what you are
trying to do using Tomcat over on the other list... it couldn't hurt).

> EMFFactory is a class I wrote that implements
> javax.naming.spi.ObjectFactory, which is then placed in my
> WEB-INF/classes path.

I think here's where you made your mistake. I would have expected Tomcat
to give you a NoClassDefFoundError or something similar because,
according to the documentation, your class should go here:

"
When you are through, place the factory class (and the corresponding
bean class) unpacked under $CATALINA_HOME/common/classes, or in a JAR
file inside $CATALINA_HOME/common/lib. In this way, the required class
files are visible to both Catalina internal resources and your web
application.
"

Try moving your factory to Tomcat's common/lib directory to see if that
helps.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkm4MdMACgkQ9CaO5/Lv0PCvbACgoAkiCP9YGdEXw+wlpR0Txeh2
6eUAnjBJylrqjBFk+Lu8NNkr+5jIzUsr
=7LLI
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org