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