KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jbi.messaging.MessageExchange;
20 import javax.jbi.messaging.NormalizedMessage;
21 import javax.xml.namespace.QName JavaDoc;
22 import javax.xml.xpath.XPathVariableResolver JavaDoc;
23
24 /**
25  * A variable resolver for XPath expressions which support properties on the messge, exchange as well
26  * as making system properties and environment properties available.
27  *
28  * @version $Revision: 426415 $
29  */

30 public class MessageVariableResolver implements XPathVariableResolver JavaDoc {
31     public static final String JavaDoc SYSTEM_PROPERTIES_NAMESPACE = "http://servicemix.org/xml/variables/system-properties";
32     public static final String JavaDoc ENVIRONMENT_VARIABLES = "http://servicemix.org/xml/variables/environment-variables";
33
34     private MessageExchange exchange;
35     private NormalizedMessage message;
36
37     public MessageExchange getExchange() {
38         return exchange;
39     }
40
41     public void setExchange(MessageExchange exchange) {
42         this.exchange = exchange;
43     }
44
45     public NormalizedMessage getMessage() {
46         return message;
47     }
48
49     public void setMessage(NormalizedMessage message) {
50         this.message = message;
51     }
52
53     public Object JavaDoc resolveVariable(QName JavaDoc name) {
54         // should we use other namespaces maybe?
55
String JavaDoc uri = name.getNamespaceURI();
56         String JavaDoc localPart = name.getLocalPart();
57
58         Object JavaDoc answer = null;
59
60         if (uri == null || uri.length() == 0) {
61             answer = message.getProperty(localPart);
62             if (answer == null) {
63                 answer = exchange.getProperty(localPart);
64             }
65         }
66         else if (uri.equals(SYSTEM_PROPERTIES_NAMESPACE)) {
67             answer = System.getProperty(localPart);
68         }
69         else if (uri.equals(ENVIRONMENT_VARIABLES)) {
70             answer = System.getProperty(System.getProperty(localPart));
71         }
72         return answer;
73     }
74 }
75
Popular Tags