Hi folks
I've implemented a TurbineService for Castor (I know, one more object
persistency api). But I thought it is usefull for me and maybe for others,
too. I've attached those files at the end of this mail. My last mail with
the files attached as a zip has bounced because of:
"Your message could not be posted to the Turbine list because the message
seemed to contain an enclosure."
The TurbineResources.properties has to be extended with the following
properties:
# Castor Service Class name
services.TurbineCastorService.classname=org.apache.turbine.services.castor.TurbineCastorService
#
# where the castor log goes
services.TurbineCastorService.properties.logfile=/tmp/castor.log
#
# what the prefix in the castor log should be
services.TurbineCastorService.properties.logfileprefix=turbinecastor
#
# where the mapping file for the database and objects is located
services.TurbineCastorService.properties.databasefile=/tmp/database.xml
#
# what the name of the database is (the name of the database tag in the mapping)
services.TurbineCastorService.properties.databasename=turbine
You get a Castor Database object by the following code snippet:
TurbineServices ts = TurbineServices.getInstance();
CastorService cs = (CastorService) ts.getService (CastorService.CASTOR_SERVICE_NAME);
Database db = cs.getDatabase ();
Castor needs a RDBMS with transaction support. I've tested it with
PostgreSQL under WinNT and Linux. Castor is located at http://castor.exolab.org.
Have fun.
Giacomo
BTW: 1. I've found a bug in the IDBroker.java code. The patch is below (my first
mail with the files attached was bounce because of enclosures)
2. I've taken a fresh snapshot from the CVS and found that it does not
compile because of BaseJspScreen.java not implementing the build method.
--
PWR Organisation & Entwicklung Tel: +41 (0)1 856 2202
Giacomo Pati Fax: +41 (0)1 856 2201
Hintereichenstrasse 7 Mailto:[EMAIL PROTECTED]
CH-8166 Niederweningen Web: http://www.pwr.ch
-------------------- START OF CastorService.java -------------------------
package org.apache.turbine.services.castor;
/*
* Copyright (c) 1997-1999 The Java Apache Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Java Apache
* Project for use in the Apache JServ servlet engine project
* <http://java.apache.org/>."
*
* 4. The names "Apache JServ", "Apache JServ Servlet Engine", "Turbine",
* "Apache Turbine", "Turbine Project", "Apache Turbine Project" and
* "Java Apache Project" must not be used to endorse or promote products
* derived from this software without prior written permission.
*
* 5. Products derived from this software may not be called "Apache JServ"
* nor may "Apache" nor "Apache JServ" appear in their names without
* prior written permission of the Java Apache Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Java Apache
* Project for use in the Apache JServ servlet engine project
* <http://java.apache.org/>."
*
* THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Java Apache Group. For more information
* on the Java Apache Project and the Apache JServ Servlet Engine project,
* please see <http://java.apache.org/>.
*
*/
// Java stuff
import java.io.PrintWriter;
// Turbine stuff
import org.apache.turbine.services.Service;
// Castor stuff
import org.exolab.castor.jdo.JDO;
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.DatabaseNotFoundException;
import org.exolab.castor.jdo.PersistenceException;
/**
* Implementations of the CastorService interface.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a>
*/
public interface CastorService extends Service {
public static final String CASTOR_SERVICE_NAME = "TurbineCastorService";
public static final String LOGFILE_PROPERTY = "logfile";
public static final String LOGPREFIX_PROPERTY = "logprefix";
public static final String DEFAULT_LOGPREFIX = "TurbineCastor";
public static final String DATABASEFILE_PROPERTY = "databasefile";
public static final String DATABASENAME_PROPERTY = "databasename";
public PrintWriter getCastorLogger ();
public JDO getJDO ();
public Database getDatabase () throws DatabaseNotFoundException,
PersistenceException;
}
-------------------- END OF CastorService.java -------------------------
-------------------- START OF TurbineCastorService.java -------------------------
package org.apache.turbine.services.castor;
/*
* Copyright (c) 1997-1999 The Java Apache Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Java Apache
* Project for use in the Apache JServ servlet engine project
* <http://java.apache.org/>."
*
* 4. The names "Apache JServ", "Apache JServ Servlet Engine", "Jyve",
* "Apache Jyve", "Jyve Project", "Apache Jyve Project" and
* "Java Apache Project" must not be used to endorse or promote products
* derived from this software without prior written permission.
*
* 5. Products derived from this software may not be called "Apache JServ"
* nor may "Apache" nor "Apache JServ" appear in their names without
* prior written permission of the Java Apache Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Java Apache
* Project for use in the Apache JServ servlet engine project
* <http://java.apache.org/>."
*
* THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Java Apache Group. For more information
* on the Java Apache Project and the Apache JServ Servlet Engine project,
* please see <http://java.apache.org/>.
*
*/
// Java Stuff
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Enumeration;
import java.util.Properties;
// Castor Stuff
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.DatabaseNotFoundException;
import org.exolab.castor.jdo.JDO;
import org.exolab.castor.jdo.OQLQuery;
import org.exolab.castor.jdo.PersistenceException;
import org.exolab.castor.jdo.QueryException;
import org.exolab.castor.jdo.TransactionNotInProgressException;
import org.exolab.castor.util.Logger;
// Turbine Stuff
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.services.castor.TurbineCastorService;
import org.apache.turbine.util.Log;
// SAX Stuff
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
/**
* This is a Service that offers access to the object to relational
* mapping from <a href="http://castor.exolab.org">Castor</a>.
* Here's an example of how you might use it:<br>
* <code><pre>
* TurbineServices ts = TurbineServices.getInstance();
* CastorService cs = (CastorService) ts.getService
(CastorService.CASTOR_SERVICE_NAME);
* Database db = cs.getDatabase ();
* db.begin();
* ...
* db.commit();
* db.close();
* </pre></code>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a>
*/
public class TurbineCastorService extends TurbineBaseService
implements CastorService
{
/** The name of the database to use */
private String databasename = null;
/** The mapping file for the database */
private String databasefile = null;
/** Castor logger */
private PrintWriter logger = null;
/**
* Called the first time the Service is used.
*/
public void init()
{
if ( getInit() )
{
return;
}
try
{
Log.note (CASTOR_SERVICE_NAME + " init()....starting!");
initCastor();
setInit(true);
Log.note (CASTOR_SERVICE_NAME + " init()....finished!");
}
catch (Exception e)
{
Log.error ( "Cannot initialize " + CASTOR_SERVICE_NAME);
Log.error (e);
}
}
private void initCastor () throws Exception {
Properties props = getProperties();
String logprefix = props.getProperty (LOGPREFIX_PROPERTY);
if (logprefix == null) {
logprefix = DEFAULT_LOGPREFIX;
}
String logfile = props.getProperty (LOGFILE_PROPERTY);
if (logfile == null) {
Log.warn ("CastorService no LogFile property specified");
} else {
logger = new Logger (new FileWriter (logfile)).setPrefix(logprefix);
}
databasename = props.getProperty (DATABASENAME_PROPERTY);
if (databasename == null) {
throw new Exception ("TurbineCastor: missing databasename propertiy");
}
databasefile = props.getProperty (DATABASEFILE_PROPERTY);
if (databasefile == null) {
throw new Exception ("TurbineCastor: missing databasefile propertiy");
}
JDO.loadConfiguration (new InputSource (databasefile),
new LocalResolver(databasefile),
getClass().getClassLoader());
}
public PrintWriter getCastorLogger () {
return logger;
}
public JDO getJDO () {
JDO jdo = new JDO (databasename);
jdo.setLogWriter (logger);
return jdo;
}
public Database getDatabase () throws DatabaseNotFoundException,
PersistenceException {
return this.getJDO().getDatabase();
}
class LocalResolver implements EntityResolver {
private String prefix = "unknown";
public LocalResolver (String databasefile) {
super ();
this.prefix = databasefile.substring (0, databasefile.lastIndexOf
(File.separatorChar)+1);
}
public InputSource resolveEntity (String publicId, String systemId) throws
FileNotFoundException {
return new InputSource (new FileReader (prefix + systemId));
}
}
}
-------------------- END OF TurbineCastorService.java -------------------------
-------------------- START OF PATCH -------------------------
Index: src/java/org/apache/turbine/util/db/IDBroker.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/turbine/util/db/IDBroker.java,v
retrieving revision 1.6
diff -u -r1.6 IDBroker.java
--- src/java/org/apache/turbine/util/db/IDBroker.java 2000/03/27 23:06:06 1.6
+++ src/java/org/apache/turbine/util/db/IDBroker.java 2000/04/18 19:38:09
@@ -287,6 +287,7 @@
ResultSet rs = statement.executeQuery( stmt.toString() );
int[] results = new int[2];
+ rs.next();
results[0] = rs.getInt(1); // next_id
results[1] = rs.getInt(2); // quantity
return results;
@@ -323,4 +324,4 @@
}
}
-}
\ No newline at end of file
+}
-------------------- END OF PATCH -------------------------
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Problems?: [EMAIL PROTECTED]