KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > property > jmx > SystemPropertyClassValue


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.util.property.jmx;
23
24 import org.jboss.logging.Logger;
25
26 /**
27  * A helper for setting system properties based on class availablity.<p>
28  *
29  * It has a static method and an MBean wrapper for dynamic configuration.<p>
30  *
31  * The class is first checked for availablity before setting the system
32  * property.
33
34  * @jmx.mbean
35  *
36  * @version <tt>$Revision: 1958 $</tt>
37  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
38  */

39 public class SystemPropertyClassValue
40    implements SystemPropertyClassValueMBean
41 {
42    public static final Logger log = Logger.getLogger(SystemPropertyClassValue.class);
43
44    /** Property name. */
45    protected String JavaDoc property;
46
47    /** Class Name. */
48    protected String JavaDoc className;
49
50    /**
51     * Constructor.
52     */

53    public SystemPropertyClassValue()
54    {
55    }
56
57    /**
58     * The system property value
59     *
60     * @jmx.managed-attribute
61     */

62    public String JavaDoc getProperty()
63    {
64       return property;
65    }
66
67    /**
68     * The system property value
69     *
70     * @jmx.managed-attribute
71     */

72    public void setProperty(String JavaDoc property)
73    {
74       this.property = property;
75    }
76
77    /**
78     * The class name to use a value for the system property
79     * when it is available
80     *
81     * @jmx.managed-attribute
82     */

83    public String JavaDoc getClassName()
84    {
85       return className;
86    }
87
88    /**
89     * The class name to use a value for the system property
90     * when it is available
91     *
92     * @jmx.managed-attribute
93     */

94    public void setClassName(String JavaDoc className)
95    {
96       this.className = className;
97    }
98
99    /**
100     * JBoss lifecycle
101     *
102     * @jmx.managed-operation
103     */

104    public void create()
105    {
106       Throwable JavaDoc error = setSystemPropertyClassValue(property, className);
107       if (error != null)
108          log.trace("Error loading class " + className + " property " + property + " not set.", error);
109    }
110
111    /**
112     * Sets the system property to a class when the class is available.
113     *
114     * @param property the property to set
115     * @param className the class to set as the properties value
116     * @return any error loading the class
117     * @exception IllegalArgumentException for a null or empty parameter
118     */

119    public static Throwable JavaDoc setSystemPropertyClassValue(String JavaDoc property, String JavaDoc className)
120    {
121       // Validation
122
if (property == null || property.trim().length() == 0)
123          throw new IllegalArgumentException JavaDoc("Null or empty property");
124       if (className == null || className.trim().length() == 0)
125          throw new IllegalArgumentException JavaDoc("Null or empty class name");
126
127       // Is the class available?
128
try
129       {
130          Thread.currentThread().getContextClassLoader().loadClass(className);
131       }
132       catch (Throwable JavaDoc problem)
133       {
134          return problem;
135       }
136
137       // The class is there, set the property.
138
System.setProperty(property, className);
139       return null;
140    }
141 }
142
Popular Tags