You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Roytman, Alex" <ro...@peacetech.com> on 2000/11/07 01:03:14 UTC

Tomcat & JNDI

I am trying to tie Tomcat with JNDI to use J2EE pattern to obtain resources:

Context context = new InitialContext();
dataSource = (DataSource)context.lookup("java:comp/env/jdbc/myDataSource");

(I know Tomcat team is planning to have it for Tomcat 4 eventually)

I have my own little in memory JNDI implementation with support for
java:comp URLs
and it works fine but provides one global JNDI for all contexts defined in
server.xml while it should be context specific. Basically the problem is how
to make InitialContext() tomcat context aware (to be precise to make my
java:comp namespace to start from different roots depending on where
InitialContext was created). 

If AdaptiveClassLoader had some context info in it I could get it from there
while creating InitialContext.


Any ideas are greatly appreciated

Alex

Re: Tomcat & JNDI

Posted by Remy Maucherat <re...@apache.org>.
> If I recall correctly, it's actually sort of tricky to get this right.
You
> might want to check the source for the Tomcat 4 implementation for
ideas -- I
> believe it depends on the servlet container calling
> Thread.setContextClassLoader() once you know which webapp you're running
in.
> This only works in a Java2 environment, though.

Almost. I use thread binding instead (which is more or less the same here).
I'll add class loader binding as an alternative to thread binding.

What is supported right now :

Only Tyrex is supported right now as the provider for javax.sql.DataSource
type (so get Tyrex first (http://tyrex.exolab.org). Then put it into the
"lib" directory in the Catalina distribution.

Then add to the web.xml :
    <resource-ref>
      <description>Test DataSource</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>
(that's an exemple, you can give it another name)

Of course, the factory doesn't really know what to do, so it will go to the
defaults, and try to create a memory based data source using hSQL. So to use
the defaults, you also need to get Hypersonic SQL from Sourceforge (and put
the hsql.jar file into "lib").

To set parameters for the data source, use env-entries with special names.
Here's a (self explanatory, IMO) example :
    <env-entry>
      <env-entry-name>jdbc/TestDB/user</env-entry-name>
      <env-entry-value>sa</env-entry-value>
      <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
    <env-entry>
      <env-entry-name>jdbc/TestDB/password</env-entry-name>
      <env-entry-value></env-entry-value>
      <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
    <env-entry>
      <env-entry-name>jdbc/TestDB/driverClassName</env-entry-name>
      <env-entry-value>org.hsql.jdbcDriver</env-entry-value>
      <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
    <env-entry>
      <env-entry-name>jdbc/TestDB/driverName</env-entry-name>
      <env-entry-value>jdbc:HypersonicSQL:database</env-entry-value>
      <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>

That's it. The lookup on "java:/comp/jdbc/TestDB" should return a usable
data source. A new instance of the data source is instantiated each time you
do the lookup, so I recommend that you bind that data source into the
per-web app writable initial context which is also provided to your servlet.

For transactions support a UserTransaction object is mapped at
"java:/comp/UserTransaction". I didn't test that functionality yet, so it
may not work.

Both resource-env-ref and ejb-ref are unsupported right now (actually, they
are, but there is no factory to instantiate the objects).
Resource references to JMS, JavaMail, .... types are also unsupported.

Remy


Re: Tomcat & JNDI

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Roytman, Alex" wrote:

> I am trying to tie Tomcat with JNDI to use J2EE pattern to obtain resources:
>
> Context context = new InitialContext();
> dataSource = (DataSource)context.lookup("java:comp/env/jdbc/myDataSource");
>
> (I know Tomcat team is planning to have it for Tomcat 4 eventually)
>

Interesting timing ... the code to do this (in Tomcat 4) was added just in the
last couple of days (after milestone 4).

>
> I have my own little in memory JNDI implementation with support for
> java:comp URLs
> and it works fine but provides one global JNDI for all contexts defined in
> server.xml while it should be context specific. Basically the problem is how
> to make InitialContext() tomcat context aware (to be precise to make my
> java:comp namespace to start from different roots depending on where
> InitialContext was created).
>
> If AdaptiveClassLoader had some context info in it I could get it from there
> while creating InitialContext.
>

If I recall correctly, it's actually sort of tricky to get this right.  You
might want to check the source for the Tomcat 4 implementation for ideas -- I
believe it depends on the servlet container calling
Thread.setContextClassLoader() once you know which webapp you're running in.
This only works in a Java2 environment, though.

>
> Any ideas are greatly appreciated
>
> Alex

Craig McClanahan



Re: Tomcat & JNDI

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Roytman, Alex" wrote:

> I am trying to tie Tomcat with JNDI to use J2EE pattern to obtain resources:
>
> Context context = new InitialContext();
> dataSource = (DataSource)context.lookup("java:comp/env/jdbc/myDataSource");
>
> (I know Tomcat team is planning to have it for Tomcat 4 eventually)
>

Interesting timing ... the code to do this (in Tomcat 4) was added just in the
last couple of days (after milestone 4).

>
> I have my own little in memory JNDI implementation with support for
> java:comp URLs
> and it works fine but provides one global JNDI for all contexts defined in
> server.xml while it should be context specific. Basically the problem is how
> to make InitialContext() tomcat context aware (to be precise to make my
> java:comp namespace to start from different roots depending on where
> InitialContext was created).
>
> If AdaptiveClassLoader had some context info in it I could get it from there
> while creating InitialContext.
>

If I recall correctly, it's actually sort of tricky to get this right.  You
might want to check the source for the Tomcat 4 implementation for ideas -- I
believe it depends on the servlet container calling
Thread.setContextClassLoader() once you know which webapp you're running in.
This only works in a Java2 environment, though.

>
> Any ideas are greatly appreciated
>
> Alex

Craig McClanahan



Re: Tomcat & JNDI

Posted by Remy Maucherat <re...@apache.org>.
----- Original Message -----
From: <cm...@yahoo.com>
To: <to...@jakarta.apache.org>
Cc: <to...@jakarta.apache.org>
Sent: Tuesday, November 07, 2000 6:50 AM
Subject: Re: Tomcat & JNDI


> > I have my own little in memory JNDI implementation with support for
> > java:comp URLs
> > and it works fine but provides one global JNDI for all contexts defined
in
> > server.xml while it should be context specific. Basically the problem is
how
> > to make InitialContext() tomcat context aware (to be precise to make my
> > java:comp namespace to start from different roots depending on where
> > InitialContext was created).
>
> One idea:
> 1. Create a custom Interceptor.
>
> 2. In preService and preInit, set a thread data object ( that can be
> accessed from your InitialContext) ( of course, your interceptor will be
> JDK1.2 specific )
>
> There are other ways to do it, the problem is how to pass a parameter (
> like the current context ) without passing it explicitely ( you don't have
> "ServletContext" params in your InitialContext constructor ). The best way
> you can do that is associating the object with the Thread ( since
> getThread() is so easy to do ).
>
> If you want JDK1.1 compat you could use a static Hashtable, keyed by
> Thread and having as value the information you need ( current context ).

All the helper classes which handle the associtations are within the
org.apache.naming package, so your interceptor should be trivial to write.

> Please contribute the result back to tomcat :-)

Tyrex also includes such an interceptor.

Remy


Re: Tomcat & JNDI

Posted by cm...@yahoo.com.
> I have my own little in memory JNDI implementation with support for
> java:comp URLs
> and it works fine but provides one global JNDI for all contexts defined in
> server.xml while it should be context specific. Basically the problem is how
> to make InitialContext() tomcat context aware (to be precise to make my
> java:comp namespace to start from different roots depending on where
> InitialContext was created). 

One idea:
1. Create a custom Interceptor. 

2. In preService and preInit, set a thread data object ( that can be
accessed from your InitialContext) ( of course, your interceptor will be
JDK1.2 specific )

There are other ways to do it, the problem is how to pass a parameter (
like the current context ) without passing it explicitely ( you don't have
"ServletContext" params in your InitialContext constructor ). The best way
you can do that is associating the object with the Thread ( since
getThread() is so easy to do ).

If you want JDK1.1 compat you could use a static Hashtable, keyed by
Thread and having as value the information you need ( current context ).

Please contribute the result back to tomcat :-)

Costin


> 
> If AdaptiveClassLoader had some context info in it I could get it from there
> while creating InitialContext.
> 
> 
> Any ideas are greatly appreciated
> 
> Alex
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org
> 


Re: Tomcat & JNDI

Posted by cm...@yahoo.com.
> I have my own little in memory JNDI implementation with support for
> java:comp URLs
> and it works fine but provides one global JNDI for all contexts defined in
> server.xml while it should be context specific. Basically the problem is how
> to make InitialContext() tomcat context aware (to be precise to make my
> java:comp namespace to start from different roots depending on where
> InitialContext was created). 

One idea:
1. Create a custom Interceptor. 

2. In preService and preInit, set a thread data object ( that can be
accessed from your InitialContext) ( of course, your interceptor will be
JDK1.2 specific )

There are other ways to do it, the problem is how to pass a parameter (
like the current context ) without passing it explicitely ( you don't have
"ServletContext" params in your InitialContext constructor ). The best way
you can do that is associating the object with the Thread ( since
getThread() is so easy to do ).

If you want JDK1.1 compat you could use a static Hashtable, keyed by
Thread and having as value the information you need ( current context ).

Please contribute the result back to tomcat :-)

Costin


> 
> If AdaptiveClassLoader had some context info in it I could get it from there
> while creating InitialContext.
> 
> 
> Any ideas are greatly appreciated
> 
> Alex
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org
>