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.management.sampled;
18  
19  import javax.management.MalformedObjectNameException;
20  import javax.management.ObjectName;
21  
22  import net.sf.ehcache.hibernate.management.impl.EhcacheHibernateMbeanNames;
23  
24  /***
25   * Utility class used for getting {@link ObjectName}'s for sampled MBeans
26   *
27   * <p />
28   *
29   * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
30   * @since 1.7
31   */
32  public abstract class SampledEhcacheMBeans {
33  
34      /***
35       * Type used for sampled cache manager mbean
36       */
37      public static final String SAMPLED_CACHE_MANAGER_TYPE = "SampledCacheManager";
38  
39      /***
40       * Type used for sampled cache mbean
41       */
42      public static final String SAMPLED_CACHE_TYPE = "SampledCache";
43  
44      /***
45       * Type used for store mbean
46       */
47      public static final String STORE_TYPE = "Store";
48  
49      /***
50       * Group id for all sampled mbeans registered
51       */
52      public static final String GROUP_ID = "net.sf.ehcache";
53  
54      /***
55       * Returns an ObjectName for the passed cacheManagerName
56       *
57       * @param cacheManagerName
58       * @return An {@link ObjectName} using the input name of cache manager
59       * @throws MalformedObjectNameException
60       */
61      public static ObjectName getCacheManagerObjectName(String clientUUID, String cacheManagerName) throws MalformedObjectNameException {
62          ObjectName objectName = new ObjectName(GROUP_ID + ":type=" + SAMPLED_CACHE_MANAGER_TYPE + ",name="
63                  + EhcacheHibernateMbeanNames.mbeanSafe(cacheManagerName) + getBeanNameSuffix(clientUUID));
64          return objectName;
65      }
66  
67      private static String getBeanNameSuffix(String clientUUID) {
68          String suffix = "";
69          if (clientUUID != null && !clientUUID.trim().equals("")) {
70              suffix = ",node=" + clientUUID;
71          }
72          return suffix;
73      }
74  
75      /***
76       * Returns an ObjectName for the passed cacheManagerName, cacheName
77       * combination
78       *
79       * @param cacheManagerName
80       * @param cacheName
81       * @return An {@link ObjectName} representing the cache using the passed
82       *         cache and the cache manager name
83       * @throws MalformedObjectNameException
84       */
85      public static ObjectName getCacheObjectName(String clientUUID, String cacheManagerName, String cacheName)
86              throws MalformedObjectNameException {
87          ObjectName objectName = new ObjectName(GROUP_ID + ":type=" + SAMPLED_CACHE_TYPE + "," + SAMPLED_CACHE_MANAGER_TYPE + "="
88                  + EhcacheHibernateMbeanNames.mbeanSafe(cacheManagerName) + ",name=" + EhcacheHibernateMbeanNames.mbeanSafe(cacheName)
89                  + getBeanNameSuffix(clientUUID));
90          return objectName;
91      }
92  
93      /***
94       * Returns an ObjectName for the passed cacheManagerName, cacheName
95       * combination
96       *
97       * @param cacheManagerName
98       * @param cacheName
99       * @return An {@link ObjectName} representing the cache using the passed
100      *         cache and the cache manager name
101      * @throws MalformedObjectNameException
102      */
103     static ObjectName getStoreObjectName(String clientUUID, String cacheManagerName, String cacheName) throws MalformedObjectNameException {
104         ObjectName objectName = new ObjectName(GROUP_ID + ":type=" + STORE_TYPE + "," + SAMPLED_CACHE_MANAGER_TYPE + "="
105                 + EhcacheHibernateMbeanNames.mbeanSafe(cacheManagerName) + ",name=" + EhcacheHibernateMbeanNames.mbeanSafe(cacheName)
106                 + getBeanNameSuffix(clientUUID));
107         return objectName;
108     }
109 
110     /***
111      * Returns an ObjectName that can be used for querying all Cache
112      * ObjectName's for the passed cacheManagerName
113      *
114      * @param cacheManagerName
115      * @return An {@link ObjectName} which can be used for querying all Cache
116      *         ObjectName's for the input cache manager name
117      * @throws MalformedObjectNameException
118      */
119     public static ObjectName getQueryCacheManagerObjectName(String clientUUID, String cacheManagerName)
120         throws MalformedObjectNameException {
121         ObjectName objectName = new ObjectName(GROUP_ID + ":*," + SAMPLED_CACHE_MANAGER_TYPE + "="
122                 + EhcacheHibernateMbeanNames.mbeanSafe(cacheManagerName) + getBeanNameSuffix(clientUUID));
123         return objectName;
124     }
125 
126     /***
127      * Returns an ObjectName that can be used to query all objectNames of {@link #SAMPLED_CACHE_MANAGER_TYPE}
128      *
129      * @return An {@link ObjectName} that can be used to query all ObjectName's
130      *         of {@value #SAMPLED_CACHE_MANAGER_TYPE}
131      * @throws MalformedObjectNameException
132      */
133     public static ObjectName getQueryCacheManagersObjectName(String clientUUID) throws MalformedObjectNameException {
134         ObjectName objectName = new ObjectName(GROUP_ID + ":type=" + SAMPLED_CACHE_MANAGER_TYPE + ",*" + getBeanNameSuffix(clientUUID));
135         return objectName;
136     }
137 }