KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > mps > PropertyValueResolver


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.mps;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.jbi.JBIException;
23 import javax.jbi.messaging.NormalizedMessage;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.servicemix.jbi.util.DOMUtil;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.NodeList JavaDoc;
30
31 /**
32  * Class to hold the list of propertyValues
33  *
34  * @author rbuckland
35  *
36  */

37 public class PropertyValueResolver {
38
39     private final transient Log logger = LogFactory.getLog(getClass());
40
41     public static final String JavaDoc XML_ELEMENT_NAME = "property";
42
43     /**
44      * The name of the JBI property that this class will set.
45      */

46     private String JavaDoc name;
47
48     /**
49      * if the set contains a static-string propertyvalue at the top of the
50      * config, we don't need to do anything so the value is stored "here". Null
51      * if the list has to be evald
52      */

53     private String JavaDoc staticValue = null;
54
55     /**
56      * Our list of PropertySetTypes
57      */

58     private List JavaDoc propertySetTypes = new ArrayList JavaDoc();
59
60     /**
61      * Construct this PVR, the Element is the .. //property-set/property element
62      *
63      * @param propertyName
64      * the name of the property that will be set
65      * @param self
66      * Element
67      * @throws JBIException
68      */

69     public PropertyValueResolver(String JavaDoc propertyName, Element JavaDoc self)
70             throws ConfigNotSupportedException {
71         this.name = propertyName;
72         addPropertySetTypes(self);
73     }
74
75     /**
76      * Set the property (this.name) on the out message based on any properties
77      * on the message
78      *
79      * @param msg
80      */

81     public void setProperty(NormalizedMessage in, NormalizedMessage out)
82             throws JBIException {
83
84         if (this.staticValue != null) {
85             out.setProperty(name, staticValue);
86         } else {
87             String JavaDoc value = resolveValue(in);
88             if (value != null) {
89                 out.setProperty(name, value);
90             } else {
91                 logger.warn("Property " + name
92                         + " was not set as the value was unresolved");
93             }
94         }
95     }
96
97     /**
98      * Get the value out of the in, and put it in the out.
99      *
100      * @param in
101      * @param out
102      * @return
103      */

104     private String JavaDoc resolveValue(NormalizedMessage message) throws JBIException {
105         // go through the list
106
// if a value is found on the one, return it, until the list is
107
// exhausted
108
String JavaDoc propValue = null;
109         logger.debug("propvrsize=" + propertySetTypes.size());
110         for (int i = 0; i < propertySetTypes.size(); i++) {
111             PropertyValue pv = (PropertyValue) propertySetTypes.get(i);
112             propValue = pv.getPropertyValue(message);
113             logger.debug("value from" + pv.getClass() + " = " + propValue);
114             if (propValue != null && !"".equals(propValue)) {
115                 break;
116             }
117             if (logger.isDebugEnabled()) {
118                 logger.debug(this.name + ": " + pv.getClass() + " was empty");
119             }
120         }
121         return propValue;
122     }
123
124     /**
125      * Given the XML below, we will locate the different propertyValueTypes and
126      * set them on us.
127      *
128      * <property name="some.property.name"> <existing-property/>
129      * <existing-property name="someproperty"/> <xpath-expression>
130      * <![CDATA[/someexpath/statement]]> </xpath-expression> <static-value><![CDATA[a
131      * value in the raw]]></static-value> </property>
132      *
133      * @param propertyNode
134      */

135     private void addPropertySetTypes(Element JavaDoc propertyElement)
136             throws ConfigNotSupportedException {
137
138         NodeList JavaDoc propertyValueNodes = propertyElement.getChildNodes();
139         // iterate of all the propertyValue nodes ..
140
// (same as equiv to select='//property[@name='x']/*'
141
for (int i = 0; i < propertyValueNodes.getLength(); i++) {
142             if (propertyValueNodes.item(i).getNodeType() != Element.ELEMENT_NODE) {
143                 continue;
144             }
145             Element JavaDoc pvElem = (Element JavaDoc) propertyValueNodes.item(i);
146             PropertyValue pv;
147             if (pvElem.getNodeName().equals(
148                     StaticStringPropertyValue.XML_ELEMENT_NAME)) {
149                 if (this.propertySetTypes.size() == 0) {
150                     this.staticValue = DOMUtil.getElementText(pvElem);
151                 }
152                 pv = new StaticStringPropertyValue(DOMUtil
153                         .getElementText(pvElem));
154             } else if (pvElem.getNodeName().equals(
155                     XPathContentMessagePropertyValue.XML_ELEMENT_NAME)) {
156                 String JavaDoc xpath = DOMUtil.getElementText(pvElem);
157                 pv = new XPathContentMessagePropertyValue(xpath);
158                 if (logger.isDebugEnabled()) {
159                     logger.debug("Created an XPath VR :" + xpath);
160                 }
161             } else if (pvElem.getNodeName().equals(
162                     ExistingPropertyCopier.XML_ELEMENT_NAME)) {
163                 // default to this parents name (so it acts like a property
164
// copy)
165

166                 String JavaDoc propertyName = this.name;
167                 if (pvElem.getAttribute("name") != null
168                         && !"".equals(pvElem.getAttribute("name"))) {
169                     // if there was a <existing-property name="somename"/>
170
// then use that name as the source JBI property
171
// in this mode it acts like a cp src dest and not a dupe.
172
propertyName = pvElem.getAttribute("name");
173                 }
174                 pv = new ExistingPropertyCopier(propertyName);
175             } else {
176                 throw new ConfigNotSupportedException("Property value type "
177                         + pvElem.getNodeName()
178                         + " is not supported for the MessagePropertySetter");
179             }
180             this.propertySetTypes.add(pv);
181
182         }
183     }
184 }
185
Popular Tags