View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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 net.sf.ehcache.hibernate.management.impl;
18  
19  import javax.management.MalformedObjectNameException;
20  import javax.management.ObjectName;
21  
22  /***
23   * Utility class used for getting {@link ObjectName}'s for ehcache hibernate MBeans
24   * 
25   * <p />
26   * 
27   * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
28   * @since 1.7
29   * 
30   */
31  public abstract class EhcacheHibernateMbeanNames {
32  
33      /***
34       * Group id for all sampled mbeans registered
35       */
36      public static final String GROUP_ID = "net.sf.ehcache.hibernate";
37  
38      /***
39       * Type for the ehcache backed hibernate second level cache statistics mbean
40       */
41      public static final String EHCACHE_HIBERNATE_TYPE = "EhcacheHibernateStats";
42  
43      /***
44       * Filter out invalid ObjectName characters from s.
45       * 
46       * @param s
47       * @return A valid JMX ObjectName attribute value.
48       */
49      public static String mbeanSafe(String s) {
50          return s == null ? "" : s.replaceAll(":|=|\n", ".");
51        }
52      
53      /***
54       * Returns an ObjectName for the passed name
55       * 
56       * @param name
57       * @return An {@link ObjectName} using the input name of cache manager
58       * @throws MalformedObjectNameException
59       */
60      public static ObjectName getCacheManagerObjectName(String cacheManagerClusterUUID, String name) throws MalformedObjectNameException {
61          ObjectName objectName = new ObjectName(GROUP_ID + ":type=" + EHCACHE_HIBERNATE_TYPE + ",name=" + mbeanSafe(name)
62                  + getBeanNameSuffix(cacheManagerClusterUUID));
63          return objectName;
64      }
65  
66      private static String getBeanNameSuffix(String cacheManagerClusterUUID) {
67          String suffix = "";
68          if (!isBlank(cacheManagerClusterUUID)) {
69              suffix = ",node=" + cacheManagerClusterUUID;
70          }
71          return suffix;
72      }
73  
74      private static boolean isBlank(String param) {
75          return param == null || "".equals(param.trim());
76      }
77  }