KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > property > SystemPropertiesService


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.varia.property;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.SortedSet JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36 import java.util.TreeSet JavaDoc;
37 import javax.management.MBeanServer JavaDoc;
38 import javax.management.ObjectName JavaDoc;
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 /**
48  * A service to access system properties.
49  *
50  * @jmx:mbean name="jboss.varia:type=Service,name=SystemProperties"
51  * extends="org.jboss.system.ServiceMBean"
52  *
53  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
54  * @author Scott.Stark@jboss.org
55  * @version $Revision: 37459 $
56  */

57 public class SystemPropertiesService
58    extends ServiceMBeanSupport
59    implements SystemPropertiesServiceMBean
60 {
61    /** The server's home dir as a string (for making urls). */
62    protected String JavaDoc serverHome;
63    
64    ///////////////////////////////////////////////////////////////////////////
65
// Property/PropertyManager Access //
66
///////////////////////////////////////////////////////////////////////////
67

68    /**
69     * Set a system property.
70     *
71     * @jmx:managed-operation
72     *
73     * @param name The name of the property to set.
74     * @param value The value of the property.
75     * @return Previous property value or null
76     */

77    public String JavaDoc set(final String JavaDoc name, final String JavaDoc value)
78    {
79       return Property.set(name, value);
80    }
81
82    /**
83     * Get a system property.
84     *
85     * @jmx:managed-operation
86     *
87     * @param name Property name
88     * @param defaultValue Default property value
89     * @return Property value or default
90     */

91    public String JavaDoc get(final String JavaDoc name, final String JavaDoc defaultValue)
92    {
93       return Property.get(name, defaultValue);
94    }
95
96    /**
97     * Get a system property.
98     *
99     * @jmx:managed-operation
100     *
101     * @param name Property name
102     * @return Property value or null
103     */

104    public String JavaDoc get(final String JavaDoc name)
105    {
106       return Property.get(name);
107    }
108
109    /**
110     * Remove a system property.
111     *
112     * @jmx:managed-operation
113     *
114     * @param name The name of the property to remove.
115     * @return Removed property value or null
116     */

117    public String JavaDoc remove(final String JavaDoc name)
118    {
119       return Property.remove(name);
120    }
121
122    /**
123     * Get an array style system property.
124     *
125     * @jmx:managed-operation
126     *
127     * @param base Base property name
128     * @param defaultValues Default property values
129     * @return ArrayList of property values or default
130     */

131    public List JavaDoc getArray(final String JavaDoc base, final List JavaDoc defaultValues)
132    {
133       String JavaDoc[] array = new String JavaDoc[defaultValues.size()];
134       defaultValues.toArray(array);
135       String JavaDoc[] values = Property.getArray(base, array);
136       return Arrays.asList(values);
137    }
138
139    /**
140     * Get an array style system property.
141     *
142     * @jmx:managed-operation
143     *
144     * @param name Property name
145     * @return ArrayList of property values or empty array
146     */

147    public List JavaDoc getArray(String JavaDoc name)
148    {
149       String JavaDoc[] array = Property.getArray(name);
150       return Arrays.asList(array);
151    }
152
153    /**
154     * Check if a system property of the given name exists.
155     *
156     * @jmx:managed-operation
157     *
158     * @param name Property name
159     * @return True if property exists
160     */

161    public boolean exists(String JavaDoc name)
162    {
163       return Property.exists(name);
164    }
165
166    /**
167     * Get a property group for under the given system property base.
168     *
169     * @jmx:managed-operation
170     *
171     * @param basename Base property name
172     * @return Property group
173     */

174    public PropertyGroup getGroup(String JavaDoc basename)
175    {
176       return Property.getGroup(basename);
177    }
178
179    /**
180     * Get a property group for under the given system property base
181     * at the given index.
182     *
183     * @jmx:managed-operation
184     *
185     * @param basename Base property name
186     * @param index Array property index
187     * @return Property group
188     */

189    public PropertyGroup getGroup(String JavaDoc basename, int index)
190    {
191       return Property.getGroup(basename, index);
192    }
193
194    /**
195     * Add a property listener.
196     *
197     * @jmx:managed-operation
198     *
199     * @param listener Property listener to add
200     */

201    public void addListener(final PropertyListener listener)
202    {
203       Property.addListener(listener);
204    }
205
206    /**
207     * Add an array of property listeners.
208     *
209     * @jmx:managed-operation
210     *
211     * @param listeners Array of property listeners to add
212     */

213    public void addListeners(final PropertyListener[] listeners)
214    {
215       Property.addListeners(listeners);
216    }
217
218    /**
219     * Remove a property listener.
220     *
221     * @jmx:managed-operation
222     *
223     * @param listener Property listener to remove
224     * @return True if listener was removed
225     */

226    public boolean removeListener(final PropertyListener listener)
227    {
228       return Property.removeListener(listener);
229    }
230
231    
232    ///////////////////////////////////////////////////////////////////////////
233
// Property Loading //
234
///////////////////////////////////////////////////////////////////////////
235

236    /**
237     * Load some system properties from the given URL.
238     *
239     * @jmx:managed-operation
240     *
241     * @param url The url to load properties from.
242     */

243    public void load(final URL JavaDoc url) throws IOException JavaDoc
244    {
245       log.trace("Loading system properties from: " + url);
246
247       Properties JavaDoc props = System.getProperties();
248       InputStream JavaDoc is = url.openConnection().getInputStream();
249       props.load(is);
250       is.close();
251
252       log.info("Loaded system properties from: " + url);
253    }
254
255    /**
256     * Load some system properties from the given URL.
257     *
258     * @jmx:managed-operation
259     *
260     * @param url The url to load properties from.
261     */

262    public void load(final String JavaDoc url) throws IOException JavaDoc, MalformedURLException JavaDoc
263    {
264       load(Strings.toURL(url, serverHome));
265    }
266    
267
268    ///////////////////////////////////////////////////////////////////////////
269
// JMX & Configuration Helpers //
270
///////////////////////////////////////////////////////////////////////////
271

272    /**
273     * Construct and add a property listener.
274     *
275     * @jmx:managed-operation
276     *
277     * @param type The type of property listener to add.
278     */

279    public void addListener(final String JavaDoc typename)
280       throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc
281    {
282       Class JavaDoc type = Class.forName(typename);
283       PropertyListener listener = (PropertyListener) type.newInstance();
284
285       addListener(listener);
286    }
287
288    /**
289     * Load system properties for each of the given comma separated urls.
290     *
291     * @jmx:managed-attribute
292     *
293     * @param list A list of comma separated urls.
294     */

295    public void setURLList(final String JavaDoc list) throws MalformedURLException JavaDoc, IOException JavaDoc
296    {
297       StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(list, ",");
298
299       while (stok.hasMoreTokens())
300       {
301          String JavaDoc url = stok.nextToken();
302          load(url);
303       }
304    }
305
306    /** Set system properties by merging the given properties object. This will
307     * replace valid references to properties of the form ${x} in 'props' or a
308     * System property with the value of x.
309     *
310     * @jmx:managed-attribute
311     *
312     * @param props Properties object to merge.
313     */

314    public void setProperties(final Properties JavaDoc props) throws IOException JavaDoc
315    {
316       log.debug("Merging with system properties: " + props);
317       System.getProperties().putAll(props);
318    }
319
320    /**
321     * Return a Map of System.getProperties() with a toString implementation
322     * that provides an html table of the key/value pairs.
323     *
324     * @jmx:managed-operation
325     */

326    public Map JavaDoc showAll()
327    {
328       return new HTMLMap(System.getProperties());
329    }
330
331    /**
332     * Return a Map of the property group for under the given system property base
333     * with a toString implementation that provides an html table of the key/value pairs.
334     *
335     * @jmx:managed-operation
336     *
337     * @param basename Base property name
338     * @return Property group
339     */

340    public Map JavaDoc showGroup(final String JavaDoc basename)
341    {
342       return new HTMLMap(getGroup(basename));
343    }
344
345    /**
346     * A helper to render a map as HTML on toString()
347     *
348     * <p>
349     * The html adapter should in theory be able to render a map (nested map
350     * list, array or whatever), but until then we can do it for it.
351     */

352    protected static class HTMLMap
353       extends HashMap JavaDoc
354    {
355       public HTMLMap(final Map JavaDoc map)
356       {
357          super(map);
358       }
359
360       public String JavaDoc toString()
361       {
362          StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
363
364          buff.append("<table>");
365
366          SortedSet JavaDoc keys = new TreeSet JavaDoc(this.keySet());
367          Iterator JavaDoc iter = keys.iterator();
368          while (iter.hasNext())
369          {
370             String JavaDoc key = (String JavaDoc) 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    ///////////////////////////////////////////////////////////////////////////
386
// ServiceMBeanSupport Overrides //
387
///////////////////////////////////////////////////////////////////////////
388

389    /**
390     * Setup our reference to the server's home directory. This is done
391     * here because one or more attribute setters makes use of this value.
392     */

393    public ObjectName JavaDoc preRegister(final MBeanServer JavaDoc server, final ObjectName JavaDoc name)
394       throws Exception JavaDoc
395    {
396       // get server's home for relative paths, need this for making urls
397
serverHome = ServerConfigLocator.locate().getServerHomeDir().getPath();
398
399       return super.preRegister(server, name);
400    }
401
402 }
403
Popular Tags