You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by pd...@apache.org on 2006/07/18 15:31:28 UTC
svn commit: r423082 - in
/incubator/servicemix/trunk/servicemix-components/src:
main/java/org/apache/servicemix/components/mps/
test/java/org/apache/servicemix/components/mps/
Author: pdodds
Date: Tue Jul 18 06:31:27 2006
New Revision: 423082
URL: http://svn.apache.org/viewvc?rev=423082&view=rev
Log:
Applied new components : see https://issues.apache.org/activemq/browse/SM-486
Added:
incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/
incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/ConfigNotSupportedException.java
incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/ExistingPropertyCopier.java
incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/MessagePropertySetterXML.java
incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertySet.java
incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertySetNotFoundException.java
incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertyValue.java
incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertyValueResolver.java
incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/StaticStringPropertyValue.java
incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/XPathContentMessagePropertyValue.java
incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/
incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/MPSSettingTest.java
incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertySetTest.java
incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertyValueResolverTest.java
incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertyValueTest.java
Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/ConfigNotSupportedException.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/ConfigNotSupportedException.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/ConfigNotSupportedException.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/ConfigNotSupportedException.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.components.mps;
+
+public class ConfigNotSupportedException extends Exception {
+
+ public ConfigNotSupportedException() {
+ super();
+ }
+ public ConfigNotSupportedException(String message) {
+ super(message);
+ }
+
+}
Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/ExistingPropertyCopier.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/ExistingPropertyCopier.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/ExistingPropertyCopier.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/ExistingPropertyCopier.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.components.mps;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.NormalizedMessage;
+
+
+/**
+ * Copy a property value from an existing property (somewhere)
+ * @author rbuckland
+ *
+ */
+public class ExistingPropertyCopier implements PropertyValue {
+
+ public final static String XML_ELEMENT_NAME = "existing-property";
+
+ private String name;
+
+ public ExistingPropertyCopier(String name) {
+ this.name = name;
+ }
+ public String getPropertyValue(NormalizedMessage msg) throws JBIException {
+ if (msg.getProperty(name) != null) {
+ return msg.getProperty(name).toString();
+ } else {
+ return null;
+ }
+
+ }
+
+}
Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/MessagePropertySetterXML.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/MessagePropertySetterXML.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/MessagePropertySetterXML.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/MessagePropertySetterXML.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,325 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.components.mps;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.jbi.JBIException;
+import javax.jbi.component.ComponentContext;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+
+import org.apache.log4j.Logger;
+import org.apache.servicemix.components.util.TransformComponentSupport;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.xpath.CachedXPathAPI;
+import org.apache.xpath.objects.XObject;
+import org.springframework.core.io.Resource;
+import org.springframework.util.Assert;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+
+
+/**
+ * Sets properties on the message, loaded from an XML MPS file
+ * where the properties to set are located in a <property-set ..>
+ * inside the XML config file.
+ *
+ * There can be more than one propertySet to "load".
+ *
+ * The property values are derived from 3 types of config.
+ * The first config that can return a value as a String to
+ * set onto the message, will be the "value" that is set
+ * as the property.
+ *
+ * <static-value>
+ * As it's name suggests, the "value" of this element
+ * will be the value of the JBI property.
+ *
+ * This is helpful as a default value, or as a static value.
+ *
+ * <exisiting-property> and <existint-property name="..."/>
+ * This will obtain the value of an existing property (itself)
+ * or another property on the same message.
+ *
+ * This can be helpful when you want the to "ONLY" change the
+ * the value of the property if there is some "xpath" expression
+ * that could not be derived.
+ *
+ * name=".." form will copy the string value of the other JBI property
+ * onto this one, (duping it). This may be handy when you have a component
+ * which expects a new property, but you have it as a different name at the moment.
+ *
+ * <xpath-expression>
+ * As it's name suggests, will locate a value in the inMessage source
+ * and set the resulting XPath String as the value of the JBI property.
+ *
+ * So given the three types, they can be arranged in any order. and the first
+ * PropertyValue Type that returns a value, will become the "value" of the JBI property.
+ *
+ *
+ * <!--
+ * <mps>
+ * <property-set name="someSetNameForASetOfProperties">
+ * <property name="some.property.name1">
+ * <static-value><![CDATA[value for the property]]></static-value>
+ * </property>
+ * <property name="some.property.name2">
+ * <xpath-expression>
+ * <![CDATA[/someexpath/statement/to/be/applied/to/message/source]]>
+ * </xpath-expression>
+ * <existing-property name="someproperty"/>
+ * <static-value><![CDATA[a value in the raw]]></static-value>
+ * </property>
+ * <property name="prop.xpath.with.static.default">
+ * <xpath-expression>
+ * <![CDATA[/someexpath/statement]]>
+ * </xpath-expression>
+ * <static-value><![CDATA[some default if xpath does not resolve]]></static-value>
+ * </property>
+ * <property name="prop.xpath.or.keep.existing">
+ * <xpath-expression>
+ * <![CDATA[/someexpath/statement]]>
+ * </xpath-expression>
+ * <existing-property/>
+ * </property>
+ * <property name="new.prop.name">
+ * <existing-property name="other.property"/>
+ * </property>
+ * <property name="...">
+ * ...
+ * </property>
+ * </property-set>
+ * <property-set name="...">
+ * ...
+ * </property-set>
+ * </mps>
+ * -->
+ *
+ *
+ * @author rbuckland
+ *
+ */
+public class MessagePropertySetterXML extends TransformComponentSupport {
+
+ /**
+ * log4j logger
+ */
+ private static final Logger _log = Logger.getLogger(MessagePropertySetterXML.class);
+
+ /**
+ * The name of our JBI property we may have to interrogate to
+ * use as the "xpath" for the set of config.
+ */
+ public static final String MPS_PROP_NAME_PROPERTYSET = "org.apache.servicemix.components.mps.propertyset";
+
+ /**
+ * The XML Element name "property-set"
+ */
+ public static final String XML_ELEMENT_NAME = "property-set";
+
+ /**
+ * This is our XML file
+ */
+ private Resource xmlConfiguration = null;
+
+ /**
+ * Our XML config MPS.
+ */
+ private Document xmlMPSdom = null;
+
+ /**
+ * Holds the propertyValueResolver objects.
+ * if propertySet is not null, this holds one.
+ * if propertySet is null, this will hold
+ * one for each propertyvalue entry used during the time
+ * we are "active".
+ */
+ private Map propertSets = new HashMap();
+
+ /**
+ * If this is set, it is hardcoded as the fixed value.
+ *
+ * If this is null
+ * we will look for a propery MPS_PROP_NAME_PROPERTYSET
+ * as the "name" of the property set we will work with.
+ * So the message incoming, seeds the set of properties to load, just by
+ * the JBI property itself.
+ */
+ private String propertySet = null;
+
+
+ /**
+ * If the XPath for a propertySet is not null
+ * we will try and locate the propertySet to use by
+ * pulling a String from the inMessage content using this
+ * XPath.
+ */
+ private String xpathForPropertySet = null;
+
+ /**
+ * Here is the transform of the message
+ * We will locate the propertySetName to use
+ * and the load up our propertySet magic wand and apply all the properties
+ * we can to the outgoing NormalizedMessage
+ */
+ protected boolean transform(MessageExchange arg0, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
+ try {
+ copyPropertiesAndAttachments(arg0,in,out);
+ out.setContent(in.getContent());
+
+ String propertySetName = "";
+ if (xpathForPropertySet != null) {
+ try {
+ CachedXPathAPI xpathApi = new org.apache.xpath.CachedXPathAPI();
+ Document doc = (Document) new SourceTransformer().toDOMNode(in);
+ XObject propSetXO = xpathApi.eval(doc.getDocumentElement(),xpathForPropertySet);
+ propertySetName = propSetXO.str();
+ } catch (Exception e) {
+ throw new MessagingException("Problem getting the propertySet using XPath", e);
+ }
+ } else if (this.propertySet != null) {
+ propertySetName = this.propertySet;
+ } else if (in.getProperty(MPS_PROP_NAME_PROPERTYSET) != null) {
+ propertySetName = in.getProperty(MPS_PROP_NAME_PROPERTYSET).toString();
+ } else {
+ return false;
+ }
+ _log.info("Applying properties from property-set [" + propertySetName + "]");
+ getPropertySetByName(propertySetName).applyProperties(in,out);
+ return true;
+ } catch (JBIException e) {
+ throw new MessagingException("Problem setting properties",e);
+ } catch (PropertySetNotFoundException e) {
+ _log.warn(e.getLocalizedMessage());
+ return false;
+ }
+ }
+
+ /**
+ * Initialise by loading the mps.xml file up into an internal DOM.
+ *
+ * @throws JBIException
+ */
+ private void initConfig() throws JBIException {
+ Assert.notNull(this.xmlConfiguration);
+ DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
+ domFactory.setIgnoringElementContentWhitespace(true);
+ domFactory.setIgnoringComments(true);
+ domFactory.setCoalescing(true); // convert CDATA to test nodes
+ DocumentBuilder domBuilder;
+ try {
+ _log.info("Intialising MessagePropertySetterXML, loading settings from " + this.xmlConfiguration.getFile().getAbsolutePath());
+ domBuilder = domFactory.newDocumentBuilder();
+ xmlMPSdom = domBuilder.parse(this.xmlConfiguration.getInputStream());
+ } catch (ParserConfigurationException e) {
+ throw new JBIException("Problem parsing the XML config file for MPS",e);
+ } catch (SAXException e) {
+ throw new JBIException("Problem loading the XML config file for MPS",e);
+ } catch (IOException e) {
+ throw new JBIException("Problem loading the XML config file for MPS",e);
+ }
+
+ }
+
+ /**
+ * We are only interested in loading the XML onfig, nothing else
+ */
+ public void init(ComponentContext context) throws JBIException {
+ super.init(context);
+ initConfig();
+ }
+
+ /**
+ * Create a propertyset (loading up the Object config based on the XML config)
+ *
+ * @param propertySetName
+ * @return
+ * @throws JBIException
+ * @throws PropertySetNotFoundException
+ */
+ private PropertySet createPropertySet(String propertySetName) throws JBIException , PropertySetNotFoundException{
+ PropertySet ps;
+ CachedXPathAPI xpath = new CachedXPathAPI();
+ StringBuffer xpathSB = new StringBuffer("//")
+ .append(XML_ELEMENT_NAME)
+ .append("[@name='")
+ .append(propertySetName)
+ .append("']");
+ try {
+ Node propertySetNode = xpath.selectSingleNode(xmlMPSdom,xpathSB.toString());
+ if (propertySetNode == null) {
+ throw new PropertySetNotFoundException("Could not find a property-set for [" + propertySetName + "] in " + xmlConfiguration.getFilename());
+ }
+ ps = new PropertySet(propertySetName,(Element) propertySetNode);
+ this.propertSets.put(propertySetName, ps);
+ } catch (TransformerException e) {
+ throw new JBIException("Could not load the PropertySet for " + propertySet,e);
+ } catch (ConfigNotSupportedException e) {
+ throw new JBIException("Could not load the PropertySet for. XMLConfig is not good for " + propertySet,e);
+ }
+ return ps;
+
+ }
+
+ /**
+ * Get a property set from ou
+ * @param name
+ * @return
+ * @throws JBIException
+ */
+ private PropertySet getPropertySetByName(String name) throws JBIException, PropertySetNotFoundException {
+ // find a pre "created" one
+ if (this.propertSets.containsKey(name)) {
+ return (PropertySet)this.propertSets.get(name);
+ } else {
+ return this.createPropertySet(name);
+ }
+
+ }
+
+ /**
+ * @param propertySet The propertySet to set.
+ */
+ public void setPropertySet(String propertySet) {
+ this.propertySet = propertySet;
+ }
+
+ /**
+ * @param xmlConfiguration The xmlConfiguration to set.
+ */
+ public void setXmlConfiguration(Resource xmlConfiguration) {
+ this.xmlConfiguration = xmlConfiguration;
+ }
+
+ /**
+ * @param xpathForPropertySet xpath to apply to a message to derive the name of the property-set we want to load
+ */
+ public void setXpathForPropertySet(String xpathForPropertySet) {
+ this.xpathForPropertySet = xpathForPropertySet;
+ }
+
+}
Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertySet.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertySet.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertySet.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertySet.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.components.mps;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.NormalizedMessage;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+public class PropertySet {
+
+
+ public final static String XML_ELEMENT_NAME = "property-set";
+
+ public final static String XML_ATTR_NAME_NAME = "name";
+
+ /**
+ * Our name
+ */
+ private String propertySetName;
+
+ /**
+ * Our list of property value resolvers
+ */
+ private ArrayList pvrs = new ArrayList();
+
+ /**
+ * Create a propertySet with a particular name
+ * Element self is //property-set
+ *
+ * @param name
+ */
+ public PropertySet(String name, Element self) throws ConfigNotSupportedException{
+ this.propertySetName = name;
+ NodeList properties = self.getElementsByTagName(PropertyValueResolver.XML_ELEMENT_NAME);
+ for (int i=0; i < properties.getLength();i++) {
+ Element property = (Element)properties.item(i);
+ this.pvrs.add((new PropertyValueResolver(property.getAttribute(XML_ATTR_NAME_NAME),property)));
+ }
+ }
+
+ /**
+ * Apply all the property values for this in/out pair
+ *
+ * @param in
+ * @param out
+ */
+ public void applyProperties(NormalizedMessage in, NormalizedMessage out) throws JBIException {
+
+ // for every property, set the value on the message
+ for (Iterator iter = pvrs.iterator(); iter.hasNext();) {
+ ((PropertyValueResolver)iter.next()).setProperty(in,out);
+ }
+ }
+
+ public String getName() {
+ return propertySetName;
+ }
+}
Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertySetNotFoundException.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertySetNotFoundException.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertySetNotFoundException.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertySetNotFoundException.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.components.mps;
+
+public class PropertySetNotFoundException extends Exception {
+
+ public PropertySetNotFoundException() {
+ super();
+ }
+
+ public PropertySetNotFoundException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public PropertySetNotFoundException(String message) {
+ super(message);
+ }
+
+ public PropertySetNotFoundException(Throwable cause) {
+ super(cause);
+ }
+
+
+}
Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertyValue.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertyValue.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertyValue.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertyValue.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.components.mps;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.NormalizedMessage;
+
+public interface PropertyValue {
+
+ public String getPropertyValue(NormalizedMessage msg) throws JBIException;
+
+}
Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertyValueResolver.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertyValueResolver.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertyValueResolver.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/PropertyValueResolver.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.components.mps;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.NormalizedMessage;
+
+import org.apache.log4j.Logger;
+import org.apache.servicemix.jbi.util.DOMUtil;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+/**
+ * Class to hold the list of propertyValues
+ *
+ * @author rbuckland
+ *
+ */
+public class PropertyValueResolver {
+
+ private static final Logger _log = Logger.getLogger(PropertyValueResolver.class);
+
+
+ public static final String XML_ELEMENT_NAME = "property";
+ /**
+ * The name of the JBI property that this class will set.
+ */
+ private String name;
+
+ /**
+ * if the set contains a static-string propertyvalue
+ * at the top of the config, we don't need to do anything
+ * so the value is stored "here". Null if the list has to be evald
+ */
+ private String staticValue = null;
+
+ /**
+ * Our list of PropertySetTypes
+ */
+ private List propertySetTypes = new ArrayList();
+
+ /**
+ * Construct this PVR, the Element
+ * is the .. //property-set/property element
+ * @param propertyName the name of the property that will be set
+ * @param self Element
+ * @throws JBIException
+ */
+ public PropertyValueResolver(String propertyName, Element self) throws ConfigNotSupportedException {
+ this.name = propertyName;
+ addPropertySetTypes(self);
+ }
+ /**
+ * Set the property (this.name) on the out message
+ * based on any properties on the message
+ *
+ * @param msg
+ */
+ public void setProperty(NormalizedMessage in, NormalizedMessage out) throws JBIException {
+
+ if (this.staticValue !=null) {
+ out.setProperty(name,staticValue);
+ } else {
+ String value = resolveValue(in);
+ if (value != null) {
+ out.setProperty(name,value);
+ } else {
+ _log.warn("Property " + name + " was not set as the value was unresolved");
+ }
+ }
+ }
+
+ /**
+ * Get the value out of the in, and put it in the out.
+ *
+ * @param in
+ * @param out
+ * @return
+ */
+ private String resolveValue(NormalizedMessage message) throws JBIException {
+ // go through the list
+ // if a value is found on the one, return it, until the list is exhausted
+ String propValue = null;
+ _log.debug("propvrsize=" + propertySetTypes.size());
+ for (int i=0; i<propertySetTypes.size();i++) {
+ PropertyValue pv = (PropertyValue)propertySetTypes.get(i);
+ propValue = pv.getPropertyValue(message);
+ _log.debug("value from" + pv.getClass() + " = " +propValue);
+ if (propValue != null && !"".equals(propValue)) {
+ break;
+ }
+ if (_log.isDebugEnabled()) {
+ _log.debug(this.name + ": " + pv.getClass() + " was empty");
+ }
+ }
+ return propValue;
+ }
+
+ /**
+ * Given the XML below, we will locate the different propertyValueTypes
+ * and set them on us.
+ *
+ * <property name="some.property.name">
+ * <existing-property/>
+ * <existing-property name="someproperty"/>
+ * <xpath-expression>
+ * <![CDATA[/someexpath/statement]]>
+ * </xpath-expression>
+ * <static-value><![CDATA[a value in the raw]]></static-value>
+ * </property>
+ *
+ * @param propertyNode
+ */
+ private void addPropertySetTypes(Element propertyElement) throws ConfigNotSupportedException {
+
+ NodeList propertyValueNodes = propertyElement.getChildNodes();
+ // iterate of all the propertyValue nodes ..
+ // (same as equiv to select='//property[@name='x']/*'
+ for (int i=0; i<propertyValueNodes.getLength(); i++) {
+ if (propertyValueNodes.item(i).getNodeType() != Element.ELEMENT_NODE) {
+ continue;
+ }
+ Element pvElem = (Element)propertyValueNodes.item(i);
+ PropertyValue pv;
+ if (pvElem.getNodeName().equals(StaticStringPropertyValue.XML_ELEMENT_NAME)) {
+ if (this.propertySetTypes.size() == 0) {
+ this.staticValue = DOMUtil.getElementText(pvElem);
+ }
+ pv = new StaticStringPropertyValue(DOMUtil.getElementText(pvElem));
+ } else if (pvElem.getNodeName().equals(XPathContentMessagePropertyValue.XML_ELEMENT_NAME)) {
+ String xpath = DOMUtil.getElementText(pvElem);
+ pv = new XPathContentMessagePropertyValue(xpath);
+ if (_log.isDebugEnabled()) {
+ _log.debug("Created an XPath VR :" + xpath);
+ }
+ } else if (pvElem.getNodeName().equals(ExistingPropertyCopier.XML_ELEMENT_NAME)) {
+ // default to this parents name (so it acts like a property copy)
+
+ String propertyName = this.name;
+ if (pvElem.getAttribute("name") != null && !"".equals(pvElem.getAttribute("name"))) {
+ // if there was a <existing-property name="somename"/>
+ // then use that name as the source JBI property
+ // in this mode it acts like a cp src dest and not a dupe.
+ propertyName = pvElem.getAttribute("name");
+ }
+ pv = new ExistingPropertyCopier(propertyName);
+ } else {
+ throw new ConfigNotSupportedException("Property value type " + pvElem.getNodeName() + " is not supported for the MessagePropertySetter");
+ }
+ this.propertySetTypes.add(pv);
+
+ }
+ }
+}
Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/StaticStringPropertyValue.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/StaticStringPropertyValue.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/StaticStringPropertyValue.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/StaticStringPropertyValue.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.components.mps;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.NormalizedMessage;
+
+
+public class StaticStringPropertyValue implements PropertyValue {
+
+ /**
+ * Our element name
+ */
+ public final static String XML_ELEMENT_NAME = "static-value";
+
+ /**
+ * This is the static value we represent
+ */
+ private String staticValue;
+
+ /**
+ * Ctor
+ * @param staticValue
+ */
+ public StaticStringPropertyValue(String staticValue) {
+ this.staticValue = staticValue;
+ }
+
+ /**
+ *
+ */
+ public String getPropertyValue(NormalizedMessage msg) throws JBIException {
+ return staticValue;
+ }
+
+}
Added: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/XPathContentMessagePropertyValue.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/XPathContentMessagePropertyValue.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/XPathContentMessagePropertyValue.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/mps/XPathContentMessagePropertyValue.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.components.mps;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.NormalizedMessage;
+
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.w3c.dom.Document;
+
+import com.sun.org.apache.xpath.internal.CachedXPathAPI;
+import com.sun.org.apache.xpath.internal.objects.XObject;
+
+/**
+ * Get the Value of a property from the message given the XPath Statement.
+ * @author rbuckland
+ *
+ */
+public class XPathContentMessagePropertyValue implements PropertyValue {
+
+ public final static String XML_ELEMENT_NAME = "xpath-expression";
+
+ private String xpath;
+
+ public XPathContentMessagePropertyValue(String xpath) {
+ this.xpath = xpath;
+ }
+
+ /**
+ * Get a value give the XPath statement preset in our Ctor.
+ * Return null if the value was empty or not there
+ */
+ public String getPropertyValue(NormalizedMessage msg) throws JBIException {
+ String resultValue = null;
+ if (msg.getContent() != null) {
+ CachedXPathAPI xpathApi = new CachedXPathAPI();
+ try {
+ Document doc = (Document) new SourceTransformer().toDOMNode(msg);
+ XObject result = xpathApi.eval(doc,xpath);
+
+ resultValue = result.toString();
+ if ("".equals(resultValue)) {
+ resultValue = null;
+ }
+ } catch (Exception e) {
+ throw new JBIException("Could not get value from message via xpath " + xpath, e);
+ }
+ }
+ return resultValue;
+
+ }
+
+}
Added: incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/MPSSettingTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/MPSSettingTest.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/MPSSettingTest.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/MPSSettingTest.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.components.mps;
+
+import javax.jbi.messaging.InOut;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.namespace.QName;
+
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.tck.TestSupport;
+import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+
+/**
+ * @author Rmaon Buckland
+ * @version $Revision: $
+ */
+public class MPSSettingTest extends TestSupport {
+
+
+ /**
+ * load the property set from the spring injects string (fixed set)
+ * @throws Exception
+ */
+ public void testStaticStringPropertySet() throws Exception {
+ QName service = new QName("http://servicemix.org/cheese/", "mpsFixed");
+
+ InOut exchange = client.createInOutExchange();
+ exchange.setService(service);
+
+ NormalizedMessage message = exchange.getInMessage();
+ message.setContent(new StringSource(createMessageXmlText(777888)));
+ message.setProperty("prop.xpath.or.keep.existing","someValue");
+ message.setProperty("other.set.property","thatOtherValue");
+ client.sendSync(exchange);
+
+ NormalizedMessage outMessage = exchange.getOutMessage();
+
+ assertTrue(outMessage.getProperty("property.1").equals("foobarAndCheese"));
+ assertTrue(outMessage.getProperty("property.2").equals("777888"));
+ assertTrue(outMessage.getProperty("prop.xpath.or.keep.existing").equals("someValue"));
+ // this property is configured, but there is no "value to resolve it to"
+ assertTrue(outMessage.getProperty("new.prop.name") == null);
+ assertTrue(outMessage.getProperty("property.3").equals("thatOtherValue"));
+
+ }
+
+
+ /**
+ * Load the property set using an xpath applied to the in message
+ * @throws Exception
+ */
+ public void testtestXpathLoadingPropertySet() throws Exception {
+ QName service = new QName("http://servicemix.org/cheese/", "mpsXpath");
+
+ InOut exchange = client.createInOutExchange();
+ exchange.setService(service);
+
+ NormalizedMessage message = exchange.getInMessage();
+ message.setContent(new StringSource(createMessageXmlText(400)));
+ client.sendSync(exchange);
+
+ NormalizedMessage outMessage = exchange.getOutMessage();
+
+ assertTrue(outMessage.getProperty("my-superdooper.property").equals("wishAusMadeItTotheFinals"));
+
+ }
+
+ /**
+ * load the property set from a JBI property value
+ * @throws Exception
+ */
+ public void testJBIPropertyLoadingPropertySet() throws Exception {
+ QName service = new QName("http://servicemix.org/cheese/", "mpsJBIPropsPS");
+
+ InOut exchange = client.createInOutExchange();
+ exchange.setService(service);
+
+ NormalizedMessage message = exchange.getInMessage();
+ message.setProperty(MessagePropertySetterXML.MPS_PROP_NAME_PROPERTYSET,"hello");
+ message.setContent(new StringSource(createMessageXmlText(54)));
+ client.sendSync(exchange);
+
+ NormalizedMessage outMessage = exchange.getOutMessage();
+
+ assertTrue(outMessage.getProperty("my-superdooper.property").equals("wishAusMadeItTotheFinals"));
+
+ }
+
+
+ protected AbstractXmlApplicationContext createBeanFactory() {
+ return new ClassPathXmlApplicationContext("org/apache/servicemix/components/mps/servicemix-mps-test.xml");
+ }
+}
+
+
Added: incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertySetTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertySetTest.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertySetTest.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertySetTest.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,327 @@
+package org.apache.servicemix.components.mps;
+
+import java.io.StringReader;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.dom.DOMSource;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Logger;
+import org.apache.servicemix.components.mps.ConfigNotSupportedException;
+import org.apache.servicemix.components.mps.PropertySet;
+import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+public class PropertySetTest extends TestCase {
+
+ private final static Logger _log = Logger.getLogger(PropertyValueResolverTest.class);
+ private final static String TEST_STRING = "PROP_TEST_STRING";
+ private final static String PROPNAME = "property1";
+ private final static String SAMPLE_MSG_XML = "<sample><get x='911'>me</get></sample>";
+ private final static String SAMPLE_MSG_XML2 = "<this><is><some attr='1234'>xml123</some></is></this>";
+
+ private final static String XML_EXISTING_PROP = "<existing-property/>";
+ private final static String XML_EXISTING_PROP_OTHER_PROP = "<existing-property name=\"newname\"/>";
+ private final static String XML_STATICVAL = "<static-value><![CDATA[a value in the raw]]></static-value>";
+ private final static String XML_XPATH = "<xpath-expression><![CDATA[//get[@x='911']]]></xpath-expression>";
+
+ private Document makeDocument(String xml) {
+ DocumentBuilder db;
+ try {
+ db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ return db.parse(new InputSource(new StringReader(xml)));
+ } catch (Exception e) {
+ fail(e.getLocalizedMessage());
+ return null;
+ }
+ }
+
+ /**
+ * helper method to return a new JBI NormalizedMessage
+ * when we need one
+ * @return
+ */
+ private NormalizedMessage makeTestMessage(String xml) {
+ NormalizedMessage message = new NormalizedMessageImpl();
+ message.setProperty(PROPNAME,TEST_STRING);
+ if (xml == null) {
+ xml = SAMPLE_MSG_XML2;
+ }
+ try {
+ DocumentBuilder db;
+ db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document dom = db.parse(new InputSource(new StringReader(xml)));
+ message.setContent(new DOMSource(dom));
+ } catch (Exception e) {
+ fail(e.getLocalizedMessage());
+ }
+ return message;
+ }
+
+ /**
+ * Test
+ * <property-set name="somename">
+ * <property name="newProperty">
+ * <existing-property name="property1"/>
+ * </property>
+ * </property-set>
+ *
+ */
+ public void testSimpleStaticValue() {
+ String xmlConfig = new StringBuffer("<property-set name='somename'>")
+ .append("<property name='testSimpleStaticValue.property'>")
+ .append("<existing-property name='property1'/>")
+ .append("</property>")
+ .append("</property-set>").toString();
+ Document propertySet = makeDocument(xmlConfig);
+ try {
+ PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
+ NormalizedMessage inMessage = makeTestMessage(null);
+ NormalizedMessage outMessage = makeTestMessage(null);
+ ps.applyProperties(inMessage,outMessage);
+
+ assertTrue(TEST_STRING.equals(outMessage.getProperty("testSimpleStaticValue.property")));
+ } catch (ConfigNotSupportedException e) {
+ fail(e.getLocalizedMessage());
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());// TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Test
+ * <property-set name="somename">
+ * <property name="newProperty">
+ * <existing-property name="property1"/>
+ * </property>
+ * </property-set>
+ *
+ */
+ public void testSimpleExistingPropInvalidXPath() {
+ String xmlConfig = new StringBuffer("<property-set name='somename'>")
+ .append("<property name='testSimpleExistingPropInvalidXPath.property'>")
+ .append("<xpath-expression><![CDATA[/someexpath/statement]]></xpath-expression>")
+ .append("<existing-property name='property1'/>")
+ .append("</property>")
+ .append("</property-set>").toString();
+ Document propertySet = makeDocument(xmlConfig);
+ try {
+ PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
+ NormalizedMessage inMessage = makeTestMessage(null);
+ NormalizedMessage outMessage = makeTestMessage(null);
+ ps.applyProperties(inMessage,outMessage);
+
+ assertTrue(TEST_STRING.equals(outMessage.getProperty("testSimpleExistingPropInvalidXPath.property")));
+ } catch (ConfigNotSupportedException e) {
+ fail(e.getLocalizedMessage());
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());// TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Test
+ * <property-set name="somename">
+ * <property name="newProperty">
+ * <existing-property name="property1"/>
+ * </property>
+ * </property-set>
+ *
+ */
+ public void testStaticValueDefaultWithBadXpath() {
+ String xmlConfig = new StringBuffer("<property-set name='somename'>")
+ .append("<property name='testStaticValueDefaultWithBadXpath.property'>")
+ .append("<xpath-expression><![CDATA[/someexpath/statement]]></xpath-expression>")
+ .append("<static-value>myvalue</static-value>")
+ .append("</property>")
+ .append("</property-set>").toString();
+ Document propertySet = makeDocument(xmlConfig);
+ try {
+ PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
+ NormalizedMessage inMessage = makeTestMessage(null);
+ NormalizedMessage outMessage = makeTestMessage(null);
+ ps.applyProperties(inMessage,outMessage);
+
+ assertTrue("myvalue".equals(outMessage.getProperty("testStaticValueDefaultWithBadXpath.property")));
+ } catch (ConfigNotSupportedException e) {
+ fail(e.getLocalizedMessage());
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());// TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ /**
+ * Test
+ * <property-set name="somename">
+ * <property name="newProperty">
+ * <existing-property name="property1"/>
+ * </property>
+ * </property-set>
+ *
+ */
+ public void testSimpleValidXPath() {
+ String xmlConfig = new StringBuffer("<property-set name='somename'>")
+ .append("<property name='testSimpleValidXPath.property'>")
+ .append("<xpath-expression><![CDATA[/this//some/@attr]]></xpath-expression>")
+ .append("<existing-property name='property1'/>")
+ .append("</property>")
+ .append("</property-set>").toString();
+ Document propertySet = makeDocument(xmlConfig);
+ try {
+ PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
+ NormalizedMessage inMessage = makeTestMessage("<this><is><some attr='1234'>xml123</some></is></this>");
+ NormalizedMessage outMessage = makeTestMessage(null);
+ ps.applyProperties(inMessage,outMessage);
+
+ assertTrue(outMessage.getProperty("testSimpleValidXPath.property").toString().equals("1234"));
+ } catch (ConfigNotSupportedException e) {
+ e.printStackTrace();
+ fail(e.getLocalizedMessage());
+ } catch (JBIException e) {
+ e.printStackTrace();
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+
+ /**
+ * Test
+ * <property-set name="somename">
+ * <property name="newProperty">
+ * <existing-property name="property1"/>
+ * </property>
+ * </property-set>
+ *
+ */
+ public void testInvalidXPathSingle() {
+ String xmlConfig = new StringBuffer("<property-set name='somename'>")
+ .append("<property name='testInvalidXPathSingle.property'>")
+ .append("<xpath-expression><![CDATA[/NoNode]]></xpath-expression>")
+ .append("</property>")
+ .append("</property-set>").toString();
+ Document propertySet = makeDocument(xmlConfig);
+ try {
+ PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
+ NormalizedMessage inMessage = makeTestMessage("<this><is><some attr='1234'>xml123</some></is></this>");
+ NormalizedMessage outMessage = makeTestMessage(null);
+ ps.applyProperties(inMessage,outMessage);
+
+ assertTrue("Node is not null",outMessage.getProperty("testInvalidXPathSingle.property") == null);
+ } catch (ConfigNotSupportedException e) {
+ e.printStackTrace();
+ fail(e.getLocalizedMessage());
+ } catch (JBIException e) {
+ e.printStackTrace();
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+ /**
+ * Test
+ * <property-set name="somename">
+ * <property name="newProperty">
+ * <existing-property name="property1"/>
+ * </property>
+ * </property-set>
+ *
+ */
+ public void testXPATH2MessagesOneHasOneHasnt() {
+ String xmlConfig = new StringBuffer("<property-set name='somename'>")
+ .append("<property name='testXPATH2MessagesOneHasOneHasnt.property'>")
+ .append("<xpath-expression><![CDATA[/this//some/@attr]]></xpath-expression>")
+ .append("<existing-property name='property1'/>")
+ .append("</property>")
+ .append("</property-set>").toString();
+ Document propertySet = makeDocument(xmlConfig);
+ try {
+ PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
+ NormalizedMessage inMessage = makeTestMessage("<this><is><some attr='1234'>xml123</some></is></this>");
+ // inMessage will have a property1=TEST_STRING
+
+ NormalizedMessage inMessage2 = makeTestMessage("<this><is><some>xml123</some></is></this>");
+ // inMessage2 will have a property1=TEST_STRING
+ NormalizedMessage outMessage = makeTestMessage(null);
+
+ ps.applyProperties(inMessage,outMessage);
+ assertTrue(outMessage.getProperty("testXPATH2MessagesOneHasOneHasnt.property").toString().equals("1234"));
+
+ ps.applyProperties(inMessage2,outMessage);
+ assertTrue(outMessage.getProperty("testXPATH2MessagesOneHasOneHasnt.property").toString().equals(TEST_STRING));
+
+ } catch (ConfigNotSupportedException e) {
+ e.printStackTrace();
+ fail(e.getLocalizedMessage());
+ } catch (JBIException e) {
+ e.printStackTrace();
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+ /**
+ * Test
+ * <property-set name="somename">
+ * <property name="newProperty">
+ * <existing-property name="property1"/>
+ * </property>
+ * </property-set>
+ *
+ */
+ public void testManyProperties() {
+ String xmlConfig = new StringBuffer("<property-set name='somename'>")
+ .append("<property name='newProperty1'>")
+ .append("<xpath-expression><![CDATA[/this//some/@attr]]></xpath-expression>")
+ .append("<existing-property name='property1'/>")
+ .append("</property>")
+ .append("<property name='newProperty2'>")
+ .append("<xpath-expression><![CDATA[/this//some/@attrNoExisting]]></xpath-expression>")
+ .append("<static-value><![CDATA[Some String I expect to be Set]]></static-value>")
+ .append("</property>")
+ .append("<property name='newProperty3'>")
+ .append("<existing-property name='missingProperty'/>")
+ .append("<existing-property name='missingProperty2'/>")
+ .append("<static-value><![CDATA[Some String I expect to be Set]]></static-value>")
+ .append("</property>")
+ .append("<property name='newProperty4'>")
+ .append("<xpath-expression><![CDATA[/this//some/nothere/@attr]]></xpath-expression>")
+ .append("<existing-property name='property1WhichIsNoteThere'/>")
+ .append("<existing-property name='testProperty'/>")
+ .append("</property>")
+ .append("<property name='newProperty5'>")
+ .append("<static-value><![CDATA[tagged]]></static-value>")
+ .append("<static-value><![CDATA[taggedNotSet]]></static-value>")
+ .append("</property>")
+ .append("<property name='newProperty6'>")
+ .append("<xpath-expression><![CDATA[/this//some/@attrNoExisting]]></xpath-expression>")
+ .append("</property>")
+ .append("</property-set>").toString();
+ Document propertySet = makeDocument(xmlConfig);
+ try {
+ PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
+ NormalizedMessage inMessage = makeTestMessage("<this><is><some attr='1234'>xml123</some></is></this>");
+ inMessage.setProperty("testProperty","value123");
+ NormalizedMessage outMessage = makeTestMessage(null);
+ ps.applyProperties(inMessage,outMessage);
+
+ assertTrue("newProperty1 was not set to expected value",outMessage.getProperty("newProperty1").toString().equals("1234"));
+ assertTrue("newProperty2 was not set to expected value",outMessage.getProperty("newProperty2").toString().equals("Some String I expect to be Set"));
+ assertTrue("newProperty3 was not set to expected value",outMessage.getProperty("newProperty3").toString().equals("Some String I expect to be Set"));
+ assertTrue("newProperty4 was not set to expected value",outMessage.getProperty("newProperty4").toString().equals("value123"));
+ assertTrue("newProperty5 was not set to expected value",outMessage.getProperty("newProperty5").toString().equals("tagged"));
+ assertTrue("newProperty5 was not set to expected value",outMessage.getProperty("newProperty6") == null);
+ } catch (ConfigNotSupportedException e) {
+ e.printStackTrace();
+ fail(e.getLocalizedMessage());
+ } catch (JBIException e) {
+ e.printStackTrace();
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+}
Added: incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertyValueResolverTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertyValueResolverTest.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertyValueResolverTest.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertyValueResolverTest.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,163 @@
+package org.apache.servicemix.components.mps;
+
+import java.io.StringReader;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.dom.DOMSource;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Logger;
+import org.apache.servicemix.components.mps.ConfigNotSupportedException;
+import org.apache.servicemix.components.mps.PropertyValueResolver;
+import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+/**
+ * Test cases for the PropertyValueResolver class
+ * @author rbuckland
+ *
+ */
+public class PropertyValueResolverTest extends TestCase {
+
+
+ private final static Logger _log = Logger.getLogger(PropertyValueResolverTest.class);
+ private final static String TEST_STRING = "PROP_TEST_STRING";
+ private final static String PROPNAME = "property1";
+ private final static String SAMPLE_MSG_XML = "<sample><get x='911'>me</get></sample>";
+
+ private final static String XML_EXISTING_PROP = "<existing-property/>";
+ private final static String XML_EXISTING_PROP_OTHER_PROP = "<existing-property name=\"newname\"/>";
+ private final static String XML_STATICVAL = "<static-value><![CDATA[a value in the raw]]></static-value>";
+ private final static String XML_XPATH = "<xpath-expression><![CDATA[//get[@x='911']]]></xpath-expression>";
+
+ private Document makeDocument(String xml) {
+ DocumentBuilder db;
+ try {
+ db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ return db.parse(new InputSource(new StringReader(xml)));
+ } catch (Exception e) {
+ fail(e.getLocalizedMessage());
+ return null;
+ }
+ }
+
+ /**
+ * helper method to return a new JBI NormalizedMessage
+ * when we need one
+ * @return
+ */
+ private NormalizedMessage makeTestMessage(String xml) {
+ NormalizedMessage message = new NormalizedMessageImpl();
+ message.setProperty(PROPNAME,TEST_STRING);
+ if (xml == null) {
+ xml = "<this><is><some attr='1234'>xml123</some></is></this>";
+ }
+ try {
+ DocumentBuilder db;
+ db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document dom = db.parse(new InputSource(new StringReader(xml)));
+ message.setContent(new DOMSource(dom));
+ } catch (Exception e) {
+ fail(e.getLocalizedMessage());
+ }
+ return message;
+ }
+
+ /**
+ * Test that a static string, does return a static string
+ *
+ */
+ public void testStaticStringAsFirst() {
+ String propertyNode =
+ (new StringBuffer("<").append(PropertyValueResolver.XML_ELEMENT_NAME)
+ .append(" name='newproperty'>")
+ .append(XML_STATICVAL)
+ .append("</")
+ .append(PropertyValueResolver.XML_ELEMENT_NAME)
+ .append(">")).toString();
+
+ NormalizedMessage in = makeTestMessage(SAMPLE_MSG_XML);
+ NormalizedMessage out = makeTestMessage(SAMPLE_MSG_XML);
+ Document doc = makeDocument(propertyNode);
+ try {
+ PropertyValueResolver pvr = new PropertyValueResolver("newproperty",doc.getDocumentElement());
+ pvr.setProperty(in,out);
+ _log.debug("prop = " + out.getProperty("newproperty"));
+ assertTrue(out.getProperty("newproperty").toString().equals("a value in the raw"));
+
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());
+ } catch (ConfigNotSupportedException e) {
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+ /**
+ * Test the xpath PVR
+ *
+ */
+ public void testXPath() {
+ String propertyNode =
+ (new StringBuffer("<").append(PropertyValueResolver.XML_ELEMENT_NAME)
+ .append(" name='newproperty'>")
+ .append(XML_XPATH)
+ .append(XML_STATICVAL)
+ .append("</")
+ .append(PropertyValueResolver.XML_ELEMENT_NAME)
+ .append(">")).toString();
+
+ NormalizedMessage in = makeTestMessage(PropertyValueResolverTest.SAMPLE_MSG_XML);
+ NormalizedMessage out = makeTestMessage(SAMPLE_MSG_XML);
+ Document doc = makeDocument(propertyNode);
+ try {
+ PropertyValueResolver pvr = new PropertyValueResolver("newproperty",doc.getDocumentElement());
+ pvr.setProperty(in,out);
+ _log.debug("xpath:@newproperty = " + out.getProperty("newproperty"));
+ assertTrue(out.getProperty("newproperty") != null);
+ assertTrue(out.getProperty("newproperty").toString().equals("me"));
+
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());
+ } catch (ConfigNotSupportedException e) {
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+ /**
+ * Test that a existing copier thingo
+ *
+ */
+ public void testExistCopier() {
+ String propertyNode =
+ (new StringBuffer("<").append(PropertyValueResolver.XML_ELEMENT_NAME)
+ .append(" name='prop.xyz'>")
+ .append(XML_EXISTING_PROP)
+ .append(XML_STATICVAL)
+ .append("</")
+ .append(PropertyValueResolver.XML_ELEMENT_NAME)
+ .append(">")).toString();
+
+ NormalizedMessage in = makeTestMessage(PropertyValueResolverTest.SAMPLE_MSG_XML);
+ in.setProperty("prop.xyz","ahahaha");
+ NormalizedMessage out = makeTestMessage(SAMPLE_MSG_XML);
+ Document doc = makeDocument(propertyNode);
+ try {
+ PropertyValueResolver pvr = new PropertyValueResolver("prop.xyz",doc.getDocumentElement());
+ pvr.setProperty(in,out);
+ _log.debug("prop = " + out.getProperty("prop.xyz"));
+ assertTrue(out.getProperty("prop.xyz") != null);
+ assertTrue(out.getProperty("prop.xyz").toString().equals("ahahaha"));
+
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());
+ } catch (ConfigNotSupportedException e) {
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+}
Added: incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertyValueTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertyValueTest.java?rev=423082&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertyValueTest.java (added)
+++ incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/mps/PropertyValueTest.java Tue Jul 18 06:31:27 2006
@@ -0,0 +1,111 @@
+package org.apache.servicemix.components.mps;
+
+import java.io.StringReader;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.dom.DOMSource;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+/**
+ * Test cases for the PeoprtyValue type Objects
+ * @author rbuckland
+ *
+ */
+public class PropertyValueTest extends TestCase {
+
+ private final static String TEST_STRING = "PROP_TEST_STRING";
+ private final static String PROPNAME = "property1";
+ /**
+ * helper method to return a new JBI NormalizedMessage
+ * when we need one
+ * @return
+ */
+ private NormalizedMessage getTestMessage() {
+ NormalizedMessage message = new NormalizedMessageImpl();
+ message.setProperty(PROPNAME,TEST_STRING);
+ String xml = "<this><is><some attr='1234'>xml123</some></is></this>";
+ try {
+ DocumentBuilder db;
+ db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document dom = db.parse(new InputSource(new StringReader(xml)));
+ message.setContent(new DOMSource(dom));
+ } catch (Exception e) {
+ fail(e.getLocalizedMessage());
+ }
+ return message;
+ }
+
+ /**
+ * Test that a static string, does return a static string
+ *
+ */
+ public void testStaticString() {
+ PropertyValue pv = new StaticStringPropertyValue("someValueX");
+ try {
+ assertTrue(pv.getPropertyValue(getTestMessage()).equals("someValueX"));
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+ /**
+ * Test that a existing property copier works
+ *
+ */
+ public void testExistingCopier() {
+ PropertyValue pv = new ExistingPropertyCopier(PROPNAME);
+ try {
+ assertTrue(pv.getPropertyValue(getTestMessage()).equals(TEST_STRING));
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+ /**
+ * Test that a existing property copier works
+ *
+ */
+ public void testXPathElementPropertyValue() {
+ PropertyValue pv = new XPathContentMessagePropertyValue("/this/is/some");
+ try {
+ assertTrue(pv.getPropertyValue(getTestMessage()).equals("xml123"));
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+ /**
+ * Test that a existing property copier works
+ *
+ */
+ public void testXPathAttrPropertyValue() {
+ PropertyValue pv = new XPathContentMessagePropertyValue("/this/is/some/@attr");
+ try {
+ assertTrue(pv.getPropertyValue(getTestMessage()).equals("1234"));
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+ /**
+ * Test that a existing property copier works
+ *
+ */
+ public void testXPathConcatPropertyValue() {
+ PropertyValue pv = new XPathContentMessagePropertyValue("concat('x','y')");
+ try {
+ assertTrue(pv.getPropertyValue(getTestMessage()).equals("xy"));
+ } catch (JBIException e) {
+ fail(e.getLocalizedMessage());
+ }
+ }
+
+}