KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.caucho.loader.DynamicClassLoader;
33 import com.caucho.loader.EnvironmentLocal;
34 import com.caucho.log.Log;
35 import com.caucho.util.L10N;
36
37 import javax.management.MBeanServerDelegate JavaDoc;
38 import javax.management.MBeanServerDelegateMBean JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * JNDI object for the Resin mbean server.
44  */

45 public class EnvironmentMBeanServer extends AbstractMBeanServer {
46   private static final L10N L = new L10N(EnvironmentMBeanServer.class);
47   private static final Logger JavaDoc log = Log.open(EnvironmentMBeanServer.class);
48
49   private EnvironmentLocal<MBeanContext> _localContext =
50     new EnvironmentLocal<MBeanContext>();
51   
52   private MBeanServerDelegate JavaDoc _globalDelegate;
53   
54   /**
55    * Creates an MBeanServerProxy based on the context class loader.
56    */

57   public EnvironmentMBeanServer(String JavaDoc domain, MBeanServerDelegate JavaDoc delegate)
58   {
59     super(domain);
60     
61     if (Jmx.getMBeanServer() == null)
62       Jmx.setMBeanServer(this);
63
64     ClassLoader JavaDoc systemLoader = ClassLoader.getSystemClassLoader();
65
66     MBeanContext context = new MBeanContext(this, systemLoader, delegate);
67
68     _localContext.set(context, systemLoader);
69     
70     try {
71       IntrospectionMBean mbean;
72       mbean = new IntrospectionMBean(delegate, MBeanServerDelegateMBean JavaDoc.class);
73
74       MBeanWrapper mbeanWrapper;
75       mbeanWrapper = new MBeanWrapper(context, SERVER_DELEGATE_NAME,
76                       delegate, mbean);
77
78       context.registerMBean(mbeanWrapper, SERVER_DELEGATE_NAME);
79     } catch (Exception JavaDoc e) {
80       log.log(Level.WARNING, e.toString(), e);
81     }
82   }
83
84   /**
85    * Returns the local context.
86    */

87   protected MBeanContext getContext(ClassLoader JavaDoc loader)
88   {
89     synchronized (_localContext) {
90       MBeanContext context = _localContext.getLevel(loader);
91
92       if (context == null) {
93     if (loader instanceof DynamicClassLoader
94         && ((DynamicClassLoader) loader).isDestroyed())
95       throw new IllegalStateException JavaDoc(L.l("JMX context {0} has been closed.",
96                           loader));
97     
98     MBeanServerDelegate JavaDoc delegate;
99     delegate = new MBeanServerDelegateImpl("Resin-JMX");
100
101     context = new MBeanContext(this, loader, delegate);
102
103     MBeanContext parent = null;
104
105     if (loader != null)
106       parent = getContext(loader.getParent());
107
108     if (parent != null)
109       context.setProperties(parent.copyProperties());
110
111     _localContext.set(context, loader);
112     
113     try {
114       IntrospectionMBean mbean;
115       mbean = new IntrospectionMBean(delegate, MBeanServerDelegateMBean JavaDoc.class);
116
117       MBeanWrapper mbeanWrapper;
118       mbeanWrapper = new MBeanWrapper(context, SERVER_DELEGATE_NAME,
119                       delegate, mbean);
120
121       context.registerMBean(mbeanWrapper, SERVER_DELEGATE_NAME);
122     } catch (Exception JavaDoc e) {
123       log.log(Level.WARNING, e.toString(), e);
124     }
125       }
126
127       return context;
128     }
129   }
130
131   /**
132    * Returns the local context.
133    */

134   protected MBeanContext getExistingContext(ClassLoader JavaDoc loader)
135   {
136     if (loader == null)
137       return _localContext.get(ClassLoader.getSystemClassLoader());
138     
139     synchronized (_localContext) {
140       return _localContext.getLevel(loader);
141     }
142   }
143
144   /**
145    * Returns the local context.
146    */

147   protected void removeContext(MBeanContext context, ClassLoader JavaDoc loader)
148   {
149     if (_localContext.get(loader) == context)
150       _localContext.remove(loader);
151   }
152
153   /**
154    * Returns the string form.
155    */

156   public String JavaDoc toString()
157   {
158     return "EnvironmentMBeanServer[]";
159   }
160 }
161
Popular Tags