001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jmx;
031:
032: import javax.management.MBeanServer;
033: import javax.management.MBeanServerInvocationHandler;
034: import javax.management.ObjectInstance;
035: import javax.management.ObjectName;
036: import javax.naming.InitialContext;
037: import java.io.InvalidObjectException;
038: import java.io.NotActiveException;
039: import java.io.ObjectStreamException;
040: import java.lang.ref.SoftReference;
041: import java.lang.reflect.Method;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: /**
046: * Serialization handle for mbeans.
047: */
048: public class MBeanHandle implements java.io.Serializable {
049: private static final Logger log = Logger
050: .getLogger(MBeanHandle.class.getName());
051:
052: private static SoftReference<MBeanServer> _globalMBeanServer;
053:
054: private final ObjectName _name;
055:
056: public MBeanHandle(ObjectName name) {
057: _name = name;
058: }
059:
060: /**
061: * Convert the handle to a proxy for deserialization.
062: */
063: private Object readResolve() throws ObjectStreamException {
064: try {
065: MBeanServer server = getMBeanServer();
066:
067: ObjectInstance instance = server.getObjectInstance(_name);
068:
069: String className = instance.getClassName();
070:
071: ClassLoader loader = Thread.currentThread()
072: .getContextClassLoader();
073:
074: Class cl = Class.forName(className, false, loader);
075:
076: return MBeanServerInvocationHandler.newProxyInstance(
077: server, _name, cl, false);
078: } catch (ObjectStreamException e) {
079: e.printStackTrace();
080:
081: throw e;
082: } catch (Exception e) {
083: e.printStackTrace();
084:
085: log.log(Level.FINE, e.toString(), e);
086:
087: throw new InvalidObjectException(e.toString());
088: }
089: }
090:
091: private static MBeanServer getMBeanServer()
092: throws ObjectStreamException {
093: MBeanServer server = null;
094:
095: if (_globalMBeanServer != null)
096: server = _globalMBeanServer.get();
097:
098: if (server != null)
099: return server;
100:
101: try {
102: InitialContext ic = new InitialContext();
103: server = (MBeanServer) ic
104: .lookup("java:comp/env/jmx/GlobalMBeanServer");
105:
106: if (server != null) {
107: _globalMBeanServer = new SoftReference<MBeanServer>(
108: server);
109:
110: return server;
111: }
112: } catch (Throwable e) {
113: log.log(Level.FINER, e.toString(), e);
114: }
115:
116: try {
117: Class cl = Class
118: .forName("java.lang.Management.ManagementFactory");
119:
120: Method method = cl.getMethod("getPlatformMBeanServer",
121: new Class[0]);
122:
123: server = (MBeanServer) method.invoke(null, new Object[0]);
124:
125: if (server != null) {
126: _globalMBeanServer = new SoftReference<MBeanServer>(
127: server);
128:
129: return server;
130: }
131: } catch (Throwable e) {
132: }
133:
134: log
135: .warning("Can't load global mbean server for proxy deserialization.");
136:
137: throw new NotActiveException(
138: "Can't load global mbean server for proxy deserialization.");
139: }
140: }
|