You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by jk...@apache.org on 2006/07/28 17:20:39 UTC

svn commit: r426560 - in /tapestry/tapestry4/trunk: ./ tapestry-examples/TimeTracker/ tapestry-examples/TimeTracker/src/config/ tapestry-examples/TimeTracker/src/context/WEB-INF/ tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/

Author: jkuhnert
Date: Fri Jul 28 08:20:38 2006
New Revision: 426560

URL: http://svn.apache.org/viewvc?rev=426560&view=rev
Log:
Fixed TimeTracker to correctly resolve servlet context relative path to DatabaseInstaller so that it works when bundled as 
a war.

Added:
    tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/createDatabase.sql
Removed:
    tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/config/createDatabase.sql
Modified:
    tapestry/tapestry4/trunk/pom.xml
    tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/pom.xml
    tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/hivemodule.xml
    tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/timetracker.db.xml
    tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/DataSourceProxyFactory.java
    tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/DatabaseInstaller.java

Modified: tapestry/tapestry4/trunk/pom.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/pom.xml?rev=426560&r1=426559&r2=426560&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/pom.xml (original)
+++ tapestry/tapestry4/trunk/pom.xml Fri Jul 28 08:20:38 2006
@@ -176,6 +176,7 @@
                 <groupId>org.apache.tapestry</groupId>
                 <artifactId>tapestry-test</artifactId>
                 <version>4.1.0-SNAPSHOT</version>
+                <scope>test</scope>
             </dependency>
             <dependency>
                 <groupId>com.javaforge.tapestry</groupId>

Modified: tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/pom.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/pom.xml?rev=426560&r1=426559&r2=426560&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/pom.xml (original)
+++ tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/pom.xml Fri Jul 28 08:20:38 2006
@@ -78,6 +78,11 @@
             <artifactId>hsqldb</artifactId>
             <version>1.8.0.1</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.tapestry</groupId>
+            <artifactId>tapestry-test</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

Added: tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/createDatabase.sql
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/createDatabase.sql?rev=426560&view=auto
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/createDatabase.sql (added)
+++ tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/createDatabase.sql Fri Jul 28 08:20:38 2006
@@ -0,0 +1,20 @@
+create table projects (
+	project_id		int				generated by default as IDENTITY not null,
+	name			varchar(40)		not null,
+	PRIMARY KEY (project_id),
+	UNIQUE (name)
+);
+
+insert into projects(name) values ('Code Zeitgeist');
+insert into projects(name) values ('Zooland Systems');
+insert into projects(name) values ('Weedasher Industries');
+
+create table tasks (
+	task_id			int				generated by default as IDENTITY not null,
+	project_id		int				not null,
+	start_dt		timestamp		not null,
+	end_dt			timestamp		not null,
+	descr_txt		varchar(200)	not null,
+	PRIMARY KEY (task_id),
+	FOREIGN KEY (project_id) references projects(project_id)
+);

Modified: tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/hivemodule.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/hivemodule.xml?rev=426560&r1=426559&r2=426560&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/hivemodule.xml (original)
+++ tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/hivemodule.xml Fri Jul 28 08:20:38 2006
@@ -16,11 +16,7 @@
 -->
 
 <module id="timetracker" version="1.0.0" package="org.apache.tapestry.timetracker">
-    
-    <contribution configuration-id="hivemind.EagerLoad">
-        <load service-id="timetracker.db.DatabaseInstaller" />
-    </contribution>
-    
+
     <contribution configuration-id="tapestry.url.ServiceEncoders">
         <asset-encoder id="asset" path="/assets" />
         <extension-encoder id="extension" extension="svc" after="*"/>

Modified: tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/timetracker.db.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/timetracker.db.xml?rev=426560&r1=426559&r2=426560&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/timetracker.db.xml (original)
+++ tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/context/WEB-INF/timetracker.db.xml Fri Jul 28 08:20:38 2006
@@ -31,6 +31,7 @@
         <invoke-factory service-id="hivemind.BuilderFactory">
             <construct class="DataSourceProxyFactory" autowire-services="false">
                 <set-service property="dataSource" service-id="HsqlDataSource" />
+                <set-service property="installer" service-id="DatabaseInstaller" />
             </construct>
         </invoke-factory>
     </service-point>
@@ -65,10 +66,9 @@
     <service-point id="DatabaseInstaller" interface="DatabaseInstaller">
         Ensures the database has been created/initialised. 
         <invoke-factory service-id="hivemind.BuilderFactory">
-            <construct class="DatabaseInstaller" autowire-services="false"
-                       initialize-method="initialise" >
-                <set-service property="connection" service-id="HsqlConnection" />
-                <set property="filePath" value="src/config/createDatabase.sql" />
+            <construct class="DatabaseInstaller" autowire-services="true" >
+                <set-service property="globals" service-id="tapestry.globals.ApplicationGlobals" />
+                <set property="filePath" value="WEB-INF/createDatabase.sql" />
             </construct>
         </invoke-factory>
     </service-point>

Modified: tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/DataSourceProxyFactory.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/DataSourceProxyFactory.java?rev=426560&r1=426559&r2=426560&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/DataSourceProxyFactory.java (original)
+++ tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/DataSourceProxyFactory.java Fri Jul 28 08:20:38 2006
@@ -15,7 +15,6 @@
 
 import java.lang.reflect.Proxy;
 import java.sql.Connection;
-import java.sql.SQLException;
 
 import org.apache.commons.dbcp.BasicDataSource;
 import org.apache.commons.logging.Log;
@@ -31,14 +30,16 @@
  *
  * @author jkuhnert
  */
-public class DataSourceProxyFactory implements ServiceImplementationFactory, 
-RegistryShutdownListener
+public class DataSourceProxyFactory implements ServiceImplementationFactory, RegistryShutdownListener
 {
     /* logger */
     protected Log _log;
+    
     /* jdbc pool */
     protected BasicDataSource _dataSource;
     
+    private DatabaseInstaller _installer;
+    
     /**
      * {@inheritDoc}
      */
@@ -51,10 +52,12 @@
         try {
             Connection conn = _dataSource.getConnection();
             
+            _installer.initialise(conn);
+            
             return Proxy.newProxyInstance(this.getClass()
                     .getClassLoader(), new Class[] { Connection.class,
                 Discardable.class }, new JdbcConnectionProxy(_log, conn));
-        } catch (SQLException e) {
+        } catch (Exception e) {
             _log.fatal("Unable to create a new DB connection", e);
             throw new RuntimeException(e);
         }
@@ -79,5 +82,11 @@
     public void setDataSource(BasicDataSource dataSource)
     {
         _dataSource = dataSource;
+    }
+    
+    /** Injected. */
+    public void setInstaller(DatabaseInstaller installer)
+    {
+        _installer = installer;
     }
 }

Modified: tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/DatabaseInstaller.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/DatabaseInstaller.java?rev=426560&r1=426559&r2=426560&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/DatabaseInstaller.java (original)
+++ tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/jdbc/DatabaseInstaller.java Fri Jul 28 08:20:38 2006
@@ -22,6 +22,7 @@
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.tapestry.services.ApplicationGlobals;
 
 
 /**
@@ -35,26 +36,35 @@
 {
     /** File ISO format. */
     public static final String ISO_FORMAT = "ISO8859_1";
+    
     protected static Log _log = LogFactory.getLog(DatabaseInstaller.class);
     
-    /** threaded db conn. */
-    protected Connection _conn;
+    /** servlet context. */
+    protected ApplicationGlobals _globals;
+    
     /** db installer file path. */
     protected String _filePath;
     
+    private boolean _initialised = false;
+    
     /** default constructor. */
     public DatabaseInstaller() { }
     
     /**
      * Invoked to cause initialization of db checks.
      */
-    public void initialise()
+    public void initialise(Connection conn)
     throws Exception
     {
+        if (_initialised)
+            return;
+        
         assert _filePath != null;
         
-        if (!tablesExist())
-            createDatabase();
+        if (!tablesExist(conn))
+            createDatabase(conn);
+        
+        _initialised = true;
     }
     
     /**
@@ -62,14 +72,14 @@
      * @return True, if any row exists in a table called "projects".
      * @throws SQLException on error
      */
-    public boolean tablesExist()
+    public boolean tablesExist(Connection conn)
     throws SQLException
     {
         PreparedStatement ps = null;
         ResultSet rs = null;
         
         try {
-            ps = _conn.prepareStatement("select 'x' from INFORMATION_SCHEMA.SYSTEM_TABLES where TABLE_NAME = 'PROJECTS'");
+            ps = conn.prepareStatement("select 'x' from INFORMATION_SCHEMA.SYSTEM_TABLES where TABLE_NAME = 'PROJECTS'");
             rs = ps.executeQuery();
             
             return rs.next();
@@ -86,32 +96,33 @@
      * database. 
      * @throws Exception If any io/db errors occur.
      */
-    protected void createDatabase()
+    protected void createDatabase(Connection conn)
     throws Exception
     {
         _log.debug("createDatabase() creating database tables..");
         PreparedStatement ps = null;
         try {
             
-            ps = _conn.prepareStatement(FileUtils.readFileToString(new File(_filePath), ISO_FORMAT));
+            ps = conn.prepareStatement(FileUtils.readFileToString(
+                    new File(_globals.getServletContext().getRealPath(_filePath)), ISO_FORMAT));
+            
             ps.execute();
             
+            conn.commit();
         } catch (Throwable t) {
             _log.error("Error creating database.", t);
-            try { if (_conn != null) _conn.rollback(); } catch (Exception e) { }
+            try { if (conn != null) conn.rollback(); } catch (Exception e) { }
             throw new RuntimeException(t);
         } finally {
             try { if (ps != null) ps.close(); } catch (Exception e) { }
+            
         }
     }
     
-    /**
-     * Sets db connection.
-     * @param conn
-     */
-    public void setConnection(Connection conn)
+    /** Injected. */
+    public void setGlobals(ApplicationGlobals globals)
     {
-        _conn = conn;
+        _globals = globals;
     }
     
     /**