1 17 18 19 package org.apache.catalina.manager; 20 21 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import javax.management.MBeanServer ; 27 import javax.management.ObjectName ; 28 import javax.management.MBeanInfo ; 29 import javax.management.MBeanAttributeInfo ; 30 import javax.management.Attribute ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.apache.tomcat.util.modeler.Registry; 37 38 44 public class JMXProxyServlet extends HttpServlet { 45 47 50 protected MBeanServer mBeanServer = null; 51 protected Registry registry; 52 54 55 58 public void init() throws ServletException { 59 registry = Registry.getRegistry(null, null); 61 mBeanServer = Registry.getRegistry(null, null).getMBeanServer(); 62 } 63 64 65 74 public void doGet(HttpServletRequest request, 75 HttpServletResponse response) 76 throws IOException , ServletException 77 { 78 79 response.setContentType("text/plain"); 80 81 PrintWriter writer = response.getWriter(); 82 String qryString= request.getQueryString(); 83 84 if( mBeanServer==null ) { 85 writer.println("Error - No mbean server"); 86 return; 87 } 88 89 String qry=request.getParameter("set"); 90 if( qry!= null ) { 91 String name=request.getParameter("att"); 92 String 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 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 writer, String onameStr, String att) { 113 try { 114 ObjectName oname = new ObjectName (onameStr); 115 Object value = mBeanServer.getAttribute(oname, att); 116 writer.println("OK - Attribute get '" + onameStr + "' - " + att + "= " + value.toString() ); 117 } catch (Exception ex) { 118 writer.println("Error - " + ex.toString()); 119 } 120 } 121 122 public void setAttribute( PrintWriter writer, 123 String onameStr, String att, String val ) 124 { 125 try { 126 ObjectName oname=new ObjectName ( onameStr ); 127 String type=registry.getType(oname, att); 128 Object valueObj=registry.convertValue(type, val ); 129 mBeanServer.setAttribute( oname, new Attribute (att, valueObj)); 130 writer.println("OK - Attribute set"); 131 } catch( Exception ex ) { 132 writer.println("Error - " + ex.toString()); 133 } 134 } 135 136 public void listBeans( PrintWriter writer, String qry ) 137 { 138 139 Set names = null; 140 try { 141 names=mBeanServer.queryNames(new ObjectName (qry), null); 142 writer.println("OK - Number of results: " + names.size()); 143 writer.println(); 144 } catch (Exception e) { 145 writer.println("Error - " + e.toString()); 146 return; 147 } 148 149 Iterator it=names.iterator(); 150 while( it.hasNext()) { 151 ObjectName oname=(ObjectName )it.next(); 152 writer.println( "Name: " + oname.toString()); 153 154 try { 155 MBeanInfo minfo=mBeanServer.getMBeanInfo(oname); 156 String code=minfo.getClassName(); 158 if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) { 159 code=(String )mBeanServer.getAttribute(oname, "modelerType"); 160 } 161 writer.println("modelerType: " + code); 162 163 MBeanAttributeInfo attrs[]=minfo.getAttributes(); 164 Object 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 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 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 valueString=value.toString(); 186 writer.println( attName + ": " + escape(valueString)); 187 } 188 } catch (Exception e) { 189 } 191 writer.println(); 192 } 193 194 } 195 196 public String escape(String value) { 197 int idx=value.indexOf( "\n" ); 201 if( idx < 0 ) return value; 202 203 int prev=0; 204 StringBuffer sb=new StringBuffer (); 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 sb, String 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 type ) { 229 return true; 230 } 231 } 232 | Popular Tags |