KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > manager > JMXProxyServlet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.manager;
20
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.management.MBeanInfo JavaDoc;
29 import javax.management.MBeanAttributeInfo JavaDoc;
30 import javax.management.Attribute JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServlet JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.apache.tomcat.util.modeler.Registry;
37
38 /**
39  * This servlet will dump JMX attributes in a simple format
40  * and implement proxy services for modeler.
41  *
42  * @author Costin Manolache
43  */

44 public class JMXProxyServlet extends HttpServlet JavaDoc {
45     // ----------------------------------------------------- Instance Variables
46

47     /**
48      * MBean server.
49      */

50     protected MBeanServer JavaDoc mBeanServer = null;
51     protected Registry registry;
52     // --------------------------------------------------------- Public Methods
53

54
55     /**
56      * Initialize this servlet.
57      */

58     public void init() throws ServletException JavaDoc {
59         // Retrieve the MBean server
60
registry = Registry.getRegistry(null, null);
61         mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
62     }
63
64
65     /**
66      * Process a GET request for the specified resource.
67      *
68      * @param request The servlet request we are processing
69      * @param response The servlet response we are creating
70      *
71      * @exception IOException if an input/output error occurs
72      * @exception ServletException if a servlet-specified error occurs
73      */

74     public void doGet(HttpServletRequest JavaDoc request,
75                       HttpServletResponse JavaDoc response)
76         throws IOException JavaDoc, ServletException JavaDoc
77     {
78
79         response.setContentType("text/plain");
80
81         PrintWriter JavaDoc writer = response.getWriter();
82         String JavaDoc qryString= request.getQueryString();
83
84         if( mBeanServer==null ) {
85             writer.println("Error - No mbean server");
86             return;
87         }
88
89         String JavaDoc qry=request.getParameter("set");
90         if( qry!= null ) {
91             String JavaDoc name=request.getParameter("att");
92             String JavaDoc val=request.getParameter("val");
93
94             setAttribute( writer, qry, name, val );
95             return;
96         }
97         qry=request.getParameter("get");
98         if( qry!= null ) {
99             String JavaDoc name=request.getParameter("att");
100             getAttribute( writer, qry, name );
101             return;
102         }
103         qry=request.getParameter("qry");
104         if( qry == null ) {
105             qry = "*:*";
106         }
107
108         listBeans( writer, qry );
109
110     }
111
112     public void getAttribute(PrintWriter JavaDoc writer, String JavaDoc onameStr, String JavaDoc att) {
113         try {
114             ObjectName JavaDoc oname = new ObjectName JavaDoc(onameStr);
115             Object JavaDoc value = mBeanServer.getAttribute(oname, att);
116             writer.println("OK - Attribute get '" + onameStr + "' - " + att + "= " + value.toString() );
117         } catch (Exception JavaDoc ex) {
118             writer.println("Error - " + ex.toString());
119         }
120     }
121
122     public void setAttribute( PrintWriter JavaDoc writer,
123                               String JavaDoc onameStr, String JavaDoc att, String JavaDoc val )
124     {
125         try {
126             ObjectName JavaDoc oname=new ObjectName JavaDoc( onameStr );
127             String JavaDoc type=registry.getType(oname, att);
128             Object JavaDoc valueObj=registry.convertValue(type, val );
129             mBeanServer.setAttribute( oname, new Attribute JavaDoc(att, valueObj));
130             writer.println("OK - Attribute set");
131         } catch( Exception JavaDoc ex ) {
132             writer.println("Error - " + ex.toString());
133         }
134     }
135
136     public void listBeans( PrintWriter JavaDoc writer, String JavaDoc qry )
137     {
138
139         Set JavaDoc names = null;
140         try {
141             names=mBeanServer.queryNames(new ObjectName JavaDoc(qry), null);
142             writer.println("OK - Number of results: " + names.size());
143             writer.println();
144         } catch (Exception JavaDoc e) {
145             writer.println("Error - " + e.toString());
146             return;
147         }
148
149         Iterator JavaDoc it=names.iterator();
150         while( it.hasNext()) {
151             ObjectName JavaDoc oname=(ObjectName JavaDoc)it.next();
152             writer.println( "Name: " + oname.toString());
153
154             try {
155                 MBeanInfo JavaDoc minfo=mBeanServer.getMBeanInfo(oname);
156                 // can't be null - I thinl
157
String JavaDoc code=minfo.getClassName();
158                 if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
159                     code=(String JavaDoc)mBeanServer.getAttribute(oname, "modelerType");
160                 }
161                 writer.println("modelerType: " + code);
162
163                 MBeanAttributeInfo JavaDoc attrs[]=minfo.getAttributes();
164                 Object JavaDoc value=null;
165
166                 for( int i=0; i< attrs.length; i++ ) {
167                     if( ! attrs[i].isReadable() ) continue;
168                     if( ! isSupported( attrs[i].getType() )) continue;
169                     String JavaDoc attName=attrs[i].getName();
170                     if( attName.indexOf( "=") >=0 ||
171                             attName.indexOf( ":") >=0 ||
172                             attName.indexOf( " ") >=0 ) {
173                         continue;
174                     }
175             
176                     try {
177                         value=mBeanServer.getAttribute(oname, attName);
178                     } catch( Throwable JavaDoc t) {
179                         System.out.println("Error getting attribute " + oname +
180                                 " " + attName + " " + t.toString());
181                         continue;
182                     }
183                     if( value==null ) continue;
184                     if( "modelerType".equals( attName)) continue;
185                     String JavaDoc valueString=value.toString();
186                     writer.println( attName + ": " + escape(valueString));
187                 }
188             } catch (Exception JavaDoc e) {
189                 // Ignore
190
}
191             writer.println();
192         }
193
194     }
195
196     public String JavaDoc escape(String JavaDoc value) {
197         // The only invalid char is \n
198
// We also need to keep the string short and split it with \nSPACE
199
// XXX TODO
200
int idx=value.indexOf( "\n" );
201         if( idx < 0 ) return value;
202
203         int prev=0;
204         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
205         while( idx >= 0 ) {
206             appendHead(sb, value, prev, idx-1);
207
208             sb.append( "\\n\n ");
209             prev=idx+1;
210             if( idx==value.length() -1 ) break;
211             idx=value.indexOf('\n', idx+1);
212         }
213         if( prev < value.length() )
214             appendHead( sb, value, prev, value.length());
215         return sb.toString();
216     }
217
218     private void appendHead( StringBuffer JavaDoc sb, String JavaDoc value, int start, int end) {
219         int pos=start;
220         while( end-pos > 78 ) {
221             sb.append( value.substring(pos, pos+78));
222             sb.append( "\n ");
223             pos=pos+78;
224         }
225         sb.append( value.substring(pos,end));
226     }
227
228     public boolean isSupported( String JavaDoc type ) {
229         return true;
230     }
231 }
232
Popular Tags