KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > AppServerMBeanServerBuilder


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * MBeanServerBuilder.java
26  *
27  * Created on August 22, 2003, 3:25 PM
28  */

29
30 package com.sun.enterprise.admin.server.core.jmx;
31
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.MBeanServerDelegate JavaDoc;
34 import javax.management.MBeanServerBuilder JavaDoc;
35
36 import java.util.logging.Logger JavaDoc;
37 import com.sun.enterprise.admin.common.constant.AdminConstants;
38 import java.util.logging.Level JavaDoc;
39
40 /**
41  * AppServer MBSBuilder for PE set as the value for javax.management.initial.builder
42  * in the environment. This builder extends from javax.management.MBeanServerBuilder
43  * and creates MBS with app server interceptors based on a flag. This flag can only be
44  * turned in this package and is called by the AppServerMBeanServerFactory.
45  *
46  * @author sridatta
47  */

48 public class AppServerMBeanServerBuilder extends javax.management.MBeanServerBuilder JavaDoc {
49     
50     private static final MBeanServerBuilder JavaDoc defaultBuilder = new MBeanServerBuilder JavaDoc();
51     
52     protected static final Logger JavaDoc _logger =
53             Logger.getLogger(AdminConstants.kLoggerName);
54     
55     /*
56      * indicates whether to instantiate App Server MBS (with interceptors)
57      */

58     private static boolean createAppServerMBeanServer = false;
59     
60     /** Creates a new instance of MBeanServerBuilder */
61     public AppServerMBeanServerBuilder() {
62     }
63       
64     /**
65      * This method creates a new MBeanServer implementation object. When creating a new
66      * MBeanServer the MBeanServerFactory first calls newMBeanServerDelegate() in
67      * order to obtain a new MBeanServerDelegate for the new MBeanServer. Then it
68      * calls newMBeanServer(defaultDomain,outer,delegate) passing the delegate that
69      * should be used by the MBeanServer implementation.
70      * Note that the passed delegate might not be directly the MBeanServerDelegate that
71      * was returned by this implementation. It could be, for instance, a new object
72      * wrapping the previously returned delegate.
73      *
74      * The outer parameter is a pointer to the MBeanServer that should be passed
75      * to the MBeanRegistration interface when registering MBeans inside the
76      * MBeanServer. If outer is null, then the MBeanServer implementation must
77      * use its own this reference when invoking the MBeanRegistration interface.
78      *
79      * This makes it possible for a MBeanServer implementation to wrap another MBeanServer
80      * implementation, in order to implement, e.g, security checks, or to prevent
81      * access to the actual MBeanServer implementation by returning a pointer to a wrapping object.
82      *
83      * Parameters:
84      * defaultDomain - Default domain of the new MBeanServer.
85      * outer - A pointer to the MBeanServer object that must be passed to the MBeans when invoking their MBeanRegistration interface.
86      * delegate - A pointer to the MBeanServerDelegate associated with the new MBeanServer. The new MBeanServer must register this MBean in its MBean repository.
87      * Returns:
88      * A new private implementation of an MBeanServer.
89      *
90      *
91      * Note that this method is sychronized on the class for the
92      * case when user requests an MBS during creation on Sun's mbs.
93      * This can seldom happen but needs to be a safe code.
94      *
95      */

96      
97      public MBeanServer JavaDoc newMBeanServer(String JavaDoc defaultDomain,
98                                     MBeanServer JavaDoc outer,
99                                     MBeanServerDelegate JavaDoc delegate) {
100                                                                
101         synchronized (AppServerMBeanServerBuilder.class) {
102              if (!createAppServerMBeanServer) {
103                  _logger.log(Level.FINE,
104                     "Creating default JMX MBeanServer in AppServerMBeanServerBuilder");
105                  return defaultBuilder.newMBeanServer(defaultDomain,
106                                                       outer,
107                                                       delegate);
108              } else {
109                  _logger.log(Level.FINEST,
110                     "Creating MBeanServer with appserver interceptors in AppServerMBeanServerBuilder");
111                 return newAppServerMBeanServer(defaultDomain, delegate);
112              }
113         }
114      }
115     
116      /**
117       * creates a jmx MBeanServer
118       * creates a AppServerInterceptor and sets the jmx mbean server
119       * to it. It then wraps any additional interceptors (nothing for PE)
120       *
121       * Note that the "outer" is SunoneInterceptor and not the additional interceptors
122       * added in addInterceptor. (commented out for now for testing purposes. Need
123       * to comeback to this. FIXME.)
124       *
125       * returns AppServerMBS
126       *
127       */

128      protected MBeanServer JavaDoc newAppServerMBeanServer(String JavaDoc defaultDomain,
129                                                 MBeanServerDelegate JavaDoc delegate) {
130          try {
131              // To allow outer to be set to the extra interceptor
132
// It does not affect PE in anyway.
133
MBeanServer JavaDoc appserverMBS = new SunoneInterceptor();
134               MBeanServer JavaDoc newAppserverMBS = addInterceptor(appserverMBS);
135          MBeanServer JavaDoc jmxMBS =
136                     defaultBuilder.newMBeanServer(defaultDomain,
137                       newAppserverMBS,
138                       delegate);
139             ((SunoneInterceptor) appserverMBS).setJmxMBeanServer(jmxMBS);
140             _logger.log(Level.FINEST,
141                 "Created MBeanServer in AppServerMBeanServerBuilder");
142             return newAppserverMBS;
143             
144          } catch (InitException e) {
145              _logger.log(Level.FINE, "InitException while creating MBeanServer", e);
146              throw new RuntimeException JavaDoc(e.getMessage(), e.getCause());
147          }
148      }
149      
150     /**
151      * This method can be used to add interceptors in different versions
152      * of the product. For PE, additional interceptor is not required
153      */

154     protected MBeanServer JavaDoc addInterceptor(MBeanServer JavaDoc mbs) {
155     // no additional interceptors in PE.
156
_logger.log(Level.FINEST,
157            "No additional interceptors added in" +
158            "addInterceptor() in AppServerMBeanServerBuilder");
159     return mbs;
160     }
161      
162     /**
163      * This method creates a new MBeanServerDelegate for a new MBeanServer.
164      * When creating a new MBeanServer the MBeanServerFactory first calls
165      * this method in order to create a new MBeanServerDelegate.
166      * Then it calls newMBeanServer(defaultDomain,outer,delegate) passing the delegate
167      * that should be used by the MBeanServer implementation.
168      * Note that the passed delegate might not be directly the MBeanServerDelegate that
169      * was returned by this method. It could be, for instance, a new object wrapping
170      * the previously returned object.
171      *
172      * return the MBeanServerDelegate from jmx defaultBuilder
173      */

174     public MBeanServerDelegate JavaDoc newMBeanServerDelegate() {
175         return defaultBuilder.newMBeanServerDelegate();
176      }
177     
178     /**
179      * This method is used to set the state whether to create jmx mbs
180      * or AppServer mbs. This method is package specific.
181      */

182     static void enableAppServerMBeanServer(boolean flag) {
183         createAppServerMBeanServer = flag;
184     }
185     
186     /**
187      * whether to create AppServer or just the vanilla jmx ri mbs
188      * @return boolean
189      */

190     static protected boolean createAppServerMBeanServer() {
191         return createAppServerMBeanServer;
192     }
193     
194     /**
195      * jmx ri default builder
196      */

197     static protected MBeanServerBuilder JavaDoc getDefaultBuilder() {
198         return defaultBuilder;
199     }
200 }
Popular Tags