servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r394253 - in /incubator/servicemix/trunk: servicemix-core/src/main/java/org/apache/servicemix/store/ servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/ servicemix-core/src/main/java/org/apache/servicemix/store/memory/ servicemi...
Date Sat, 15 Apr 2006 04:25:57 GMT
Author: gnodet
Date: Fri Apr 14 21:25:52 2006
New Revision: 394253

URL: http://svn.apache.org/viewcvs?rev=394253&view=rev
Log:
Add a JDBC based Store implementation

Added:
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/JdbcStoreFactory.java
    incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/store/
    incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/store/jdbc/
    incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTest.java
Modified:
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/Store.java
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/StoreFactory.java
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/memory/MemoryStore.java
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/memory/MemoryStoreFactory.java
    incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/EIPEndpoint.java
    incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/support/XPathPredicate.java

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/Store.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/Store.java?rev=394253&r1=394252&r2=394253&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/Store.java
(original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/Store.java
Fri Apr 14 21:25:52 2006
@@ -17,20 +17,60 @@
 
 import java.io.IOException;
 
+/**
+ * A Store is an interface representing a storage where objects can be
+ * put and retrieved.  A store can support different features, mainly
+ * persistence, clustered or transactional.
+ * 
+ *  A store is not designed to be a thread-safe map.  If a user tries to
+ *  store an object with an existing id, the behavior is undefined.
+ *  
+ * @author gnodet
+ */
 public interface Store {
 
     String PERSISTENT = "Persistent";
     
     String CLUSTERED = "Clustered";
     
+    String TRANSACTIONAL = "Transactional";
+    
+    /**
+     * Returns true if the store implementation supports the given feature.
+     * @param name the feature to check
+     * @return <code>true</code> if the feature is supported
+     */
     public boolean hasFeature(String name);
     
+    /**
+     * Put an object in the store under the given id.
+     * This method must be used with caution and the behavior is
+     * unspecified if an object already exist for the same id.
+     *  
+     * @param id the id of the object to store
+     * @param data the object to store
+     * @throws IOException if an error occurs
+     */
     public void store(String id, Object data) throws IOException;
     
+    /**
+     * Put an object into the store and return the unique id that
+     * may be used at a later time to retrieve the object.
+     * 
+     * @param data the object to store
+     * @return the id of the object stored
+     * @throws IOException if an error occurs
+     */
     public String store(Object data) throws IOException;
     
+    /**
+     * Loads an object that has been previously stored under the specified key.
+     * The object is removed from the store.
+     * 
+     * @param id the id of the object
+     * @return the object, or <code>null></code> if the object could not be
found
+     * @throws IOException if an error occurs
+     */
     public Object load(String id) throws IOException;
-    
-    public void remove(String id) throws IOException;
     
 }

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/StoreFactory.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/StoreFactory.java?rev=394253&r1=394252&r2=394253&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/StoreFactory.java
(original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/StoreFactory.java
Fri Apr 14 21:25:52 2006
@@ -19,8 +19,8 @@
 
 public interface StoreFactory {
 
-    public Store get(String name) throws IOException;
+    public Store open(String name) throws IOException;
     
-    public void release(Store store) throws IOException;
+    public void close(Store store) throws IOException;
     
 }

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java?rev=394253&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java
(added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java
Fri Apr 14 21:25:52 2006
@@ -0,0 +1,98 @@
+package org.apache.servicemix.store.jdbc;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.store.Store;
+
+public class JdbcStore implements Store {
+
+    private static final Log log = LogFactory.getLog(JdbcStore.class);
+
+    private JdbcStoreFactory factory;
+    private String name;
+    
+    public JdbcStore(JdbcStoreFactory factory, String name) {
+        this.factory = factory;
+        this.name = name;
+    }
+
+    public boolean hasFeature(String name) {
+        return PERSISTENT.equals(name) ||
+               (CLUSTERED.equals(name) && factory.isClustered()) ||
+               (TRANSACTIONAL.equals(name) && factory.isTransactional());
+    }
+
+    public void store(String id, Object data) throws IOException {
+        log.debug("Storing object with id: " + id);
+        Connection connection = null;
+        try {
+            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+            ObjectOutputStream out = new ObjectOutputStream(buffer);
+            out.writeObject(data);
+            out.close();
+            connection = factory.getDataSource().getConnection();
+            PreparedStatement ps = connection.prepareStatement("INSERT INTO " + factory.getTableName()
+ " (ID, DATA) VALUES (?, ?)");
+            ps.setString(1, name + ":" + id);
+            ps.setBytes(2, buffer.toByteArray());
+            ps.execute();
+        } catch (Exception e) {
+            throw (IOException) new IOException("Error storing object").initCause(e);
+        } finally {
+            if (connection != null) {
+                try {
+                    connection.close();
+                } catch (Exception e) {
+                    throw (IOException) new IOException("Error closing connection").initCause(e);
+                }
+            }
+        }
+    }
+
+    public String store(Object data) throws IOException {
+        String id = factory.getIdGenerator().generateId();
+        store(id, data);
+        return id;
+    }
+
+    public Object load(String id) throws IOException {
+        log.debug("Loading object with id: " + id);
+        Connection connection = null;
+        try {
+            connection = factory.getDataSource().getConnection();
+            PreparedStatement ps = connection.prepareStatement("SELECT DATA FROM " + factory.getTableName()
+ " WHERE ID = ?");
+            ps.setString(1, name + ":" + id);
+            ResultSet rs = ps.executeQuery();
+            if (rs.next()) {
+                byte[] data = rs.getBytes(1);
+                ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
+                Object result = ois.readObject();
+                ps = connection.prepareStatement("DELETE FROM " + factory.getTableName()
+ " WHERE ID = ?");
+                ps.setString(1, name + ":" + id);
+                ps.execute();
+                return result;
+            } else {
+                return null;
+            }
+        } catch (Exception e) {
+            throw (IOException) new IOException("Error storing object").initCause(e);
+        } finally {
+            if (connection != null) {
+                try {
+                    connection.close();
+                } catch (Exception e) {
+                    throw (IOException) new IOException("Error closing connection").initCause(e);
+                }
+            }
+        }
+    }
+
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/JdbcStoreFactory.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/JdbcStoreFactory.java?rev=394253&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/JdbcStoreFactory.java
(added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/jdbc/JdbcStoreFactory.java
Fri Apr 14 21:25:52 2006
@@ -0,0 +1,162 @@
+package org.apache.servicemix.store.jdbc;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.sql.DataSource;
+
+import org.apache.activemq.util.IdGenerator;
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.PlatformFactory;
+import org.apache.ddlutils.model.Column;
+import org.apache.ddlutils.model.Database;
+import org.apache.ddlutils.model.Table;
+import org.apache.servicemix.store.Store;
+import org.apache.servicemix.store.StoreFactory;
+
+public class JdbcStoreFactory implements StoreFactory {
+
+    private boolean transactional;
+    private boolean clustered;
+    private DataSource dataSource;
+    private IdGenerator idGenerator = new IdGenerator();
+    private Map stores = new HashMap();
+    private String tableName = "SM_STORE";
+    private boolean createDataBase = true;
+    private boolean firstStore = true;
+    
+    /* (non-Javadoc)
+     * @see org.apache.servicemix.store.ExchangeStoreFactory#get(java.lang.String)
+     */
+    public synchronized Store open(String name) throws IOException {
+        if (firstStore && createDataBase) {
+            firstStore = false;
+            try {
+                createDataBase();
+            } catch (Exception e) {
+                throw (IOException) new IOException("Exception while creating database").initCause(e);

+            }
+        }
+        JdbcStore store = (JdbcStore) stores.get(name);
+        if (store == null) {
+            store = new JdbcStore(this, name);
+            stores.put(name, store);
+        }
+        return store;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.servicemix.store.ExchangeStoreFactory#release(org.apache.servicemix.store.ExchangeStore)
+     */
+    public synchronized void close(Store store) throws IOException {
+        stores.remove(store);
+    }
+    
+    /**
+     * @return Returns the dataSource.
+     */
+    public DataSource getDataSource() {
+        return dataSource;
+    }
+
+    /**
+     * @param dataSource The dataSource to set.
+     */
+    public void setDataSource(DataSource dataSource) {
+        this.dataSource = dataSource;
+    }
+
+    /**
+     * @return Returns the clustered.
+     */
+    public boolean isClustered() {
+        return clustered;
+    }
+
+    /**
+     * @param clustered The clustered to set.
+     */
+    public void setClustered(boolean clustered) {
+        this.clustered = clustered;
+    }
+
+    /**
+     * @return Returns the transactional.
+     */
+    public boolean isTransactional() {
+        return transactional;
+    }
+
+    /**
+     * @param transactional The transactional to set.
+     */
+    public void setTransactional(boolean transactional) {
+        this.transactional = transactional;
+    }
+
+    /**
+     * @return Returns the idGenerator.
+     */
+    public IdGenerator getIdGenerator() {
+        return idGenerator;
+    }
+
+    /**
+     * @param idGenerator The idGenerator to set.
+     */
+    public void setIdGenerator(IdGenerator idGenerator) {
+        this.idGenerator = idGenerator;
+    }
+
+    /**
+     * @return Returns the tableName.
+     */
+    public String getTableName() {
+        return tableName;
+    }
+
+    /**
+     * @param tableName The tableName to set.
+     */
+    public void setTableName(String tableName) {
+        this.tableName = tableName;
+    }
+
+    /**
+     * @return Returns the createDataBase.
+     */
+    public boolean isCreateDataBase() {
+        return createDataBase;
+    }
+
+    /**
+     * @param createDataBase The createDataBase to set.
+     */
+    public void setCreateDataBase(boolean createDataBase) {
+        this.createDataBase = createDataBase;
+    }
+    
+    public void createDataBase() throws SQLException {
+        Database db = new Database();
+        db.setName("JdbcStore");
+        Table table = new Table();
+        table.setName(tableName);
+        Column id = new Column();
+        id.setName("ID");
+        id.setType("VARCHAR");
+        id.setPrimaryKey(true);
+        id.setRequired(true);
+        table.addColumn(id);
+        Column exchange = new Column();
+        exchange.setName("DATA");
+        exchange.setType("BLOB");
+        table.addColumn(exchange);
+        db.addTable(table);
+
+        Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);
+        platform.createTables(db, false, true);
+    }
+
+}

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/memory/MemoryStore.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/memory/MemoryStore.java?rev=394253&r1=394252&r2=394253&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/memory/MemoryStore.java
(original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/memory/MemoryStore.java
Fri Apr 14 21:25:52 2006
@@ -25,6 +25,12 @@
 
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 
+/**
+ * A simple memory store implementation based on a simple map.
+ * This store is neither clusterable, nor persistent, nor transactional.
+ * 
+ * @author gnodet
+ */
 public class MemoryStore implements Store {
     
     private static final Log log = LogFactory.getLog(MemoryStore.class);
@@ -47,7 +53,7 @@
     
     public String store(Object data) throws IOException {
         String id = idGenerator.generateId();
-        datas.put(id, data);
+        store(id, data);
         return id;
     }
 
@@ -55,10 +61,5 @@
         log.debug("Loading object with id: " + id);
         return datas.get(id);
     }
-    
-    public void remove(String id) throws IOException {
-        datas.remove(id);
-    }
-
     
 }

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/memory/MemoryStoreFactory.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/memory/MemoryStoreFactory.java?rev=394253&r1=394252&r2=394253&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/memory/MemoryStoreFactory.java
(original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/store/memory/MemoryStoreFactory.java
Fri Apr 14 21:25:52 2006
@@ -31,7 +31,7 @@
     /* (non-Javadoc)
      * @see org.apache.servicemix.store.ExchangeStoreFactory#get(java.lang.String)
      */
-    public synchronized Store get(String name) throws IOException {
+    public synchronized Store open(String name) throws IOException {
         MemoryStore store = (MemoryStore) stores.get(name);
         if (store == null) {
             store = new MemoryStore(idGenerator);
@@ -43,9 +43,8 @@
     /* (non-Javadoc)
      * @see org.apache.servicemix.store.ExchangeStoreFactory#release(org.apache.servicemix.store.ExchangeStore)
      */
-    public synchronized void release(Store store) throws IOException {
+    public synchronized void close(Store store) throws IOException {
         stores.remove(store);
     }
     
-
 }

Added: incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTest.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTest.java?rev=394253&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTest.java
(added)
+++ incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTest.java
Fri Apr 14 21:25:52 2006
@@ -0,0 +1,58 @@
+/*
+ * 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.servicemix.store.jdbc;
+
+import java.sql.Connection;
+
+import javax.sql.DataSource;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.store.Store;
+import org.apache.servicemix.store.StoreFactory;
+import org.hsqldb.jdbc.jdbcDataSource;
+
+public class JdbcStoreTest extends TestCase {
+
+    private DataSource dataSource;
+    private Connection connection;
+    private StoreFactory factory;
+
+    protected void setUp() throws Exception {
+        jdbcDataSource ds = new jdbcDataSource();
+        ds.setDatabase("jdbc:hsqldb:mem:aname");
+        ds.setUser("sa");
+        dataSource = ds;
+        connection = dataSource.getConnection();
+        JdbcStoreFactory f = new JdbcStoreFactory();
+        f.setDataSource(dataSource);
+        factory = f;
+    }
+    
+    protected void tearDown() throws Exception {
+        if (connection != null) {
+            connection.close();
+        }
+    }
+
+    public void testStoreLoad() throws Exception {
+        Store store = factory.open("store");
+        String id = store.store(new Integer(10));
+        Integer i = (Integer) store.load(id);
+        assertEquals(10, i.intValue());
+        assertNull(store.load("a"));
+    }
+}

Modified: incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/EIPEndpoint.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/EIPEndpoint.java?rev=394253&r1=394252&r2=394253&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/EIPEndpoint.java
(original)
+++ incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/EIPEndpoint.java
Fri Apr 14 21:25:52 2006
@@ -108,7 +108,7 @@
             if (storeFactory == null) {
                 storeFactory = new MemoryStoreFactory();
             }
-            store = storeFactory.get(getService().toString() + getEndpoint());
+            store = storeFactory.open(getService().toString() + getEndpoint());
         }
         start();
     }

Modified: incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/support/XPathPredicate.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/support/XPathPredicate.java?rev=394253&r1=394252&r2=394253&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/support/XPathPredicate.java
(original)
+++ incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/support/XPathPredicate.java
Fri Apr 14 21:25:52 2006
@@ -6,8 +6,6 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.servicemix.expression.JAXPBooleanXPathExpression;
-import org.apache.servicemix.jbi.jaxp.SourceTransformer;
-import org.w3c.dom.Node;
 
 /**
  * @author gnodet



Mime
View raw message