You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Rob Gregory <Ro...@RosesGroup.co.uk> on 2006/01/18 22:44:14 UTC

RE: Dyamic datasource configuration (2.1.7) is this possible? [JNDI]

Thanks for the advice Geert and for taking it the time to provide a complete
solution/example for others to follow.

Unfortunately, with the exception of the flowscript I have been doing this
approach from the start with the exception of duplicating database
connection details in both cocoon and our own context.xml. I say
unfortunately because my original approach is no longer acceptable and I
really can't get my alternative to work! 

We provide an application that utilises the excellent benefits of Cocoon
while still using our own in house framework. I need to provide datasources
on-the-fly and on-demand during servlet startup. I think I can do this via
Tomcat by manually creating the pools and binding them to Tomcat's initial
context via JNDI (I'm also struggling to get this working as the context is
read-only but I think binding elsewhere will work?!?). I have also managed
to get Cocoon to use the same J2EE binding that I provide in Tomcat.

Problem is that my solution for dynamic means it can't be binded to the
standard JNDI namespace of java:comp/env/jdbc as this seems to be read-only
and Cocoon config of:-

   <j2ee name="logindb">
     <dbname>logindb</dbname>
   </j2ee>

Doesn't specify the jndi lookup location (java:comp/env/jdbc) so can't find
my alternative binding.

I not sure if my problem is with Tomcat, Cocoon or my lack of knowledge on
JNDI. But after trying to explain it I'm now thinking it's my miss
understanding of JNDI and try to get the dynamically created datasource
registered in the correct location java:comp/env/jdbc some other way and
ignore the Tomcat read-only context.

Food for Thought.

Thanks again for your time.

Rob





-----Original Message-----
From: Geert Josten [mailto:Geert.Josten@daidalos.nl] 
Sent: 18 January 2006 07:07
To: users@cocoon.apache.org
Subject: Re: Dyamic datasource configuration (2.1.7) is this possible?

> After posting I thought my description may be a little vague...

(snip)

> Any keywords for Google or code samples are more than welcome in my
pursuit
> to get this working...

I'll just supply some code snippets, they might give you new ideas.. (or
not) :-P

One additional note: we decided to code pretty much all business logic in
plain Java and connect to 
these classes from FlowScript. (that is really easy)

Tomcat 5.0 server.xml config snippets (I removed customer specific details):
   ...
   <GlobalNamingResources>
     <!-- 1. Define the global datasource (MYDS) -->
     <Resource name="jdbc/MYDS" auth="Container" scope="Shareable"
type="javax.sql.DataSource"/>

	(you can probably name as many as you like, but dynamically as in
without restarting tomcat I don't 
know..)

     <!-- 2. Define the datasource config params -->
     <ResourceParams name="jdbc/MYDS">
       <parameter>
         <name>driverClassName</name>
         <value>oracle.jdbc.OracleDriver</value>
       </parameter>
       <parameter>
         <name>url</name>
 
<value>jdbc:oracle:thin:@myoracleserver:myoracleport:myoracledbname</value>
       </parameter>
       <parameter>
         <name>username</name>
         <value>myuser</value>
       </parameter>
       <parameter>
         <name>password</name>
         <value>mypassword</value>
       </parameter>
       <parameter>
         <name>maxActive</name>
         <value>20</value>
       </parameter>
       <parameter>
         <name>maxIdle</name>
         <value>10</value>
       </parameter>
       <parameter>
         <name>maxWait</name>
         <value>-1</value>
       </parameter>
     </ResourceParams>
   </GlobalNamingResources>
   ...
   <Service name="Catalina">
     <Connector port="8080"/>
     <Engine name="Catalina" defaultHost="localhost">
       <Logger className="org.apache.catalina.logger.FileLogger"/>
       <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
       <Host name="localhost" appBase="webapps">

         <!-- 3. Define a resourcelink in the defaultcontext that links to
the global datasource -->
         <DefaultContext>
           <ResourceLink name="jdbc/MYDS" global="jdbc/MYDS"
type="javax.sql.DataSource"/>
         </DefaultContext>

         <!-- 4. Optional. Define an extra context that points to the
webapps folder in the 
project-development environment (for easy reloading) -->
         <Context path="/test_site" docBase="C:\Projects\cocoon\webapp"/>
       </Host>
     </Engine>
   </Service>
   ...

Cocoon's web.xml snippet:
   ...
   <resource-ref>
     <res-ref-name>jdbc/MYDS</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
   </resource-ref>
   <env-entry>
     <env-entry-name>datasource.ref</env-entry-name>
     <env-entry-value>jdbc/MYDS</env-entry-value>
     <env-entry-type>java.lang.String</env-entry-type>
   </env-entry>
   <env-entry>
     <env-entry-name>database.sortingkey.default</env-entry-name>
     <env-entry-value>5-1;4-0;2-1</env-entry-value>
     <env-entry-type>java.lang.String</env-entry-type>
   </env-entry>
   ...

Java code that creates a DataSource by looking up a setting from web.xml:
	...
         Context context = new InitialContext();
         Context env = (Context) context.lookup("java:comp/env");

         // lookup the datasource reference
         String dataSourceRef = (String) config.get(GPS_AS_DATASOURCE_REF);

         // get the datasource
         dataSource = (DataSource) env.lookup(dataSourceRef);
	...

The java-classes are wrapped in a jar, dropped in the web-inf/lib/ directory
of Cocoon and in 
FlowScript we do an importPackage("Package.our.package.path") and create and
access objects from 
them just as you would create and access any other class in Flowscript.

So you see, this isn't really Cocoon-specific. Nor does it tell how to use
the datasource from 
cocoon components. On the other hand. You could handle database connectivity
solely in Flowscript 
and pass any data relevant to components to them through parameters and
alike..

HTH,
Geert

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


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