KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > jmx > remote > server > MBeanServerRequestHandler


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 /* CVS information
25  * $Header: /cvs/glassfish/jmx-remote/rjmx-impl/src/java/com/sun/enterprise/admin/jmx/remote/server/MBeanServerRequestHandler.java,v 1.4 2005/12/25 04:26:36 tcfujii Exp $
26  * $Revision: 1.4 $
27  * $Date: 2005/12/25 04:26:36 $
28  */

29
30
31 package com.sun.enterprise.admin.jmx.remote.server;
32
33 import java.lang.reflect.Method JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37 import javax.management.remote.message.MBeanServerRequestMessage;
38 import javax.management.remote.message.MBeanServerResponseMessage;
39
40 import javax.management.MBeanServerConnection JavaDoc;
41 import javax.management.MBeanServer JavaDoc;
42 import javax.management.MBeanServerFactory JavaDoc;
43 import javax.security.auth.Subject JavaDoc;
44 /* BEGIN -- S1WS_MOD */
45 import javax.servlet.ServletConfig JavaDoc;
46
47 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration;
48 /* END -- S1WS_MOD */
49 import com.sun.enterprise.admin.jmx.remote.protocol.Version;
50 /* BEGIN -- S1WS_MOD */
51 import com.sun.enterprise.admin.jmx.remote.server.notification.ServerNotificationManager;
52 /* END -- S1WS_MOD */
53 import com.sun.enterprise.admin.jmx.remote.server.callers.MethodCallers;
54 import com.sun.enterprise.admin.jmx.remote.server.callers.MBeanServerConnectionMethodCaller;
55
56 import com.sun.enterprise.admin.jmx.remote.internal.Shifter;
57 /* BEGIN -- S1WS_MOD */
58
59 /* END -- S1WS_MOD */
60
61 /** Handles the request that is received from the servlet. Responsible for
62  * producing a guaranteed MBeanServerResponseMessage. Delegates the requests
63  * to the System MBean Server. Currently it searches for the MBeanServer after
64  * finding the list of MBeanServers.
65  * @author Kedar Mhaswade
66  * @since S1AS8.0
67  * @version 1.0
68  */

69
70 public class MBeanServerRequestHandler {
71     
72     /** Creates a new instance of MBeanServerResponseHandler */
73     
74     final private Set JavaDoc callers;
75
76     private static final Logger JavaDoc logger = Logger.getLogger(
77         DefaultConfiguration.JMXCONNECTOR_LOGGER);/*,
78         DefaultConfiguration.LOGGER_RESOURCE_BUNDLE_NAME );*/

79
80     private static Version sv;
81     static {
82             try {
83                     sv = (Version)Class.forName(Version.CLASS_NAME).newInstance();
84             }
85             catch(Exception JavaDoc e) {
86                     throw new RuntimeException JavaDoc(e);
87             }
88     }
89     
90     final private static ServerVersionMatcher matcher = ServerVersionMatcher.getMatcher();
91 /* BEGIN -- S1WS_MOD */
92     private ServerNotificationManager notifyMgr = null;
93
94 // public MBeanServerRequestHandler() {
95
public MBeanServerRequestHandler(ServletConfig JavaDoc cfg) {
96         MBeanServerConnection JavaDoc mbsc = getMBeanServerConnection(cfg);
97         notifyMgr = new ServerNotificationManager(mbsc);
98         notifyMgr.setBufSiz(cfg);
99 // callers = MethodCallers.callers(getMBeanServerConnection());
100
callers = MethodCallers.callers(mbsc, notifyMgr);
101 /* END -- S1WS_MOD */
102         logger.finer("Server Jmx Connector Version: " + sv.toString());
103     }
104
105 /* BEGIN -- S1WS_MOD */
106     public ServerNotificationManager getNotificationManager() {
107         return notifyMgr;
108     }
109     
110 // private MBeanServerConnection getMBeanServerConnection() {
111
private MBeanServerConnection JavaDoc getMBeanServerConnection(ServletConfig JavaDoc cfg) {
112         //first get through reflection.
113
String JavaDoc factoryClass = cfg.getInitParameter(DefaultConfiguration.MBEANSERVER_FACTORY_PROPERTY_NAME);
114 // final MBeanServerConnection mbsc = introspectMBS();
115
final MBeanServerConnection JavaDoc mbsc = introspectMBS(factoryClass);
116 /* END -- S1WS_MOD */
117         if (mbsc != null) {
118             return mbsc;
119         }
120         final java.util.ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
121         final MBeanServer JavaDoc systemMBS = (MBeanServer JavaDoc)servers.get(0);
122         return ((MBeanServerConnection JavaDoc)systemMBS);
123     }
124     
125     public MBeanServerResponseMessage handle(MBeanServerRequestMessage request0) {
126         assert (request0.getParams().length >= 1) : "Invalid Object Array"; //has to have at least one elem
127
MBeanServerResponseMessage response = null;
128         final Version cv = (Version)request0.getParams()[0];
129         logger.finer("Client Version = " + cv.toString());
130         if (! isCompatible(cv)) {
131             response = incompatibleVersionMessage(request0);
132             return ( response );
133         }
134         //should come here iff the version is compatible.
135
final MBeanServerRequestMessage request = removeVersion(request0);
136         boolean handled = false;
137         final Iterator JavaDoc iter = callers.iterator();
138         while (iter.hasNext()) {
139             final MBeanServerConnectionMethodCaller caller =
140             (MBeanServerConnectionMethodCaller)iter.next();
141             if (caller.canCall(request)) {
142                 response = caller.call(request);
143                 handled = true;
144                 break;
145             }
146         }
147         assert handled : "The request is not handled -- catastrophe";
148         return ( response );
149     }
150     
151     /** Returns the instance of S1AS MBeanServer. This information could well
152      * come from deployment descriptor, but for now I am hardcoding it -- 08/04/03.
153      * Returns a null if none could be found.
154      * @return MBeanServerConnection instance or null
155      */

156 /* BEGIN -- S1WS_MOD */
157 // private MBeanServerConnection introspectMBS() {
158
private MBeanServerConnection JavaDoc introspectMBS(String JavaDoc factoryClass) {
159 /* END -- S1WS_MOD */
160         MBeanServerConnection JavaDoc mbsc = null;
161 /* BEGIN -- S1WS_MOD */
162         //final String FACTORY_CLASS = "com.sun.enterprise.admin.common.MBeanServerFactory";
163
String JavaDoc FACTORY_CLASS = factoryClass;
164         if (factoryClass == null || factoryClass.trim().length() == 0)
165             FACTORY_CLASS = System.getProperty(DefaultConfiguration.MBEANSERVER_FACTORY_PROPERTY_NAME);
166 /* END -- S1WS_MOD */
167         final String JavaDoc FACTORY_METHOD = "getMBeanServer";
168 /* BEGIN -- S1WS_MOD */
169         if (FACTORY_CLASS == null || FACTORY_CLASS.trim().length() == 0)
170             return null;
171 /* END -- S1WS_MOD */
172         try {
173             logger.finer("Introspecting the MBeanServerConnection");
174             final Class JavaDoc c = Class.forName(FACTORY_CLASS); //loaded by the same CL
175
final Method JavaDoc m = c.getMethod(FACTORY_METHOD, null);
176             final Object JavaDoc r = m.invoke(c, null);
177             assert (r instanceof MBeanServer JavaDoc) : "Reflection does not return the correct type";
178             mbsc = (MBeanServerConnection JavaDoc)r;
179             logger.finer("Introspected the MBeanServerConnection successfully!!");
180         }
181         catch (Throwable JavaDoc t) {
182             logger.throwing(this.getClass().getName(), "introspectMBS", t);
183         }
184         return ( mbsc );
185     }
186     
187     private MBeanServerRequestMessage removeVersion(MBeanServerRequestMessage from) {
188         final int id = from.getMethodId();
189         final Subject JavaDoc s = from.getDelegationSubject();
190         final Shifter sh = new Shifter(from.getParams());
191         sh.shiftLeft();
192         final Object JavaDoc[] np = sh.state();
193         return ( new MBeanServerRequestMessage(id, np, s) );
194     }
195     private boolean isCompatible(Version cv) {
196         return ( matcher.match(cv, sv) );
197     }
198     
199     private MBeanServerResponseMessage incompatibleVersionMessage(MBeanServerRequestMessage r) {
200         //should come in this method only in case of incompatible versions
201
final int id = r.getMethodId();
202         final boolean isException = true;
203         final Version cv = (Version)r.getParams()[0];
204         Exception JavaDoc e = null;
205         assert (! isCompatible(cv)) : "No message for compatible versions";
206         
207         if (! matcher.majorCompatible(cv, sv)) {
208             e = majorVersionIncompatible(cv, sv);
209         }
210         else if (! matcher.minorCompatible(cv, sv)) {
211             e = minorVersionIncompatible(cv, sv);
212         }
213         else if (! matcher.upgradeCompatible(cv, sv)) {
214             e = upgradeIncompatible(cv, sv);
215         }
216         assert (e != null) : "Either minor/major version or upgrade data have to fail the match";
217         
218         return ( new MBeanServerResponseMessage(id, e, isException) );
219     }
220     
221     private Exception JavaDoc majorVersionIncompatible(Version cv, Version sv) {
222         //i18n
223
final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
224         sb.append("The major versions don't match: ").
225         append("Client Major Version = " + cv.getMajorVersion()).
226         append("Server Major Version = " + sv.getMajorVersion()).
227         append(" Upgrade the software accordingly");
228         
229         return ( new RuntimeException JavaDoc(sb.toString()) );
230     }
231     private Exception JavaDoc minorVersionIncompatible(Version cv, Version sv) {
232         //i18n
233
final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
234         sb.append("The minor versions don't match: ").
235         append("Client Minor Version = " + cv.getMinorVersion()).
236         append("Server Minor Version = " + sv.getMinorVersion()).
237         append(" Upgrade the software accordingly");
238         
239         return ( new RuntimeException JavaDoc(sb.toString()) );
240     }
241     private Exception JavaDoc upgradeIncompatible(Version cv, Version sv) {
242         //i18n
243
final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
244         sb.append("The upgrade data in versions does not match: ").
245         append("Client Upgrade Data = " + cv.toString()).
246         append("Server Upgrade Data = " + sv.toString()).
247         append(" Upgrade the software accordingly");
248         
249         return ( new RuntimeException JavaDoc(sb.toString()) );
250     }
251 }
252
Popular Tags