001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jmx.support;
018:
019: import java.util.Hashtable;
020:
021: import javax.management.MalformedObjectNameException;
022: import javax.management.ObjectName;
023:
024: import org.springframework.util.ClassUtils;
025:
026: /**
027: * Helper class for the creation of {@link javax.management.ObjectName} instances.
028: *
029: * <p><code>ObjectName</code> instances will be cached on JMX 1.2,
030: * whereas they will be recreated for each request on JMX 1.0.
031: *
032: * @author Rob Harrop
033: * @author Juergen Hoeller
034: * @since 1.2
035: * @see javax.management.ObjectName#getInstance(String)
036: */
037: public class ObjectNameManager {
038:
039: // Determine whether the JMX 1.2 <code>ObjectName.getInstance</code> method is available.
040: private static final boolean getInstanceAvailable = ClassUtils
041: .hasMethod(ObjectName.class, "getInstance",
042: new Class[] { String.class });
043:
044: /**
045: * Retrieve the <code>ObjectName</code> instance corresponding to the supplied name.
046: * @param objectName the <code>ObjectName</code> in <code>ObjectName</code> or
047: * <code>String</code> format
048: * @return the <code>ObjectName</code> instance
049: * @throws MalformedObjectNameException in case of an invalid object name specification
050: * @see ObjectName#ObjectName(String)
051: * @see ObjectName#getInstance(String)
052: */
053: public static ObjectName getInstance(Object objectName)
054: throws MalformedObjectNameException {
055: if (objectName instanceof ObjectName) {
056: return (ObjectName) objectName;
057: }
058: if (!(objectName instanceof String)) {
059: throw new MalformedObjectNameException(
060: "Invalid ObjectName value type ["
061: + objectName.getClass().getName()
062: + "]: only ObjectName and String supported.");
063: }
064: return getInstance((String) objectName);
065: }
066:
067: /**
068: * Retrieve the <code>ObjectName</code> instance corresponding to the supplied name.
069: * @param objectName the <code>ObjectName</code> in <code>String</code> format
070: * @return the <code>ObjectName</code> instance
071: * @throws MalformedObjectNameException in case of an invalid object name specification
072: * @see ObjectName#ObjectName(String)
073: * @see ObjectName#getInstance(String)
074: */
075: public static ObjectName getInstance(String objectName)
076: throws MalformedObjectNameException {
077: if (getInstanceAvailable) {
078: return ObjectName.getInstance(objectName);
079: } else {
080: return new ObjectName(objectName);
081: }
082: }
083:
084: /**
085: * Retrieve an <code>ObjectName</code> instance for the specified domain and a
086: * single property with the supplied key and value.
087: * @param domainName the domain name for the <code>ObjectName</code>
088: * @param key the key for the single property in the <code>ObjectName</code>
089: * @param value the value for the single property in the <code>ObjectName</code>
090: * @return the <code>ObjectName</code> instance
091: * @throws MalformedObjectNameException in case of an invalid object name specification
092: * @see ObjectName#ObjectName(String, String, String)
093: * @see ObjectName#getInstance(String, String, String)
094: */
095: public static ObjectName getInstance(String domainName, String key,
096: String value) throws MalformedObjectNameException {
097:
098: if (getInstanceAvailable) {
099: return ObjectName.getInstance(domainName, key, value);
100: } else {
101: return new ObjectName(domainName, key, value);
102: }
103: }
104:
105: /**
106: * Retrieve an <code>ObjectName</code> instance with the specified domain name
107: * and the supplied key/name properties.
108: * @param domainName the domain name for the <code>ObjectName</code>
109: * @param properties the properties for the <code>ObjectName</code>
110: * @return the <code>ObjectName</code> instance
111: * @throws MalformedObjectNameException in case of an invalid object name specification
112: * @see ObjectName#ObjectName(String, java.util.Hashtable)
113: * @see ObjectName#getInstance(String, java.util.Hashtable)
114: */
115: public static ObjectName getInstance(String domainName,
116: Hashtable properties) throws MalformedObjectNameException {
117:
118: if (getInstanceAvailable) {
119: return ObjectName.getInstance(domainName, properties);
120: } else {
121: return new ObjectName(domainName, properties);
122: }
123: }
124:
125: }
|