2
0
Fork 0

Small refactor for comming converters

This commit is contained in:
Willem Cazander 2012-01-22 08:16:15 +01:00
parent 75b3d5e0a0
commit 1c308a684a
178 changed files with 5865 additions and 1531 deletions

View file

@ -0,0 +1,40 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS 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 COPYRIGHT HOLDER OR 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.
*/
package net.forwardfire.vasc.backend.mongodb;
import com.mongodb.DB;
/**
* Provides an mongodb DB connection to the backend.
*
* @author Willem Cazander
* @version 1.0 Dec 30, 2011
*/
public interface MongodbConnectionProvider {
/**
* Returns a DB connection.
* @return An DB connection to mongodb
*/
public DB getMongodbConnection();
}

View file

@ -0,0 +1,163 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS 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 COPYRIGHT HOLDER OR 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.
*/
package net.forwardfire.vasc.backend.mongodb;
import java.net.UnknownHostException;
import java.util.logging.Logger;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;
/**
* Provides an mongodb DB connection to the backend.
*
* @author Willem Cazander
* @version 1.0 Dec 30, 2011
*/
public class MongodbConnectionProviderImpl implements MongodbConnectionProvider {
private Logger logger = Logger.getLogger(MongodbConnectionProviderImpl.class.getName());
private String hostname = "localhost";
private int port = 27017;
private String database = null;
private String username = null;
private String password = null;
private boolean readonly = false;
protected static Mongo mongo = null;
protected static DB db = null;
public DB getMongodbConnection() {
if (db!=null) {
return db;
}
synchronized (this) {
ServerAddress server;
try {
server = new ServerAddress(hostname,port);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
MongoOptions options = new MongoOptions();
mongo = new Mongo(server,options);
db = mongo.getDB(database);
if (username!=null && password!=null) {
boolean auth = db.authenticate(username, password.toCharArray());
if (auth==false) {
throw new RuntimeException("Could not auth to db: "+database+" with username: "+username);
}
}
if (readonly) {
db.setReadOnly(true);
}
logger.info("Connection to: "+db.getName());
}
return db;
}
/**
* @return the hostname
*/
public String getHostname() {
return hostname;
}
/**
* @param hostname the hostname to set
*/
public void setHostname(String hostname) {
this.hostname = hostname;
}
/**
* @return the port
*/
public int getPort() {
return port;
}
/**
* @param port the port to set
*/
public void setPort(int port) {
this.port = port;
}
/**
* @return the database
*/
public String getDatabase() {
return database;
}
/**
* @param database the database to set
*/
public void setDatabase(String database) {
this.database = database;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the readonly
*/
public boolean isReadonly() {
return readonly;
}
/**
* @param readonly the readonly to set
*/
public void setReadonly(boolean readonly) {
this.readonly = readonly;
}
}

View file

@ -0,0 +1,151 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS 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 COPYRIGHT HOLDER OR 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.
*/
package net.forwardfire.vasc.backend.mongodb;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import net.forwardfire.vasc.backend.AbstractVascBackend;
import net.forwardfire.vasc.backend.VascBackendState;
import net.forwardfire.vasc.core.VascEntry;
import net.forwardfire.vasc.core.VascEntryField;
import net.forwardfire.vasc.core.VascException;
import net.forwardfire.vasc.core.entry.VascEntryFieldValue;
import net.forwardfire.vasc.core.entry.VascEntryRecordCreator;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.BasicDBObject;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
/**
* Provides backend for mongodb.
*
* @author Willem Cazander
* @version 1.0 Dec 30, 2011
*/
public class MongodbVascBackend extends AbstractVascBackend {
private Logger logger = null;
private MongodbConnectionProvider connectionProvider = null;
private String collection = null;
private DB database = null;
public MongodbVascBackend() {
logger = Logger.getLogger(MongodbVascBackend.class.getName());
}
public List<Object> execute(VascBackendState state) throws VascException {
DBCollection coll = getDBCollection();
DBObject query = new BasicDBObject();
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
query.put(key, value);
logger.finer("Setting query parameter key: '"+key+"' value: '"+value+"'");
}
DBCursor cur = coll.find(query);
//cur.count();
List<Object> result = new ArrayList<Object>(cur.count());
while (cur.hasNext()) {
DBObject row = cur.next();
result.add(row);
}
return result;
}
public void persist(Object object) throws VascException {
DBCollection coll = getDBCollection();
coll.insert((DBObject)object);
}
public Object merge(Object object) throws VascException {
DBCollection coll = getDBCollection();
DBObject row = (DBObject)object;
DBObject query = new BasicDBObject();
query.put("_id",row.get("_id"));
WriteResult wr = coll.update(query,row,false,false,WriteConcern.SAFE);
logger.info("WriteResult: "+wr);
return object;
}
public void delete(Object object) throws VascException {
DBCollection coll = getDBCollection();
DBObject query = new BasicDBObject();
query.put("_id",((DBObject)object).get("_id"));
coll.remove(query); // remove by _id
}
public VascEntryFieldValue provideVascEntryFieldValue(VascEntryField field) {
return new MongodbVascEntryFieldValue();
}
public VascEntryRecordCreator provideVascEntryRecordCreator(VascEntry vascEntry) {
return new MongodbVascEntryRecordCreator();
}
private DBCollection getDBCollection() {
if (database!=null) {
return database.getCollection(collection);
}
if (connectionProvider==null) {
throw new RuntimeException("Can't get DBCollection from null connectionProvider.");
}
synchronized (connectionProvider) {
database = connectionProvider.getMongodbConnection();
}
return database.getCollection(collection);
}
/**
* @return the connectionProvider
*/
public MongodbConnectionProvider getConnectionProvider() {
return connectionProvider;
}
/**
* @param connectionProvider the connectionProvider to set
*/
public void setConnectionProvider(MongodbConnectionProvider connectionProvider) {
this.connectionProvider = connectionProvider;
}
/**
* @return the collection
*/
public String getCollection() {
return collection;
}
/**
* @param collection the collection to set
*/
public void setCollection(String collection) {
this.collection = collection;
}
}

View file

@ -0,0 +1,59 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS 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 COPYRIGHT HOLDER OR 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.
*/
package net.forwardfire.vasc.backend.mongodb;
import net.forwardfire.vasc.core.VascEntryField;
import net.forwardfire.vasc.core.VascException;
import net.forwardfire.vasc.core.entry.VascEntryFieldValue;
import com.mongodb.BasicDBObject;
/**
* Provides a mongodb field entry knowlege.
*
* @author Willem Cazander
* @version 1.0 Dec 30, 2011
*/
public class MongodbVascEntryFieldValue implements VascEntryFieldValue {
private static final long serialVersionUID = -7371273796529818557L;
public Object getValue(VascEntryField field, Object record) throws VascException {
BasicDBObject row = (BasicDBObject)record;
Object r = row.get(field.getBackendName());
if (r==null) {
return ""; // create new value, TODO
}
return r;
}
public String getDisplayValue(VascEntryField field, Object record) throws VascException {
return ""+getValue(field,record); // not supported TODO
}
public void setValue(VascEntryField field, Object record, Object value) throws VascException {
BasicDBObject row = (BasicDBObject)record;
row.put(field.getBackendName(), value);
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS 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 COPYRIGHT HOLDER OR 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.
*/
package net.forwardfire.vasc.backend.mongodb;
import net.forwardfire.vasc.core.VascEntry;
import net.forwardfire.vasc.core.entry.VascEntryRecordCreator;
import com.mongodb.BasicDBObject;
/**
* Provides a mongodb object to create a new row.
*
* @author Willem Cazander
* @version 1.0 Dec 30, 2011
*/
public class MongodbVascEntryRecordCreator implements VascEntryRecordCreator {
private static final long serialVersionUID = -9213830731796787384L;
public Object newRecord(VascEntry entry) throws Exception {
return new BasicDBObject();
}
public Class<?> getObjectClass() {
return BasicDBObject.class;
}
}

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<eld:root xmlns:eld="http://eld.x4o.org/eld/eld-lang.eld">
<eld:elementClass tag="mongodbBackend" objectClassName="net.forwardfire.vasc.backend.mongodb.MongodbVascBackend">
<eld:elementConfigurator bean.class="net.forwardfire.vasc.impl.x4o.VascBackendElementConfigurator" configAction="true"/>
</eld:elementClass>
<eld:elementClass tag="mongodbConnectionProvider" objectClassName="net.forwardfire.vasc.backend.mongodb.MongodbConnectionProviderImpl"/>
</eld:root>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>
Vasc namespace for the mongodb backend
</comment>
<entry key="eld.http://vasc.forwardfire.net/eld/vasc-backend-mongodb.eld">vasc-backend-mongodb.eld</entry>
</properties>