001/* 002 * Copyright 2002-2012 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 * https://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 017package org.springframework.jmx.support; 018 019import java.util.Hashtable; 020import javax.management.MalformedObjectNameException; 021import javax.management.ObjectName; 022 023/** 024 * Helper class for the creation of {@link javax.management.ObjectName} instances. 025 * 026 * @author Rob Harrop 027 * @author Juergen Hoeller 028 * @since 1.2 029 * @see javax.management.ObjectName#getInstance(String) 030 */ 031public class ObjectNameManager { 032 033 /** 034 * Retrieve the {@code ObjectName} instance corresponding to the supplied name. 035 * @param objectName the {@code ObjectName} in {@code ObjectName} or 036 * {@code String} format 037 * @return the {@code ObjectName} instance 038 * @throws MalformedObjectNameException in case of an invalid object name specification 039 * @see ObjectName#ObjectName(String) 040 * @see ObjectName#getInstance(String) 041 */ 042 public static ObjectName getInstance(Object objectName) throws MalformedObjectNameException { 043 if (objectName instanceof ObjectName) { 044 return (ObjectName) objectName; 045 } 046 if (!(objectName instanceof String)) { 047 throw new MalformedObjectNameException("Invalid ObjectName value type [" + 048 objectName.getClass().getName() + "]: only ObjectName and String supported."); 049 } 050 return getInstance((String) objectName); 051 } 052 053 /** 054 * Retrieve the {@code ObjectName} instance corresponding to the supplied name. 055 * @param objectName the {@code ObjectName} in {@code String} format 056 * @return the {@code ObjectName} instance 057 * @throws MalformedObjectNameException in case of an invalid object name specification 058 * @see ObjectName#ObjectName(String) 059 * @see ObjectName#getInstance(String) 060 */ 061 public static ObjectName getInstance(String objectName) throws MalformedObjectNameException { 062 return ObjectName.getInstance(objectName); 063 } 064 065 /** 066 * Retrieve an {@code ObjectName} instance for the specified domain and a 067 * single property with the supplied key and value. 068 * @param domainName the domain name for the {@code ObjectName} 069 * @param key the key for the single property in the {@code ObjectName} 070 * @param value the value for the single property in the {@code ObjectName} 071 * @return the {@code ObjectName} instance 072 * @throws MalformedObjectNameException in case of an invalid object name specification 073 * @see ObjectName#ObjectName(String, String, String) 074 * @see ObjectName#getInstance(String, String, String) 075 */ 076 public static ObjectName getInstance(String domainName, String key, String value) 077 throws MalformedObjectNameException { 078 079 return ObjectName.getInstance(domainName, key, value); 080 } 081 082 /** 083 * Retrieve an {@code ObjectName} instance with the specified domain name 084 * and the supplied key/name properties. 085 * @param domainName the domain name for the {@code ObjectName} 086 * @param properties the properties for the {@code ObjectName} 087 * @return the {@code ObjectName} instance 088 * @throws MalformedObjectNameException in case of an invalid object name specification 089 * @see ObjectName#ObjectName(String, java.util.Hashtable) 090 * @see ObjectName#getInstance(String, java.util.Hashtable) 091 */ 092 public static ObjectName getInstance(String domainName, Hashtable<String, String> properties) 093 throws MalformedObjectNameException { 094 095 return ObjectName.getInstance(domainName, properties); 096 } 097 098}