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 com.caucho.loader.DynamicClassLoader;
033: import com.caucho.loader.EnvironmentLocal;
034: import com.caucho.log.Log;
035: import com.caucho.util.L10N;
036:
037: import javax.management.MBeanServerDelegate;
038: import javax.management.MBeanServerDelegateMBean;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: /**
043: * JNDI object for the Resin mbean server.
044: */
045: public class EnvironmentMBeanServer extends AbstractMBeanServer {
046: private static final L10N L = new L10N(EnvironmentMBeanServer.class);
047: private static final Logger log = Log
048: .open(EnvironmentMBeanServer.class);
049:
050: private EnvironmentLocal<MBeanContext> _localContext = new EnvironmentLocal<MBeanContext>();
051:
052: private MBeanServerDelegate _globalDelegate;
053:
054: private MBeanContext _globalContext;
055:
056: /**
057: * Creates an MBeanServerProxy based on the context class loader.
058: */
059: public EnvironmentMBeanServer(String domain,
060: MBeanServerDelegate delegate) {
061: super (domain);
062:
063: if (Jmx.getMBeanServer() == null)
064: Jmx.setMBeanServer(this );
065:
066: ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
067:
068: MBeanContext context = new MBeanContext(this , systemLoader,
069: delegate, null);
070:
071: _globalContext = context;
072:
073: _localContext.setGlobal(context);
074:
075: try {
076: IntrospectionMBean mbean;
077: mbean = new IntrospectionMBean(delegate,
078: MBeanServerDelegateMBean.class);
079:
080: MBeanWrapper mbeanWrapper;
081: mbeanWrapper = new MBeanWrapper(context,
082: SERVER_DELEGATE_NAME, delegate, mbean);
083:
084: context.registerMBean(mbeanWrapper, SERVER_DELEGATE_NAME);
085: } catch (Exception e) {
086: log.log(Level.WARNING, e.toString(), e);
087: }
088: }
089:
090: /**
091: * Returns the local context.
092: */
093: protected MBeanContext createContext(ClassLoader loader) {
094: synchronized (_localContext) {
095: MBeanContext context = _localContext.getLevel(loader);
096:
097: if (context == null) {
098: if (loader instanceof DynamicClassLoader
099: && ((DynamicClassLoader) loader).isDestroyed())
100: throw new IllegalStateException(L.l(
101: "JMX context {0} has been closed.", loader));
102:
103: MBeanServerDelegate delegate;
104: delegate = new MBeanServerDelegateImpl("Resin-JMX");
105:
106: context = new MBeanContext(this , loader, delegate,
107: _globalContext);
108:
109: MBeanContext parent = null;
110:
111: if (loader != null)
112: parent = createContext(loader.getParent());
113:
114: if (parent != null)
115: context.setProperties(parent.copyProperties());
116:
117: _localContext.set(context, loader);
118:
119: try {
120: IntrospectionMBean mbean;
121: mbean = new IntrospectionMBean(delegate,
122: MBeanServerDelegateMBean.class);
123:
124: MBeanWrapper mbeanWrapper;
125: mbeanWrapper = new MBeanWrapper(context,
126: SERVER_DELEGATE_NAME, delegate, mbean);
127:
128: context.registerMBean(mbeanWrapper,
129: SERVER_DELEGATE_NAME);
130: } catch (Exception e) {
131: log.log(Level.WARNING, e.toString(), e);
132: }
133: }
134:
135: return context;
136: }
137: }
138:
139: /**
140: * Returns the local context.
141: */
142: protected MBeanContext getExistingContext(ClassLoader loader) {
143: if (loader == null)
144: return _localContext
145: .get(ClassLoader.getSystemClassLoader());
146:
147: synchronized (_localContext) {
148: return _localContext.getLevel(loader);
149: }
150: }
151:
152: /**
153: * Returns the local context.
154: */
155: protected MBeanContext getContext(ClassLoader loader) {
156: return _localContext.get(loader);
157: }
158:
159: /**
160: * Returns the local context.
161: */
162: protected void removeContext(MBeanContext context,
163: ClassLoader loader) {
164: if (_localContext.get(loader) == context)
165: _localContext.remove(loader);
166: }
167:
168: /**
169: * Returns the string form.
170: */
171: public String toString() {
172: return "EnvironmentMBeanServer[]";
173: }
174: }
|