KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jmx > MBeanHandle


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jmx;
31
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.MBeanServerInvocationHandler JavaDoc;
34 import javax.management.ObjectInstance JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import java.io.InvalidObjectException JavaDoc;
38 import java.io.NotActiveException JavaDoc;
39 import java.io.ObjectStreamException JavaDoc;
40 import java.lang.ref.SoftReference JavaDoc;
41 import java.lang.reflect.Method JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * Serialization handle for mbeans.
47  */

48 public class MBeanHandle implements java.io.Serializable JavaDoc {
49   private static final Logger JavaDoc log
50     = Logger.getLogger(MBeanHandle.class.getName());
51   
52   private static SoftReference JavaDoc<MBeanServer JavaDoc> _globalMBeanServer;
53
54   private final ObjectName JavaDoc _name;
55
56   public MBeanHandle(ObjectName JavaDoc name)
57   {
58     _name = name;
59   }
60
61   /**
62    * Convert the handle to a proxy for deserialization.
63    */

64   private Object JavaDoc readResolve()
65     throws ObjectStreamException JavaDoc
66   {
67     try {
68       MBeanServer JavaDoc server = getMBeanServer();
69
70       ObjectInstance JavaDoc instance = server.getObjectInstance(_name);
71
72       String JavaDoc className = instance.getClassName();
73
74       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
75       
76       Class JavaDoc cl = Class.forName(className, false, loader);
77
78       return MBeanServerInvocationHandler.newProxyInstance(server,
79                                _name,
80                                cl,
81                                false);
82     } catch (ObjectStreamException JavaDoc e) {
83       e.printStackTrace();
84       
85       throw e;
86     } catch (Exception JavaDoc e) {
87       e.printStackTrace();
88       
89       log.log(Level.FINE, e.toString(), e);
90       
91       throw new InvalidObjectException JavaDoc(e.toString());
92     }
93   }
94
95   private static MBeanServer JavaDoc getMBeanServer()
96     throws ObjectStreamException JavaDoc
97   {
98     MBeanServer JavaDoc server = null;
99
100     if (_globalMBeanServer != null)
101       server = _globalMBeanServer.get();
102
103     if (server != null)
104       return server;
105
106     try {
107       InitialContext JavaDoc ic = new InitialContext JavaDoc();
108       server = (MBeanServer JavaDoc) ic.lookup("java:comp/env/jmx/GlobalMBeanServer");
109
110       if (server != null) {
111     _globalMBeanServer = new SoftReference JavaDoc<MBeanServer JavaDoc>(server);
112
113     return server;
114       }
115     } catch (Throwable JavaDoc e) {
116       log.log(Level.FINER, e.toString(), e);
117     }
118
119     try {
120       Class JavaDoc cl = Class.forName("java.lang.Management.ManagementFactory");
121
122       Method JavaDoc method = cl.getMethod("getPlatformMBeanServer", new Class JavaDoc[0]);
123
124       server = (MBeanServer JavaDoc) method.invoke(null, new Object JavaDoc[0]);
125
126       if (server != null) {
127     _globalMBeanServer = new SoftReference JavaDoc<MBeanServer JavaDoc>(server);
128
129     return server;
130       }
131     } catch (Throwable JavaDoc e) {
132     }
133
134     log.warning("Can't load global mbean server for proxy deserialization.");
135
136     throw new NotActiveException JavaDoc("Can't load global mbean server for proxy deserialization.");
137   }
138 }
139
140
Popular Tags