1 45 46 package org.exolab.jms.net.util; 47 48 import java.util.Map ; 49 import java.util.HashMap ; 50 51 import org.exolab.jms.net.connector.ResourceException; 52 import org.exolab.jms.net.uri.InvalidURIException; 53 import org.exolab.jms.net.uri.URI; 54 import org.exolab.jms.net.uri.URIHelper; 55 56 57 63 public final class Properties { 64 65 68 private final Map _properties; 69 70 74 private final String _prefix; 75 76 77 83 public Properties(String prefix) { 84 this(null, prefix); 85 } 86 87 94 public Properties(Map properties, String prefix) { 95 _properties = (properties != null) ? properties : new HashMap (); 96 _prefix = prefix; 97 } 98 99 106 public void set(String name, String value) { 107 _properties.put(getName(name), value); 108 } 109 110 117 public void setNonNull(String name, String value) { 118 if (value != null) { 119 _properties.put(getName(name), value); 120 } 121 } 122 123 130 public void set(String name, boolean value) { 131 Boolean bool = (value) ? Boolean.TRUE : Boolean.FALSE; 132 set(name, bool.toString()); 133 } 134 135 142 public void set(String name, int value) { 143 set(name, Integer.toString(value)); 144 } 145 146 153 public void set(String name, Object value) { 154 if (value != null) { 155 set(name, value.toString()); 156 } else { 157 set(name, null); 158 } 159 } 160 161 169 public void setNonNull(String name, Object value) { 170 if (value != null) { 171 set(name, value.toString()); 172 } 173 } 174 175 183 public String get(String name) throws ResourceException { 184 Object result = null; 185 name = getName(name); 186 result = _properties.get(name); 187 if (result != null && !(result instanceof String )) { 188 throw new ResourceException("Invalid type for property=" + name); 189 } 190 return (String ) result; 191 } 192 193 202 public boolean getBoolean(String name, boolean defaultValue) 203 throws ResourceException { 204 boolean result = defaultValue; 205 String value = get(name); 206 if (value != null) { 207 if (value.equalsIgnoreCase("true")) { 208 result = true; 209 } else if (value.equalsIgnoreCase("false")) { 210 result = false; 211 } else { 212 throw new ResourceException("Invalid boolean for property=" 213 + getName(name) 214 + ": " + value); 215 } 216 } 217 return result; 218 } 219 220 229 public int getInt(String name, int defaultValue) 230 throws ResourceException { 231 int result = defaultValue; 232 String value = get(name); 233 if (value != null) { 234 try { 235 result = Integer.parseInt(value); 236 } catch (NumberFormatException exception) { 237 throw new ResourceException("Invalid int for property=" 238 + getName(name) 239 + ": " + value); 240 } 241 } 242 return result; 243 } 244 245 252 public URI getURI(String name) throws ResourceException { 253 URI result = null; 254 String uri = get(name); 255 if (uri != null) { 256 try { 257 result = URIHelper.parse(uri); 258 } catch (InvalidURIException exception) { 259 throw new ResourceException("Invalid URI for property=" 260 + getName(name) 261 + ": " + uri); 262 } 263 } 264 return result; 265 } 266 267 272 public Map getProperties() { 273 return _properties; 274 } 275 276 284 private String getName(String name) { 285 String result; 286 if (_prefix != null && name.indexOf('.') == -1) { 287 result = _prefix + name; 288 } else { 289 result = name; 290 } 291 return result; 292 } 293 294 } 295 | Popular Tags |