Author: asankha
Date: Mon Sep 17 13:13:14 2007
New Revision: 576575
URL: http://svn.apache.org/viewvc?rev=576575&view=rev
Log:
Commit initial versions of DB lookup and report mediators
- need to clean up and do proper error handling and logging
- add support for DataSources/Connection Pools
- synchronize use of the DB connections and auto fix failed connections
Added:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediator.java
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediatorFactory.java
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediatorSerializer.java
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/Query.java
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediator.java
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediatorFactory.java
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediatorSerializer.java
Modified:
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/ProxyService.java
webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer
Modified:
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/ProxyService.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/ProxyService.java?rev=576575&r1=576574&r2=576575&view=diff
==============================================================================
---
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/ProxyService.java
(original)
+++
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/ProxyService.java
Mon Sep 17 13:13:14 2007
@@ -77,91 +77,83 @@
private static final Log log = LogFactory.getLog(ProxyService.class);
private static final Log trace = LogFactory.getLog(Constants.TRACE_LOGGER);
/**
- * The proxy service name
+ * The name of the proxy service
*/
private String name;
/**
- * The proxy service description
+ * The proxy service description. This could be optional informative text
about the service
*/
private String description;
/**
- * The transport/s over which this service should be exposed
+ * The transport/s over which this service should be exposed, or defaults
to all available
*/
- //private String transports;
private ArrayList transports;
/**
- * The target endpoint, if assigned
+ * The target endpoint key
*/
private String targetEndpoint = null;
/**
- * The target inSequence, if assigned
+ * The target inSequence key
*/
private String targetInSequence = null;
/**
- * The target outSequence, if assigned
+ * The target outSequence key
*/
private String targetOutSequence = null;
/**
- * The target faultSequence, if assigned
+ * The target faultSequence key
*/
private String targetFaultSequence = null;
/**
- * The target endpoint, if assigned
+ * The inlined definition of the target endpoint, if defined
*/
private Endpoint targetInLineEndpoint = null;
/**
- * The target inSequence, if assigned
+ * The inlined definition of the target in-sequence, if defined
*/
private SequenceMediator targetInLineInSequence = null;
/**
- * The target outSequence, if assigned
+ * The inlined definition of the target out-sequence, if defined
*/
private SequenceMediator targetInLineOutSequence = null;
/**
- * The target faultSequence, if assigned
+ * The inlined definition of the target fault-sequence, if defined
*/
private SequenceMediator targetInLineFaultSequence = null;
- // if a target endpoint or sequence is not specified,
- // the default Synapse main mediator will be used
/**
- * A list parameters
+ * A list of any service parameters (e.g. JMS parameters etc)
*/
private Map parameters = new HashMap();
-
/**
- * The key for the base WSDL, if specified
+ * The key for the base WSDL
*/
private String wsdlKey;
/**
- * The URI for the base WSDL, if specified
+ * The URI for the base WSDL, if defined as a URL
*/
private URI wsdlURI;
/**
- * Inline XML representation of wsdl
+ * The inlined representation of the service WSDL, if defined inline
*/
private Object inLineWSDL;
/**
- * The keys for any supplied schemas
- */ // todo: do we need this
- private List schemaKeys = new ArrayList();
- /**
* The keys for any supplied policies that would apply at the service level
*/
private List serviceLevelPolicies = new ArrayList();
/**
- * Should WS RM (default configuration) be engaged on this service
+ * Should WS RM be engaged on this service
*/
private boolean wsRMEnabled = false;
/**
- * Should WS Sec (default configuration) be engaged on this service
+ * Should WS Sec be engaged on this service
*/
private boolean wsSecEnabled = false;
/**
- * This will say weather need to start the service at the load or not
+ * Should this service be started by default on initialization?
*/
private boolean startOnLoad = true;
/**
- * This will hold the status of the proxy weather it is running or not
+ * Is this service running now?
*/
private boolean running = false;
@@ -176,9 +168,13 @@
*/
protected int traceState = Constants.TRACING_UNSET;
- public ProxyService() {
- }
-
+ /**
+ * Build the underlying Axis2 service from the Proxy service definition
+ *
+ * @param synCfg the Synapse configuration
+ * @param axisCfg the Axis2 configuration
+ * @return the Axis2 service for the Proxy
+ */
public AxisService buildAxisService(SynapseConfiguration synCfg,
AxisConfiguration axisCfg) {
AxisService proxyService = null;
@@ -282,7 +278,7 @@
}
proxyService.setName(name);
if (description != null) {
- proxyService.setServiceDescription(description);
+ proxyService.setDocumentation(description);
}
// process transports and expose over requested transports. If none
@@ -467,14 +463,6 @@
public void setWSDLKey(String wsdlKey) {
this.wsdlKey = wsdlKey;
- }
-
- public List getSchemas() {
- return schemaKeys;
- }
-
- public void setSchemas(List schemas) {
- this.schemaKeys = schemas;
}
public List getServiceLevelPolicies() {
Added:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediator.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediator.java?rev=576575&view=auto
==============================================================================
---
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediator.java
(added)
+++
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediator.java
Mon Sep 17 13:13:14 2007
@@ -0,0 +1,147 @@
+/*
+ * 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.synapse.mediators.dblookup;
+
+import org.apache.synapse.mediators.AbstractMediator;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.ManagedLifecycle;
+import org.apache.synapse.core.SynapseEnvironment;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.xml.namespace.QName;
+import java.sql.*;
+import java.util.*;
+
+/**
+ * Simple database table lookup mediator. Designed only for read/lookup
+ */
+public class DBLookupMediator extends AbstractMediator implements
ManagedLifecycle {
+
+ private static final Log log = LogFactory.getLog(DBLookupMediator.class);
+
+ /** Hold JDBC properties */
+ private Map jdbcProps = new HashMap();
+ /** The connection to the database */
+ Connection conn = null;
+ /** Query map */
+ Map queryMap = new HashMap();
+ /** Result cache */
+ Map cacheMap = new HashMap();
+
+ public boolean mediate(MessageContext msgCtx) {
+
+ for (Iterator iter = queryMap.keySet().iterator(); iter.hasNext(); ) {
+ processQuery((String) iter.next(), msgCtx);
+ }
+ return true;
+ }
+
+ private void processQuery(String attName, MessageContext msgCtx) {
+
+ // if available in cache, serve from cache and return
+ String result = (String) cacheMap.get(attName);
+ if (result != null) {
+ msgCtx.setProperty(attName, result);
+ return;
+ }
+
+ Query query = (Query) queryMap.get(attName);
+
+ try {
+ PreparedStatement ps = query.getStatement();
+
+ // set parameters if any
+ List params = query.getParameters();
+ int column = 1;
+ for (Iterator pi = params.iterator(); pi.hasNext(); ) {
+ Query.Parameter param = (Query.Parameter) pi.next();
+ switch (param.getType()) {
+ case Types.VARCHAR: {
+ ps.setString(column++,
+ param.getPropertyName() != null ?
+ (String)
msgCtx.getProperty(param.getPropertyName()) :
+
Axis2MessageContext.getStringValue(param.getXpath(), msgCtx));
+ break;
+ }
+ case Types.INTEGER: {
+ ps.setInt(column++,
+ Integer.parseInt(param.getPropertyName() != null ?
+ (String)
msgCtx.getProperty(param.getPropertyName()) :
+
Axis2MessageContext.getStringValue(param.getXpath(), msgCtx)));
+ break;
+ }
+ default: {
+ // todo handle this
+ }
+ }
+ }
+
+ ResultSet rs = ps.executeQuery();
+ if (rs.next()) {
+ Object obj = rs.getObject(1);
+ if (obj != null) {
+ msgCtx.setProperty(attName, obj.toString());
+ cacheMap.put(attName, obj.toString());
+ } else {
+ // todo handle this
+ }
+ }
+ } catch (SQLException e) {
+ // todo handle this
+ e.printStackTrace();
+ }
+ }
+
+ public void init(SynapseEnvironment se) {
+ // establish database connection
+ }
+
+ public void destroy() {
+ // disconnect from the database
+ log.debug("Shutting down database connection of the DB Lookup
mediator");
+ try {
+ conn.close();
+ } catch (SQLException e) {
+ log.warn("Error shutting down the database connection", e);
+ }
+ }
+
+ public void setConn(Connection conn) {
+ this.conn = conn;
+ }
+
+ public void addQuery(String name, Query q) {
+ queryMap.put(name, q);
+ }
+
+ public void addJDBCProperty(QName name, String value) {
+ jdbcProps.put(name, value);
+ }
+
+ public Map getJdbcProps() {
+ return jdbcProps;
+ }
+
+ public Map getQueryMap() {
+ return queryMap;
+ }
+}
Added:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediatorFactory.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediatorFactory.java?rev=576575&view=auto
==============================================================================
---
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediatorFactory.java
(added)
+++
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediatorFactory.java
Mon Sep 17 13:13:14 2007
@@ -0,0 +1,177 @@
+/*
+ * 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.synapse.mediators.dblookup;
+
+import org.apache.synapse.config.xml.AbstractMediatorFactory;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.Constants;
+import org.apache.synapse.SynapseException;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jaxen.JaxenException;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Connection;
+
+/**
+ * <dblookup>
+ * <connection>
+ * <jdbc>
+ * <driver/>
+ * <url/>
+ * <user/>
+ * <password/>
+ * </jdbc>
+ * </connection>
+ * <query setAttribute="queue_name">
+ * <sql>select queue from table where device_id = ?</sql>
+ * <parameter [property="" | xpath=""] type="int|string"/>*
+ * </query>+
+ * </dblookup>
+ */
+public class DBLookupMediatorFactory extends AbstractMediatorFactory {
+
+ private static final Log log =
LogFactory.getLog(DBLookupMediatorFactory.class);
+
+ private static final QName DBLOOKUP_Q = new
QName(Constants.SYNAPSE_NAMESPACE, "dblookup");
+
+ private static final QName DRIVER_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"driver");
+ private static final QName URL_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"url");
+ private static final QName USER_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"user");
+ private static final QName PASS_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"password");
+
+ private static final QName QUERY_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"query");
+ private static final QName Q_ATT_Q = new QName("setAttribute");
+ private static final QName SQL_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"sql");
+ private static final QName PARAM_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"parameter");
+ private static final QName PROPERTY_Q = new QName("property");
+ private static final QName XPATH_Q = new QName("xpath");
+ private static final QName TYPE_Q = new QName("type");
+
+ public Mediator createMediator(OMElement elem) {
+
+ DBLookupMediator mediator = new DBLookupMediator();
+ Connection conn = null;
+
+ try {
+ AXIOMXPath xpath = new AXIOMXPath("//syn:connection/syn:jdbc");
+ xpath.addNamespace("syn",
org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE);
+ OMElement jdbc = (OMElement) xpath.selectSingleNode(elem);
+
+ try {
+ Class.forName(getValue(jdbc, DRIVER_Q));
+ conn = DriverManager.getConnection(
+ getValue(jdbc, URL_Q), getValue(jdbc, USER_Q),
getValue(jdbc, PASS_Q));
+ mediator.setConn(conn);
+ mediator.addJDBCProperty(DRIVER_Q, getValue(jdbc, DRIVER_Q));
+ mediator.addJDBCProperty(URL_Q, getValue(jdbc, URL_Q));
+ mediator.addJDBCProperty(USER_Q, getValue(jdbc, USER_Q));
+ mediator.addJDBCProperty(PASS_Q, getValue(jdbc, PASS_Q));
+
+ } catch (SQLException e) {
+ handleException("Error connecting to Database using : " +
jdbc, e);
+ } catch (ClassNotFoundException e) {
+ handleException("Error loading JDBC driver using : " + jdbc,
e);
+ }
+
+ } catch (JaxenException e) {
+ // in future handle connection pools and data sources, but for now
fail if not jdbc
+ handleException("JDBC Database connection information must be
specified");
+ }
+
+ Iterator iter = elem.getChildrenWithName(QUERY_Q);
+ while (iter.hasNext()) {
+
+ OMElement qryElt = (OMElement) iter.next();
+ Query query = new Query();
+ if (qryElt.getAttribute(Q_ATT_Q) != null) {
+ try {
+ query.setStatement(conn.prepareStatement(getValue(qryElt,
SQL_Q)));
+ query.setRawStatement(getValue(qryElt, SQL_Q));
+ } catch (SQLException e) {
+ handleException("Invalid SQL query for Lookup : " +
getValue(qryElt, SQL_Q), e);
+ }
+
+ Iterator paramIter = qryElt.getChildrenWithName(PARAM_Q);
+ while (paramIter.hasNext()) {
+
+ OMElement paramElt = (OMElement) paramIter.next();
+ try {
+ query.addParameter(
+ getAttribute(paramElt, PROPERTY_Q),
+ getAttribute(paramElt, XPATH_Q),
+ getAttribute(paramElt, TYPE_Q));
+ } catch (JaxenException e) {
+ handleException("Invalid XPath expression for query : "
+ + getAttribute(paramElt, XPATH_Q));
+ }
+ }
+ mediator.addQuery(qryElt.getAttributeValue(Q_ATT_Q), query);
+ }
+ }
+
+ return mediator;
+ }
+
+ private String getValue(OMElement elt, QName qName) {
+ OMElement e = elt.getFirstChildWithName(qName);
+ if (e != null) {
+ return e.getText();
+ } else {
+ handleException("Unable to read configuration value for : " +
qName);
+ }
+ return null;
+ }
+
+ private String getAttribute(OMElement elt, QName qName) {
+ OMAttribute a = elt.getAttribute(qName);
+ if (a != null) {
+ return a.getAttributeValue();
+ }
+ return null;
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ private void handleException(String msg, Exception e) {
+ log.error(msg, e);
+ throw new SynapseException(msg, e);
+ }
+
+ public QName getTagQName() {
+ return DBLOOKUP_Q;
+ }
+}
Added:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediatorSerializer.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediatorSerializer.java?rev=576575&view=auto
==============================================================================
---
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediatorSerializer.java
(added)
+++
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/DBLookupMediatorSerializer.java
Mon Sep 17 13:13:14 2007
@@ -0,0 +1,141 @@
+/*
+ * 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.synapse.mediators.dblookup;
+
+import org.apache.synapse.config.xml.AbstractMediatorSerializer;
+import org.apache.synapse.Mediator;
+import org.apache.axiom.om.OMElement;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.Log;
+import org.apache.synapse.SynapseException;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+import java.util.Map;
+import java.sql.Types;
+
+/**
+ * <dblookup>
+ * <connection>
+ * <jdbc>
+ * <driver/>
+ * <url/>
+ * <user/>
+ * <password/>
+ * </jdbc>
+ * </connection>
+ * <query setAttribute="queue_name">
+ * <sql>select queue from table where device_id = ?</sql>
+ * <parameter [property="" | xpath=""] type="int|string"/>*
+ * </query>+
+ * </dblookup>
+ */
+public class DBLookupMediatorSerializer extends AbstractMediatorSerializer {
+
+ private static final Log log =
LogFactory.getLog(DBLookupMediatorSerializer.class);
+
+ public OMElement serializeMediator(OMElement parent, Mediator m) {
+
+ if (!(m instanceof DBLookupMediator)) {
+ handleException("Unsupported mediator passed in for serialization
: " + m.getType());
+ }
+
+ DBLookupMediator mediator = (DBLookupMediator) m;
+ OMElement dbLookup = fac.createOMElement("dblookup", synNS);
+ finalizeSerialization(dbLookup,mediator);
+
+ // process jdbc info
+ OMElement connElt = fac.createOMElement("connection", synNS);
+ OMElement jdbcElt = fac.createOMElement("jdbc", synNS);
+
+ Iterator iter = mediator.getJdbcProps().keySet().iterator();
+ while (iter.hasNext()) {
+ QName name = (QName) iter.next();
+ OMElement elt = fac.createOMElement(name);
+ elt.setText((String) mediator.getJdbcProps().get(name));
+ jdbcElt.addChild(elt);
+ }
+ connElt.addChild(jdbcElt);
+ dbLookup.addChild(connElt);
+
+ // process queries
+ Map queryMap = mediator.getQueryMap();
+ iter = queryMap.keySet().iterator();
+
+ while (iter.hasNext()) {
+
+ String attName = (String) iter.next();
+ Query query = (Query) queryMap.get(attName);
+
+ OMElement queryElt = fac.createOMElement("query", synNS);
+ queryElt.addAttribute(fac.createOMAttribute("setAttribute",
nullNS, attName));
+
+ OMElement sqlElt = fac.createOMElement("sql", synNS);
+ sqlElt.setText(query.getRawStatement());
+ queryElt.addChild(sqlElt);
+
+ for (Iterator it = query.getParameters().iterator(); it.hasNext();
) {
+
+ Query.Parameter param = (Query.Parameter) it.next();
+ OMElement paramElt = fac.createOMElement("parameter", synNS);
+
+ if (param.getPropertyName() != null) {
+ paramElt.addAttribute(
+ fac.createOMAttribute("property", nullNS,
param.getPropertyName()));
+ }
+ if (param.getXpath() != null) {
+ paramElt.addAttribute(
+ fac.createOMAttribute("xpath", nullNS,
param.getXpath().toString()));
+ super.serializeNamespaces(paramElt, param.getXpath());
+ }
+
+ switch (param.getType()) {
+ case Types.VARCHAR: {
+ paramElt.addAttribute(fac.createOMAttribute("type",
nullNS, "string"));
+ break;
+ }
+ case Types.INTEGER: {
+ paramElt.addAttribute(fac.createOMAttribute("type",
nullNS, "int"));
+ break;
+ }
+ default:
+ // TODO handle
+ }
+
+ queryElt.addChild(paramElt);
+ dbLookup.addChild(queryElt);
+ }
+ }
+
+ if (parent != null) {
+ parent.addChild(dbLookup);
+ }
+ return dbLookup;
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ public String getMediatorClassName() {
+ return DBLookupMediator.class.getName();
+ }
+}
Added:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/Query.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/Query.java?rev=576575&view=auto
==============================================================================
---
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/Query.java
(added)
+++
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dblookup/Query.java
Mon Sep 17 13:13:14 2007
@@ -0,0 +1,92 @@
+/*
+ * 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.synapse.mediators.dblookup;
+
+import org.jaxen.JaxenException;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.sql.PreparedStatement;
+import java.sql.Types;
+
+public class Query {
+ String rawStatement = null;
+ List parameters = new ArrayList();
+ PreparedStatement statement = null;
+
+ public PreparedStatement getStatement() {
+ return statement;
+ }
+
+ public void setStatement(PreparedStatement statement) {
+ this.statement = statement;
+ }
+
+ public void addParameter(String propertyName, String xpath, String type)
throws JaxenException {
+ parameters.add(new Parameter(propertyName, xpath, type));
+ }
+
+ public List getParameters() {
+ return parameters;
+ }
+
+ public class Parameter {
+ String propertyName = null;
+ AXIOMXPath xpath = null;
+ int type = 0;
+
+ Parameter(String value, String xpath, String type) throws
JaxenException {
+ this.propertyName = value;
+ if (xpath != null) {
+ this.xpath = new AXIOMXPath(xpath);
+ }
+
+ if ("string".equals(type)) {
+ this.type = Types.VARCHAR;
+ } else if ("int".equals(type)) {
+ this.type = Types.INTEGER;
+ } else {
+ // todo
+ }
+ }
+
+ public String getPropertyName() {
+ return propertyName;
+ }
+
+ public AXIOMXPath getXpath() {
+ return xpath;
+ }
+
+ public int getType() {
+ return type;
+ }
+ }
+
+ public String getRawStatement() {
+ return rawStatement;
+ }
+
+ public void setRawStatement(String rawStatement) {
+ this.rawStatement = rawStatement;
+ }
+}
+
Added:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediator.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediator.java?rev=576575&view=auto
==============================================================================
---
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediator.java
(added)
+++
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediator.java
Mon Sep 17 13:13:14 2007
@@ -0,0 +1,128 @@
+/*
+ * 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.synapse.mediators.dbreport;
+
+import org.apache.synapse.mediators.AbstractMediator;
+import org.apache.synapse.mediators.dblookup.Query;
+import org.apache.synapse.ManagedLifecycle;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.core.SynapseEnvironment;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.xml.namespace.QName;
+import java.util.*;
+import java.sql.*;
+
+public class DBReportMediator extends AbstractMediator implements
ManagedLifecycle {
+
+ private static final Log log = LogFactory.getLog(DBReportMediator.class);
+
+ /** Hold JDBC properties */
+ private Map jdbcProps = new HashMap();
+ /** The connection to the database */
+ Connection conn = null;
+ /** Query map */
+ List queryList = new ArrayList();
+
+ public boolean mediate(MessageContext msgCtx) {
+ for (Iterator iter = queryList.iterator(); iter.hasNext(); ) {
+ processQuery((Query) iter.next(), msgCtx);
+ }
+ return true;
+ }
+
+ private void processQuery(Query query, MessageContext msgCtx) {
+
+ try {
+ PreparedStatement ps = query.getStatement();
+
+ // set parameters if any
+ List params = query.getParameters();
+ int column = 1;
+ for (Iterator pi = params.iterator(); pi.hasNext(); ) {
+ Query.Parameter param = (Query.Parameter) pi.next();
+ switch (param.getType()) {
+ case Types.VARCHAR: {
+ ps.setString(column++,
+ param.getPropertyName() != null ?
+ (String)
msgCtx.getProperty(param.getPropertyName()) :
+
Axis2MessageContext.getStringValue(param.getXpath(), msgCtx));
+ break;
+ }
+ case Types.INTEGER: {
+ ps.setInt(column++,
+ Integer.parseInt(param.getPropertyName() != null ?
+ (String)
msgCtx.getProperty(param.getPropertyName()) :
+
Axis2MessageContext.getStringValue(param.getXpath(), msgCtx)));
+ break;
+ }
+ default: {
+ // todo handle this
+ }
+ }
+ }
+
+ if (ps.executeUpdate() > 0) {
+ if (log.isDebugEnabled()) {
+ log.debug("Added a row to the table");
+ }
+ }
+ } catch (SQLException e) {
+ // todo handle this
+ e.printStackTrace();
+ }
+ }
+
+ public void init(SynapseEnvironment se) {
+ // establish database connection
+ }
+
+ public void destroy() {
+ // disconnect from the database
+ log.debug("Shutting down database connection of the DB Report
mediator");
+ try {
+ conn.close();
+ } catch (SQLException e) {
+ log.warn("Error shutting down the database connection", e);
+ }
+ }
+
+ public void setConn(Connection conn) {
+ this.conn = conn;
+ }
+
+ public void addQuery(Query q) {
+ queryList.add(q);
+ }
+
+ public void addJDBCProperty(QName name, String value) {
+ jdbcProps.put(name, value);
+ }
+
+ public Map getJdbcProps() {
+ return jdbcProps;
+ }
+
+ public List getQueryList() {
+ return queryList;
+ }
+}
Added:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediatorFactory.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediatorFactory.java?rev=576575&view=auto
==============================================================================
---
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediatorFactory.java
(added)
+++
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediatorFactory.java
Mon Sep 17 13:13:14 2007
@@ -0,0 +1,179 @@
+/*
+ * 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.synapse.mediators.dbreport;
+
+import org.apache.synapse.config.xml.AbstractMediatorFactory;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.Constants;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.mediators.dblookup.Query;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jaxen.JaxenException;
+
+import javax.xml.namespace.QName;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Iterator;
+
+/**
+ * <dbreport>
+ * <connection>
+ * <jdbc>
+ * <driver/>
+ * <url/>
+ * <user/>
+ * <password/>
+ * </jdbc>
+ * </connection>
+ * <insert>
+ * <sql>insert into table values (?, ?, ..)</sql>
+ * <parameter [property="" | xpath=""] type="int|string"/>*
+ * </insert>+
+ * </dbreport>
+ */
+public class DBReportMediatorFactory extends AbstractMediatorFactory {
+
+ private static final Log log =
LogFactory.getLog(DBReportMediatorFactory.class);
+
+ private static final QName DBREPORT_Q = new
QName(Constants.SYNAPSE_NAMESPACE, "dbreport");
+
+ private static final QName DRIVER_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"driver");
+ private static final QName URL_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"url");
+ private static final QName USER_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"user");
+ private static final QName PASS_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"password");
+
+ private static final QName INSERT_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"insert");
+ private static final QName SQL_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"sql");
+ private static final QName PARAM_Q =
+ new QName(org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE,
"parameter");
+
+ private static final QName PROPERTY_Q = new QName("property");
+ private static final QName XPATH_Q = new QName("xpath");
+ private static final QName TYPE_Q = new QName("type");
+
+ public Mediator createMediator(OMElement elem) {
+
+ DBReportMediator mediator = new DBReportMediator();
+ Connection conn = null;
+
+ try {
+ AXIOMXPath xpath = new AXIOMXPath("//syn:connection/syn:jdbc");
+ xpath.addNamespace("syn",
org.apache.synapse.config.xml.Constants.SYNAPSE_NAMESPACE);
+ OMElement jdbc = (OMElement) xpath.selectSingleNode(elem);
+
+ try {
+ Class.forName(getValue(jdbc, DRIVER_Q));
+ conn = DriverManager.getConnection(
+ getValue(jdbc, URL_Q), getValue(jdbc, USER_Q),
getValue(jdbc, PASS_Q));
+ mediator.setConn(conn);
+ mediator.addJDBCProperty(DRIVER_Q, getValue(jdbc, DRIVER_Q));
+ mediator.addJDBCProperty(URL_Q, getValue(jdbc, URL_Q));
+ mediator.addJDBCProperty(USER_Q, getValue(jdbc, USER_Q));
+ mediator.addJDBCProperty(PASS_Q, getValue(jdbc, PASS_Q));
+
+ } catch (SQLException e) {
+ handleException("Error connecting to Database using : " +
jdbc, e);
+ } catch (ClassNotFoundException e) {
+ handleException("Error loading JDBC driver using : " + jdbc,
e);
+ }
+
+ } catch (JaxenException e) {
+ // in future handle connection pools and data sources, but for now
fail if not jdbc
+ handleException("JDBC Database connection information must be
specified");
+ }
+
+ Iterator iter = elem.getChildrenWithName(INSERT_Q);
+ while (iter.hasNext()) {
+
+ OMElement qryElt = (OMElement) iter.next();
+ Query query = new Query();
+
+ try {
+ query.setStatement(conn.prepareStatement(getValue(qryElt,
SQL_Q)));
+ query.setRawStatement(getValue(qryElt, SQL_Q));
+ } catch (SQLException e) {
+ handleException("Invalid SQL query for Lookup : " +
getValue(qryElt, SQL_Q), e);
+ }
+
+ Iterator paramIter = qryElt.getChildrenWithName(PARAM_Q);
+ while (paramIter.hasNext()) {
+
+ OMElement paramElt = (OMElement) paramIter.next();
+ try {
+ query.addParameter(
+ getAttribute(paramElt, PROPERTY_Q),
+ getAttribute(paramElt, XPATH_Q),
+ getAttribute(paramElt, TYPE_Q));
+ } catch (JaxenException e) {
+ handleException("Invalid XPath expression for query : "
+ + getAttribute(paramElt, XPATH_Q));
+ }
+ }
+
+ mediator.addQuery(query);
+ }
+
+ return mediator;
+ }
+
+ private String getValue(OMElement elt, QName qName) {
+ OMElement e = elt.getFirstChildWithName(qName);
+ if (e != null) {
+ return e.getText();
+ } else {
+ handleException("Unable to read configuration value for : " +
qName);
+ }
+ return null;
+ }
+
+ private String getAttribute(OMElement elt, QName qName) {
+ OMAttribute a = elt.getAttribute(qName);
+ if (a != null) {
+ return a.getAttributeValue();
+ }
+ return null;
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ private void handleException(String msg, Exception e) {
+ log.error(msg, e);
+ throw new SynapseException(msg, e);
+ }
+
+
+ public QName getTagQName() {
+ return DBREPORT_Q;
+ }
+}
Added:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediatorSerializer.java
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediatorSerializer.java?rev=576575&view=auto
==============================================================================
---
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediatorSerializer.java
(added)
+++
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/dbreport/DBReportMediatorSerializer.java
Mon Sep 17 13:13:14 2007
@@ -0,0 +1,137 @@
+/*
+ * 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.synapse.mediators.dbreport;
+
+import org.apache.synapse.config.xml.AbstractMediatorSerializer;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.mediators.dblookup.Query;
+import org.apache.axiom.om.OMElement;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+import java.util.List;
+import java.sql.Types;
+
+/**
+ * <dbreport>
+ * <connection>
+ * <jdbc>
+ * <driver/>
+ * <url/>
+ * <user/>
+ * <password/>
+ * </jdbc>
+ * </connection>
+ * <insert>
+ * <sql>insert into table values (?, ?, ..)</sql>
+ * <parameter [property="" | xpath=""] type="int|string"/>*
+ * </insert>+
+ * </dbreport>
+ */
+public class DBReportMediatorSerializer extends AbstractMediatorSerializer {
+
+ private static final Log log =
LogFactory.getLog(DBReportMediatorSerializer.class);
+
+ public OMElement serializeMediator(OMElement parent, Mediator m) {
+
+ if (!(m instanceof DBReportMediator)) {
+ handleException("Unsupported mediator passed in for serialization
: " + m.getType());
+ }
+
+ DBReportMediator mediator = (DBReportMediator) m;
+ OMElement dbReport = fac.createOMElement("dbreport", synNS);
+ finalizeSerialization(dbReport, mediator);
+
+ // process jdbc info
+ OMElement connElt = fac.createOMElement("connection", synNS);
+ OMElement jdbcElt = fac.createOMElement("jdbc", synNS);
+
+ Iterator iter = mediator.getJdbcProps().keySet().iterator();
+ while (iter.hasNext()) {
+ QName name = (QName) iter.next();
+ OMElement elt = fac.createOMElement(name);
+ elt.setText((String) mediator.getJdbcProps().get(name));
+ jdbcElt.addChild(elt);
+ }
+ connElt.addChild(jdbcElt);
+ dbReport.addChild(connElt);
+
+ // process queries
+ iter = mediator.getQueryList().iterator();
+
+ while (iter.hasNext()) {
+ Query query = (Query) iter.next();
+
+ OMElement insertElt = fac.createOMElement("insert", synNS);
+
+ OMElement sqlElt = fac.createOMElement("sql", synNS);
+ sqlElt.setText(query.getRawStatement());
+ insertElt.addChild(sqlElt);
+
+ for (Iterator it = query.getParameters().iterator(); it.hasNext();
) {
+
+ Query.Parameter param = (Query.Parameter) it.next();
+ OMElement paramElt = fac.createOMElement("parameter", synNS);
+
+ if (param.getPropertyName() != null) {
+ paramElt.addAttribute(
+ fac.createOMAttribute("property", nullNS,
param.getPropertyName()));
+ }
+ if (param.getXpath() != null) {
+ paramElt.addAttribute(
+ fac.createOMAttribute("xpath", nullNS,
param.getXpath().toString()));
+ super.serializeNamespaces(paramElt, param.getXpath());
+ }
+
+ switch (param.getType()) {
+ case Types.VARCHAR: {
+ paramElt.addAttribute(fac.createOMAttribute("type",
nullNS, "string"));
+ break;
+ }
+ case Types.INTEGER: {
+ paramElt.addAttribute(fac.createOMAttribute("type",
nullNS, "int"));
+ break;
+ }
+ default:
+ // TODO handle
+ }
+ insertElt.addChild(paramElt);
+ }
+ dbReport.addChild(insertElt);
+ }
+
+ if (parent != null) {
+ parent.addChild(dbReport);
+ }
+ return dbReport;
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ public String getMediatorClassName() {
+ return DBReportMediator.class.getName();
+ }
+}
Modified:
webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory?rev=576575&r1=576574&r2=576575&view=diff
==============================================================================
---
webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
(original)
+++
webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
Mon Sep 17 13:13:14 2007
@@ -1,4 +1,6 @@
org.apache.synapse.mediators.spring.SpringMediatorFactory
org.apache.synapse.mediators.bsf.ScriptMediatorFactory
org.apache.synapse.mediators.attachment.AttachmentMediatorFactory
-org.apache.synapse.mediators.throttle.ThrottleMediatorFactory
\ No newline at end of file
+org.apache.synapse.mediators.throttle.ThrottleMediatorFactory
+org.apache.synapse.mediators.dblookup.DBLookupMediatorFactory
+org.apache.synapse.mediators.dbreport.DBReportMediatorFactory
Modified:
webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer
URL:
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer?rev=576575&r1=576574&r2=576575&view=diff
==============================================================================
---
webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer
(original)
+++
webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer
Mon Sep 17 13:13:14 2007
@@ -1,3 +1,5 @@
org.apache.synapse.mediators.spring.SpringMediatorSerializer
org.apache.synapse.mediators.bsf.ScriptMediatorSerializer
-org.apache.synapse.mediators.throttle.ThrottleMediatorSerializer
\ No newline at end of file
+org.apache.synapse.mediators.throttle.ThrottleMediatorSerializer
+org.apache.synapse.mediators.dblookup.DBLookupMediatorSerializer
+org.apache.synapse.mediators.dbreport.DBReportMediatorSerializer
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]