I will do at some point.. I 'll tidy up this one.. I just knew if i stuck it here it would be somewhere on the web and it could save a few folks some days working things out. The torque project as slick as it isn't as well documented as struts, in fact us struts users are really quite spoilt..

As it is I've been a little cyptic on some details, I'll be more likely to stick a demo app up someplace.

On Thursday, July 10, 2003, at 04:01 PM, James Childers wrote:

Thanks Mark. This should probably be archived somewhere outside of the mailing list. Do you have any plans to put it on a web page somewhere?

-= J

-----Original Message-----
From: Mark Lowe [mailto:[EMAIL PROTECTED]
Sent: Thursday, July 10, 2003 6:53 AM
To: Struts List
Cc: Garry Dillon; Jeremy Pocock; Simon Brown; Simon Fox;
[EMAIL PROTECTED]
Subject: [OT] Setting up torque with struts


This is how to configure torque to work with struts. a few weeks or ,perhaps, months ago, Ville (vilho[at]students[dot]cc[dot]tut[dot]fi) after he'd spent a week bringing together all the required resources, he kindly mailed me some instructions on how to do so. I tested his instructions and got stuff running. I was waiting for him to post something on this but I guess he hasn't had time etc, so i thought I'd do it with any additional knowledge I've gained through going through the motions.

You can read about torque here:
http://db.apache.org/torque

...and struts here...
http://jakarta.apache.org/struts

Assumptions
I'll also assume that you're familiar with webapp
development, and your
development follows this organization:

http://jakarta.apache.org/tomcat/tomcat-4.1-doc/appdev/index.html

..and that you've got a compliant container running etc.

In anticipation of any questions, I don't know how to
configure IDE's
if you know your IDE then you should know how to set it up. I'd
recommend a terminal, ant an a trusty text editor.

1. Download a stable release of torque [3.02 seems to be the
puppy at
this time].

2. Untar torque and put it somewhere where you like to work. (e.g.
~/Projects)..

3. Copy Torque's jar files to /web/WEB-INF/lib
This way all the relevant jars are in the right place, its a
bit messy
as there are several versions of some classes, I anticipated having
problems here but none thus far.

4. Edit/Add these properties to build-torque.xml and
[myproject]-schema.xml (see docs)

<property name="torque.output.dir" value="../src"/>
<property name="torque.doc.dir" value="../doc"/>
<property name="lib.dir" value="../web/WEB-INF/lib"/>

The paths will depend on where you prefer to organize your projects.

You can add these in build.properties.. I prefer doing it in
XML, but
I'm sure there are good reasons for using the properties file instead.

e.g. in [myproject]-schema.xml

<database name="sparrow" package=com.sparrow.torque">

rather than defining package in a properties file, i think its more
maintainable as a attribute of database.

Also..
make sure that your jdbc jar is specified in your
"torque-classpath"
file set list and avoid wild carding. Both struts and torque
make use
of some of the commons sub projects, torque has a greater
dependency on
some old versions of commons-collections and such like. To
get running
I just use both together and i've had no problems as yet, I'd
like to
get the time and look through the torque source and perhaps make it
less dependent on deprecated code.

5. Then run.

ant -f build-torque.xml

Hopefully the OM classes have been generated to your source directory, and thus can be compiled with you servlets etc.

e.g.

/src/java/com/sparrow/struts
/src/java/com/sparrow/torque
/src/sql

the following tasks fire up the sql scripts and do the DB stuff.
ant -f build-torque.xml create-db
ant -f build-torque.xml id-table-init-sql
ant -f build-torque.xml insert-sql

6. Now create a servlet that fires up you torque generated OM. Also copy the Torque.properties file here. I have a different package name than my OM so I can remove all the generated stuff without destroying my servlet and properties file. This servlet isn't anything new there are loads of resources on the torque site referencing this, I just changed the name from InitTorque because 'start' seems a perfectly good word for folks to read.

package com.sparrow.servlets;

import java.io.InputStream;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.*;
import javax.servlet.*;

import org.apache.torque.Torque;
import org.apache.torque.TorqueException;
import org.apache.commons.configuration.PropertiesConfiguration;


public class StartTorque extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); try { InputStream configStream = getServletContext().getResourceAsStream(config.getInitParamete r("config" )); PropertiesConfiguration c = new PropertiesConfiguration(); c.load( configStream ); Torque.init( c ); } catch (IOException e) { throw new ServletException( e.toString() ); } catch ( TorqueException e ) { throw new ServletException( e.toString() ); } } }

Edit the Torque.properties file to suit your needs. This will
read the
values in Torque.properties file (covered in Torque docs).

e.g.
##i love sparrows!!!!!!
torque.database.default=sparrow
torque.database.sparrow.adapter=mysql

## Using Jdbc2Pool
torque.dsfactory.nsalliance.factory =
org.apache.torque.dsfactory.Jdbc2PoolDataSourceFactory
torque.dsfactory. sparrow.pool.defaultMaxActive = 10
torque.dsfactory. sparrow.pool.testOnBorrow = true

##change to select * from dual for Oracle etc
torque.dsfactory. sparrow.pool.validationQuery = SELECT 1
torque.dsfactory. sparrow.connection.driver = com.mysql.jdbc.Driver
torque.dsfactory. sparrow.connection.url =
jdbc:mysql://localhost:3306/sparrow
torque.dsfactory. sparrow.connection.user = dbuser
torque.dsfactory. sparrow.connection.password = sparrow

In web.xml

<servlet>
   <servlet-name>StartTorque</servlet-name>
   <servlet-class>com.sparrow.servlets.StartTorque</servlet-class>
   <init-param>
     <param-name>config</param-name>

<param-value>/WEB-INF/classes/com/sparrow/servlets/Torque.prop
erties</
param-value>
   </init-param>
   <load-on-startup>3</load-on-startup>
</servlet>

7. Finally you should be able to build your whole project using your
build file that you use for your webapp. You'll just have to import
your OM classes into your actions and the torque docs should
give you a
clear run from here.

e.g.
import com.sparrow.torque.*;
import org.apache.torque.util.Criteria;

Again torque docs cover using criteria etc.

Notes.
If you're building an application from scratch you'll be able to
generate your OM from your xml schema. If you're using an
existing DB
then you can generate your schema and then generate your OM.
If you've
had DBA types geeking around with outer joins there can be issues.
Torque doesn't really support outer joins.

I'm non beard-sporting, partial to quiche and I pride myself
on my low
IQ, so please don't post me anything too clever cos i'll get
confused
start dribbling and talking about sparrows :)


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to