KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > support > ObjectNameManager


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jmx.support;
18
19 import java.util.Hashtable JavaDoc;
20
21 import javax.management.MalformedObjectNameException JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23
24 import org.springframework.util.ClassUtils;
25
26 /**
27  * Helper class for the creation of {@link javax.management.ObjectName} instances.
28  *
29  * <p><code>ObjectName</code> instances will be cached on JMX 1.2,
30  * whereas they will be recreated for each request on JMX 1.0.
31  *
32  * @author Rob Harrop
33  * @author Juergen Hoeller
34  * @since 1.2
35  * @see javax.management.ObjectName#getInstance(String)
36  */

37 public class ObjectNameManager {
38
39     // Determine whether the JMX 1.2 <code>ObjectName.getInstance</code> method is available.
40
private static final boolean getInstanceAvailable =
41             ClassUtils.hasMethod(ObjectName JavaDoc.class, "getInstance", new Class JavaDoc[] {String JavaDoc.class});
42
43
44     /**
45      * Retrieve the <code>ObjectName</code> instance corresponding to the supplied name.
46      * @param objectName the <code>ObjectName</code> in <code>ObjectName</code> or
47      * <code>String</code> format
48      * @return the <code>ObjectName</code> instance
49      * @throws MalformedObjectNameException in case of an invalid object name specification
50      * @see ObjectName#ObjectName(String)
51      * @see ObjectName#getInstance(String)
52      */

53     public static ObjectName JavaDoc getInstance(Object JavaDoc objectName) throws MalformedObjectNameException JavaDoc {
54         if (objectName instanceof ObjectName JavaDoc) {
55             return (ObjectName JavaDoc) objectName;
56         }
57         if (!(objectName instanceof String JavaDoc)) {
58             throw new MalformedObjectNameException JavaDoc("Invalid ObjectName value type [" +
59                     objectName.getClass().getName() + "]: only ObjectName and String supported.");
60         }
61         return getInstance((String JavaDoc) objectName);
62     }
63
64     /**
65      * Retrieve the <code>ObjectName</code> instance corresponding to the supplied name.
66      * @param objectName the <code>ObjectName</code> in <code>String</code> format
67      * @return the <code>ObjectName</code> instance
68      * @throws MalformedObjectNameException in case of an invalid object name specification
69      * @see ObjectName#ObjectName(String)
70      * @see ObjectName#getInstance(String)
71      */

72     public static ObjectName JavaDoc getInstance(String JavaDoc objectName) throws MalformedObjectNameException JavaDoc {
73         if (getInstanceAvailable) {
74             return ObjectName.getInstance(objectName);
75         }
76         else {
77             return new ObjectName JavaDoc(objectName);
78         }
79     }
80
81     /**
82      * Retrieve an <code>ObjectName</code> instance for the specified domain and a
83      * single property with the supplied key and value.
84      * @param domainName the domain name for the <code>ObjectName</code>
85      * @param key the key for the single property in the <code>ObjectName</code>
86      * @param value the value for the single property in the <code>ObjectName</code>
87      * @return the <code>ObjectName</code> instance
88      * @throws MalformedObjectNameException in case of an invalid object name specification
89      * @see ObjectName#ObjectName(String, String, String)
90      * @see ObjectName#getInstance(String, String, String)
91      */

92     public static ObjectName JavaDoc getInstance(String JavaDoc domainName, String JavaDoc key, String JavaDoc value)
93             throws MalformedObjectNameException JavaDoc {
94
95         if (getInstanceAvailable) {
96             return ObjectName.getInstance(domainName, key, value);
97         }
98         else {
99             return new ObjectName JavaDoc(domainName, key, value);
100         }
101     }
102
103     /**
104      * Retrieve an <code>ObjectName</code> instance with the specified domain name
105      * and the supplied key/name properties.
106      * @param domainName the domain name for the <code>ObjectName</code>
107      * @param properties the properties for the <code>ObjectName</code>
108      * @return the <code>ObjectName</code> instance
109      * @throws MalformedObjectNameException in case of an invalid object name specification
110      * @see ObjectName#ObjectName(String, java.util.Hashtable)
111      * @see ObjectName#getInstance(String, java.util.Hashtable)
112      */

113     public static ObjectName JavaDoc getInstance(String JavaDoc domainName, Hashtable JavaDoc properties)
114             throws MalformedObjectNameException JavaDoc {
115
116         if (getInstanceAvailable) {
117             return ObjectName.getInstance(domainName, properties);
118         }
119         else {
120             return new ObjectName JavaDoc(domainName, properties);
121         }
122     }
123
124 }
125
Popular Tags