[jboss-svn-commits] JBL Code SVN: r6833 - in labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format: . db
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Oct 16 17:14:09 EDT 2006
Author: daniel.brum at jboss.com
Date: 2006-10-16 17:14:08 -0400 (Mon, 16 Oct 2006)
New Revision: 6833
Added:
labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/
labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/DBConnectionManager.java
labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/DBMessageStorePlugin.java
labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/MessageStoreImpl.java
Log:
Message Store
Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/DBConnectionManager.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/DBConnectionManager.java 2006-10-16 21:14:05 UTC (rev 6832)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/DBConnectionManager.java 2006-10-16 21:14:08 UTC (rev 6833)
@@ -0,0 +1,172 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006,
+ * @author daniel.brum at jboss.com
+ */
+
+package org.jboss.internal.soa.esb.persistence.format.db;
+
+import java.io.IOException;
+import java.sql.Connection;
+
+import org.apache.commons.pool.ObjectPool;
+import org.apache.commons.pool.impl.GenericObjectPool;
+import org.apache.commons.dbcp.ConnectionFactory;
+import org.apache.commons.dbcp.PoolingDataSource;
+import org.apache.commons.dbcp.PoolableConnectionFactory;
+import org.apache.commons.dbcp.DriverManagerConnectionFactory;
+import org.jboss.soa.esb.common.Configuration;
+import org.jboss.soa.esb.common.Environment;
+
+import com.arjuna.common.internal.util.propertyservice.plugins.io.XMLFilePlugin;
+import com.arjuna.common.util.exceptions.LoadPropertiesException;
+import com.arjuna.common.util.propertyservice.PropertyManager;
+import com.arjuna.common.util.propertyservice.PropertyManagerFactory;
+
+/*
+ * the DBConnectionManager class (singleton) is used by classes implenting the MessageStore interface
+ * to obtain a connection to the persistence store database.
+ * This class uses the Apache Commons DB Connection Pooling to handle all aspects of managing the connections
+ * in a pool.
+ * $author Daniel Brum
+ */
+
+
+public class DBConnectionManager {
+
+ /*
+ * DBConnectionManager configurable properties
+ */
+ protected static final String MSG_STORE_DB_CONNECTION_URI = "org.jboss.soa.esb.persistence.db.connection.uri";
+ protected static final String MSG_STORE_DB_CONNECTION_USER = "org.jboss.soa.esb.persistence.db.user";
+ protected static final String MSG_STORE_DB_CONNECTION_PWD = "org.jboss.soa.esb.persistence.db.pwd";
+
+
+ private static DBConnectionManager instance =null;
+ private PoolingDataSource dataSource = null;
+// private PropertyManager propMgr = null;
+
+ protected DBConnectionManager() {
+ //debug
+// System.setProperty("com.arjuna.common.util.propertyservice.verbosePropertyManager", "ON");
+//
+// propMgr = PropertyManagerFactory.getPropertyManager("message.store", "dbstore");
+// try {
+// propMgr.load(XMLFilePlugin.class.getName(), "jbossesb-properties.xml");
+// } catch (IOException e) {
+// e.printStackTrace();
+// } catch (ClassNotFoundException e) {
+// e.printStackTrace();
+// } catch (LoadPropertiesException e) {
+// e.printStackTrace();
+// }
+ }
+
+ public static DBConnectionManager getInstance() {
+ if (null == instance) {
+ try {
+ instance = new DBConnectionManager();
+ instance.init();
+ }catch(Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+ return instance;
+ }
+
+ private void init() throws Exception{
+// String connectURL=propMgr.getProperty(Environment.MSG_STORE_DB_CONNECTION_URL);
+ String connectURL = Configuration.getStoreUrl();
+ //Class.forName(propMgr.getProperty(Environment.MSG_STORE_DB_JDBC_DRIVER));
+ Class.forName(Configuration.getStoreDriver());
+
+ ObjectPool connectionPool = new GenericObjectPool(null);
+
+// ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURL,
+// propMgr.getProperty(Environment.MSG_STORE_DB_CONNECTION_USER),
+// propMgr.getProperty(Environment.MSG_STORE_DB_CONNECTION_PWD));
+ ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURL,
+ Configuration.getStoreUser(), Configuration.getStorePwd());
+
+ @SuppressWarnings("unused")
+// PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
+// connectionPool,null,propMgr.getProperty(Environment.MSG_STORE_DB_VALIDATE_SQL),false,true);
+ PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
+ connectionPool,null,Configuration.getStoreValidateSql(),false,true);
+
+// int poolSize = Integer.valueOf(propMgr.getProperty(Environment.MSG_STORE_DB_POOL_SIZE)).intValue();
+ int poolSize = Integer.valueOf(Configuration.getStorePoolSize()).intValue();
+
+ //add the max # of connections into the pool
+ for (int i=0 ; i<poolSize ; i++) {
+ try {
+ System.out.println(this.getClass().getSimpleName() + ": adding connection # " + (i+1) + " to the pool...");
+ connectionPool.addObject();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ dataSource = new PoolingDataSource(connectionPool);
+ }
+
+ public Connection getConnection() throws Exception{
+ return dataSource.getConnection();
+ }
+
+
+
+// public static void main(String args[]) {
+// try {
+// System.out.println("Creating connection.");
+// Connection conn = DBConnectionManager.getInstance().getConnection();
+// Statement stmt = null;
+// ResultSet rset = null;
+//
+//
+//
+// System.out.println("Creating statement.");
+// stmt = conn.createStatement();
+// System.out.println("Executing statement.");
+// rset = stmt.executeQuery("select * from uid_table");
+// System.out.println("Results:");
+// int numcols = rset.getMetaData().getColumnCount();
+// while(rset.next()) {
+// for(int i=1;i<=numcols;i++) {
+// System.out.print("\t" + rset.getString(i));
+// }
+// System.out.println("");
+// }
+//
+// } catch (Exception e) {
+// // TODO Auto-generated catch block
+// e.printStackTrace();
+// }
+//
+// //uuid test
+// for (int x=0;x<10;x++) {
+// System.out.println("esb://" + UUID.randomUUID());
+// }
+
+
+
+// }
+
+
+
+}
Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/DBMessageStorePlugin.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/DBMessageStorePlugin.java 2006-10-16 21:14:05 UTC (rev 6832)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/DBMessageStorePlugin.java 2006-10-16 21:14:08 UTC (rev 6833)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006,
+ * @author daniel.brum at jboss.com
+ */
+
+package org.jboss.internal.soa.esb.persistence.format.db;
+
+import java.net.URI;
+
+import org.jboss.soa.esb.persistence.MessageStore;
+import org.jboss.soa.esb.persistence.format.MessageStorePlugin;
+import org.jboss.soa.esb.persistence.format.MessageStoreType;
+
+/**
+ * @author dbrum
+ *
+ */
+public class DBMessageStorePlugin implements MessageStorePlugin {
+
+
+ public MessageStore getMessageStore() {
+ return new MessageStoreImpl();
+ }
+
+
+ public URI getType() {
+ return MessageStoreType.DATABASE;
+ }
+
+}
Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/MessageStoreImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/MessageStoreImpl.java 2006-10-16 21:14:05 UTC (rev 6832)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/persistence/format/db/MessageStoreImpl.java 2006-10-16 21:14:08 UTC (rev 6833)
@@ -0,0 +1,168 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006,
+ * @author daniel.brum at jboss.com
+ */
+
+package org.jboss.internal.soa.esb.persistence.format.db;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.Serializable;
+import java.io.StringWriter;
+import java.net.URI;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import java.util.UUID;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+
+
+import org.jboss.internal.soa.esb.thirdparty.Base64;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageType;
+import org.jboss.soa.esb.persistence.MessageStore;
+
+import org.w3c.dom.Document;
+
+import com.sun.org.apache.xml.internal.serialize.OutputFormat;
+import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
+
+
+public class MessageStoreImpl implements MessageStore {
+
+ private DBConnectionManager mgr = null;
+
+ public MessageStoreImpl() {
+ mgr = DBConnectionManager.getInstance();
+ }
+
+ public String addMessage(Message message){
+
+ Connection conn = null;
+ PreparedStatement ps = null;
+ String messageString = null;
+ String uid = null;
+
+ //get a UID
+ String uuid = UUID.randomUUID().toString();
+ uid = "urn:jboss:esb:message:UID:" + uuid;
+
+ try {
+ conn = mgr.getConnection();
+
+ if (message.getType().equals(MessageType.JBOSS_XML) ) {
+ Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+ ((org.jboss.internal.soa.esb.message.format.xml.MessageImpl)message).toXML(doc);
+ StringWriter sWriter = new StringWriter();
+ OutputFormat format = new OutputFormat();
+ format.setIndenting(true);
+ XMLSerializer xmlS = new XMLSerializer(sWriter, format);
+ xmlS.asDOMSerializer();
+ xmlS.serialize(doc);
+ messageString = sWriter.toString();
+ }
+
+ if (message.getType().equals(MessageType.JAVA_SERIALIZED) ) {
+ messageString = Base64.encodeObject((Serializable)message);
+ }
+
+ //insert into the database
+ String sql = "insert into message(uid, type, message) values(?,?,?)";
+ ps = conn.prepareStatement(sql);
+ ps.setString(1, uid);
+ ps.setString(2, message.getType().toString());
+ ps.setString(3, messageString);
+ ps.execute();
+
+ conn.close();
+ ps.close();
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return uid;
+ }
+
+ public Message getMessage(String uid) throws Exception{
+
+ Connection conn = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+
+ String sql = "select uid,type,message from message where uid=?";
+
+ try {
+ conn = mgr.getConnection();
+ ps = conn.prepareStatement(sql);
+ ps.setString(1, uid);
+
+ rs = ps.executeQuery();
+ if (! rs.next()) throw new Exception("Non existing Message for UID: " + uid);
+ URI type= URI.create(rs.getString(2));
+ String msg=rs.getString(3);
+
+ if (type.equals(MessageType.JBOSS_XML)) {
+
+ InputStream inStream = new ByteArrayInputStream(msg.getBytes());
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ Document doc = builder.parse(inStream);
+
+ org.jboss.internal.soa.esb.message.format.xml.MessageImpl message =
+ new org.jboss.internal.soa.esb.message.format.xml.MessageImpl();
+ message.fromXML(doc);
+ return message;
+
+ }
+ if (type.equals(MessageType.JAVA_SERIALIZED)) {
+
+ Object message = Base64.decodeToObject(msg);
+ return (org.jboss.internal.soa.esb.message.format.serialized.MessageImpl)message;
+
+ }
+
+ rs.close();
+ ps.close();
+ conn.close();
+
+ } catch (SQLException e) {
+ e.printStackTrace();
+ return null;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ return null;
+ }
+
+
+
+
+
+
+}
More information about the jboss-svn-commits
mailing list