KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > agent > MEJBHelper


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 package com.sun.enterprise.management.agent;
25
26 import java.security.*;
27 import java.rmi.RemoteException JavaDoc;
28 import java.util.*;
29 import java.io.ObjectInputStream JavaDoc;
30 import javax.management.*;
31 import javax.management.j2ee.*;
32 import javax.naming.*;
33 import com.sun.enterprise.management.util.J2EEModuleUtil;
34 // imports for getting MBeanServerConnection
35
import com.sun.enterprise.admin.meta.MBeanRegistryFactory;
36 import com.sun.enterprise.admin.AdminContext;
37 import com.sun.enterprise.config.ConfigContext;
38 import com.sun.enterprise.config.serverbeans.ServerHelper;
39 import com.sun.enterprise.config.serverbeans.Server;
40 import com.sun.enterprise.config.ConfigException;
41 import java.util.logging.Logger JavaDoc;
42 import java.util.logging.Level JavaDoc;
43
44
45 /**
46  * @ejbHome <{com.sun.enterprise.management.agent.MEJBHome}>
47  * @ejbRemote <{com.sun.enterprise.management.agent.MEJB}>
48  *
49  * @author Sreenivas Munnangi
50  */

51 public class MEJBHelper {
52
53     // variables
54
private MBeanServerConnection server;
55     private static MEJBHelper mejbHelper = null;
56     private ListenerRegistry listenerRegistry = null;
57     static Logger JavaDoc _logger = MBeanRegistryFactory.getAdminContext().getAdminLogger();
58
59     // default constructor
60
private MEJBHelper() {
61
62     /**
63      * This code has been added so that
64      * any access to MejbApp will be routed to DAS' MEjbApp transparently
65      *
66      * If the instance is DAS then continue to use the same mBean server
67      *
68      * If the instance is not DAS, meaning now this is running in a server instance
69      * then get jmx connector to the corresponding DAS and obtain
70      * the mBeanServerConnection. Now on use this DAS' mBeanServerConnection as
71      * wherever mBeanServer is referred.
72      */

73
74     AdminContext adminContext = MBeanRegistryFactory.getAdminContext();
75     ConfigContext configContext = adminContext.getRuntimeConfigContext();
76     String JavaDoc serverName = adminContext.getServerName();
77
78         try {
79       // check if DAS
80
boolean isDas = ServerHelper.isDAS(configContext, serverName);
81       if (isDas) {
82         // DAS instance
83
ArrayList mbservers = (ArrayList) AccessController.doPrivileged(
84         new PrivilegedAction() {
85                     public java.lang.Object JavaDoc run() {
86                         return MBeanServerFactory.findMBeanServer(null);
87                     }
88                 });
89             if (mbservers.isEmpty()) {
90                 server = null;
91             }
92             else {
93                 server = (MBeanServer)mbservers.get(0);
94             }
95       } else {
96         // non DAS instance
97
server = null;
98         Server dasSrv = ServerHelper.getDAS(configContext);
99         String JavaDoc dasName = dasSrv.getName();
100         MBeanServerConnection mbsc = ServerHelper.connect(configContext, dasName);
101         if (mbsc != null) {
102         server = mbsc;
103         }
104       }
105     } catch (Exception JavaDoc e) {
106               _logger.log(Level.WARNING, "MEJBHelper.exception_determining_mbean_server_connection");
107     }
108     }
109
110     public static MEJBHelper getMEJBHelper(){
111         if(mejbHelper == null){
112             mejbHelper = new MEJBHelper();
113         }
114         return mejbHelper;
115     }
116
117     /**
118      * Gets the names of managed objects controlled by the MEJB. This method
119      * enables any of the following to be obtained: The names of all managed objects,
120      * the names of a set of managed objects specified by pattern matching on the
121      * <CODE>ObjectName</CODE> and/or a Query expression, a specific managed object name (equivalent to
122      * testing whether an managed object is registered). When the object name is
123      * null or no domain and key properties are specified, all objects are selected (and filtered if a
124      * query is specified). It returns the set of ObjectNames for the managed objects selected.
125      * @param name The object name pattern identifying the managed objects to be retrieved. If
126      * null or no domain and key properties are specified, all the managed objects registered will be retrieved.
127      * @param query The query expression to be applied for selecting managed objects. If null
128      * no query expression will be applied for selecting managed objects.
129      * @return A set containing the ObjectNames for the managed objects selected.
130      * If no managed object satisfies the query, an empty list is returned.
131      */

132     
133     public Set queryNames(ObjectName name, QueryExp query) throws Exception JavaDoc {
134         return server.queryNames(name, query);
135     }
136
137     
138     /**
139      * Checks whether an MBean, identified by its object name, is already registered with the MBean server.
140      * @param name The object name of the MBean to be checked.
141      * @return True if the MBean is already registered in the MBean server, false otherwise.
142      */

143     public boolean isRegistered(ObjectName name) throws Exception JavaDoc {
144         return server.isRegistered(name);
145     }
146
147     
148     
149     /** Returns the number of MBeans registered in the MBean server. */
150     public Integer JavaDoc getMBeanCount() throws java.io.IOException JavaDoc, Exception JavaDoc {
151         return server.getMBeanCount();
152     }
153      
154     
155     /**
156      * This method discovers the attributes and operations that an MBean exposes for management.
157      * @param name The name of the MBean to analyze
158      * @return An instance of <CODE>MBeanInfo</CODE> allowing the retrieval of all attributes and operations of this MBean.
159      * @exception IntrospectionException An exception occurs during introspection.
160      * @exception InstanceNotFoundException The MBean specified is not found.
161      * @exception ReflectionException An exception occurred when trying to invoke the getMBeanInfo of a Dynamic MBean.
162      */

163     public MBeanInfo getMBeanInfo(ObjectName name)
164     throws javax.management.InstanceNotFoundException JavaDoc,
165             javax.management.IntrospectionException JavaDoc,
166         javax.management.ReflectionException JavaDoc,
167         java.io.IOException JavaDoc,
168         RemoteException JavaDoc {
169         return server.getMBeanInfo(name);
170     }
171      
172
173     /**
174      * Gets the value of a specific attribute of a named MBean. The MBean is identified by its object name.
175      * @param name The object name of the MBean from which the attribute is to be retrieved.
176      * @param attribute A String specifying the name of the attribute to be retrieved.
177      * @return The value of the retrieved attribute.
178      * @exception AttributeNotFoundException The attribute specified is not accessible in the MBean.
179      * @exception MBeanException Wraps an exception thrown by the MBean's getter.
180      * @exception InstanceNotFoundException The MBean specified is not registered in the MBean server.
181      * @exception ReflectionException Wraps a <CODE>java.lang.Exception</CODE> thrown when trying to invoke the setter.
182      * @exception RuntimeOperationsException Wraps a <CODE>java.lang.IllegalArgumentException</CODE>: The object name in
183      * parameter is null or the attribute in parameter is null.
184      */

185     public Object JavaDoc getAttribute(ObjectName name, String JavaDoc attribute) throws MBeanException,
186         javax.management.AttributeNotFoundException JavaDoc, javax.management.InstanceNotFoundException JavaDoc,
187         javax.management.ReflectionException JavaDoc, java.io.IOException JavaDoc, RemoteException JavaDoc {
188             return server.getAttribute(name, attribute);
189     }
190
191     /**
192      * Enables the values of several attributes of a named MBean. The MBean is identified by its object name.
193      * @param name The object name of the MBean from which the attributes are retrieved.
194      * @param attributes A list of the attributes to be retrieved.
195      * @return The list of the retrieved attributes.
196      * @exception InstanceNotFoundException The MBean specified is not registered in the MBean server.
197      * @exception ReflectionException An exception occurred when trying to invoke the getAttributes method of a Dynamic MBean.
198      * @exception RuntimeOperationsException Wrap a <CODE>java.lang.IllegalArgumentException</CODE>: The object name in
199      * parameter is null or attributes in parameter is null.
200      */

201     public javax.management.AttributeList JavaDoc getAttributes(ObjectName name, String JavaDoc[] attributes)
202         throws javax.management.InstanceNotFoundException JavaDoc, javax.management.ReflectionException JavaDoc,
203     java.io.IOException JavaDoc, RemoteException JavaDoc {
204        return server.getAttributes(name, attributes);
205     }
206
207     /**
208      * Sets the value of a specific attribute of a named MBean. The MBean is identified by its object name.
209      * @param name The name of the MBean within which the attribute is to be set.
210      * @param attribute The identification of the attribute to be set and the value it is to be set to.
211      * @return The value of the attribute that has been set.
212      * @exception InstanceNotFoundException The MBean specified is not registered in the MBean server.
213      * @exception AttributeNotFoundException The attribute specified is not accessible in the MBean.
214      * @exception InvalidAttributeValueException The value specified for the attribute is not valid.
215      * @exception MBeanException Wraps an exception thrown by the MBean's setter.
216      * @exception ReflectionException Wraps a <CODE>java.lang.Exception</CODE> thrown when trying to invoke the setter.
217      * @exception RuntimeOperationsException Wraps a <CODE>java.lang.IllegalArgumentException</CODE>: The object name in
218      * parameter is null or the attribute in parameter is null.
219      */

220     public void setAttribute(ObjectName name, javax.management.Attribute JavaDoc attribute)
221         throws javax.management.InstanceNotFoundException JavaDoc, javax.management.AttributeNotFoundException JavaDoc,
222         javax.management.InvalidAttributeValueException JavaDoc, MBeanException,
223         javax.management.ReflectionException JavaDoc, java.io.IOException JavaDoc, RemoteException JavaDoc {
224         server.setAttribute(name, attribute);
225     }
226
227     /**
228      * Sets the values of several attributes of a named MBean. The MBean is identified by its object name.
229      * @param name The object name of the MBean within which the attributes are to be set.
230      * @param attributes A list of attributes: The identification of the
231      * attributes to be set and the values they are to be set to.
232      * @return The list of attributes that were set, with their new values.
233      * @exception InstanceNotFoundException The MBean specified is not registered in the MBean server.
234      * @exception ReflectionException An exception occurred when trying to invoke the getAttributes method of a Dynamic MBean.
235      * @exception RuntimeOperationsException Wraps a <CODE>java.lang.IllegalArgumentException</CODE>: The object name in
236      * parameter is null or attributes in parameter is null.
237      */

238     public javax.management.AttributeList JavaDoc setAttributes(ObjectName name, javax.management.AttributeList JavaDoc attributes)
239         throws javax.management.InstanceNotFoundException JavaDoc, javax.management.ReflectionException JavaDoc,
240     java.io.IOException JavaDoc, RemoteException JavaDoc {
241         return server.setAttributes(name, attributes);
242     }
243
244     /**
245      * Invokes an operation on an MBean.
246      * @param name The object name of the MBean on which the method is to be invoked.
247      * @param operationName The name of the operation to be invoked.
248      * @param params An array containing the parameters to be set when the operation is invoked
249      * @param signature An array containing the signature of the operation. The class objects will
250      * be loaded using the same class loader as the one used for loading the MBean on which the operation was invoked.
251      * @return The object returned by the operation, which represents the result ofinvoking the operation
252      * on the MBean specified.
253      * @exception InstanceNotFoundException The MBean specified is not registered in the MBean server.
254      * @exception MBeanException Wraps an exception thrown by the MBean's invoked method.
255      * @exception ReflectionException Wraps a <CODE>java.lang.Exception</CODE> thrown while trying to invoke the method.
256      */

257     public Object JavaDoc invoke(ObjectName name, String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature)
258         throws javax.management.InstanceNotFoundException JavaDoc, MBeanException,
259         java.io.IOException JavaDoc, javax.management.ReflectionException JavaDoc, RemoteException JavaDoc {
260         return server.invoke(name, operationName, params, signature);
261     }
262
263     /**
264      * Returns the default domain used for naming the managed object.
265      * The default domain name is used as the domain part in the ObjectName
266      * of managed objects if no domain is specified by the user.
267      */

268     public String JavaDoc getDefaultDomain() throws java.io.IOException JavaDoc {
269         return server.getDefaultDomain();
270     }
271
272     /*
273      * returns the ListenerRegistry implementation for this MEJB
274      */

275     public ListenerRegistration getListenerRegistry() {
276         if (listenerRegistry == null) {
277             try {
278                 listenerRegistry = new ListenerRegistry(java.net.InetAddress.getLocalHost().getHostAddress());
279             } catch (java.net.UnknownHostException JavaDoc e) {
280                 listenerRegistry = new ListenerRegistry(J2EEModuleUtil.getDomainName());
281             }
282         }
283         return listenerRegistry;
284     }
285
286 }
287
Popular Tags