activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r412110 - in /incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store: DefaultPersistenceAdapterFactory.java jdbc/DataSourceSupport.java jdbc/JDBCPersistenceAdapter.java
Date Tue, 06 Jun 2006 13:06:54 GMT
Author: jstrachan
Date: Tue Jun  6 06:06:54 2006
New Revision: 412110

URL: http://svn.apache.org/viewvc?rev=412110&view=rev
Log:
minor refactor to reuse the lazy construction logic of the DataSource across the journaled
and pure JDBC persistence adapter

Added:
    incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DataSourceSupport.java
  (with props)
Modified:
    incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/DefaultPersistenceAdapterFactory.java
    incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/JDBCPersistenceAdapter.java

Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/DefaultPersistenceAdapterFactory.java
URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/DefaultPersistenceAdapterFactory.java?rev=412110&r1=412109&r2=412110&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/DefaultPersistenceAdapterFactory.java
(original)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/DefaultPersistenceAdapterFactory.java
Tue Jun  6 06:06:54 2006
@@ -19,29 +19,26 @@
 import java.io.File;
 import java.io.IOException;
 
-import javax.sql.DataSource;
 
 import org.apache.activeio.journal.Journal;
 import org.apache.activeio.journal.active.JournalImpl;
+import org.apache.activemq.store.jdbc.DataSourceSupport;
 import org.apache.activemq.store.jdbc.JDBCAdapter;
 import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
 import org.apache.activemq.store.jdbc.Statements;
 import org.apache.activemq.store.journal.JournalPersistenceAdapter;
 import org.apache.activemq.store.journal.QuickJournalPersistenceAdapter;
 import org.apache.activemq.thread.TaskRunnerFactory;
-import org.apache.derby.jdbc.EmbeddedDataSource;
 
 /**
  * Factory class that can create PersistenceAdapter objects.
  *
  * @version $Revision: 1.4 $
  */
-public class DefaultPersistenceAdapterFactory implements PersistenceAdapterFactory {
+public class DefaultPersistenceAdapterFactory extends DataSourceSupport implements PersistenceAdapterFactory
{
     
     private int journalLogFileSize = 1024*1024*20;
     private int journalLogFiles = 2;
-    private File dataDirectory;
-    private DataSource dataSource;
     private TaskRunnerFactory taskRunnerFactory;
     private Journal journal;
     private boolean useJournal=true;
@@ -50,11 +47,11 @@
     private JDBCPersistenceAdapter jdbcPersistenceAdapter = new JDBCPersistenceAdapter();
     
     public PersistenceAdapter createPersistenceAdapter() throws IOException {
-        File dataDirectory = getDataDirectory();                
         jdbcPersistenceAdapter.setDataSource(getDataSource());
         
-        if( !useJournal )
+        if( !useJournal ) {
             return jdbcPersistenceAdapter;
+        }
         
         // Setup the Journal
         if( useQuickJournal ) {
@@ -64,17 +61,6 @@
         }
     }
 
-    public File getDataDirectory() {
-        if( dataDirectory==null ) {
-            dataDirectory = new File("activemq-data");
-        }
-        return dataDirectory;
-    }
-
-    public void setDataDirectory(File dataDirectory) {
-        this.dataDirectory = dataDirectory;
-    }
-
     public int getJournalLogFiles() {
         return journalLogFiles;
     }
@@ -91,17 +77,6 @@
         this.journalLogFileSize = journalLogFileSize;
     }
     
-    public DataSource getDataSource() throws IOException {
-        if (dataSource == null) {
-            dataSource = createDataSource();
-        }
-        return dataSource;
-    }
-
-    public void setDataSource(DataSource dataSource) {
-        this.dataSource = dataSource;
-    }
-
     public JDBCPersistenceAdapter getJdbcAdapter() {
         return jdbcPersistenceAdapter;
     }
@@ -173,21 +148,6 @@
     }
     public void setStatements(Statements statements) {
         jdbcPersistenceAdapter.setStatements(statements);
-    }
-
-    // Implementation methods
-    // -------------------------------------------------------------------------
-    protected DataSource createDataSource() throws IOException {
-        
-        // Setup the Derby datasource.
-        System.setProperty("derby.system.home", getDataDirectory().getCanonicalPath());
-        System.setProperty("derby.storage.fileSyncTransactionLog", "true");
-        System.setProperty("derby.storage.pageCacheSize", "100");
-        
-        final EmbeddedDataSource ds = new EmbeddedDataSource();
-        ds.setDatabaseName("derbydb");
-        ds.setCreateDatabase("create");
-        return ds;
     }
 
     /**

Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DataSourceSupport.java
URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DataSourceSupport.java?rev=412110&view=auto
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DataSourceSupport.java
(added)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DataSourceSupport.java
Tue Jun  6 06:06:54 2006
@@ -0,0 +1,82 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.store.jdbc;
+
+import org.apache.derby.jdbc.EmbeddedDataSource;
+
+import javax.sql.DataSource;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * A helper class which provides a factory method to create a default
+ * {@link DataSource) if one is not provided.
+ * 
+ * @version $Revision$
+ */
+public class DataSourceSupport {
+
+    private File dataDirectory;
+    private DataSource dataSource;
+
+    public DataSourceSupport() {
+    }
+
+    public DataSourceSupport(DataSource dataSource) {
+        this.dataSource = dataSource;
+    }
+
+    public File getDataDirectory() {
+        if (dataDirectory == null) {
+            dataDirectory = new File("activemq-data");
+        }
+        return dataDirectory;
+    }
+
+    public void setDataDirectory(File dataDirectory) {
+        this.dataDirectory = dataDirectory;
+    }
+
+    public DataSource getDataSource() throws IOException {
+        if (dataSource == null) {
+            dataSource = createDataSource();
+            if (dataSource == null) { 
+                throw new IllegalArgumentException("No dataSource property has been configured");
+            }
+        }
+        return dataSource;
+    }
+
+    public void setDataSource(DataSource dataSource) {
+        this.dataSource = dataSource;
+    }
+
+    protected DataSource createDataSource() throws IOException {
+
+        // Setup the Derby datasource.
+        System.setProperty("derby.system.home", getDataDirectory().getCanonicalPath());
+        System.setProperty("derby.storage.fileSyncTransactionLog", "true");
+        System.setProperty("derby.storage.pageCacheSize", "100");
+
+        final EmbeddedDataSource ds = new EmbeddedDataSource();
+        ds.setDatabaseName("derbydb");
+        ds.setCreateDatabase("create");
+        return ds;
+    }
+
+}

Propchange: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DataSourceSupport.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DataSourceSupport.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DataSourceSupport.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/JDBCPersistenceAdapter.java
URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/JDBCPersistenceAdapter.java?rev=412110&r1=412109&r2=412110&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/JDBCPersistenceAdapter.java
(original)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/JDBCPersistenceAdapter.java
Tue Jun  6 06:06:54 2006
@@ -57,13 +57,12 @@
  * 
  * @version $Revision: 1.9 $
  */
-public class JDBCPersistenceAdapter implements PersistenceAdapter {
+public class JDBCPersistenceAdapter extends DataSourceSupport implements PersistenceAdapter
{
 
     private static final Log log = LogFactory.getLog(JDBCPersistenceAdapter.class);
     private static FactoryFinder factoryFinder = new FactoryFinder("META-INF/services/org/apache/activemq/store/jdbc/");
 
     private WireFormat wireFormat = new OpenWireFormat();
-    private DataSource dataSource;
     private Statements statements;
     private JDBCAdapter adapter;
     private MemoryTransactionStore transactionStore;
@@ -76,24 +75,31 @@
     }
 
     public JDBCPersistenceAdapter(DataSource ds, WireFormat wireFormat) {
-        this.dataSource = ds;
+        super(ds);
         this.wireFormat = wireFormat;
     }
 
     public Set getDestinations() {
         // Get a connection and insert the message into the DB.
-        TransactionContext c = getTransactionContext();
+        TransactionContext c = null;
         try {
+            c = getTransactionContext();
             return getAdapter().doGetDestinations(c);
-        } catch (IOException e) {
+        }
+        catch (IOException e) {
             return Collections.EMPTY_SET;
-        } catch (SQLException e) {
-            JDBCPersistenceAdapter.log("JDBC Failure: ",e);
+        }
+        catch (SQLException e) {
+            JDBCPersistenceAdapter.log("JDBC Failure: ", e);
             return Collections.EMPTY_SET;
-        } finally {
-            try {
-                c.close();
-            } catch (Throwable e) {
+        }
+        finally {
+            if (c != null) {
+                try {
+                    c.close();
+                }
+                catch (Throwable e) {
+                }
             }
         }
     }
@@ -172,20 +178,26 @@
     }
 
     public void cleanup() {
-        TransactionContext c = getTransactionContext();
+        TransactionContext c = null;
         try {
             log.debug("Cleaning up old messages.");
             c = getTransactionContext();
             getAdapter().doDeleteOldMessages(c);
-        } catch (IOException e) {
+        }
+        catch (IOException e) {
             log.warn("Old message cleanup failed due to: " + e, e);
-        } catch (SQLException e) {
+        }
+        catch (SQLException e) {
             log.warn("Old message cleanup failed due to: " + e);
-            JDBCPersistenceAdapter.log("Failure Details: ",e);
-        } finally {
-            try {
-                c.close();
-            } catch (Throwable e) {
+            JDBCPersistenceAdapter.log("Failure Details: ", e);
+        }
+        finally {
+            if (c != null) {
+                try {
+                    c.close();
+                }
+                catch (Throwable e) {
+                }
             }
             log.debug("Cleanup done.");
         }
@@ -260,14 +272,6 @@
         this.adapter.setStatements(getStatements());
     }
 
-    public DataSource getDataSource() {
-        return dataSource;
-    }
-
-    public void setDataSource(DataSource dataSource) {
-        this.dataSource = dataSource;
-    }
-
     public WireFormat getWireFormat() {
         return wireFormat;
     }
@@ -276,24 +280,21 @@
         this.wireFormat = wireFormat;
     }
 
-    public TransactionContext getTransactionContext(ConnectionContext context) {
+    public TransactionContext getTransactionContext(ConnectionContext context) throws IOException
{
         if (context == null) {
             return getTransactionContext();
         } else {
             TransactionContext answer = (TransactionContext) context.getLongTermStoreContext();
             if (answer == null) {
-                answer = new TransactionContext(dataSource);
+                answer = new TransactionContext(getDataSource());
                 context.setLongTermStoreContext(answer);
             }
             return answer;
         }
     }
 
-    public TransactionContext getTransactionContext() {
-        if (dataSource == null) { 
-            throw new IllegalArgumentException("No dataSource property has been configured");
-        }
-        return new TransactionContext(dataSource);
+    public TransactionContext getTransactionContext() throws IOException {
+        return new TransactionContext(getDataSource());
     }
 
     public void beginTransaction(ConnectionContext context) throws IOException {



Mime
View raw message