craigmcc    02/04/25 19:27:33

  Modified:    catalina/src/share/org/apache/catalina/mbeans
                        StandardServerMBean.java mbeans-descriptors.xml
  Log:
  Add the beginnings of the ability to save the current Tomcat configuration
  to conf/server.xml.
  
  Revision  Changes    Path
  1.8       +237 -6    
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/StandardServerMBean.java
  
  Index: StandardServerMBean.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/StandardServerMBean.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- StandardServerMBean.java  8 Mar 2002 00:42:14 -0000       1.7
  +++ StandardServerMBean.java  26 Apr 2002 02:27:33 -0000      1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/StandardServerMBean.java,v
 1.7 2002/03/08 00:42:14 amyroh Exp $
  - * $Revision: 1.7 $
  - * $Date: 2002/03/08 00:42:14 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/StandardServerMBean.java,v
 1.8 2002/04/26 02:27:33 craigmcc Exp $
  + * $Revision: 1.8 $
  + * $Date: 2002/04/26 02:27:33 $
    *
    * ====================================================================
    *
  @@ -64,14 +64,19 @@
   package org.apache.catalina.mbeans;
   
   
  +import java.io.File;
  +import java.io.FileWriter;
  +import java.io.IOException;
  +import java.io.PrintWriter;
  +import javax.management.InstanceNotFoundException;
  +import javax.management.MBeanAttributeInfo;
   import javax.management.MBeanException;
  +import javax.management.MBeanInfo;
   import javax.management.MBeanServer;
   import javax.management.ObjectName;
   import javax.management.RuntimeOperationsException;
   import org.apache.catalina.Server;
  -import org.apache.catalina.Service;
   import org.apache.catalina.core.StandardServer;
  -import org.apache.catalina.core.StandardService;
   import org.apache.commons.modeler.BaseModelMBean;
   
   
  @@ -80,7 +85,7 @@
    * <code>org.apache.catalina.core.StandardServer</code> component.</p>
    *
    * @author Amy Roh
  - * @version $Revision: 1.7 $ $Date: 2002/03/08 00:42:14 $
  + * @version $Revision: 1.8 $ $Date: 2002/04/26 02:27:33 $
    */
   
   public class StandardServerMBean extends BaseModelMBean {
  @@ -114,10 +119,236 @@
       // ------------------------------------------------------------- Attributes
   
   
  +    /**
  +     * Set the value of a specific attribute of this MBean.
  +     *
  +     * @param attribute The identification of the attribute to be set
  +     *  and the new value
  +     *
  +     * @exception AttributeNotFoundException if this attribute is not
  +     *  supported by this MBean
  +     * @exception MBeanException if the initializer of an object
  +     *  throws an exception
  +     * @exception ReflectionException if a Java reflection exception
  +     *  occurs when invoking the getter
  +     */
  +    public void setAttribute(javax.management.Attribute attribute)
  +        throws javax.management.AttributeNotFoundException,
  +               MBeanException,
  +               javax.management.ReflectionException {
  +
  +        // KLUDGE - This is only here to force calling store()
  +        // until the admin webapp calls it directly
  +        super.setAttribute(attribute);
  +        try {
  +            store();
  +        } catch (InstanceNotFoundException e) {
  +            throw new MBeanException(e);
  +        }
  +
  +    }
   
   
       // ------------------------------------------------------------- Operations
   
  +
  +    /**
  +     * Write the configuration information for this entire <code>Server</code>
  +     * out to the server.xml configuration file.
  +     *
  +     * @exception InstanceNotFoundException if the managed resource object
  +     *  cannot be found
  +     * @exception MBeanException if the initializer of the object throws
  +     *  an exception, or persistence is not supported
  +     * @exception RuntimeOperationsException if an exception is reported
  +     *  by the persistence mechanism
  +     */
  +    public synchronized void store() throws InstanceNotFoundException,
  +        MBeanException, RuntimeOperationsException {
  +
  +        // Calculate file objects for the old and new configuration files.
  +        String configFile = "conf/server.xml"; // FIXME - configurable?
  +        File configOld = new File(configFile);
  +        if (!configOld.isAbsolute()) {
  +            configOld = new File(System.getProperty("catalina.base"),
  +                                 configFile);
  +        }
  +        File configNew = new File(configFile + ".new");
  +        if (!configNew.isAbsolute()) {
  +            configNew = new File(System.getProperty("catalina.base"),
  +                                 configFile + ".new");
  +        }
  +
  +        // Open an output writer for the new configuration file
  +        PrintWriter writer = null;
  +        try {
  +            writer = new PrintWriter(new FileWriter(configNew));
  +        } catch (IOException e) {
  +            if (writer != null) {
  +                try {
  +                    writer.close();
  +                } catch (Throwable t) {
  +                    ;
  +                }
  +            }
  +            throw new MBeanException(e, "Creating conf/server.xml.new");
  +        }
  +
  +        // Store the state of this Server MBean
  +        // (which will recursively store everything
  +        ObjectName oname = null;
  +        try {
  +            oname =
  +                MBeanUtils.createObjectName("Catalina",
  +                                            (Server) getManagedResource());
  +            storeServer(writer, 0, oname);
  +        } catch (Exception e) {
  +            if (writer != null) {
  +                try {
  +                    writer.close();
  +                } catch (Throwable t) {
  +                    ;
  +                }
  +            }
  +            throw new MBeanException(e, "Writing conf/server.xml.new");
  +        }
  +
  +        // Close the output file and rename to the original
  +        try {
  +            writer.flush();
  +        } catch (Exception e) {
  +            throw new MBeanException(e, "Flushing conf/server.xml.new");
  +        }
  +        try {
  +            writer.close();
  +        } catch (Exception e) {
  +            throw new MBeanException(e, "Closing conf/server.xml.new");
  +        }
  +        ; // FIXME - do not rename until 100% of server.xml is being written!
  +
  +    }
  +
  +
  +    // -------------------------------------------------------- Private Methods
  +
  +
  +    /**
  +     * Store the relevant attributes of the specified MBean.
  +     *
  +     * @param writer PrintWriter to which we are storing
  +     * @param oname ObjectName of the MBean for the object we are storing
  +     *
  +     * @exception Exception if an exception occurs while storing
  +     */
  +    private void storeAttributes(PrintWriter writer,
  +                                 ObjectName oname) throws Exception {
  +
  +        // Acquire the set of attributes we should be saving
  +        MBeanInfo minfo = mserver.getMBeanInfo(oname);
  +        MBeanAttributeInfo ainfo[] = minfo.getAttributes();
  +        if (ainfo == null) {
  +            ainfo = new MBeanAttributeInfo[0];
  +        }
  +
  +        // Save the value of each relevant attribute
  +        for (int i = 0; i < ainfo.length; i++) {
  +
  +            // Make sure this is an attribute we want to save
  +            String aname = ainfo[i].getName();
  +            if ("managedResource".equals(aname)) {
  +                continue; // KLUDGE - these should be removed
  +            }
  +            if (!ainfo[i].isReadable() || !ainfo[i].isWritable()) {
  +                continue; // We cannot configure this attribute
  +            }
  +
  +            // Acquire the value of this attribute
  +            Object value = mserver.getAttribute(oname, aname);
  +            if (value == null) {
  +                value = "";
  +            }
  +            if (!(value instanceof String)) {
  +                value = value.toString();
  +            }
  +
  +            // Add this attribute value to our output
  +            writer.print(" ");
  +            writer.print(aname);
  +            writer.print("=\"");
  +            writer.print((String) value);
  +            writer.print("\"");
  +
  +        }
  +
  +
  +    }
  +
  +
  +    /**
  +     * Store the specified Server properties.
  +     *
  +     * @param writer PrintWriter to which we are storing
  +     * @param indent Number of spaces to indent this element
  +     * @param oname ObjectName of the MBean for the object we are storing
  +     *
  +     * @exception Exception if an exception occurs while storing
  +     */
  +    private void storeServer(PrintWriter writer, int indent,
  +                             ObjectName oname) throws Exception {
  +
  +        // Store the beginning of this element
  +        for (int i = 0; i < indent; i++) {
  +            writer.print(' ');
  +        }
  +        writer.print("<Server");
  +        storeAttributes(writer, oname);
  +        writer.println(">");
  +
  +        // Store all nested elements
  +        ; // FIXME - <Listener>s
  +        ; // FIXME - <GlobalNamingResources>
  +        ; // FIXME - <Service>s
  +
  +        // Store the ending of this element
  +        for (int i = 0; i < indent; i++) {
  +            writer.print(' ');
  +        }
  +        writer.println("</Server>");
  +
  +    }
  +
  +
  +    /**
  +     * Store the specified Service properties.
  +     *
  +     * @param writer PrintWriter to which we are storing
  +     * @param indent Number of spaces to indent this element
  +     * @param oname ObjectName of the MBean for the object we are storing
  +     *
  +     * @exception Exception if an exception occurs while storing
  +     */
  +    private void storeService(PrintWriter writer, int indent,
  +                              ObjectName oname) throws Exception {
  +
  +        // Store the beginning of this element
  +        for (int i = 0; i < indent; i++) {
  +            writer.print(' ');
  +        }
  +        writer.print("<Service");
  +        storeAttributes(writer, oname);
  +        writer.println(">");
  +
  +        // Store all nested elements
  +        ; // FIXME - <Connector>s
  +        ; // FIXME - <Engine>
  +
  +        // Store the ending of this element
  +        for (int i = 0; i < indent; i++) {
  +            writer.print(' ');
  +        }
  +        writer.println("</Service>");
  +
  +    }
   
   
   }
  
  
  
  1.46      +7 -1      
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/mbeans-descriptors.xml
  
  Index: mbeans-descriptors.xml
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/mbeans-descriptors.xml,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- mbeans-descriptors.xml    25 Apr 2002 19:30:44 -0000      1.45
  +++ mbeans-descriptors.xml    26 Apr 2002 02:27:33 -0000      1.46
  @@ -6,7 +6,7 @@
   <!--
        Descriptions of JMX MBeans for Catalina
   
  -     $Id: mbeans-descriptors.xml,v 1.45 2002/04/25 19:30:44 amyroh Exp $
  +     $Id: mbeans-descriptors.xml,v 1.46 2002/04/26 02:27:33 craigmcc Exp $
    -->
   
   <mbeans-descriptors>
  @@ -2084,6 +2084,12 @@
       <attribute   name="shutdown"
             description="Shutdown password"
                    type="java.lang.String"/>
  +
  +    <operation   name="store"
  +          description="Save current state to server.xml file"
  +               impact="ACTION"
  +           returnType="void">
  +    </operation>
   
     </mbean>
   
  
  
  

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

Reply via email to