lookup jndi resource without resource-ref definition - java

I would like to lookup a datasource defined on application server but without define a resource-ref on web.xml. The name of jndi resource is defined on an external file in my web application. I did a lot experiment on Tomcat but it seems not possible. It's possible to define at runtime a resource "resource-ref"? Do you have some suggestions?
Thanks

I asked the same question. And here is what I found: instead add in web.xml you can modify the 'context.xml' either in Tomcat or in /META-INF/condext.xml in your application and add a resource there:
<Context>
<Resource name="jdbc/EmployeeDB" auth="Container"
type="javax.sql.DataSource"
description="Employees Database for HR Applications"/>
</Context>
But I didn't find if it's possible to do this programmatically.
link to tomcat doc

Related

JNDI Resources in web-fragment

I'm working on a web-fragment to isolate a specialized process needed in only one part of our overall production process. I'm trying to define a JNDI resource specific to the web-fragment, but I keep running into a brick wall.
The application is running in Tomcat 9.
I am using Web-Fragment specification 4.0.
The working model, right now, is I have the database connection defined as a global resource in the server.xml as such:
<Resource name="localappserver"
auth="Container"
driverClassName="oracle.jdbc.OracleDriver"
factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory"
initialSize="5"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
maxTotal="15"
maxIdle="3"
maxWaitMillis="1000"
removeAbandonedOnBorrow="true"
removeAbandonedOnMaintenance="true"
removeAbandonedTimeout="15"
testOnBorrow="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="300000"
type="javax.sql.DataSource"
username="${jdbc.user}"
password="${jdbc.password}"
url="${jdbc.url}" />
and is referenced in the larger web application context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="..." path="/..." reloadable="true" swallowOutput="true">
...
<ResourceLink global="localappserver" name="localappserver" type="javax.sql.DataSource" />
...
</Context>
This works as expected and I can connect to the resource and retrieve data. However, I do not want to pollute the main application's context.cml with items that do not pertain to the main application.
I've tried moving the resource link from the main context.xml to the web-fragment's context.xml but that results in the following error:
INFO: javax.naming.NameNotFoundException: Name [localappserver] is not bound in this Context. Unable to find [localappserver].
at org.apache.naming.NamingContext.lookup(NamingContext.java:833)
at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
at org.apache.naming.NamingContext.lookup(NamingContext.java:174)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
at java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409)
at com.obj.Data.Connect(Data.java:141)
I've moving the resource definition from the server.xml to the web-fragment's web-fragment.xml and even the web-fragment's context.xml. Both instances return the same error.
While I can live with this one instance, I know we will be adding and/or migrating more pieces to web-fragments and I really want to separate the resource references to their specific jar.
EDIT
I should mention I am testing all of this while running tomcat from inside Eclipse. I don't think that will make a difference, but I want to mention it
Is this even possible?
We have decided to containerize the application. Thus, as we have multiple locations in our production process, this will become a non-issue.
By placing the resource link <ResourceLink global="localappserver" name="localappserver" type="javax.sql.DataSource" /> in the individual location's [TOMCAT_HOME]/conf/context.xml we eliminate the need to clutter the main application's /META-INF/context.xml
This should work as we divide up each location's requirements into individual web-fragment projects.

what is the significance of auth attribute of datasources defined in tomcat

In tomcat we define datasources. In datasources there is a attribute "auth". Generally i used to have its value equal to "container". for example.
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#127.0.0.1:1521:mysid"
username="scott" password="tiger" maxActive="20" maxIdle="10"
maxWait="-1"/>
Can somebody please tell me what is the significance of this "auth" attribute? I could not find significance of this attribute after googling also.
Go through the doc , it explains it :
Specify whether the web Application code signs on to the corresponding resource manager programmatically, or whether the Container will sign on to the resource manager on behalf of the application. The value of this attribute must be Application or Container. This attribute is required if the web application will use a element in the web application deployment descriptor, but is optional if the application uses a instead.

what is resource-ref tag meant for, in web.xml? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
What is resource-ref in web.xml used for?
I added the following tag in context.xml of Tomcat :
<Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
driverClassName="org.apache.derby.jdbc.ClientDriver" url="jdbc:derby://localhost:1527/poll_database;create=true"
username="suhail" password="suhail" maxActive="20" maxIdle="10" maxWait="-1" />
The above tag gets read,when the server is started and the application is deployed. I also added the following in web.xml of my project :
<resource-ref>
<description>my connection</description>
<res-ref-name>jdbc/PollDatasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
What is the above tag meant for ?
Because when i write a statement like:
DataSource ds = (DataSource)envContext.lookup("jdbc/PollDatasource")
doesn't it directly look into the context.xml of the server ? I know I am wrong.In this case please help me understand what is the resource-ref tag meant for in web.xml ?
you have to lookup with following code
DataSource ds = (DataSource)envContext.lookup("java:comp/env/jdbc/PollDatasource");
within your web application,
The JNDI context is different within application and outside of the application, in this case inside context.xml, where you defined your resources.
All the resouces defined in resouce-ref section are mapped to application's Env Context. so , application can access those resources. The cross-reference between application context and container context is done at the deployment time.
I believe its meant as a "hint" or live documentation that your web application expects to have access to JNDI resource with the specific name and other attributes. The context.xml file is a Tomcat artifact and while many app servers embed the Tomcat engine, it is far from universal.

Javamail as JNDI resource in tomcat

We want to send mail from inside my tomcat web application.
I inherited some code from a predecessor that uses a context configuration file inside tomcat to define the data source that I use in my application through a JNDI lookup. This file currently looks like this:
<Context path="/chronicle" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="ej-Log." suffix=".txt" timestamp="true"/>
<Resource name="jdbc/chronicle"
auth="Container"
type="javax.sql.DataSource"
username="sa"
password="xxxxxxxx"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://localhost/PsDb"
maxActive="-1"
maxIdle="0"
/>
</Context>
I have never been able to find documentation for this and never met the guy who did it so I don't really understand it, I simply know that it works.
This morning, it occurred to me that I could use the same tools to configure the mail server. Does anyone know how to do this inside the context configuration file?
The Tomcat user guide tells you how to do it:
Tomcat 7 JNDI How-To
I didn't check to see if it changed from Tomcat 6 so here's that too:
Tomcat 6 JNDI How-To
Where on earth have you looked if you weren't able to find documentation for this? The Tomcat documentation has a separate chapter for declaring JNDI resources and in that chapter, there is a section describing how to declare JavaMail sessions.

How to configure a global JNDI datasource in Tomcat 6?

I'm struggling to configure a simple JNDI pooled datasource in Tomcat 6.0.32.
I want to create the datasource definition outside my web application artifact. Basically I don't want my application to know the credentials to access the database server.
I was under the assumption that, like in Weblogic, it should be possible to create a "global" JNDI datasource in Tomcat, but so far I have been unsuccessful.
I have tried to add the datasource definition in CATALINA_HOME/conf/context:
<Resource name="jdbc/mydb"
auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:#1.1.1.1.:xxx"
user="xxxx"
password="yyyy"/>
The result is that the server outputs the following line, when booting:
SEVERE: Null component Catalina:type=DataSource,path=/,host=localhost,class=javax.sql.DataSource,name="jdbc/mydb"
Any pointer would be appreciated.
Move your data source configuration to server.xml <GlobalNamingResources>. (And don't forget to add the driver jar file to tomcat lib).
Configure your context.xml so that all Application are aware of the global resource.
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<ResourceLink global="jdbc/mydb" name="jdbc/mydb" type="javax.sql.DataSource"/>
</Context>
Your mistake: user="xxxx", you need to write username="xxxx" instead.

Categories