You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Eric Wulff <ew...@gmail.com> on 2004/11/07 04:05:12 UTC

DataSource and initialContext.lookup("java:comp/env")

I just returned from working on a tutorial at sun which I was inspired
to go over based on my lack of understanding of the code snippet
below.  I'm trying to fully understand what's going on.  Docs state
that the code...

initialContext.lookup("java:comp/env") 

returns a named object.  Best I can tell is that this is a reference
to a starting point in my directory... or something like that.  Is
anyone willing to explain what initialContext.lookup() fundamentally
takes care of when called?

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

thx
Eric

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


Re: DataSource and initialContext.lookup("java:comp/env")

Posted by "Steven J. Owens" <pu...@darksleep.com>.
On Sat, Nov 06, 2004 at 07:05:12PM -0800, Eric Wulff wrote:
> I just returned from working on a tutorial at sun which I was inspired
> to go over based on my lack of understanding of the code snippet
> below.  I'm trying to fully understand what's going on.  Docs state
> that the code...
> 
> initialContext.lookup("java:comp/env") 
> 
> returns a named object.  Best I can tell is that this is a reference
> to a starting point in my directory... or something like that.  Is
> anyone willing to explain what initialContext.lookup() fundamentally
> takes care of when called?

     When I first read your question above, it seemed that you had a
profound lack of understanding.  Then on a second read it looks like
maybe you might have a vague idea - it depends on what _you_ mean when
you use the word "directory" in that question :-).

     Read up on JNDI (Java Naming and Directory Interface).  Basically
it's sort of like DNS, except that it's in the java world and the
results of a lookup are a java object.  Other examples of the concept
are LDAP, COS, NIS, in fact,the JNDI API is meant to act as a proxy
to such services, so you might be doing a JNDI lookup that's actually
hitting an LDAP server, or a your file system, or a database.

http://en.wikipedia.org/wiki/JNDI
http://java.sun.com/products/jndi/overview.html
http://java.sun.com/products/jndi/tutorial/
http://www.javaworld.com/javaworld/jw-01-2000/jw-01-howto.html
	
          RMI uses JNDI.  J2EE APIs use JNDI.  Tomcat comes with a
database connection pool from Jakarta Commons, the DBCP.  The
following URLS document it a bit more, but you don't *really* need to
know that much about either JNDI or DBCP just to use it (this is not
to be construed as suggesting it's good to *not* know and understand
everything you're using :-).

http://jakarta.apache.org/commons/dbcp/
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-resources-howto.html
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-datasource-examples-howto.html

-- 
Steven J. Owens
puff@darksleep.com

"I'm going to make broad, sweeping generalizations and strong,
 declarative statements, because otherwise I'll be here all night and
 this document will be four times longer and much less fun to read.
 Take it all with a grain of salt." - http://darksleep.com/notablog


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


Re: DataSource and initialContext.lookup("java:comp/env")

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
java:comp/env is the default namespace defined by JNDI.  The call to 
lookup() with this as an argument is returning an instance of a context 
corresponding to that namespace, which you need to do a further lookup 
for your specific resource.

On a related note, I'm pretty sure you could condense all the code you 
have to just one line, something like:

DataSource ds = (DateSource)new 
InitialContext().lookup("java:comp/env/jdbc/wms")

I'm fairly certain that works.  That might be clearer to you.

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com

Eric Wulff wrote:
> helpful somewhat...  at least in helping me narrow my question.  So
> what is this "java:comp/env"?  What exactly is being returned based on
> this string passed to lookup()?
> 
> Eric
> 
> 
> On Sat, 06 Nov 2004 23:35:34 -0500, Frank W. Zammetti
> <fz...@omnytex.com> wrote:
> 
>>I'll give it a shot...
>>
>>All JNDI resource lookups occur relative to some context.  The code you
>>posted first gets the default initial context, then looks for a resource
>>named "java:comp/env" relative to that default context.  It then gets a
>>reference to a DataSource that exists within the context "jdbc/wms"
>>relative to "java:comp/env"
>>
>>You can ROUGHLY think of this the same way you do URLs... in a sense,
>>the "java:comp/env" might be something like www.apache.org.  That gives
>>you a starting context.  Then if you want to retrieve a particular file
>>in that domain, you look it up relative to the domain (i.e.,
>>www.apache.org/tomcat.htm).
>>
>>Same thing here... There is a hierarchy of JNDI resources, a tree
>>structure, that has an initial context as the root.  You then are in a
>>sense constructing a path to your DataSource, and it might look
>>something like /initial_context/java:comp/env/jdbc/wms (not exactly, but
>>you get the idea).
>>
>>Does that help any?
>>
>>--
>>Frank W. Zammetti
>>Founder and Chief Software Architect
>>Omnytex Technologies
>>http://www.omnytex.com
>>
>>
>>
>>Eric Wulff wrote:
>>
>>>I just returned from working on a tutorial at sun which I was inspired
>>>to go over based on my lack of understanding of the code snippet
>>>below.  I'm trying to fully understand what's going on.  Docs state
>>>that the code...
>>>
>>>initialContext.lookup("java:comp/env")
>>>
>>>returns a named object.  Best I can tell is that this is a reference
>>>to a starting point in my directory... or something like that.  Is
>>>anyone willing to explain what initialContext.lookup() fundamentally
>>>takes care of when called?
>>>
>>>Context initialContext = new InitialContext();
>>>Context context = (Context) initialContext.lookup("java:comp/env");
>>>DataSource dataSource = (DataSource) context.lookup("jdbc/wms");
>>>
>>>thx
>>>Eric
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>>
>>>
>>>
>>>
>>>.
>>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>>
> 
> 
> 
> 
> 



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


Re: DataSource and initialContext.lookup("java:comp/env")

Posted by Eric Wulff <ew...@gmail.com>.
helpful somewhat...  at least in helping me narrow my question.  So
what is this "java:comp/env"?  What exactly is being returned based on
this string passed to lookup()?

Eric


On Sat, 06 Nov 2004 23:35:34 -0500, Frank W. Zammetti
<fz...@omnytex.com> wrote:
> I'll give it a shot...
> 
> All JNDI resource lookups occur relative to some context.  The code you
> posted first gets the default initial context, then looks for a resource
> named "java:comp/env" relative to that default context.  It then gets a
> reference to a DataSource that exists within the context "jdbc/wms"
> relative to "java:comp/env"
> 
> You can ROUGHLY think of this the same way you do URLs... in a sense,
> the "java:comp/env" might be something like www.apache.org.  That gives
> you a starting context.  Then if you want to retrieve a particular file
> in that domain, you look it up relative to the domain (i.e.,
> www.apache.org/tomcat.htm).
> 
> Same thing here... There is a hierarchy of JNDI resources, a tree
> structure, that has an initial context as the root.  You then are in a
> sense constructing a path to your DataSource, and it might look
> something like /initial_context/java:comp/env/jdbc/wms (not exactly, but
> you get the idea).
> 
> Does that help any?
> 
> --
> Frank W. Zammetti
> Founder and Chief Software Architect
> Omnytex Technologies
> http://www.omnytex.com
> 
> 
> 
> Eric Wulff wrote:
> > I just returned from working on a tutorial at sun which I was inspired
> > to go over based on my lack of understanding of the code snippet
> > below.  I'm trying to fully understand what's going on.  Docs state
> > that the code...
> >
> > initialContext.lookup("java:comp/env")
> >
> > returns a named object.  Best I can tell is that this is a reference
> > to a starting point in my directory... or something like that.  Is
> > anyone willing to explain what initialContext.lookup() fundamentally
> > takes care of when called?
> >
> > Context initialContext = new InitialContext();
> > Context context = (Context) initialContext.lookup("java:comp/env");
> > DataSource dataSource = (DataSource) context.lookup("jdbc/wms");
> >
> > thx
> > Eric
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> >
> >
> >
> > .
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
>

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


Re[2]: DataSource and initialContext.lookup("java:comp/env")

Posted by Allen Holub <al...@holub.com>.
It's also helpful, I think, to look at a JNDI directory as a database
that's organized in a directory-like fashion. The database contains
objects, not arbitrary data, however. The "context" is effectively the
database name, and when you "lookup" something in the context, you're
effectively looking it up in that database. Don't confuse any of this
with the file-system directory out on your hard disk.


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


Re: DataSource and initialContext.lookup("java:comp/env")

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
I'll give it a shot...

All JNDI resource lookups occur relative to some context.  The code you 
posted first gets the default initial context, then looks for a resource 
named "java:comp/env" relative to that default context.  It then gets a 
reference to a DataSource that exists within the context "jdbc/wms" 
relative to "java:comp/env"

You can ROUGHLY think of this the same way you do URLs... in a sense, 
the "java:comp/env" might be something like www.apache.org.  That gives 
you a starting context.  Then if you want to retrieve a particular file 
in that domain, you look it up relative to the domain (i.e., 
www.apache.org/tomcat.htm).

Same thing here... There is a hierarchy of JNDI resources, a tree 
structure, that has an initial context as the root.  You then are in a 
sense constructing a path to your DataSource, and it might look 
something like /initial_context/java:comp/env/jdbc/wms (not exactly, but 
you get the idea).

Does that help any?

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com

Eric Wulff wrote:
> I just returned from working on a tutorial at sun which I was inspired
> to go over based on my lack of understanding of the code snippet
> below.  I'm trying to fully understand what's going on.  Docs state
> that the code...
> 
> initialContext.lookup("java:comp/env") 
> 
> returns a named object.  Best I can tell is that this is a reference
> to a starting point in my directory... or something like that.  Is
> anyone willing to explain what initialContext.lookup() fundamentally
> takes care of when called?
> 
> Context initialContext = new InitialContext();
> Context context = (Context) initialContext.lookup("java:comp/env");
> DataSource dataSource = (DataSource) context.lookup("jdbc/wms");
> 
> thx
> Eric
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 
> 
> 
> .
> 




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