Hello all!

The attached file is a patch that implements a
management console in the FTP Server when started
by using the command line mode.

I implemented it by using the MX4J library,
providing an HTTP console by using the MX4J HTTP
adaptor. Currently, it just suspends and resumes
the FTP server. To see the HTTP console go to
http://localhost:9999/ (the port should be added
to the list of configuration properties).

Can you take a look at it and let me know what do
you think? If you like I will submit an issue in
Jira with a new patch with more comments and
functions.

If you are not like using MX4J, what would you
like to use or how would you like to implement a
management facility?

This runs with no problems with both JDK 1.5 and
JDK 1.4.2 (using the MX4J implementation).

This patch was tested on Windows 2000 only, no
idea how it works on Linux or other environments.

Thanks,
Luis
Index: project.xml
===================================================================
--- project.xml (revision 330245)
+++ project.xml (working copy)
@@ -86,7 +86,7 @@
         <contributor>
             <name>Luis Sanabria</name>
             <organization/>
-            <email/>
+            <email>luis.{d0T}.sanabria.{aT}.gmail.{d0T}.org</email>
         </contributor>
         <contributor>
             <name>Niklas Gustavsson</name>
@@ -141,6 +141,16 @@
             <version>1.2.12</version>
         </dependency>
         <dependency>
+            <groupId>mx4j</groupId>
+            <artifactId>mx4j</artifactId>
+            <version>3.0.1</version>
+        </dependency>
+        <dependency>
+            <groupId>mx4j</groupId>
+            <artifactId>mx4j-tools</artifactId>
+            <version>2.1.1</version>
+        </dependency>
+        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>3.8.1</version>
Index: src/java/org/apache/ftpserver/commandline/CommandLine.java
===================================================================
--- src/java/org/apache/ftpserver/commandline/CommandLine.java  (revision 
330245)
+++ src/java/org/apache/ftpserver/commandline/CommandLine.java  (working copy)
@@ -18,6 +18,13 @@
 
 import java.io.FileInputStream;
 
+import javax.management.MBeanServerFactory;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import mx4j.tools.adaptor.http.HttpAdaptor;
+import mx4j.tools.adaptor.http.XSLTProcessor;
+
 import org.apache.ftpserver.FtpConfigImpl;
 import org.apache.ftpserver.FtpServer;
 import org.apache.ftpserver.config.PropertiesConfiguration;
@@ -25,6 +32,7 @@
 import org.apache.ftpserver.ftplet.Configuration;
 import org.apache.ftpserver.ftplet.EmptyConfiguration;
 import org.apache.ftpserver.interfaces.IFtpConfig;
+import org.apache.ftpserver.management.FtpServerManagement;
 import org.apache.ftpserver.util.IoUtils;
 
 /**
@@ -41,7 +49,7 @@
  *   <li>-prop :: properties configuration will be used. User has to specify 
the file.</li>
  * </ul>
  *
- * @author Luis Sanabria
+ * @author <a href="mailto:[EMAIL PROTECTED]">Luis Sanabria</a>
  */
 public 
 class CommandLine {
@@ -56,7 +64,8 @@
 
     /**
      * This method is the FtpServer starting poing when running by using the
-     * command line mode.
+     * command line mode. This class creates the FTP Server and the MBean for
+     * the server management by using JMX
      *
      * @param args The first element of this array must specify the kind of
      *             configuration to be used to start the server.
@@ -80,6 +89,22 @@
 
             // add shutdown hook if possible
             addShutdownHook(server);
+            
+            // Creating the MBeanServer.
+            MBeanServer mbs = MBeanServerFactory.createMBeanServer();
+
+            // Creating the MBean for management of the FTP Server
+            ObjectName name = new 
ObjectName("org.apache.ftpserver.management:name=FtpServerManagement");
+            FtpServerManagement ftp_manager = new FtpServerManagement (server);
+            mbs.registerMBean(ftp_manager, name);
+            
+            // Creating the HTTP Adaptor.
+            ObjectName HTTPName = new 
ObjectName("mx4j:class=mx4j.tools.adaptor.http.HttpAdaptor,id=1");
+            HttpAdaptor adaptor = new HttpAdaptor(9999, "0.0.0.0");
+            mbs.registerMBean(adaptor, HTTPName);
+            mbs.invoke(HTTPName, "start", null, null);
+            adaptor.setProcessor(new XSLTProcessor());
+            
         }
         catch(Exception ex) {
             ex.printStackTrace();
Index: src/java/org/apache/ftpserver/management/FtpServerManagementMBean.java
===================================================================
--- src/java/org/apache/ftpserver/management/FtpServerManagementMBean.java      
(revision 0)
+++ src/java/org/apache/ftpserver/management/FtpServerManagementMBean.java      
(revision 0)
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ftpserver.management;
+
+/**
+ * Interface used to publish the properties and operations that can be
+ * performed by using the JMX mechanism.
+ * 
+ * @author <a href="mailto:[EMAIL PROTECTED]">Luis Sanabria</a>
+ */
+public interface FtpServerManagementMBean {
+
+       /**
+        * Resumes the FTP server if it was suspended. This operation will 
return
+        * <code>true</code> if the server is not suspended and it is running. 
+        * <code>fase</code> will be returned if no FTP Server is associated 
with
+        * the management been. 
+        * 
+        * @return <code>true</code> if the operation was executed successfully,
+        * <code>false</code> if the operation could not be executed.
+        */
+       boolean resumeServer ();
+       
+       /**
+        * Suspends the FTP server. This operation will return <code>true</code>
+        * if the server is already suspended. <code>fase</code> will be 
returned
+        * if no FTP Server is associated with the management been. 
+        * 
+        * @return <code>true</code> if the operation was executed successfully,
+        * <code>false</code> if the operation could not be executed.
+        */
+       boolean suspendServer();
+       
+}
Index: src/java/org/apache/ftpserver/management/FtpServerManagement.java
===================================================================
--- src/java/org/apache/ftpserver/management/FtpServerManagement.java   
(revision 0)
+++ src/java/org/apache/ftpserver/management/FtpServerManagement.java   
(revision 0)
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ftpserver.management;
+
+import org.apache.ftpserver.FtpServer;
+
+/**
+ * Implements the methods defined in the FtpServerManagementMBean for
+ * the administration of the FtpServer by using the JMX technology.
+ * 
+ * @author <a href="mailto:[EMAIL PROTECTED]">Luis Sanabria</a>
+ */
+public class FtpServerManagement implements FtpServerManagementMBean {
+
+       private FtpServer m_managedServer;
+       
+       public FtpServerManagement(){
+               m_managedServer = null;
+       }
+       
+       public FtpServerManagement(FtpServer managedServer){
+               m_managedServer = managedServer;
+       }
+       
+       public boolean resumeServer() {
+               
+               if ((m_managedServer == null)|| m_managedServer.isStopped()){
+                       return false;
+               }else{
+                       m_managedServer.resume();
+                       return true;
+               }
+               
+       }
+
+       public boolean suspendServer() {
+
+               if ((m_managedServer == null)|| m_managedServer.isStopped()){
+                       return false;
+               }else{
+                       m_managedServer.suspend();
+                       return true;
+               }
+
+       }
+
+}
Index: bin/ftpd.bat
===================================================================
--- bin/ftpd.bat        (revision 330245)
+++ bin/ftpd.bat        (working copy)
@@ -33,4 +33,4 @@
 rem ----- call java.. ---------------------------------------------------
 set MAIN_CLASS=org.apache.ftpserver.commandline.CommandLine
 set JAVA_CMD=%JAVA_HOME%\bin\java
-"%JAVA_CMD%" -classpath "%FTPD_CLASSPATH%" %MAIN_CLASS% %*
+"%JAVA_CMD%" -Dcom.sun.management.jmxremote -classpath "%FTPD_CLASSPATH%" 
%MAIN_CLASS% %*
Index: bin/ftpd.sh
===================================================================
--- bin/ftpd.sh (revision 330245)
+++ bin/ftpd.sh (working copy)
@@ -128,7 +128,7 @@
 CURR_DIR=`pwd`
 cd $FTPD_HOME
 MAIN_CLASS=org.apache.ftpserver.commandline.CommandLine
-"$JAVACMD" -classpath "$FTPD_CLASSPATH" $MAIN_CLASS $@
+"$JAVACMD" -Dcom.sun.management.jmxremote -classpath "$FTPD_CLASSPATH" 
$MAIN_CLASS $@
 RESULT=$?
 cd $CURR_DIR
 exit $RESULT

Reply via email to