KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > jmx > JmxUtil


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.cache.jmx;
23
24 import org.jboss.cache.config.Configuration;
25 import org.jboss.cache.interceptors.Interceptor;
26
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerFactory JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * Various JMX related utilities
35  *
36  * @author Jerry Gauthier
37  * @author Manik Surtani
38  * @version $Id: JmxUtil.java,v 1.9 2007/01/05 14:27:15 msurtani Exp $
39  */

40 public class JmxUtil
41 {
42    public static final String JavaDoc MBEAN_CLASS_SUFFIX = "MBean";
43    public static final String JavaDoc MBEAN_KEY = ",cache-interceptor=";
44    public static final String JavaDoc JBOSS_CACHE_DOMAIN = "jboss.cache";
45    public static final String JavaDoc PREFIX = JBOSS_CACHE_DOMAIN + ":service=Cache,clusterName=";
46    private static final String JavaDoc JBOSS_SERVER_DOMAIN = "jboss";
47    public static final String JavaDoc CACHE_TYPE_KEY = "cacheType=";
48    public static final String JavaDoc PLAIN_CACHE_TYPE = "cacheType=Cache";
49
50    public static void registerCacheMBean(MBeanServer JavaDoc server, CacheJmxWrapperMBean cache, String JavaDoc cacheObjectName)
51            throws Exception JavaDoc
52    {
53       ObjectName JavaDoc on = new ObjectName JavaDoc(cacheObjectName);
54       if (!server.isRegistered(on))
55       {
56          server.registerMBean(cache, on);
57       }
58    }
59
60    /*
61     * Register the associated mbeans for cache interceptors
62     *
63     * @param server the mbean server with which the mbeans should be registered
64     * @param cache the cache having the set of interceptors to be registered
65     * @param registerCache whether the cache itself should be registered
66     */

67    public static void registerInterceptors(MBeanServer JavaDoc server, List JavaDoc<Interceptor> interceptors, String JavaDoc cacheObjectName)
68            throws Exception JavaDoc
69    {
70       if (server == null || interceptors == null || cacheObjectName == null)
71       {
72          return;
73       }
74
75       Interceptor interceptor = null;
76
77       for (int i = 0; i < interceptors.size(); i++)
78       {
79          interceptor = interceptors.get(i);
80          boolean mbeanExists = true;
81          try
82          {
83             // the mbean for interceptor AbcInterceptor will be named AbcInterceptorMBean
84
Class.forName(interceptor.getClass().getName() + MBEAN_CLASS_SUFFIX);
85          }
86          catch (Throwable JavaDoc e)
87          {
88             // if the class can't be instantiated, no mbean is available
89
mbeanExists = false;
90          }
91
92          // for JDK 1.4, must parse name and remove package prefix
93
// for JDK 1.5, can use getSimpleName() to establish class name without package prefix
94
String JavaDoc className = interceptor.getClass().getName();
95          String JavaDoc serviceName = cacheObjectName + MBEAN_KEY + className.substring(className.lastIndexOf('.') + 1);
96
97          ObjectName JavaDoc objName = new ObjectName JavaDoc(serviceName);
98          if (!server.isRegistered(objName))
99          {
100             if (mbeanExists)
101             // register associated interceptor mbean
102
{
103                server.registerMBean(interceptor, objName);
104             }
105             //else
106
// register dummy interceptor mbean
107
// server.registerMBean(new BaseInterceptor(), objName);
108
}
109       }
110    }
111
112    public static String JavaDoc getCacheObjectName(org.jboss.cache.CacheSPI cache)
113    {
114       // get the cache's registration name
115
return getCacheObjectName(cache.getConfiguration(), cache.getClass().getName());
116    }
117
118    public static String JavaDoc getCacheObjectName(Configuration config, String JavaDoc cacheSPIClass)
119    {
120       // get the cache's registration name
121
String JavaDoc tmpName = config.getServiceName();
122       if (tmpName == null)
123       {
124          if (config.getClusterName() == null)
125          {
126             tmpName = PREFIX + cacheSPIClass + System.currentTimeMillis();
127          }
128          else
129          {
130             tmpName = PREFIX + config.getClusterName();
131          }
132       }
133
134       return tmpName;
135    }
136
137    public static void unregisterCacheMBean(MBeanServer JavaDoc server, String JavaDoc cacheObjectName)
138            throws Exception JavaDoc
139    {
140       server.unregisterMBean(new ObjectName JavaDoc(cacheObjectName));
141    }
142
143    /*
144    * Unregister the associated mbeans for cache interceptors
145    *
146    * @param server the mbean server for which the mbeans should be unregistered
147    * @param cache the cache having the set of interceptors to be unregistered
148    * @param unregisterCache whether the cache itself should be unregistered
149    */

150    public static void unregisterInterceptors(MBeanServer JavaDoc server, List JavaDoc<Interceptor> interceptors, String JavaDoc cacheObjectName)
151            throws Exception JavaDoc
152    {
153       if (server == null || interceptors == null || cacheObjectName == null)
154       {
155          return;
156       }
157
158       Interceptor interceptor = null;
159
160       for (int i = 0; i < interceptors.size(); i++)
161       {
162          interceptor = interceptors.get(i);
163
164          // for JDK 1.4, must parse name and remove package prefix
165
// for JDK 1.5, can use getSimpleName() to establish class name without package prefix
166
String JavaDoc className = interceptor.getClass().getName();
167          String JavaDoc serviceName = cacheObjectName + MBEAN_KEY + className.substring(className.lastIndexOf('.') + 1);
168
169          ObjectName JavaDoc objName = new ObjectName JavaDoc(serviceName);
170          if (server.isRegistered(objName))
171          {
172             server.unregisterMBean(objName);
173          }
174       }
175    }
176
177    public static MBeanServer JavaDoc getMBeanServer()
178    {
179       ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
180       if (servers == null || servers.size() == 0)
181       {
182          return null;
183       }
184
185       // return 'jboss' server if available
186
for (int i = 0; i < servers.size(); i++)
187       {
188          MBeanServer JavaDoc server = (MBeanServer JavaDoc) servers.get(i);
189          if (server.getDefaultDomain().equalsIgnoreCase(JBOSS_SERVER_DOMAIN))
190          {
191             return server;
192          }
193       }
194
195       // return first available server
196
return (MBeanServer JavaDoc) servers.get(0);
197
198    }
199 }
200
Popular Tags