Hi there,

I've trying to write a servlet that changes the acceptCount value of the Tomcat Stand-Alone connector when it is hit. I'm having some problems with it (mainly NullPointerExceptions at the line where i call the mBServer.setAttribute() and mBServer.invoke() functions.

This is my first time writing a JMX agent/program and I mainly referred to the code of the Admin servlet included with Tomcat ( SaveConnectorAction.java and CommitChangesAction.java ).

One of the possible reasons is that I don't think i'm getting the Object Naming right.. this is how i do it:

cname = new ObjectName( "Catalina:type=Connector,serviceName=Tomcat-Standalone,port=8080,address=" );

Could the JMX experts out there take a look at my code (at the bottom) and tell me what I'm doing wrong?!?

======================================================================

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.JMException;
import javax.management.Attribute;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.QueryExp;
import javax.management.Query;

import org.apache.commons.modeler.Registry;


public class ConfigureTomcat extends HttpServlet {


   private MBeanServer mBServer = null;
   protected Registry registry = null;

   public void doGet(HttpServletRequest request,
                     HttpServletResponse response)
       throws IOException, ServletException
   {

PrintWriter out = response.getWriter();

       out.println( "<HTML>" );
       out.println( "<HEAD>" );
       out.println( "</HEAD>" );
       out.println( "<BODY>" );
       out.println( "This is my HTML." );



registry = (Registry) getServletContext().getAttribute ("org.apache.catalina.Registry");

out.println( "<br>Registry found." );

// Acquire a reference to the MBeanServer containing our MBeans
try {
mBServer = (MBeanServer)getServletContext().getAttribute("org.apache.catalina.MBeanServer");
} catch (Throwable t) {
throw new ServletException
("Cannot acquire MBeanServer reference", t);
}


out.println( "<br>MBeanServer found." );

ObjectName cname = null;
StringBuffer sb = null;
try {
cname = new ObjectName( "Catalina:type=Connector,serviceName=Tomcat-Standalone,port=8080,address=" );
} catch (Exception e) {
e.printStackTrace();
}


int acceptCount = 100;

try {
mBServer.setAttribute(cname, new Attribute("acceptCount", new Integer(acceptCount)));
}
catch( Exception e ) {
e.printStackTrace();
}


out.println( "<br>Set Attribute." );

ObjectName sname = null;

       try {
          sname = new ObjectName("Catalina:type=Server");
       } catch (Exception e) {
           e.printStackTrace();
       }
       String operation = "store";

out.println( "<br>ObjectName sname found." );

      try {
           mBServer.invoke(sname, operation, null, null);
       } catch (Throwable t) {
           System.out.println( "Could not store changes." );
           t.printStackTrace();
       }

       out.println( "<br>Invoked." );
       out.println( "</BODY>" );
       out.println( "</HTML>" );

System.out.println( "Stored changes." );


} }

=========================================================

Nick





_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to