Author: jbonofre
Date: Sat Dec 18 14:50:00 2010
New Revision: 1050640
URL: http://svn.apache.org/viewvc?rev=1050640&view=rev
Log:
[SM-2014] Add a MongoDB store.
Added:
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/MongoStore.java
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/MongoStoreFactory.java
Modified:
servicemix/utils/trunk/pom.xml
Modified: servicemix/utils/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/servicemix/utils/trunk/pom.xml?rev=1050640&r1=1050639&r2=1050640&view=diff
==============================================================================
--- servicemix/utils/trunk/pom.xml (original)
+++ servicemix/utils/trunk/pom.xml Sat Dec 18 14:50:00 2010
@@ -145,6 +145,11 @@
<version>1.4</version>
<optional>true</optional>
</dependency>
+ <dependency>
+ <groupId>org.mongodb</groupId>
+ <artifactId>mongo-java-driver</artifactId>
+ <version>2.3</version>
+ </dependency>
<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
Added:
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/MongoStore.java
URL:
http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/MongoStore.java?rev=1050640&view=auto
==============================================================================
---
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/MongoStore.java
(added)
+++
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/MongoStore.java
Sat Dec 18 14:50:00 2010
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.mongo;
+
+import com.mongodb.*;
+import org.apache.servicemix.store.Store;
+
+import java.io.*;
+
+/**
+ * <p>
+ * A store which uses MongoDB.
+ * </p>
+ *
+ * @author iocanel
+ * @author jbonofre
+ */
+public class MongoStore implements Store {
+
+ private static final String ID = "_id";
+ private static final String DATA = "data";
+ private static final String TIMESTAMP = "_timestamp";
+
+ private DB db;
+ DBCollection collection;
+
+ private Long timeout;
+
+ /**
+ * <p>
+ * Constructor with the Mongo DB and collection name to use.
+ * </p>
+ *
+ * @param db the Mongo DB.
+ * @param collectionName the Mongo collection name.
+ */
+ public MongoStore(DB db, String collectionName) {
+ this.db = db;
+ this.collection = db.getCollection(collectionName);
+ }
+
+ /**
+ * <p>
+ * Constructor with the Mongo DB and collection name to use.
+ * This constructor defines a connection timeout too.
+ * </p>
+ *
+ * @param db the Mongo DB.
+ * @param collectionName the Mongo collection name.
+ * @param timeout the connection timeout.
+ */
+ public MongoStore(DB db, String collectionName, Long timeout) {
+ this.db = db;
+ this.collection = db.getCollection(collectionName);
+ this.timeout = timeout;
+ }
+
+ /**
+ * <p>
+ * Returns true if feature is provided by the store, false else.
+ * </p>
+ *
+ * @param feature the feature.
+ * @return true if the given feature is provided by the store, false else.
+ */
+ public boolean hasFeature(String feature) {
+ if (PERSISTENT.equals(feature) || CLUSTERED.equals(feature))
+ return true;
+ return false;
+ }
+
+ /**
+ * <p>
+ * Stores {...@param data} to a {...@link DBObject} with the given
{...@param id}.
+ * </p>
+ *
+ * @param id the id of the object to store
+ * @param data the object to store
+ * @throws IOException
+ */
+ public void store(String id, Object data) throws IOException {
+ DBObject object = new BasicDBObject();
+ try {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ ObjectOutputStream out = new ObjectOutputStream(buffer);
+ out.writeObject(data);
+ out.close();
+ object.put(ID, id);
+ object.put(DATA, buffer.toByteArray());
+ object.put(TIMESTAMP, System.currentTimeMillis());
+ } catch (Exception e) {
+ throw (IOException) new IOException("Error storing
object").initCause(e);
+ }
+ WriteResult result = collection.insert(object);
+ // check result for errors
+ if (result.getError() != null) {
+ throw new IOException(result.getError());
+ }
+ }
+
+ /**
+ * <p>
+ * Stores {...@param data} to a {...@link DBObject} and return the
generated ID.
+ * </p>
+ *
+ * @param data the object to store
+ * @return the generated ID.
+ * @throws IOException
+ */
+ public String store(Object data) throws IOException {
+ DBObject object = new BasicDBObject();
+ object.put(DATA, data);
+ WriteResult result = collection.insert(object);
+ // check result for errors
+ if (result.getError() != null) {
+ throw new IOException(result.getError());
+ }
+ return String.valueOf(result.getField(ID));
+ }
+
+ /**
+ * <p>
+ * Retrieves the data of the object with the given {...@param id}.
+ * </p>
+ *
+ * @param id the id of the object
+ * @return the data object
+ * @throws IOException
+ */
+ public Object load(String id) throws IOException {
+ evict();
+ Object obj = null;
+ try {
+ DBObject object = new BasicDBObject();
+ object.put(ID, id);
+ DBObject item = collection.findOne(object);
+ WriteResult result = collection.remove(object);
+ if (item == null) {
+ throw new IOException("Could not find item with id " + id);
+ }
+ byte[] data = (byte[]) item.get(DATA);
+ if (data != null) {
+ ObjectInputStream ois = new ObjectInputStream(new
ByteArrayInputStream(data));
+ obj = ois.readObject();
+ }
+ } catch (Exception e) {
+ throw (IOException) new IOException("Error loading
object").initCause(e);
+ }
+ return obj;
+ }
+
+ /**
+ * <p>
+ * Retrieves the data of the object with the given {...@param id} without
removing it.
+ * </p>
+ *
+ * @param id the id of the object
+ * @return the data object
+ * @throws IOException
+ */
+ public Object peek(String id) throws IOException {
+ evict();
+ Object obj = null;
+ try {
+ DBObject object = new BasicDBObject();
+ object.put(ID, id);
+ DBObject item = collection.findOne(object);
+ byte[] data = (byte[]) item.get(DATA);
+ if (data != null) {
+ ObjectInputStream ois = new ObjectInputStream(new
ByteArrayInputStream(data));
+ obj = ois.readObject();
+ }
+ } catch (Exception e) {
+ throw (IOException) new IOException("Error loading
object").initCause(e);
+ }
+ return obj;
+ }
+
+ /**
+ * <p>
+ * Removes objects that have been expired.
+ * </p>
+ */
+ protected void evict() {
+ if (timeout != null) {
+ DBObject object = new BasicDBObject();
+ object.put(TIMESTAMP, new BasicDBObject("<",
System.currentTimeMillis() - timeout));
+ collection.remove(object);
+ }
+ }
+}
Added:
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/MongoStoreFactory.java
URL:
http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/MongoStoreFactory.java?rev=1050640&view=auto
==============================================================================
---
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/MongoStoreFactory.java
(added)
+++
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/mongo/MongoStoreFactory.java
Sat Dec 18 14:50:00 2010
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.mongo;
+
+import com.mongodb.DB;
+import com.mongodb.Mongo;
+import org.apache.servicemix.store.Store;
+import org.apache.servicemix.store.StoreFactory;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * <p>
+ * A store factory which use MongoDB.
+ * </p>
+ *
+ * @author iocanel
+ * @author jbonofre
+ */
+public class MongoStoreFactory implements StoreFactory {
+
+ private Mongo mongo;
+ private DB db;
+
+ private String host;
+ private Integer port;
+
+ private String username;
+ private String password;
+
+ private String database;
+
+ private Long timeout;
+
+ private Map<String, MongoStore> stores = new HashMap<String, MongoStore>();
+
+ /**
+ * <p>
+ * Open a {...@link MongoStore} using a new {...@param collection}.
+ * </p>
+ *
+ * @param collection the Mongo store collection.
+ * @return the opened Mongo store.
+ * @throws IOException
+ */
+ public synchronized Store open(String collection) throws IOException {
+ String key = database + "/" + collection;
+ MongoStore store = stores.get(key);
+ if (store == null) {
+ if (mongo == null) {
+ if (host == null || port == null)
+ throw new IOException("MongoDB host and port are
required.");
+ mongo = new Mongo(host, port);
+ }
+ if (db == null) {
+ if (database == null)
+ throw new IOException("MongoDB database name is
required.");
+ db = mongo.getDB(database);
+ }
+ // if credentials are provided
+ if (username != null && password != null) {
+ boolean authenticated = db.authenticate(username,
password.toCharArray());
+ if (!authenticated)
+ throw new IOException("MongoDB authentication failed.");
+ }
+ if (timeout != null)
+ store = new MongoStore(db, collection, timeout);
+ else store = new MongoStore(db, collection);
+ stores.put(key, store);
+ }
+ return store;
+ }
+
+ public synchronized void close(Store store) throws IOException {
+ if (mongo != null)
+ mongo.close();
+ }
+
+ public Mongo getMongo() {
+ return mongo;
+ }
+
+ public void setMongo(Mongo mongo) {
+ this.mongo = mongo;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ public Integer getPort() {
+ return port;
+ }
+
+ public void setPort(Integer port) {
+ this.port = port;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public String getDatabase() {
+ return database;
+ }
+
+ public void setDatabase(String database) {
+ this.database = database;
+ }
+
+ public Long getTimeout() {
+ return timeout;
+ }
+
+ public void setTimeout(Long timeout) {
+ this.timeout = timeout;
+ }
+
+}