KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > jms > JMSURLHelper


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

16
17 package org.apache.axis.transport.jms;
18
19 import java.net.URL JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * JMSURLHelper provides access to properties in the URL.
28  * The URL must be of the form: "jms:/<destination>?[<property>=<key>&]*"
29  *
30  * @author Ray Chun (rchun@sonicsoftware.com)
31  */

32 public class JMSURLHelper
33 {
34     private URL JavaDoc url;
35
36     // the only property not in the query string
37
private String JavaDoc destination;
38
39     // vendor-specific properties
40
private HashMap JavaDoc properties;
41     
42     // required properties
43
private Vector JavaDoc requiredProperties;
44
45     //application-specific JMS message properties
46
private Vector JavaDoc appProperties;
47
48     public JMSURLHelper(java.net.URL JavaDoc url) throws java.net.MalformedURLException JavaDoc {
49         this(url, null);
50     }
51
52     public JMSURLHelper(java.net.URL JavaDoc url, String JavaDoc[] requiredProperties) throws java.net.MalformedURLException JavaDoc {
53         this.url = url;
54         properties = new HashMap JavaDoc();
55         appProperties = new Vector JavaDoc();
56
57         // the path should be something like '/SampleQ1'
58
// clip the leading '/' if there is one
59
destination = url.getPath();
60         if (destination.startsWith("/"))
61             destination = destination.substring(1);
62
63         if ((destination == null) || (destination.trim().length() < 1))
64             throw new java.net.MalformedURLException JavaDoc("Missing destination in URL");
65
66         // parse the query string and populate the properties table
67
String JavaDoc query = url.getQuery();
68         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(query, "&;");
69         while (st.hasMoreTokens()) {
70             String JavaDoc keyValue = st.nextToken();
71             int eqIndex = keyValue.indexOf("=");
72             if (eqIndex > 0)
73             {
74                 String JavaDoc key = keyValue.substring(0, eqIndex);
75                 String JavaDoc value = keyValue.substring(eqIndex+1);
76                 if (key.startsWith(JMSConstants._MSG_PROP_PREFIX)) {
77                     key = key.substring(
78                         JMSConstants._MSG_PROP_PREFIX.length());
79                     addApplicationProperty(key);
80                 }
81                 properties.put(key, value);
82             }
83         }
84
85         // set required properties
86
addRequiredProperties(requiredProperties);
87         validateURL();
88     }
89
90     public String JavaDoc getDestination() {
91         return destination;
92     }
93     
94     public void setDestination(String JavaDoc destination) {
95         this.destination = destination;
96     }
97
98     public String JavaDoc getVendor() {
99         return getPropertyValue(JMSConstants._VENDOR);
100     }
101
102     public String JavaDoc getDomain() {
103         return getPropertyValue(JMSConstants._DOMAIN);
104     }
105
106     public HashMap JavaDoc getProperties() {
107         return properties;
108     }
109
110     public String JavaDoc getPropertyValue(String JavaDoc property) {
111         return (String JavaDoc)properties.get(property);
112     }
113
114     public void addRequiredProperties(String JavaDoc[] properties)
115     {
116         if (properties == null)
117             return;
118
119         for (int i = 0; i < properties.length; i++)
120         {
121             addRequiredProperty(properties[i]);
122         }
123     }
124
125     public void addRequiredProperty(String JavaDoc property) {
126         if (property == null)
127             return;
128
129         if (requiredProperties == null)
130             requiredProperties = new Vector JavaDoc();
131
132         requiredProperties.addElement(property);
133     }
134
135     public Vector JavaDoc getRequiredProperties() {
136         return requiredProperties;
137     }
138
139     /** Adds the name of a property from the url properties that should
140      * be added to the JMS message.
141      */

142     public void addApplicationProperty(String JavaDoc property) {
143         if (property == null)
144             return;
145
146         if (appProperties == null)
147             appProperties = new Vector JavaDoc();
148
149         appProperties.addElement(property);
150     }
151
152     /** Adds the name and value od the application property to the
153      * JMS URL.
154      */

155     public void addApplicationProperty(String JavaDoc property, String JavaDoc value) {
156         if (property == null)
157             return;
158
159         if (appProperties == null)
160             appProperties = new Vector JavaDoc();
161         
162         properties.put(property, value);
163         appProperties.addElement(property);
164     }
165
166     /** Returns a collection of properties that are defined within the
167      * JMS URL to be added directly to the JMS messages.
168         @return collection or null depending on presence of elements
169      */

170     public Vector JavaDoc getApplicationProperties() {
171         return appProperties;
172     }
173     
174     
175     /**
176         Returns a URL formatted String. The properties of the URL may not
177         end up in the same order as the JMS URL that was originally used to
178         create this object.
179     */

180     public String JavaDoc getURLString() {
181         StringBuffer JavaDoc text = new StringBuffer JavaDoc("jms:/");
182         text.append(getDestination());
183         text.append("?");
184         Map JavaDoc props = (Map JavaDoc)properties.clone();
185         boolean firstEntry = true;
186         for(Iterator JavaDoc itr=properties.keySet().iterator(); itr.hasNext();) {
187             String JavaDoc key = (String JavaDoc)itr.next();
188             if (!firstEntry) {
189                 text.append("&");
190             }
191             if (appProperties.contains(key)) {
192                 text.append(JMSConstants._MSG_PROP_PREFIX);
193             }
194             text.append(key);
195             text.append("=");
196             text.append(props.get(key));
197             firstEntry = false;
198         }
199         return text.toString();
200     }
201     
202     /** Returns a formatted URL String with the assigned properties */
203     public String JavaDoc toString() {
204         return getURLString();
205     }
206
207     private void validateURL()
208         throws java.net.MalformedURLException JavaDoc {
209         Vector JavaDoc required = getRequiredProperties();
210         if (required == null)
211             return;
212
213         for (int i = 0; i < required.size(); i++)
214         {
215             String JavaDoc key = (String JavaDoc)required.elementAt(i);
216             if (properties.get(key) == null)
217                 throw new java.net.MalformedURLException JavaDoc();
218         }
219     }
220 }
Popular Tags