Javamail as JNDI resource in tomcat - java

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.

Related

lookup jndi resource without resource-ref definition

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

Mimic Tomcat's JNDI datasource for Crystal Reports Java API in stand-alone program

Apparently this Tomcat data source gets registered in JNDI. The crystal reports API will then find the "jdbc/TESTDB" (below) under the JNDI name of "TESTDB". What is the easiest way to set this up in a stand-alone program? I have several data-sources reports, so my preferred method is to configure all JNDI entries so any that are needed will be referenced and used on-demand. Existing reports will not change and reference the ODBC connections.
I need to mimic this JNDI Tomcat entry in a stand-alone application. I'm not concerned about any of the max parameters:
<Context>
<Resource name="jdbc/TESTDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="user" password="passwd" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#dbserver:1521:db1"/>
</Context>
This is the crystal reports example that shows this setup in action using Tomcat:
http://www.javathinking.com/2011/09/using-the-crystal-reports-java-api-to-generate-pdf/
You need a JNDI provider. SimpleJNDI - http://code.google.com/p/osjava/wiki/SimpleJNDI - is usable in a application scenario outside a Java EE container.
You will need to investigate how to represent the data structure that Crystal Report needs, in the formats supported by SimpleJNDI.

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.

JBoss JNP as standalone server and using Resource configs from tomcat's context.xml

I have a couple of modules which now have the need of JNDI.
One of these modules runs via Apache Tomcat while the rest run standalone as J2SE application.
I was able to configure the module which uses tomcat without a problem and I've googled a bit and gathered that JBoss JNP can be a good standalone JNDI server to use.
What I fail to understand is how I can reuse the resources definitions which I have already configured (for my module which runs via tomcat in the context.xml file).
Let's say I have the following resource defined in the XML file:
<Resource name="jdbc/dataSource" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mchange.v2.c3p0.ComboPooledDataSource"
url="jdbc:sqlserver://******
username="**" password="**"/>
And the relevant code which starts the JNP is:
System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
System.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
NamingBeanImpl jnpServer = new NamingBeanImpl();
jnpServer.start();
How can I have an InitialContext instance identify the jdbc/dataSource binding?
I don't think you can.
You probably can define your jdbs resource in jboss naming context and look it up in your tomcat web app.
More info here: http://www.amitysolutions.com.au/documents/JBossTomcatJNDI-technote.pdf

How to configure tomcat 6.0 for mysql

I'm using Tomcat 6.0, and I want to know how can I configure Tomcat's server.xml file to connect to mysql database, and enable form based authentication in java.
I'm currently using mysql 5.1, and I've already downloaded mysql connector jar file, and put in lib directory of Tomcat.
I'm guessing you want Tomcat to create connection pool to MySQL database. In that case, you don't need to configure server.xml file. In the context.xml file you need to add a <Resource> element, something like this:
<Resource name="jdbc/MySQLPool" auth="Container" type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
maxActive="100" maxIdle="30" maxWait="10000"
username="USERNAME" password="PASSWORD"
driverClassName="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"
url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=Cp1251"
removeAbandoned="true" />
(I'm not sure if that's the correct driverClassName for MySQL, but your Resource should look somewhat like this).
For more info, try checking Tomcat's documentation on JNDI Resources and JDBC DataSources.
Typically context.xml and server.xml are separated, and you usually configure a data source on web-app level, that is in the context of that web app. The reason for that is that a data source connects not so much to a server but to a database within that server, and having multiple apps accessing the same database is not always a good idea (if you didn't design the apps for that).
That said, have a look at this tomcat wiki page which describes what you want (or what I think you want).
For authentication check out this thread on velocity reviews.

Categories