KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > expression > JaxenVariableContext


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.expression;
18
19 import org.jaxen.UnresolvableException;
20 import org.jaxen.VariableContext;
21
22 import javax.jbi.messaging.MessageExchange;
23 import javax.jbi.messaging.NormalizedMessage;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * A variable resolver for XPath expressions which support properties on the messge, exchange as well
30  * as making system properties and environment properties available.
31  *
32  * @version $Revision: 426415 $
33  */

34 public class JaxenVariableContext implements VariableContext {
35     public static final String JavaDoc MESSAGE_NAMESPACE = "http://servicemix.org/xml/variables/message";
36     public static final String JavaDoc EXCHANGE_NAMESPACE = "http://servicemix.org/xml/variables/exchange";
37     public static final String JavaDoc SYSTEM_PROPERTIES_NAMESPACE = "http://servicemix.org/xml/variables/system-properties";
38     public static final String JavaDoc ENVIRONMENT_VARIABLES_NAMESPACE = "http://servicemix.org/xml/variables/environment-variables";
39
40     private MessageExchange exchange;
41     private NormalizedMessage message;
42     private Map JavaDoc variables;
43
44     public MessageExchange getExchange() {
45         return exchange;
46     }
47
48     public void setExchange(MessageExchange exchange) {
49         this.exchange = exchange;
50     }
51
52     public NormalizedMessage getMessage() {
53         return message;
54     }
55
56     public void setMessage(NormalizedMessage message) {
57         this.message = message;
58     }
59
60     public Map JavaDoc getVariables() {
61         return variables;
62     }
63
64     /**
65      * Allows other variables to be added to the variable scope
66      *
67      * @param variables
68      */

69     public void setVariables(Map JavaDoc variables) {
70         this.variables = variables;
71     }
72
73     public Object JavaDoc getVariableValue(String JavaDoc uri, String JavaDoc prefix, String JavaDoc localPart) throws UnresolvableException {
74         Object JavaDoc answer = null;
75
76         if (uri == null || uri.length() == 0) {
77             answer = message.getProperty(localPart);
78             if (answer == null) {
79                 answer = exchange.getProperty(localPart);
80             }
81         }
82         else if (uri.equals(MESSAGE_NAMESPACE)) {
83             answer = message.getProperty(localPart);
84         }
85         else if (uri.equals(EXCHANGE_NAMESPACE)) {
86             answer = message.getProperty(localPart);
87         }
88         else if (uri.equals(SYSTEM_PROPERTIES_NAMESPACE)) {
89             answer = System.getProperty(localPart);
90         }
91         else if (uri.equals(ENVIRONMENT_VARIABLES_NAMESPACE)) {
92             answer = System.getProperty(System.getProperty(localPart));
93         }
94         return answer;
95     }
96
97     /**
98      * Allows a variable to be specified
99      *
100      * @param localPart
101      * @param value
102      */

103     public void setVariableValue(String JavaDoc localPart, Object JavaDoc value) {
104         if (variables == null) {
105             variables = new HashMap JavaDoc();
106         }
107         variables.put(localPart, value);
108     }
109
110 }
111
Popular Tags