KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * as indicated by the @author tags. See the copyright.txt file in the
5  * distribution for a 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.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.jboss.cache.Cache;
27 import org.jboss.cache.CacheImpl;
28 import org.jboss.cache.config.Configuration;
29 import org.jboss.cache.config.ConfigurationException;
30 import org.jboss.cache.factories.DefaultCacheFactory;
31 import org.jboss.cache.interceptors.CacheMgmtInterceptor;
32 import org.jboss.cache.interceptors.Interceptor;
33
34 import javax.management.ListenerNotFoundException JavaDoc;
35 import javax.management.MBeanNotificationInfo JavaDoc;
36 import javax.management.MBeanRegistration JavaDoc;
37 import javax.management.MBeanServer JavaDoc;
38 import javax.management.MalformedObjectNameException JavaDoc;
39 import javax.management.NotificationFilter JavaDoc;
40 import javax.management.NotificationListener JavaDoc;
41 import javax.management.ObjectName JavaDoc;
42
43 public class CacheJmxWrapper implements CacheJmxWrapperMBean, MBeanRegistration JavaDoc
44 {
45    private Log log = LogFactory.getLog(getClass().getName());
46
47    private MBeanServer JavaDoc server;
48    private String JavaDoc cacheObjectName;
49    private boolean registeredInterceptorsInCreate;
50    private boolean interceptorsRegistered;
51    private CacheImpl cache;
52    private boolean selfConstructed = false;
53    private Configuration config;
54    private boolean created;
55    private boolean registerInterceptors = true;
56
57    public CacheJmxWrapper()
58    {
59    }
60
61    public CacheJmxWrapper(Cache cache)
62    {
63       setCache(cache);
64    }
65
66    public org.jboss.cache.Cache getCache()
67    {
68       return cache;
69    }
70
71    public Configuration getConfiguration()
72    {
73       return cache == null ? null : cache.getConfiguration();
74    }
75
76    public String JavaDoc getConfigurationAsString()
77    {
78       return cache == null ? "Cache is null" : cache.getConfiguration().toString();
79    }
80
81    public String JavaDoc getConfigurationAsHtmlString()
82    {
83       return cache == null ? "Cache is null" : formatHtml(cache.getConfiguration().toString());
84    }
85
86    public String JavaDoc getCacheDetails()
87    {
88       return cache == null ? "Cache is null" : cache.printDetails();
89    }
90
91    public String JavaDoc getCacheDetailsAsHtml()
92    {
93       return cache == null ? "Cache is null" : formatHtml(cache.printDetails());
94    }
95
96    public int getNumberOfNodes()
97    {
98       return cache == null ? -1 : cache.getNumberOfNodes();
99    }
100
101    public int getNumberOfAttributes()
102    {
103       return cache == null ? -1 : cache.getNumberOfAttributes();
104    }
105
106    public String JavaDoc getLockInfo()
107    {
108       return cache == null ? "Cache is null" : cache.printLockInfo();
109    }
110
111    public String JavaDoc getLockInfoAsHtml()
112    {
113       return cache == null ? "Cache is null" : formatHtml(cache.printLockInfo());
114    }
115
116    public void addNotificationListener(NotificationListener JavaDoc notificationListener, NotificationFilter JavaDoc notificationFilter, Object JavaDoc object) throws IllegalArgumentException JavaDoc
117    {
118       getCacheMgmtInterceptor().addNotificationListener(notificationListener, notificationFilter, object);
119    }
120
121    public void removeNotificationListener(NotificationListener JavaDoc notificationListener) throws ListenerNotFoundException JavaDoc
122    {
123       getCacheMgmtInterceptor().removeNotificationListener(notificationListener);
124    }
125
126    public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
127    {
128       return getCacheMgmtInterceptor().getNotificationInfo();
129    }
130
131    private CacheMgmtInterceptor getCacheMgmtInterceptor()
132    {
133       for (Interceptor i : cache.getInterceptors())
134       {
135          if (i instanceof CacheMgmtInterceptor) return (CacheMgmtInterceptor) i;
136       }
137       throw new RuntimeException JavaDoc("Cache management interceptor not found");
138    }
139
140    /**
141     * Formats a given String for display as an HTML snippet.
142     *
143     * @param s string to format
144     * @return formatted string
145     */

146    protected String JavaDoc formatHtml(String JavaDoc s)
147    {
148       s = s.replaceAll("\r\n", "<br />");
149       s = s.replaceAll("\r", "<br />");
150       s = s.replaceAll("\n", "<br />");
151       s = s.replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
152       s = s.replaceAll(" ", "&nbsp;");
153       return s;
154    }
155
156    public void create() throws Exception JavaDoc
157    {
158       if (cache == null)
159       {
160          if (config == null)
161          {
162             throw new ConfigurationException("Must call setConfiguration() or setCache() before call to create()");
163          }
164
165          constructCache();
166       }
167
168       if (selfConstructed)
169       {
170          cache.create();
171       }
172
173       registeredInterceptorsInCreate = registerInterceptors();
174
175       created = true;
176    }
177
178    public void start() throws Exception JavaDoc
179    {
180       if (selfConstructed)
181       {
182          cache.start();
183       }
184    }
185
186    public void stop()
187    {
188       if (selfConstructed)
189       {
190          cache.stop();
191       }
192    }
193
194    public void destroy()
195    {
196       if (selfConstructed)
197       {
198          cache.destroy();
199
200          if (registeredInterceptorsInCreate)
201          {
202             unregisterInterceptors();
203          }
204       }
205
206       created = false;
207    }
208
209    public boolean getRegisterInterceptors()
210    {
211       return registerInterceptors;
212    }
213
214    public void setRegisterInterceptors(boolean register)
215    {
216       this.registerInterceptors = register;
217    }
218
219    protected void constructCache() throws Exception JavaDoc
220    {
221       log.debug("Constructing Cache");
222       setCache(DefaultCacheFactory.getInstance().createCache(config, false));
223       selfConstructed = true;
224    }
225
226    protected boolean registerInterceptors() throws Exception JavaDoc
227    {
228       if (registerInterceptors && !interceptorsRegistered && server != null)
229       {
230          log.debug("Registering interceptors");
231          JmxUtil.registerInterceptors(server, cache.getInterceptorChain(), cacheObjectName);
232          interceptorsRegistered = true;
233          return true;
234       }
235       return false;
236    }
237
238    protected void unregisterInterceptors()
239    {
240       if (registerInterceptors && interceptorsRegistered && server != null)
241       {
242          try
243          {
244             log.debug("Unreqistering interceptors");
245             JmxUtil.unregisterInterceptors(server, cache.getInterceptorChain(), getCacheObjectName());
246             interceptorsRegistered = false;
247          }
248          catch (Exception JavaDoc e)
249          {
250             log.error("Exception unregistering interceptors from JMX", e);
251          }
252       }
253    }
254
255
256    // -------------------------------------------------------------- MBeanRegistration
257

258    /**
259     * Caches the provided <code>server</code> and <code>objName</code>.
260     */

261    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc objName)
262            throws Exception JavaDoc
263    {
264       this.server = server;
265
266       if (cacheObjectName == null)
267       {
268          if (objName == null)
269          {
270             // Calling this will create a value for cacheObjectName
271
getCacheObjectName();
272          }
273          else
274          {
275             cacheObjectName = objName.getCanonicalName();
276          }
277       }
278
279       return new ObjectName JavaDoc(cacheObjectName);
280    }
281
282    /**
283     * Registers the cache's interceptors, if {@link #getRegisterInterceptors()}
284     * is <code>true</code>.
285     */

286    public void postRegister(Boolean JavaDoc registrationDone)
287    {
288       if (Boolean.TRUE.equals(registrationDone))
289       {
290          log.debug("Registered in JMX under " + cacheObjectName);
291
292          if (cache != null)
293          {
294             try
295             {
296                registerInterceptors();
297             }
298             catch (Exception JavaDoc e)
299             {
300                log.error("Caught exception registering cache interceptors with JMX", e);
301             }
302          }
303       }
304    }
305
306    /**
307     * No-op.
308     */

309    public void preDeregister() throws Exception JavaDoc
310    {
311    }
312
313    /**
314     * Unregisters the interceptors, if {@link #getRegisterInterceptors()} is
315     * <code>true</code>.
316     */

317    public void postDeregister()
318    {
319       unregisterInterceptors();
320
321       server = null;
322    }
323
324    // --------------------------------------------------------------- Public methods
325

326    /**
327     * Sets the configuration that the underlying cache should use.
328     *
329     * @param config the configuration
330     * @throws IllegalArgumentException if <code>config</code> is <code>null</code>.
331     */

332    public void setConfiguration(Configuration config)
333    {
334       this.config = config;
335    }
336
337    /**
338     * Allows direct injection of the underlying cache.
339     *
340     * @param cache
341     */

342    public void setCache(Cache cache)
343    {
344       if (created)
345       {
346          throw new IllegalStateException JavaDoc("Cannot set underlying cache after call to create()");
347       }
348
349       // FIXME -- the only reason we need to cast here is to support printCacheDetails
350
this.cache = (CacheImpl) cache;
351       this.config = (cache == null ? null : cache.getConfiguration());
352    }
353
354    public String JavaDoc getCacheObjectName()
355    {
356       if (cacheObjectName == null)
357       {
358          cacheObjectName = JmxUtil.getCacheObjectName(config, CacheImpl.class.getName());
359       }
360       return cacheObjectName;
361    }
362
363    public void setCacheObjectName(String JavaDoc name) throws MalformedObjectNameException JavaDoc
364    {
365       if (name != null)
366       {
367          // test the name
368
new ObjectName JavaDoc(name);
369       }
370       this.cacheObjectName = name;
371    }
372
373    // ------------------------------------------------------ Protected Methods
374

375    public MBeanServer JavaDoc getMBeanServer()
376    {
377       return server;
378    }
379
380    // -------------------------------------------------------- Private methods
381

382 }
383
Popular Tags