1 16 17 package org.apache.axis.transport.jms; 18 19 import java.net.URL ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.StringTokenizer ; 24 import java.util.Vector ; 25 26 32 public class JMSURLHelper 33 { 34 private URL url; 35 36 private String destination; 38 39 private HashMap properties; 41 42 private Vector requiredProperties; 44 45 private Vector appProperties; 47 48 public JMSURLHelper(java.net.URL url) throws java.net.MalformedURLException { 49 this(url, null); 50 } 51 52 public JMSURLHelper(java.net.URL url, String [] requiredProperties) throws java.net.MalformedURLException { 53 this.url = url; 54 properties = new HashMap (); 55 appProperties = new Vector (); 56 57 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 ("Missing destination in URL"); 65 66 String query = url.getQuery(); 68 StringTokenizer st = new StringTokenizer (query, "&;"); 69 while (st.hasMoreTokens()) { 70 String keyValue = st.nextToken(); 71 int eqIndex = keyValue.indexOf("="); 72 if (eqIndex > 0) 73 { 74 String key = keyValue.substring(0, eqIndex); 75 String 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 addRequiredProperties(requiredProperties); 87 validateURL(); 88 } 89 90 public String getDestination() { 91 return destination; 92 } 93 94 public void setDestination(String destination) { 95 this.destination = destination; 96 } 97 98 public String getVendor() { 99 return getPropertyValue(JMSConstants._VENDOR); 100 } 101 102 public String getDomain() { 103 return getPropertyValue(JMSConstants._DOMAIN); 104 } 105 106 public HashMap getProperties() { 107 return properties; 108 } 109 110 public String getPropertyValue(String property) { 111 return (String )properties.get(property); 112 } 113 114 public void addRequiredProperties(String [] 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 property) { 126 if (property == null) 127 return; 128 129 if (requiredProperties == null) 130 requiredProperties = new Vector (); 131 132 requiredProperties.addElement(property); 133 } 134 135 public Vector getRequiredProperties() { 136 return requiredProperties; 137 } 138 139 142 public void addApplicationProperty(String property) { 143 if (property == null) 144 return; 145 146 if (appProperties == null) 147 appProperties = new Vector (); 148 149 appProperties.addElement(property); 150 } 151 152 155 public void addApplicationProperty(String property, String value) { 156 if (property == null) 157 return; 158 159 if (appProperties == null) 160 appProperties = new Vector (); 161 162 properties.put(property, value); 163 appProperties.addElement(property); 164 } 165 166 170 public Vector getApplicationProperties() { 171 return appProperties; 172 } 173 174 175 180 public String getURLString() { 181 StringBuffer text = new StringBuffer ("jms:/"); 182 text.append(getDestination()); 183 text.append("?"); 184 Map props = (Map )properties.clone(); 185 boolean firstEntry = true; 186 for(Iterator itr=properties.keySet().iterator(); itr.hasNext();) { 187 String key = (String )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 203 public String toString() { 204 return getURLString(); 205 } 206 207 private void validateURL() 208 throws java.net.MalformedURLException { 209 Vector required = getRequiredProperties(); 210 if (required == null) 211 return; 212 213 for (int i = 0; i < required.size(); i++) 214 { 215 String key = (String )required.elementAt(i); 216 if (properties.get(key) == null) 217 throw new java.net.MalformedURLException (); 218 } 219 } 220 } | Popular Tags |