Author: ruwan
Date: Sat Dec  5 12:13:11 2009
New Revision: 887530

URL: http://svn.apache.org/viewvc?rev=887530&view=rev
Log:
Applying the patch from Eric on 
https://issues.apache.org/jira/browse/SYNAPSE-594

Modified:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractDBMediatorFactory.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractDBMediatorSerializer.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/db/AbstractDBMediator.java
    
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/db/DBLookupMediatorTest.java
    
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/db/DBReportMediatorTest.java

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractDBMediatorFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractDBMediatorFactory.java?rev=887530&r1=887529&r2=887530&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractDBMediatorFactory.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractDBMediatorFactory.java
 Sat Dec  5 12:13:11 2009
@@ -21,17 +21,14 @@
 
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMElement;
-import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.synapse.commons.datasource.DataSourceInformation;
+import org.apache.synapse.commons.security.secret.SecretInformation;
 import org.apache.synapse.mediators.db.AbstractDBMediator;
 import org.apache.synapse.mediators.db.Statement;
-import org.apache.synapse.commons.datasource.*;
-import org.apache.synapse.commons.jmx.MBeanRepository;
 import org.apache.synapse.util.xpath.SynapseXPath;
-import org.apache.synapse.commons.security.secret.SecretManager;
 import org.jaxen.JaxenException;
 
 import javax.naming.Context;
-import javax.sql.DataSource;
 import javax.xml.namespace.QName;
 import java.sql.Connection;
 import java.util.Iterator;
@@ -50,6 +47,8 @@
  *       <user/>
  *       <password/>
  *     |
+ *       <dsName
+ *     |
  *       <dsName/>
  *       <icClass/>
  *       <url/>
@@ -103,196 +102,170 @@
 
     static final QName ATT_COLUMN = new QName("column");
     static final QName ATT_TYPE = new QName("type");
-
+    
+    /**
+     * Reads the data source configuration for all mediators based on the 
<code>AbstractDBMediator</code>
+     * and stores the configuration in the mediator for datasource 
initialization and de-serialization.
+     *  
+     * @param elem the configuration element of the mediator
+     * @param mediator the mediator on which the configuration shall be stored
+     */
     protected void buildDataSource(OMElement elem, AbstractDBMediator 
mediator) {
 
         OMElement pool;
-        // get the 'pool' element and determine if we need to create a 
DataSource or
-        // look up using JNDI
+        
         try {
+            // get the 'pool' element and determine if we need to create a 
DataSource or
+            // lookup using JNDI
             SynapseXPath xpath = new 
SynapseXPath("self::node()/syn:connection/syn:pool");
             xpath.addNamespace("syn", XMLConfigConstants.SYNAPSE_NAMESPACE);
             pool = (OMElement) xpath.selectSingleNode(elem);
-
-            if (pool.getFirstChildWithName(DRIVER_Q) != null) {
-                mediator.setDataSource(createCustomDataSource(pool, mediator));
-
-            } else if (pool.getFirstChildWithName(DSNAME_Q) != null) {
-                mediator.setDataSource(lookupDataSource(pool, mediator));
+            if (pool.getFirstChildWithName(DSNAME_Q) != null) {
+                readLookupConfig(mediator, pool);
+            } else if (pool.getFirstChildWithName(DRIVER_Q) != null) {
+                readCustomDataSourceConfig(pool, mediator);
             } else {
                 handleException("The DataSource connection information must be 
specified for " +
                         "using a custom DataSource connection pool or for a 
JNDI lookup");
             }
-
         } catch (JaxenException e) {
             handleException("Error looking up DataSource connection 
information", e);
         }
     }
 
-    /**
-     * Lookup the DataSource on JNDI using the specified properties
-     *
-     * @param pool     the toplevel 'pool' element that holds DataSource 
information
-     * @param mediator the mediator to store properties for serialization
-     * @return a DataSource looked up using specified properties
-     */
-    private DataSource lookupDataSource(OMElement pool, AbstractDBMediator 
mediator) {
-
-        String dsName = getValue(pool, DSNAME_Q);
-        mediator.addDataSourceProperty(DSNAME_Q, dsName);
-        DataSource dataSource = null;
-        RepositoryBasedDataSourceFinder finder = DataSourceHelper.getInstance()
-                .getRepositoryBasedDataSourceFinder();
-
-        if (finder.isInitialized()) {
-            dataSource = finder.find(dsName);
-        }
-        if (dataSource != null) {
-            MBeanRepository mBeanRepository = 
DatasourceMBeanRepository.getInstance();
-            Object mBean = mBeanRepository.getMBean(dsName);
-            if (mBean instanceof DBPoolView) {
-                mediator.setDbPoolView((DBPoolView) mBean);
-            }
-            return dataSource;
-        }
-        Properties props = new Properties();
-        String password = getValue(pool, PASS_Q);
-        // load the minimum required properties
-        props.put(Context.INITIAL_CONTEXT_FACTORY, (getValue(pool, 
ICCLASS_Q)));
-        props.put(Context.SECURITY_PRINCIPAL, getValue(pool, USER_Q));
-        if (password != null && !"".equals(password)) {
-            props.put(Context.SECURITY_CREDENTIALS, 
getActualPassword(password));
-        }
-        props.put(Context.PROVIDER_URL, getValue(pool, URL_Q));
-
-        dataSource = DataSourceFinder.find(dsName, props);
-        if (dataSource == null) {
-            handleException("Cannot find a DataSource for given properties :" 
+ props);
+    private void readLookupConfig(AbstractDBMediator mediator, OMElement pool) 
{
+        String dataSourceName = getValue(pool, DSNAME_Q);
+        mediator.setDataSourceName(dataSourceName);
+        saveElementConfig(pool, DSNAME_Q, mediator);
+        
+        if (pool.getFirstChildWithName(ICCLASS_Q) != null) {
+            
+            Properties props = new Properties();
+            props.put(Context.INITIAL_CONTEXT_FACTORY, getValue(pool, 
ICCLASS_Q));
+            props.put(Context.PROVIDER_URL, getValue(pool, URL_Q));
+            props.put(Context.SECURITY_PRINCIPAL, getValue(pool, USER_Q));
+            props.put(Context.SECURITY_CREDENTIALS, getValue(pool, PASS_Q));
+            mediator.setJndiProperties(props);
+            
+            // Save element configuration for later de-serialization
+            saveElementConfig(pool, ICCLASS_Q, mediator);
+            saveElementConfig(pool, URL_Q, mediator);
+            saveElementConfig(pool, USER_Q, mediator);
+            saveElementConfig(pool, PASS_Q, mediator);
         }
-        //save loaded properties for later
-        mediator.addDataSourceProperty(ICCLASS_Q, getValue(pool, ICCLASS_Q));
-        mediator.addDataSourceProperty(URL_Q, getValue(pool, URL_Q));
-        mediator.addDataSourceProperty(USER_Q, getValue(pool, USER_Q));
-        mediator.addDataSourceProperty(PASS_Q, password);
-
-        return dataSource;
     }
 
-    /**
-     * Create a custom DataSource using the specified properties and Apache 
DBCP
-     *
-     * @param pool     the toplevel 'pool' element that holds DataSource 
information
-     * @param mediator the mediator to store properties for serialization
-     * @return a DataSource created using specified properties
-     */
-    private DataSource createCustomDataSource(OMElement pool, 
AbstractDBMediator mediator) {
+    private void readCustomDataSourceConfig(OMElement pool, AbstractDBMediator 
mediator) {
 
-        BasicDataSource ds = new BasicDataSource();
+            DataSourceInformation dataSourceInformation = new 
DataSourceInformation();
 
-        // load the minimum required properties
-        ds.setDriverClassName(getValue(pool, DRIVER_Q));
-        ds.setUsername(getValue(pool, USER_Q));
-        String password = getValue(pool, PASS_Q);
-        if (password != null && !"".equals(password)) {
-            ds.setPassword(getActualPassword(password));
-        }
-        ds.setUrl(getValue(pool, URL_Q));
+            dataSourceInformation.setDriver(getValue(pool, DRIVER_Q));
+            dataSourceInformation.setUrl(getValue(pool, URL_Q));
+            
+            SecretInformation secretInformation = new SecretInformation();
+            secretInformation.setUser(getValue(pool, USER_Q));
+            secretInformation.setAliasSecret(getValue(pool, PASS_Q));
+            dataSourceInformation.setSecretInformation(secretInformation);
+
+            // Save element configuration for later de-serialization
+            saveElementConfig(pool, DRIVER_Q, mediator);
+            saveElementConfig(pool, URL_Q, mediator);
+            saveElementConfig(pool, USER_Q, mediator);
+            saveElementConfig(pool, PASS_Q, mediator);
+            
+            Iterator poolPropIter = pool.getChildrenWithName(PROP_Q);
+            while (poolPropIter.hasNext()) {
+                OMElement poolProp = (OMElement) poolPropIter.next();
+                readPoolProperty(mediator, dataSourceInformation, poolProp);
+            }
 
-        //save loaded properties for later
-        mediator.addDataSourceProperty(DRIVER_Q, getValue(pool, DRIVER_Q));
-        mediator.addDataSourceProperty(URL_Q, getValue(pool, URL_Q));
-        mediator.addDataSourceProperty(USER_Q, getValue(pool, USER_Q));
-        mediator.addDataSourceProperty(PASS_Q, password);
-
-        Iterator props = pool.getChildrenWithName(PROP_Q);
-        while (props.hasNext()) {
-
-            OMElement prop = (OMElement) props.next();
-            String name = prop.getAttribute(ATT_NAME).getAttributeValue();
-            String value = prop.getAttribute(ATT_VALUE).getAttributeValue();
-            // save property for later
-            mediator.addDataSourceProperty(name, value);
-
-            if ("autocommit".equals(name)) {
-                if ("true".equals(value)) {
-                    ds.setDefaultAutoCommit(true);
-                } else if ("false".equals(value)) {
-                    ds.setDefaultAutoCommit(false);
-                }
-            } else if ("isolation".equals(name)) {
-                try {
-                    if ("Connection.TRANSACTION_NONE".equals(value)) {
-                        
ds.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
-                    } else if 
("Connection.TRANSACTION_READ_COMMITTED".equals(value)) {
-                        
ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
-                    } else if 
("Connection.TRANSACTION_READ_UNCOMMITTED".equals(value)) {
-                        
ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
-                    } else if 
("Connection.TRANSACTION_REPEATABLE_READ".equals(value)) {
-                        
ds.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
-                    } else if 
("Connection.TRANSACTION_SERIALIZABLE".equals(value)) {
-                        
ds.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
-                    }
-                } catch (NumberFormatException ignore) {
-                }
-            } else if ("initialsize".equals(name)) {
-                try {
-                    ds.setInitialSize(Integer.parseInt(value));
-                } catch (NumberFormatException ignore) {
-                }
-            } else if ("maxactive".equals(name)) {
-                try {
-                    ds.setMaxActive(Integer.parseInt(value));
-                } catch (NumberFormatException ignore) {
-                }
-            } else if ("maxidle".equals(name)) {
-                try {
-                    ds.setMaxIdle(Integer.parseInt(value));
-                } catch (NumberFormatException ignore) {
-                }
-            } else if ("maxopenstatements".equals(name)) {
-                try {
-                    ds.setMaxOpenPreparedStatements(Integer.parseInt(value));
-                } catch (NumberFormatException ignore) {
-                }
-            } else if ("maxwait".equals(name)) {
-                try {
-                    ds.setMaxWait(Long.parseLong(value));
-                } catch (NumberFormatException ignore) {
-                }
-            } else if ("minidle".equals(name)) {
-                try {
-                    ds.setMinIdle(Integer.parseInt(value));
-                } catch (NumberFormatException ignore) {
-                }
-            } else if ("poolstatements".equals(name)) {
-                if ("true".equals(value)) {
-                    ds.setPoolPreparedStatements(true);
-                } else if ("false".equals(value)) {
-                    ds.setPoolPreparedStatements(false);
-                }
-            } else if ("testonborrow".equals(name)) {
-                if ("true".equals(value)) {
-                    ds.setTestOnBorrow(true);
-                } else if ("false".equals(value)) {
-                    ds.setTestOnBorrow(false);
-                }
-            } else if ("testonreturn".equals(name)) {
-                if ("true".equals(value)) {
-                    ds.setTestOnReturn(true);
-                } else if ("false".equals(value)) {
-                    ds.setTestOnReturn(false);
-                }
-            } else if ("testwhileidle".equals(name)) {
-                if ("true".equals(value)) {
-                    ds.setTestWhileIdle(true);
-                } else if ("false".equals(value)) {
-                    ds.setTestWhileIdle(false);
+            mediator.setDataSourceInformation(dataSourceInformation);
+    }
+
+    private void readPoolProperty(AbstractDBMediator mediator, 
DataSourceInformation dataSourceInformation,
+            OMElement prop) {
+        String name = prop.getAttribute(ATT_NAME).getAttributeValue();
+        String value = prop.getAttribute(ATT_VALUE).getAttributeValue();
+        mediator.addDataSourceProperty(name, value);
+        
+        if ("autocommit".equals(name)) {
+            if ("true".equals(value)) {
+                dataSourceInformation.setDefaultAutoCommit(true);
+            } else if ("false".equals(value)) {
+                dataSourceInformation.setDefaultAutoCommit(false);
+            }
+        } else if ("isolation".equals(name)) {
+            try {
+                if ("Connection.TRANSACTION_NONE".equals(value)) {
+                    
dataSourceInformation.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
+                } else if 
("Connection.TRANSACTION_READ_COMMITTED".equals(value)) {
+                    
dataSourceInformation.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
+                } else if 
("Connection.TRANSACTION_READ_UNCOMMITTED".equals(value)) {
+                    
dataSourceInformation.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
+                } else if 
("Connection.TRANSACTION_REPEATABLE_READ".equals(value)) {
+                    
dataSourceInformation.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
+                } else if 
("Connection.TRANSACTION_SERIALIZABLE".equals(value)) {
+                    
dataSourceInformation.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
                 }
-            } else if ("validationquery".equals(name)) {
-                ds.setValidationQuery(value);
+            } catch (NumberFormatException ignore) {
+            }
+        } else if ("initialsize".equals(name)) {
+            try {
+                dataSourceInformation.setInitialSize(Integer.parseInt(value));
+            } catch (NumberFormatException ignore) {
+            }
+        } else if ("maxactive".equals(name)) {
+            try {
+                dataSourceInformation.setMaxActive(Integer.parseInt(value));
+            } catch (NumberFormatException ignore) {
+            }
+        } else if ("maxidle".equals(name)) {
+            try {
+                dataSourceInformation.setMaxIdle(Integer.parseInt(value));
+            } catch (NumberFormatException ignore) {
             }
+        } else if ("maxopenstatements".equals(name)) {
+            try {
+                
dataSourceInformation.setMaxOpenPreparedStatements(Integer.parseInt(value));
+            } catch (NumberFormatException ignore) {
+            }
+        } else if ("maxwait".equals(name)) {
+            try {
+                dataSourceInformation.setMaxWait(Long.parseLong(value));
+            } catch (NumberFormatException ignore) {
+            }
+        } else if ("minidle".equals(name)) {
+            try {
+                dataSourceInformation.setMinIdle(Integer.parseInt(value));
+            } catch (NumberFormatException ignore) {
+            }
+        } else if ("poolstatements".equals(name)) {
+            if ("true".equals(value)) {
+                dataSourceInformation.setPoolPreparedStatements(true);
+            } else if ("false".equals(value)) {
+                dataSourceInformation.setPoolPreparedStatements(false);
+            }
+        } else if ("testonborrow".equals(name)) {
+            if ("true".equals(value)) {
+                dataSourceInformation.setTestOnBorrow(true);
+            } else if ("false".equals(value)) {
+                dataSourceInformation.setTestOnBorrow(false);
+            }
+        } else if ("testonreturn".equals(name)) {
+            if ("true".equals(value)) {
+                dataSourceInformation.setTestOnReturn(true);
+            } else if ("false".equals(value)) {
+                dataSourceInformation.setTestOnReturn(false);
+            }
+        } else if ("testwhileidle".equals(name)) {
+            if ("true".equals(value)) {
+                dataSourceInformation.setTestWhileIdle(true);
+            } else if ("false".equals(value)) {
+                dataSourceInformation.setTestWhileIdle(false);
+            }
+        } else if ("validationquery".equals(name)) {
+            dataSourceInformation.setValidationQuery(value);
         }
-        return ds;
     }
 
     protected void processStatements(OMElement elem, AbstractDBMediator 
mediator) {
@@ -359,21 +332,9 @@
         }
         return null;
     }
-
-    /**
-     * Get the password from SecretManager . here only use SecretManager
-     *
-     * @param aliasPasword alias password
-     * @return if the SecretManager is initiated , then , get the 
corresponding secret
-     *         , else return alias itself
-     */
-
-    private String getActualPassword(String aliasPasword) {
-        SecretManager secretManager = SecretManager.getInstance();
-        if (secretManager.isInitialized()) {
-            return secretManager.getSecret(aliasPasword);
-        }
-        return aliasPasword;
+    
+    private void saveElementConfig(OMElement element, QName qname, 
AbstractDBMediator mediator) {
+        mediator.addDataSourceProperty(qname, getValue(element, qname));
     }
 }
 

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractDBMediatorSerializer.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractDBMediatorSerializer.java?rev=887530&r1=887529&r2=887530&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractDBMediatorSerializer.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractDBMediatorSerializer.java
 Sat Dec  5 12:13:11 2009
@@ -20,6 +20,7 @@
 package org.apache.synapse.config.xml;
 
 import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMText;
 import org.apache.synapse.SynapseException;
 import org.apache.synapse.mediators.db.AbstractDBMediator;
@@ -28,7 +29,6 @@
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamConstants;
 import java.sql.Types;
-import java.util.Iterator;
 
 /**
  * Base class for serializers for database related mediators.
@@ -43,6 +43,8 @@
  *       <password/>
  *     |
  *       <dsName/>
+ *     |
+ *       <dsName/>
  *       <icClass/>
  *       <url/>
  *       <user/>
@@ -82,13 +84,20 @@
     protected void serializeDBInformation(AbstractDBMediator mediator, 
OMElement dbParent) {
 
         OMElement connElt = fac.createOMElement("connection", synNS);
-        OMElement poolElt = fac.createOMElement("pool", synNS);
+        connElt.addChild(createPoolElement(mediator));
+        dbParent.addChild(connElt);
+
+        // process statements
+        for (Statement statement : mediator.getStatementList()) {
+            dbParent.addChild(createStatementElement(statement));
+        }
+    }
 
-        Iterator iter = mediator.getDataSourceProps().keySet().iterator();
-        while (iter.hasNext()) {
+    private OMNode createPoolElement(AbstractDBMediator mediator) {
+        OMElement poolElt = fac.createOMElement("pool", synNS);
+        for (Object o : mediator.getDataSourceProps().keySet()) {
 
-            Object o = iter.next();
-            String value = (String) mediator.getDataSourceProps().get(o);
+            String value = mediator.getDataSourceProps().get(o);
 
             if (o instanceof QName) {
                 QName name = (QName) o;
@@ -102,133 +111,129 @@
                 elt.addAttribute(fac.createOMAttribute("name", nullNS, 
(String) o));
                 elt.addAttribute(fac.createOMAttribute("value", nullNS, 
value));
                 poolElt.addChild(elt);
-            }
+             }
         }
+        return poolElt;
+    }
 
-        connElt.addChild(poolElt);
-        dbParent.addChild(connElt);
+    private OMNode createStatementElement(Statement statement) {
+        
+        OMElement stmntElt = fac.createOMElement(
+            AbstractDBMediatorFactory.STMNT_Q.getLocalPart(), synNS);
+
+        OMElement sqlElt = fac.createOMElement(
+            AbstractDBMediatorFactory.SQL_Q.getLocalPart(), synNS);
+        OMText sqlText = fac.createOMText(statement.getRawStatement(), 
XMLStreamConstants.CDATA);
+        sqlElt.addChild(sqlText);
+        stmntElt.addChild(sqlElt);
+
+        // serialize parameters of the statement
+        for (Statement.Parameter param : statement.getParameters()) {
+            OMElement paramElt = createStatementParamElement(param);
+            stmntElt.addChild(paramElt);
+        }
 
-        // process statements
-        Iterator statementIter = mediator.getStatementList().iterator();
-        while (statementIter.hasNext()) {
+        // serialize any optional results of the statement
+        for (String name : statement.getResultsMap().keySet()) {
 
-            Statement statement = (Statement) statementIter.next();
-            OMElement stmntElt = fac.createOMElement(
-                AbstractDBMediatorFactory.STMNT_Q.getLocalPart(), synNS);
-
-            OMElement sqlElt = fac.createOMElement(
-                AbstractDBMediatorFactory.SQL_Q.getLocalPart(), synNS);
-            OMText sqlText = fac.createOMText(statement.getRawStatement(), 
XMLStreamConstants.CDATA);
-            sqlElt.addChild(sqlText);
-            stmntElt.addChild(sqlElt);
-
-            // serialize parameters of the statement
-            for (Iterator it = statement.getParameters().iterator(); 
it.hasNext(); ) {
-
-                Statement.Parameter param = (Statement.Parameter) it.next();
-                OMElement paramElt = fac.createOMElement(
-                    AbstractDBMediatorFactory.PARAM_Q.getLocalPart(), synNS);
-
-                if (param.getPropertyName() != null) {
-                    paramElt.addAttribute(
-                        fac.createOMAttribute("value", nullNS, 
param.getPropertyName()));
-                }
-                if (param.getXpath() != null) {
-                    SynapseXPathSerializer.serializeXPath(param.getXpath(), 
paramElt, "expression");
-                }
-
-                switch (param.getType()) {
-                    case Types.CHAR: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "CHAR"));
-                        break;
-                    }
-                    case Types.VARCHAR: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "VARCHAR"));
-                        break;
-                    }
-                    case Types.LONGVARCHAR: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "LONGVARCHAR"));
-                        break;
-                    }
-                    case Types.NUMERIC: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "NUMERIC"));
-                        break;
-                    }
-                    case Types.DECIMAL: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "DECIMAL"));
-                        break;
-                    }
-                    case Types.BIT: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "BIT"));
-                        break;
-                    }
-                    case Types.TINYINT: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "TINYINT"));
-                        break;
-                    }
-                    case Types.SMALLINT: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "SMALLINT"));
-                        break;
-                    }
-                    case Types.INTEGER: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "INTEGER"));
-                        break;
-                    }
-                    case Types.BIGINT: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "BIGINT"));
-                        break;
-                    }
-                    case Types.REAL: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "REAL"));
-                        break;
-                    }
-                    case Types.FLOAT: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "FLOAT"));
-                        break;
-                    }
-                    case Types.DOUBLE: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "DOUBLE"));
-                        break;
-                    }
-                    case Types.DATE: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "DATE"));
-                        break;
-                    }
-                    case Types.TIME: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "TIME"));
-                        break;
-                    }
-                    case Types.TIMESTAMP: {
-                        paramElt.addAttribute(fac.createOMAttribute("type", 
nullNS, "TIMESTAMP"));
-                        break;
-                    }
-                    default: {
-                        throw new SynapseException("Unknown or unsupported 
JDBC type : " +
-                            param.getType());                            
-                    }
-                }
-
-                stmntElt.addChild(paramElt);
-            }
-
-            // serialize any optional results of the statement
-            for (Iterator it = statement.getResultsMap().keySet().iterator(); 
it.hasNext(); ) {
-
-                String name = (String) it.next();
-                String columnStr = (String) 
statement.getResultsMap().get(name);
-
-                OMElement resultElt = fac.createOMElement(
-                    AbstractDBMediatorFactory.RESULT_Q.getLocalPart(), synNS);
-
-                resultElt.addAttribute(
-                    fac.createOMAttribute("name", nullNS, name));
-                resultElt.addAttribute(
-                    fac.createOMAttribute("column", nullNS, columnStr));
+            String columnStr = statement.getResultsMap().get(name);
 
-                stmntElt.addChild(resultElt);
-            }
+            OMElement resultElt = fac.createOMElement(
+                AbstractDBMediatorFactory.RESULT_Q.getLocalPart(), synNS);
+
+            resultElt.addAttribute(
+                fac.createOMAttribute("name", nullNS, name));
+            resultElt.addAttribute(
+                fac.createOMAttribute("column", nullNS, columnStr));
+
+            stmntElt.addChild(resultElt);
+        }
+
+        return stmntElt;
+    }
 
-            dbParent.addChild(stmntElt);
+    private OMElement createStatementParamElement(Statement.Parameter param) {
+        OMElement paramElt = fac.createOMElement(
+            AbstractDBMediatorFactory.PARAM_Q.getLocalPart(), synNS);
+
+        if (param.getPropertyName() != null) {
+            paramElt.addAttribute(
+                fac.createOMAttribute("value", nullNS, 
param.getPropertyName()));
+        }
+        if (param.getXpath() != null) {
+            SynapseXPathSerializer.serializeXPath(param.getXpath(), paramElt, 
"expression");
+        }
+
+        switch (param.getType()) {
+            case Types.CHAR: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"CHAR"));
+                break;
+            }
+            case Types.VARCHAR: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"VARCHAR"));
+                break;
+            }
+            case Types.LONGVARCHAR: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"LONGVARCHAR"));
+                break;
+            }
+            case Types.NUMERIC: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"NUMERIC"));
+                break;
+            }
+            case Types.DECIMAL: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"DECIMAL"));
+                break;
+            }
+            case Types.BIT: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"BIT"));
+                break;
+            }
+            case Types.TINYINT: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"TINYINT"));
+                break;
+            }
+            case Types.SMALLINT: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"SMALLINT"));
+                break;
+            }
+            case Types.INTEGER: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"INTEGER"));
+                break;
+            }
+            case Types.BIGINT: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"BIGINT"));
+                break;
+            }
+            case Types.REAL: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"REAL"));
+                break;
+            }
+            case Types.FLOAT: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"FLOAT"));
+                break;
+            }
+            case Types.DOUBLE: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"DOUBLE"));
+                break;
+            }
+            case Types.DATE: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"DATE"));
+                break;
+            }
+            case Types.TIME: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"TIME"));
+                break;
+            }
+            case Types.TIMESTAMP: {
+                paramElt.addAttribute(fac.createOMAttribute("type", nullNS, 
"TIMESTAMP"));
+                break;
+            }
+            default: {
+                throw new SynapseException("Unknown or unsupported JDBC type : 
" +
+                    param.getType());                            
+            }
         }
+        return paramElt;
     }
 }

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/db/AbstractDBMediator.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/db/AbstractDBMediator.java?rev=887530&r1=887529&r2=887530&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/db/AbstractDBMediator.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/db/AbstractDBMediator.java
 Sat Dec  5 12:13:11 2009
@@ -21,45 +21,65 @@
 
 import org.apache.commons.dbcp.BasicDataSource;
 import org.apache.commons.dbcp.datasources.PerUserPoolDataSource;
+import org.apache.commons.logging.LogFactory;
 import org.apache.synapse.ManagedLifecycle;
 import org.apache.synapse.MessageContext;
 import org.apache.synapse.SynapseException;
 import org.apache.synapse.SynapseLog;
-import org.apache.synapse.commons.datasource.DBPoolView;
-import org.apache.synapse.config.xml.AbstractDBMediatorFactory;
+import org.apache.synapse.commons.datasource.*;
+import org.apache.synapse.commons.datasource.factory.DataSourceFactory;
+import org.apache.synapse.commons.jmx.MBeanRepository;
+import org.apache.synapse.commons.security.secret.SecretManager;
 import org.apache.synapse.core.SynapseEnvironment;
 import org.apache.synapse.mediators.AbstractMediator;
 
+import javax.naming.Context;
 import javax.sql.DataSource;
 import javax.xml.namespace.QName;
 import java.math.BigDecimal;
 import java.sql.*;
 import java.sql.Date;
 import java.util.*;
-
 /**
  * This abstract DB mediator will perform common DB connection pooling etc. 
for all DB mediators
  */
 public abstract class AbstractDBMediator extends AbstractMediator implements 
ManagedLifecycle {
 
-    /** Hold JDBC properties */
-    protected final Map dataSourceProps = new HashMap();
+    /** The information needed to create a data source */
+    private DataSourceInformation dataSourceInformation;
+
+    /** The name of the data source to lookup. */
+    private String dataSourceName;
+
+    /** The information needed to lookup a data source from a JNDI provider */
+    private Properties jndiProperties = new Properties();
+    
     /** The DataSource to get DB connections */
-    private DataSource dataSource = null;
-    /**
-     * MBean for DBPool monitoring
-     */
+    private DataSource dataSource;
+
+    /** MBean for DBPool monitoring */
     private DBPoolView dbPoolView;
+
     /** Statements */
     private final List<Statement> statementList = new ArrayList<Statement>();
 
+    /** Map to store the pool configuration for de-serialization */
+    private Map<Object, String> dataSourceProps = new HashMap<Object, 
String>();
+
     /**
-     * Initializes the mediator. Does nothing right now. If DataSource lookup 
is supported, could
-     * do the IC lookup here
+     * Initializes the mediator - either an existing data source will be 
looked up
+     * from an in- or external JNDI provider or a custom data source will be 
created
+     * based on the provide configuration (using Apache DBCP).
+     *  
      * @param se the Synapse environment reference
      */
     public void init(SynapseEnvironment se) {
-        // do nothing
+        // check whether we shall try to lookup an existing data source or 
create a new custom data source
+        if (dataSourceName != null) {
+            dataSource = lookupDataSource(dataSourceName, jndiProperties);
+        } else if (dataSourceInformation != null) {
+            dataSource = createCustomDataSource(dataSourceInformation);
+        }
     }
 
     /**
@@ -122,12 +142,20 @@
      * @return a unique name or URL to refer to the DataSource being used
      */
     protected String getDSName() {
-        String name = (String) 
dataSourceProps.get(AbstractDBMediatorFactory.URL_Q);
+        String name = dataSourceInformation.getUrl();
         if (name == null) {
-            name = (String) 
dataSourceProps.get(AbstractDBMediatorFactory.DSNAME_Q);
+            name = dataSourceInformation.getDatasourceName();
         }
         return name;
     }
+    
+    public void setDataSourceInformation(DataSourceInformation 
dataSourceInformation) {
+        this.dataSourceInformation = dataSourceInformation;
+    }
+
+    public void setJndiProperties(Properties jndiProperties) {
+        this.jndiProperties = jndiProperties;
+    }
 
     public DataSource getDataSource() {
         return dataSource;
@@ -137,6 +165,10 @@
         this.dataSource = dataSource;
     }
 
+    public void setDataSourceName(String dataSourceName) {
+        this.dataSourceName = dataSourceName;
+    }
+
     public void addDataSourceProperty(QName name, String value) {
         dataSourceProps.put(name, value);
     }
@@ -145,18 +177,22 @@
         dataSourceProps.put(name, value);
     }
 
-    public Map getDataSourceProps() {
-        return dataSourceProps;
-    }
-
     public void addStatement(Statement stmnt) {
         statementList.add(stmnt);
     }
 
-    public List getStatementList() {
+    public List<Statement> getStatementList() {
         return statementList;
     }
 
+    public DBPoolView getDbPoolView() {
+        return dbPoolView;
+    }
+
+    public void setDbPoolView(DBPoolView dbPoolView) {
+        this.dbPoolView = dbPoolView;
+    }
+
     /**
      * Return a Prepared statement for the given Statement object, which is 
ready to be executed
      * @param stmnt
@@ -188,7 +224,6 @@
             int numIdle = basicDataSource.getNumIdle();
             String connectionId = Integer.toHexString(con.hashCode());
 
-
             DBPoolView dbPoolView = getDbPoolView();
             if (dbPoolView != null) {
                 dbPoolView.setNumActive(numActive);
@@ -301,11 +336,87 @@
         return ps;
     }
 
-    public DBPoolView getDbPoolView() {
-        return dbPoolView;
+    /**
+     * Lookup the DataSource on JNDI using the specified name and optional 
properties
+     *
+     * @param dataSourceName the name of the data source to lookup
+     * @param jndiProperties the JNDI properties identifying a data source 
provider  
+     * 
+     * @return a DataSource looked up using the specified JNDI properties
+     */
+    private DataSource lookupDataSource(String dataSourceName, Properties 
jndiProperties) {
+
+        DataSource dataSource = null;
+        RepositoryBasedDataSourceFinder finder = DataSourceHelper.getInstance()
+                .getRepositoryBasedDataSourceFinder();
+
+        if (finder.isInitialized()) {
+            // first try a lookup based on the data source name only
+            dataSource = finder.find(dataSourceName);
+
+            if (dataSource != null) {
+                MBeanRepository mBeanRepository = 
DatasourceMBeanRepository.getInstance();
+                Object mBean = mBeanRepository.getMBean(dataSourceName);
+                if (mBean instanceof DBPoolView) {
+                    setDbPoolView((DBPoolView) mBean);
+                }
+            } else {
+                // decrypt the password if needed
+                String password = 
jndiProperties.getProperty(Context.SECURITY_CREDENTIALS);
+                if (password != null && !"".equals(password)) {
+                    jndiProperties.put(Context.SECURITY_CREDENTIALS, 
getActualPassword(password));
+                }
+
+                // lookup the data source using the specified jndi properties
+                dataSource = DataSourceFinder.find(dataSourceName, 
jndiProperties);
+                if (dataSource == null) {
+                    handleException("Cannot find a DataSource " + 
dataSourceName + " for given JNDI properties :" + jndiProperties);
+                }
+            }
+        }
+        log.info("Sunccessfully looked up datasource " + dataSourceName + ".");
+
+        return dataSource;
     }
 
-    public void setDbPoolView(DBPoolView dbPoolView) {
-        this.dbPoolView = dbPoolView;
+    /**
+     * Create a custom DataSource using the specified data source information.
+     *
+     * @param dataSourceInformation the data source information to create a 
data source
+     * 
+     * @return a DataSource created using specified properties
+     */
+    protected DataSource createCustomDataSource(DataSourceInformation 
dataSourceInformation) {
+        
+        DataSource dataSource = 
DataSourceFactory.createDataSource(dataSourceInformation);
+        if (dataSource != null) {
+            log.info("Sunccessfully created data source for " + 
dataSourceInformation.getUrl() + ".");
+        }
+        
+        return dataSource;
+    }
+
+    /**
+     * Get the password from SecretManager . here only use SecretManager
+     *
+     * @param aliasPasword alias password
+     * @return if the SecretManager is initiated , then , get the 
corresponding secret
+     *         , else return alias itself
+     */
+    private String getActualPassword(String aliasPasword) {
+        SecretManager secretManager = SecretManager.getInstance();
+        if (secretManager.isInitialized()) {
+            return secretManager.getSecret(aliasPasword);
+        }
+        return aliasPasword;
+    }
+
+    protected void handleException(String message) {
+        LogFactory.getLog(this.getClass()).error(message);
+        throw new SynapseException(message);
+    }
+
+    public Map<Object, String> getDataSourceProps() {
+        return dataSourceProps;
     }
 }

Modified: 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/db/DBLookupMediatorTest.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/db/DBLookupMediatorTest.java?rev=887530&r1=887529&r2=887530&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/db/DBLookupMediatorTest.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/db/DBLookupMediatorTest.java
 Sat Dec  5 12:13:11 2009
@@ -23,7 +23,9 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.SynapseConfiguration;
 import org.apache.synapse.config.xml.DBLookupMediatorFactory;
+import org.apache.synapse.core.axis2.Axis2SynapseEnvironment;
 import org.apache.synapse.mediators.AbstractMediatorTestCase;
 import org.apache.synapse.mediators.TestUtils;
 
@@ -50,6 +52,7 @@
     public static Test suite() {
         return new TestSetup(new TestSuite(DBLookupMediatorTest.class)) {
 
+            @Override
             protected void setUp() throws Exception {
 
                 String baseDir = System.getProperty("basedir");
@@ -80,6 +83,8 @@
                             "</dblookup>"
                     ));
 
+                lookup.init(new Axis2SynapseEnvironment(new 
SynapseConfiguration()));
+
                 java.sql.Statement s = 
lookup.getDataSource().getConnection().createStatement();
                 try {
                     s.execute("drop table destinations");
@@ -91,6 +96,7 @@
                 s.close();
             }
 
+            @Override
             protected void tearDown() throws Exception {
 
             }

Modified: 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/db/DBReportMediatorTest.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/db/DBReportMediatorTest.java?rev=887530&r1=887529&r2=887530&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/db/DBReportMediatorTest.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/db/DBReportMediatorTest.java
 Sat Dec  5 12:13:11 2009
@@ -23,16 +23,17 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.SynapseConfiguration;
 import org.apache.synapse.config.xml.DBReportMediatorFactory;
+import org.apache.synapse.core.axis2.Axis2SynapseEnvironment;
 import org.apache.synapse.mediators.AbstractMediatorTestCase;
 import org.apache.synapse.mediators.TestUtils;
 
-import java.io.File;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 
-public class DBReportMediatorTest extends AbstractMediatorTestCase {    
+public class DBReportMediatorTest extends AbstractMediatorTestCase {
 
     private static DBReportMediator report;
 
@@ -56,6 +57,7 @@
     public static Test suite() {
         return new TestSetup(new TestSuite(DBReportMediatorTest.class)) {
 
+            @Override
             protected void setUp() throws Exception {
 
                 String baseDir = System.getProperty("basedir");
@@ -85,6 +87,8 @@
                             "  </statement>\n" +
                             "</dblookup>"
                     ));
+                
+                report.init(new Axis2SynapseEnvironment(new 
SynapseConfiguration()));
 
                 java.sql.Statement s = 
report.getDataSource().getConnection().createStatement();
                 try {
@@ -94,6 +98,7 @@
                 s.close();
             }
 
+            @Override
             protected void tearDown() throws Exception {
 
             }


Reply via email to