1 22 package org.jboss.varia.property; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.Arrays ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 import java.util.SortedSet ; 35 import java.util.StringTokenizer ; 36 import java.util.TreeSet ; 37 import javax.management.MBeanServer ; 38 import javax.management.ObjectName ; 39 40 import org.jboss.system.ServiceMBeanSupport; 41 import org.jboss.system.server.ServerConfigLocator; 42 import org.jboss.util.Strings; 43 import org.jboss.util.property.Property; 44 import org.jboss.util.property.PropertyGroup; 45 import org.jboss.util.property.PropertyListener; 46 47 57 public class SystemPropertiesService 58 extends ServiceMBeanSupport 59 implements SystemPropertiesServiceMBean 60 { 61 62 protected String serverHome; 63 64 68 77 public String set(final String name, final String value) 78 { 79 return Property.set(name, value); 80 } 81 82 91 public String get(final String name, final String defaultValue) 92 { 93 return Property.get(name, defaultValue); 94 } 95 96 104 public String get(final String name) 105 { 106 return Property.get(name); 107 } 108 109 117 public String remove(final String name) 118 { 119 return Property.remove(name); 120 } 121 122 131 public List getArray(final String base, final List defaultValues) 132 { 133 String [] array = new String [defaultValues.size()]; 134 defaultValues.toArray(array); 135 String [] values = Property.getArray(base, array); 136 return Arrays.asList(values); 137 } 138 139 147 public List getArray(String name) 148 { 149 String [] array = Property.getArray(name); 150 return Arrays.asList(array); 151 } 152 153 161 public boolean exists(String name) 162 { 163 return Property.exists(name); 164 } 165 166 174 public PropertyGroup getGroup(String basename) 175 { 176 return Property.getGroup(basename); 177 } 178 179 189 public PropertyGroup getGroup(String basename, int index) 190 { 191 return Property.getGroup(basename, index); 192 } 193 194 201 public void addListener(final PropertyListener listener) 202 { 203 Property.addListener(listener); 204 } 205 206 213 public void addListeners(final PropertyListener[] listeners) 214 { 215 Property.addListeners(listeners); 216 } 217 218 226 public boolean removeListener(final PropertyListener listener) 227 { 228 return Property.removeListener(listener); 229 } 230 231 232 236 243 public void load(final URL url) throws IOException 244 { 245 log.trace("Loading system properties from: " + url); 246 247 Properties props = System.getProperties(); 248 InputStream is = url.openConnection().getInputStream(); 249 props.load(is); 250 is.close(); 251 252 log.info("Loaded system properties from: " + url); 253 } 254 255 262 public void load(final String url) throws IOException , MalformedURLException 263 { 264 load(Strings.toURL(url, serverHome)); 265 } 266 267 268 272 279 public void addListener(final String typename) 280 throws ClassNotFoundException , IllegalAccessException , InstantiationException 281 { 282 Class type = Class.forName(typename); 283 PropertyListener listener = (PropertyListener) type.newInstance(); 284 285 addListener(listener); 286 } 287 288 295 public void setURLList(final String list) throws MalformedURLException , IOException 296 { 297 StringTokenizer stok = new StringTokenizer (list, ","); 298 299 while (stok.hasMoreTokens()) 300 { 301 String url = stok.nextToken(); 302 load(url); 303 } 304 } 305 306 314 public void setProperties(final Properties props) throws IOException 315 { 316 log.debug("Merging with system properties: " + props); 317 System.getProperties().putAll(props); 318 } 319 320 326 public Map showAll() 327 { 328 return new HTMLMap(System.getProperties()); 329 } 330 331 340 public Map showGroup(final String basename) 341 { 342 return new HTMLMap(getGroup(basename)); 343 } 344 345 352 protected static class HTMLMap 353 extends HashMap 354 { 355 public HTMLMap(final Map map) 356 { 357 super(map); 358 } 359 360 public String toString() 361 { 362 StringBuffer buff = new StringBuffer (); 363 364 buff.append("<table>"); 365 366 SortedSet keys = new TreeSet (this.keySet()); 367 Iterator iter = keys.iterator(); 368 while (iter.hasNext()) 369 { 370 String key = (String ) iter.next(); 371 buff.append("<tr><td align=\"left\"><b>") 372 .append(key) 373 .append("</b></td><td align=\"left\">") 374 .append(this.get(key)) 375 .append("</td></tr>\n\r"); 376 } 377 378 buff.append("</table>"); 379 380 return buff.toString(); 381 } 382 } 383 384 385 389 393 public ObjectName preRegister(final MBeanServer server, final ObjectName name) 394 throws Exception 395 { 396 serverHome = ServerConfigLocator.locate().getServerHomeDir().getPath(); 398 399 return super.preRegister(server, name); 400 } 401 402 } 403 | Popular Tags |