You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by ng...@apache.org on 2008/07/12 22:39:50 UTC

svn commit: r676236 - in /mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main: ./ CommandLine.java Daemon.java

Author: ngn
Date: Sat Jul 12 13:39:50 2008
New Revision: 676236

URL: http://svn.apache.org/viewvc?rev=676236&view=rev
Log:
Adding new runners for the Spring config

Added:
    mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/
    mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/CommandLine.java   (with props)
    mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/Daemon.java   (with props)

Added: mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/CommandLine.java
URL: http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/CommandLine.java?rev=676236&view=auto
==============================================================================
--- mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/CommandLine.java (added)
+++ mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/CommandLine.java Sat Jul 12 13:39:50 2008
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.main;
+
+import org.apache.ftpserver.FtpServer;
+import org.springframework.beans.factory.xml.XmlBeanFactory;
+import org.springframework.core.io.FileSystemResource;
+
+/**
+ * This class is the starting point for the FtpServer when it is started
+ * using the command line mode.
+ */
+public 
+class CommandLine {
+
+    /**
+     * The purpose of this class is to allow the final user to start the
+     * FtpServer application. Because of that it has only <code>static</code>
+     * methods and cannot be instanced.
+     */
+    private CommandLine() {
+    }
+
+    /**
+     * This method is the FtpServer starting point when running by using the
+     * command line mode.
+     *
+     * @param args The first element of this array must specify the kind of
+     *             configuration to be used to start the server.
+     */
+    public static void main(String args[]) {
+
+        try {
+
+            // get configuration
+            FtpServer server = getConfiguration(args);
+            if(server == null) {
+                return;
+            }
+
+            // start the server
+            server.start();
+
+            // add shutdown hook if possible
+            addShutdownHook(server);
+        }
+        catch(Exception ex) {
+            ex.printStackTrace();
+        }
+    }
+
+    /**
+     * Add shutdown hook.
+     */
+    private static void addShutdownHook(final FtpServer engine) {
+
+        // create shutdown hook
+        Runnable shutdownHook = new Runnable() {
+            public void run() {
+                System.out.println("Stopping server...");
+                engine.stop();
+            }
+        };
+
+        // add shutdown hook
+        Runtime runtime = Runtime.getRuntime();
+        runtime.addShutdownHook(new Thread(shutdownHook));
+    }
+
+    /**
+     * Print the usage message.
+     */
+    private static void usage() {
+        System.err.println("Usage: java org.apache.ftpserver.main.CommandLine [OPTION] [CONFIGFILE]");
+        System.err.println("Starts FtpServer using the default configuration of the ");
+        System.err.println("configuration file if provided.");
+        System.err.println("");
+        System.err.println("      --default              use the default configuration, ");
+        System.err.println("                             also used if no command line argument is given ");
+        System.err.println("  -?, --help                 print this message");
+    }
+
+    /**
+     * Get the configuration object.
+     */
+    private static FtpServer getConfiguration(String[] args) throws Exception {
+
+        FtpServer server = null;
+        if(args.length == 0) {
+            System.out.println("Using default configuration");
+            server = new FtpServer();
+        } else if( (args.length == 1) && args[0].equals("-default") ) {
+            // supported for backwards compatibility, but not documented
+            System.out.println("The -default switch is deprecated, please use --default instead");
+            System.out.println("Using default configuration");
+            server = new FtpServer();
+        } else if( (args.length == 1) && args[0].equals("--default") ) {
+            System.out.println("Using default configuration");
+            server = new FtpServer();
+        } else if( (args.length == 1) && args[0].equals("--help") ) {
+            usage();
+        } else if( (args.length == 1) && args[0].equals("-?") ) {
+            usage();
+        } else if( args.length == 1 ) {
+            System.out.println("Using XML configuration file " + args[0] + "...");
+            XmlBeanFactory bf = new XmlBeanFactory(new FileSystemResource(args[0]));
+            if(bf.containsBean("server")) {
+                server = (FtpServer) bf.getBean("server");
+            } else {
+                String[] beanNames = bf.getBeanNamesForType(FtpServer.class);
+                if(beanNames.length == 1) {
+                    server = (FtpServer) bf.getBean(beanNames[0]);
+                } else if(beanNames.length > 1) {
+                    System.out.println("Using the first server defined in the configuration, named " + beanNames[0]);
+                    server = (FtpServer) bf.getBean(beanNames[0]);
+                } else {
+                    System.err.println("XML configuration does not contain a server configuration");
+                }
+                
+            }
+        }
+        else {
+            usage();
+        }
+        
+        return server;
+    }
+}
\ No newline at end of file

Propchange: mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/CommandLine.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/Daemon.java
URL: http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/Daemon.java?rev=676236&view=auto
==============================================================================
--- mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/Daemon.java (added)
+++ mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/Daemon.java Sat Jul 12 13:39:50 2008
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.main;
+
+import org.apache.ftpserver.FtpServer;
+import org.apache.ftpserver.ftplet.FtpException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.xml.XmlBeanFactory;
+import org.springframework.core.io.FileSystemResource;
+
+/**
+ * Invokes FtpServer as a daemon, running in the background. 
+ * Used for example for the Windows service.
+ */
+public class Daemon {
+
+    private static final Logger LOG = LoggerFactory.getLogger(Daemon.class);
+    
+    private static FtpServer server;
+    private static Object lock = new Object();
+    
+    public static void main(String[] args) throws Exception {
+        try{
+            if(server == null) {
+                // get configuration
+                FtpServer server = getConfiguration(args);
+                if(server == null) {
+                    LOG.error("No configuration provided");
+                    throw new FtpException("No configuration provided");
+                }
+            }
+            
+            String command = "start";
+            
+            if(args != null && args.length > 0) {
+                command = args[0];
+            }
+            
+            if(command.equals("start")) {
+                LOG.info("Starting FTP server daemon");
+                server.start();
+                
+                synchronized (lock) {
+                    lock.wait();
+                }
+            } else if(command.equals("stop")) {
+                synchronized (lock) {
+                    lock.notify();
+                }
+                LOG.info("Stopping FTP server daemon");
+                server.stop();
+            }
+        } catch(Throwable t) {
+            LOG.error("Daemon error", t);
+        }
+    }
+
+    /**
+     * Get the configuration object.
+     */
+    private static FtpServer getConfiguration(String[] args) throws Exception {
+    
+        FtpServer server = null;
+        if(args == null || args.length < 2) {
+            LOG.info("Using default configuration....");
+            server = new FtpServer();
+        } else if( (args.length == 2) && args[1].equals("-default") ) {
+            // supported for backwards compatibility, but not documented
+            System.out.println("The -default switch is deprecated, please use --default instead");
+            LOG.info("Using default configuration....");
+            server = new FtpServer();
+        } else if( (args.length == 2) && args[1].equals("--default") ) {
+            LOG.info("Using default configuration....");
+            server = new FtpServer();
+        }
+        else if( args.length == 2 ) {
+            LOG.info("Using xml configuration file " + args[1] + "...");
+            XmlBeanFactory bf = new XmlBeanFactory(new FileSystemResource(args[1]));
+            if(bf.containsBean("server")) {
+                server = (FtpServer) bf.getBean("server");
+            } else {
+                String[] beanNames = bf.getBeanNamesForType(FtpServer.class);
+                if(beanNames.length == 1) {
+                    server = (FtpServer) bf.getBean(beanNames[0]);
+                } else if(beanNames.length > 1) {
+                    System.out.println("Using the first server defined in the configuration, named " + beanNames[0]);
+                    server = (FtpServer) bf.getBean(beanNames[0]);
+                } else {
+                    System.err.println("XML configuration does not contain a server configuration");
+                }
+            }
+        } else {
+            throw new FtpException("Invalid configuration option");
+        }
+        
+        return server;
+    }
+}

Propchange: mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/main/Daemon.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain