[jboss-svn-commits] JBL Code SVN: r15928 - labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/actions.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Oct 18 14:53:31 EDT 2007
Author: mark.little at jboss.com
Date: 2007-10-18 14:53:30 -0400 (Thu, 18 Oct 2007)
New Revision: 15928
Added:
labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/actions/PreparedStatementAction.java
Log:
Added: labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/actions/PreparedStatementAction.java
===================================================================
--- labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/actions/PreparedStatementAction.java (rev 0)
+++ labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/actions/PreparedStatementAction.java 2007-10-18 18:53:30 UTC (rev 15928)
@@ -0,0 +1,144 @@
+package org.jboss.soa.esb.actions;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import javax.naming.InitialContext;
+import javax.sql.DataSource;
+
+import org.jboss.soa.esb.actions.AbstractActionLifecycle;
+import org.jboss.soa.esb.actions.ActionLifecycleException;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.ListenerUtil;
+import org.jboss.soa.esb.message.Message;
+
+/**
+ * @author <a href="mailto:bernhardg at mac.com">Bernhard C Gass</a>
+ */
+public class PreparedStatementAction extends AbstractActionLifecycle {
+
+ public static final String DATA_SOURCE = "org.jboss.soa.esb.actions.preparedStatement.dataSource";
+ public static final String SQL_STATEMENT = "org.jboss.soa.esb.actions.preparedStatement.sqlStatement";
+ public static final String SQL_PREPARED_FIELDS = "org.jboss.soa.esb.actions.preparedStatement.preparedFields";
+
+ public PreparedStatementAction(ConfigTree config) {
+ this._config = config;
+ }
+
+ @Override
+ public void initialise() throws ActionLifecycleException {
+ super.initialise();
+
+ this._dataSourceId = ListenerUtil.getValue(_config, DATA_SOURCE, "");
+ this._sqlStatement = ListenerUtil.getValue(_config, SQL_STATEMENT, "");
+ this._preparedFields = ListenerUtil.getValue(_config, SQL_PREPARED_FIELDS, "");
+ this._preparedFieldsArray = this.parseFields(this._preparedFields);
+
+ try {
+ this._preparedStatement = this.getConnection().prepareStatement(this._sqlStatement);
+ }
+ catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ public Message process(Message message) {
+ this.executePreparedStatment((ArrayList<HashMap>)message.getBody().get());
+ return message;
+ }
+
+ @Override
+ public void destroy() throws ActionLifecycleException {
+ super.destroy();
+ if (this._connection != null) {
+ try {
+ this._connection.rollback();
+ this._connection.close();
+ }
+ catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ }
+
+
+ protected Connection getConnection() {
+ try {
+ if (null == this._dataSource) {
+ InitialContext ic = new InitialContext();
+ this._dataSource = (DataSource) ic.lookup(this._dataSourceId);
+ this._connection = this._dataSource.getConnection();
+ this._connection.setAutoCommit(false);
+ }
+
+ return this._connection;
+ }
+ catch (Exception ex) {
+ ex.printStackTrace();
+ return null;
+ }
+ }
+
+ protected void executePreparedStatment(List<HashMap> rows) {
+ Object obj = 0;
+
+ try {
+ if (rows != null && rows.size() > 0) {
+ for (HashMap hm: rows) {
+
+ for (int i = 0; i < this._preparedFieldsArray.size(); i++) {
+ obj = hm.get(this._preparedFieldsArray.get(i));
+ this._preparedStatement.setObject( i + 1, obj );
+ }
+
+ this._preparedStatement.executeUpdate();
+ }
+ this.getConnection().commit();
+ }
+ }
+ catch (Exception ex) {
+ ex.printStackTrace();
+ try {
+ this.getConnection().rollback();
+ }
+ catch (Exception ex2) {
+ ex2.printStackTrace();
+ }
+ }
+ }
+
+ protected List parseFields(String insertFields) {
+
+ List<String> list = new ArrayList<String>();
+ StringTokenizer st = null;
+
+ if (insertFields != null && !insertFields.equals("")) {
+ st = new StringTokenizer(insertFields, ",");
+ while (st.hasMoreTokens())
+ list.add(st.nextToken());
+ }
+ return list;
+ }
+
+
+
+ protected String _sqlStatement;
+
+ protected String _dataSourceId;
+
+ protected DataSource _dataSource;
+
+ protected Connection _connection;
+
+ protected ConfigTree _config;
+
+ protected PreparedStatement _preparedStatement;
+
+ protected String _preparedFields;
+
+ protected List _preparedFieldsArray;
+}
More information about the jboss-svn-commits
mailing list