client/ftpd/src/se/lu/thep/coreftpd/webserver/ClassChooser.java

Code
Comments
Other
Rev Date Author Line
741 10 Oct 06 olle 1 /*
1652 22 May 07 gregory 2  $Id$
741 10 Oct 06 olle 3
1916 31 Aug 07 jari 4  Copyright (C) 2006 Olle Mansson
1916 31 Aug 07 jari 5  Copyright (C) 2007 Gregory Vincic, Olle Mansson
741 10 Oct 06 olle 6
1652 22 May 07 gregory 7  This file is part of Proteios.
1652 22 May 07 gregory 8  Available at http://www.proteios.org/
741 10 Oct 06 olle 9
1652 22 May 07 gregory 10  Proteios is free software; you can redistribute it and/or modify it
1652 22 May 07 gregory 11  under the terms of the GNU General Public License as published by
1652 22 May 07 gregory 12  the Free Software Foundation; either version 2 of the License, or
1652 22 May 07 gregory 13  (at your option) any later version.
741 10 Oct 06 olle 14
1652 22 May 07 gregory 15  Proteios is distributed in the hope that it will be useful, but
1652 22 May 07 gregory 16  WITHOUT ANY WARRANTY; without even the implied warranty of
1652 22 May 07 gregory 17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1652 22 May 07 gregory 18  General Public License for more details.
741 10 Oct 06 olle 19
1652 22 May 07 gregory 20  You should have received a copy of the GNU General Public License
1652 22 May 07 gregory 21  along with this program; if not, write to the Free Software
1652 22 May 07 gregory 22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1652 22 May 07 gregory 23  02111-1307, USA.
1652 22 May 07 gregory 24  */
741 10 Oct 06 olle 25
741 10 Oct 06 olle 26 //  Xerver Free Web Server
741 10 Oct 06 olle 27 //  Copyright (C) 2002-2005 Omid Rouhani
741 10 Oct 06 olle 28 //
741 10 Oct 06 olle 29 //
741 10 Oct 06 olle 30 //  This program is free software; you can redistribute it and/or
741 10 Oct 06 olle 31 //  modify it under the terms of the GNU General Public License
741 10 Oct 06 olle 32 //  as published by the Free Software Foundation; either version 2
741 10 Oct 06 olle 33 //  of the License, or (at your option) any later version.
741 10 Oct 06 olle 34 //
741 10 Oct 06 olle 35 //  This program is distributed in the hope that it will be useful,
741 10 Oct 06 olle 36 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
741 10 Oct 06 olle 37 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
741 10 Oct 06 olle 38 //  GNU General Public License for more details.
741 10 Oct 06 olle 39 //
741 10 Oct 06 olle 40 //  You should have received a copy of the GNU General Public License
741 10 Oct 06 olle 41 //  along with this program; if not, write to the Free Software
741 10 Oct 06 olle 42 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
741 10 Oct 06 olle 43 //
741 10 Oct 06 olle 44 //
741 10 Oct 06 olle 45 //  #############################################################
741 10 Oct 06 olle 46 //  ##  YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT:          ##
741 10 Oct 06 olle 47 //  ##  HTTP://WWW.JAVASCRIPT.NU/XERVER/                       ##
741 10 Oct 06 olle 48 //  ##                                                         ##
741 10 Oct 06 olle 49 //  ##  IF YOUR SOFTWARE IS NOT RELEASED UNDER THE             ##
741 10 Oct 06 olle 50 //  ##  GNU GENERAL PUBLIC LICENSE (GPL),                      ##
741 10 Oct 06 olle 51 //  ##  PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!!   ##
741 10 Oct 06 olle 52 //  ##                                                         ##
741 10 Oct 06 olle 53 //  ##  FOR FULL LICENSE, PLEASE READ "XERVER LICENSE".        ##
741 10 Oct 06 olle 54 //  #############################################################
741 10 Oct 06 olle 55
791 23 Oct 06 olle 56 package se.lu.thep.coreftpd.webserver;
790 23 Oct 06 olle 57
791 23 Oct 06 olle 58 import se.lu.thep.coreftpd.common.HostInfo;
791 23 Oct 06 olle 59 import se.lu.thep.coreftpd.ftp_server.FTPNewConnection;
791 23 Oct 06 olle 60 import se.lu.thep.coreftpd.ftp_server.FTPServer;
791 23 Oct 06 olle 61 import se.lu.thep.coreftpd.ftp_server.FTPSetup;
741 10 Oct 06 olle 62 import javax.swing.UIManager;
741 10 Oct 06 olle 63
741 10 Oct 06 olle 64 /**
1652 22 May 07 gregory 65  * 
1652 22 May 07 gregory 66  * <B>About this class:</B> <BR>
1652 22 May 07 gregory 67  * This class allows users to start several different classes while running the
1652 22 May 07 gregory 68  * jar-file by passing on parameters to this class.
1652 22 May 07 gregory 69  * 
741 10 Oct 06 olle 70  * @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a>
741 10 Oct 06 olle 71  * @version 1.0
741 10 Oct 06 olle 72  */
741 10 Oct 06 olle 73
1652 22 May 07 gregory 74 final public class ClassChooser {
1652 22 May 07 gregory 75   private static boolean b_printOutput = false;
741 10 Oct 06 olle 76
790 23 Oct 06 olle 77   /**
790 23 Oct 06 olle 78    * Logger used. Used to log specific events.
790 23 Oct 06 olle 79    */
790 23 Oct 06 olle 80   private static final org.apache.log4j.Logger log = org.apache.log4j.LogManager
1652 22 May 07 gregory 81       .getLogger("se.lu.thep.coreftpd.webserver");
741 10 Oct 06 olle 82
790 23 Oct 06 olle 83   /**
1652 22 May 07 gregory 84    * Depending on what argument we get, we will start different classes.
1652 22 May 07 gregory 85    */
1676 25 May 07 olle 86   public static void main(String[] s) {
741 10 Oct 06 olle 87     try {
1652 22 May 07 gregory 88       UIManager
1652 22 May 07 gregory 89           .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // Try
1652 22 May 07 gregory 90                                               // to
1652 22 May 07 gregory 91                                               // use
1652 22 May 07 gregory 92                                               // Windows
1652 22 May 07 gregory 93                                               // interface
1652 22 May 07 gregory 94     } catch (Exception e) {
741 10 Oct 06 olle 95     }
741 10 Oct 06 olle 96
1652 22 May 07 gregory 97     if (s.length == 0) {
741 10 Oct 06 olle 98       new Start();
1652 22 May 07 gregory 99     } else {
741 10 Oct 06 olle 100       if (s[0].equalsIgnoreCase("Setup"))
741 10 Oct 06 olle 101         startSetup(getArrayExceptFirstArgument(s));
741 10 Oct 06 olle 102       else if (s[0].equalsIgnoreCase("Server"))
741 10 Oct 06 olle 103         startServer(getArrayExceptFirstArgument(s));
741 10 Oct 06 olle 104       else if (s[0].equalsIgnoreCase("FTPSetup"))
741 10 Oct 06 olle 105         startFTPSetup(getArrayExceptFirstArgument(s));
741 10 Oct 06 olle 106       else if (s[0].equalsIgnoreCase("FTPServer"))
741 10 Oct 06 olle 107         startFTPServer(getArrayExceptFirstArgument(s));
1652 22 May 07 gregory 108       else {
741 10 Oct 06 olle 109         System.out.println("Valid arguments are:");
741 10 Oct 06 olle 110         System.out.println("Server    - Start Web Server");
741 10 Oct 06 olle 111         System.out.println("Setup     - Web Server Setup");
741 10 Oct 06 olle 112         System.out.println("FTPServer - Start FTP Server");
741 10 Oct 06 olle 113         System.out.println("FTPSetup  - FTP Server Setup");
741 10 Oct 06 olle 114       }
741 10 Oct 06 olle 115     }
741 10 Oct 06 olle 116   }
741 10 Oct 06 olle 117
741 10 Oct 06 olle 118   /**
1652 22 May 07 gregory 119    * Returns an array which does not contain the first element. ([1,2,3] ==>
1652 22 May 07 gregory 120    * [2,3])
1652 22 May 07 gregory 121    */
1652 22 May 07 gregory 122   private static String[] getArrayExceptFirstArgument(String[] o1) {
1652 22 May 07 gregory 123     if (o1.length == 0) {
741 10 Oct 06 olle 124       return null;
1652 22 May 07 gregory 125     } else {
1652 22 May 07 gregory 126       String[] o2 = new String[o1.length - 1];
741 10 Oct 06 olle 127
1652 22 May 07 gregory 128       for (int i = 0; i < o1.length - 1; i++) {
1652 22 May 07 gregory 129         o2[i] = o1[i + 1];
741 10 Oct 06 olle 130       }
741 10 Oct 06 olle 131
741 10 Oct 06 olle 132       return o2;
741 10 Oct 06 olle 133     }
741 10 Oct 06 olle 134   }
741 10 Oct 06 olle 135
1652 22 May 07 gregory 136   private static void startServer(String[] s) {
1652 22 May 07 gregory 137     boolean b_startServer = true;
1652 22 May 07 gregory 138     b_printOutput = true;
1652 22 May 07 gregory 139     int choice = -1;
741 10 Oct 06 olle 140
1652 22 May 07 gregory 141     if (s.length != 0) {
1652 22 May 07 gregory 142       for (int i = 0; i < s.length; i++) {
1652 22 May 07 gregory 143         if (s[i].equals("-nw")) {
1652 22 May 07 gregory 144           choice = 0;
1652 22 May 07 gregory 145         } else if (s[i].startsWith("-s")) {
1652 22 May 07 gregory 146           try {
1652 22 May 07 gregory 147             choice = Integer.parseInt(s[i].substring(2));
1652 22 May 07 gregory 148           } catch (Exception e) {
1652 22 May 07 gregory 149             b_startServer = false;
741 10 Oct 06 olle 150             showOutput("Invalid startup mode.");
741 10 Oct 06 olle 151             showOutput("The flag given should be \"-sX\" where X is 0, 1, 2 or 3.");
741 10 Oct 06 olle 152           }
1652 22 May 07 gregory 153         } else// if (s[i].equals("-help") || s[i].equals("-h"))
741 10 Oct 06 olle 154         {
1652 22 May 07 gregory 155           b_startServer = false;
741 10 Oct 06 olle 156           showOutput("Flags:");
741 10 Oct 06 olle 157           showOutput("-h or -help = Show this help text.");
741 10 Oct 06 olle 158           showOutput("-nw = is equivalent to -s0");
741 10 Oct 06 olle 159           showOutput("-sX = Startup mode where X shall be 0, 1, 2 or 3:");
741 10 Oct 06 olle 160           showOutput("    = 0 means start with no GUI.");
741 10 Oct 06 olle 161           showOutput("    = 1 means start with a basic AWT-interface.");
741 10 Oct 06 olle 162           showOutput("    = 2 means start with an advanced Swing-interface (not minimized at startup).");
741 10 Oct 06 olle 163           showOutput("    = 3 means start with an advanced Swing-interface (minimized at startup).");
741 10 Oct 06 olle 164         }
741 10 Oct 06 olle 165       }
741 10 Oct 06 olle 166     }
741 10 Oct 06 olle 167
1652 22 May 07 gregory 168     if (choice != -1
1652 22 May 07 gregory 169         && (choice != 0 && choice != 1 && choice != 2 && choice != 3)) {
1652 22 May 07 gregory 170       b_startServer = false;
741 10 Oct 06 olle 171       showOutput("The flag given should be \"-sX\" where X is 0, 1, 2 or 3.");
741 10 Oct 06 olle 172     }
741 10 Oct 06 olle 173
1652 22 May 07 gregory 174     if (b_startServer) {
741 10 Oct 06 olle 175       new Start(choice);
741 10 Oct 06 olle 176     }
741 10 Oct 06 olle 177   }
741 10 Oct 06 olle 178
1652 22 May 07 gregory 179   private static void startSetup(String[] s) {
1652 22 May 07 gregory 180     boolean b_startSetup = true;
1652 22 May 07 gregory 181     boolean showAWTWindow = true;
1652 22 May 07 gregory 182     boolean b_startRemote = false;
1652 22 May 07 gregory 183     int port = -1;
1652 22 May 07 gregory 184     b_printOutput = true;
741 10 Oct 06 olle 185
1652 22 May 07 gregory 186     if (s.length != 0) {
1652 22 May 07 gregory 187       for (int i = 0; i < s.length; i++) {
1652 22 May 07 gregory 188         if (s[i].equals("-nw")) {
1652 22 May 07 gregory 189           showAWTWindow = false;
1652 22 May 07 gregory 190         } else if (s[i].startsWith("-p")) {
1652 22 May 07 gregory 191           try {
1652 22 May 07 gregory 192             port = Integer.parseInt(s[i].substring(2));
1652 22 May 07 gregory 193             if (port <= 0)
1652 22 May 07 gregory 194               throw new Exception(
1652 22 May 07 gregory 195                   "Please use a positive integer when you set port.");
1652 22 May 07 gregory 196           } catch (Exception e) {
1652 22 May 07 gregory 197             b_startSetup = false;
741 10 Oct 06 olle 198             showOutput("Invalid port.");
741 10 Oct 06 olle 199             showOutput("The flag given should be \"-pXX\" where XX is a port number.");
741 10 Oct 06 olle 200           }
1652 22 May 07 gregory 201         } else if (s[i].equals("-remote") || s[i].equals("-r")) {
1652 22 May 07 gregory 202           b_startRemote = true;
1652 22 May 07 gregory 203         } else// if (s[i].equals("-help") || s[i].equals("-h"))
741 10 Oct 06 olle 204         {
1652 22 May 07 gregory 205           b_startSetup = false;
741 10 Oct 06 olle 206           showOutput("Flags:");
741 10 Oct 06 olle 207           showOutput("-h or -help = Show this help text.");
741 10 Oct 06 olle 208           showOutput("-nw = Start setup with no GUI.");
741 10 Oct 06 olle 209           showOutput("-pXX = Start setup on port XX.");
741 10 Oct 06 olle 210           showOutput("-r or -remote = Allow all IP-addresses to change settings in setup.");
741 10 Oct 06 olle 211         }
741 10 Oct 06 olle 212       }
741 10 Oct 06 olle 213     }
741 10 Oct 06 olle 214
1652 22 May 07 gregory 215     if (b_startSetup) {
1652 22 May 07 gregory 216       new Thread(
1652 22 May 07 gregory 217           new SetupXerverKernel(showAWTWindow, port, b_startRemote))
1652 22 May 07 gregory 218           .start();
741 10 Oct 06 olle 219     }
741 10 Oct 06 olle 220   }
741 10 Oct 06 olle 221
1652 22 May 07 gregory 222   private static void startFTPServer(String[] s) {
1652 22 May 07 gregory 223     FTPServer server = null;
1652 22 May 07 gregory 224     boolean b_startRemote = false;
1652 22 May 07 gregory 225     boolean b_startServer = true;
1652 22 May 07 gregory 226     int port = -1;
2668 07 May 08 olle 227     String tmpDirPath = null;
1652 22 May 07 gregory 228     b_printOutput = true;
741 10 Oct 06 olle 229
1652 22 May 07 gregory 230     if (s.length != 0) {
1652 22 May 07 gregory 231       for (int i = 0; i < s.length; i++) {
1652 22 May 07 gregory 232         if (s[i].equals("-c") || s[i].equals("-commands")) {
1652 22 May 07 gregory 233           FTPNewConnection.b_showAllCommands = true;
1652 22 May 07 gregory 234         } else if (s[i].startsWith("-p")) {
1652 22 May 07 gregory 235           try {
1652 22 May 07 gregory 236             port = Integer.parseInt(s[i].substring(2));
1652 22 May 07 gregory 237             if (port <= 0)
1652 22 May 07 gregory 238               throw new Exception(
1652 22 May 07 gregory 239                   "Please use a positive integer when you set port.");
1652 22 May 07 gregory 240           } catch (Exception e) {
1652 22 May 07 gregory 241             b_startServer = false;
741 10 Oct 06 olle 242             showOutput("Invalid port.");
741 10 Oct 06 olle 243             showOutput("The flag given should be \"-pXX\" where XX is a port number.");
741 10 Oct 06 olle 244           }
2668 07 May 08 olle 245         } else if (s[i].startsWith("-t")) {
2668 07 May 08 olle 246           // Path to directory for temporary files
2668 07 May 08 olle 247           if (s[i].length() > 2)
2668 07 May 08 olle 248           {
2668 07 May 08 olle 249             tmpDirPath = s[i].substring(2);
2668 07 May 08 olle 250           }
1652 22 May 07 gregory 251         } else// if (s[i].equals("-help") || s[i].equals("-h"))
741 10 Oct 06 olle 252         {
1652 22 May 07 gregory 253           b_startServer = false;
741 10 Oct 06 olle 254           showOutput("Flags:");
741 10 Oct 06 olle 255           showOutput("-c or -commands = Print all requests and responses to output.");
741 10 Oct 06 olle 256           showOutput("-h or -help = Show this help text.");
741 10 Oct 06 olle 257           showOutput("-pXX = Start FTP server on port XX.");
741 10 Oct 06 olle 258         }
741 10 Oct 06 olle 259       }
741 10 Oct 06 olle 260     }
741 10 Oct 06 olle 261
1652 22 May 07 gregory 262     if (b_startServer) {
790 23 Oct 06 olle 263       /*
790 23 Oct 06 olle 264        * Set FTPServer SessionControl class variable
790 23 Oct 06 olle 265        */
790 23 Oct 06 olle 266       // *** Debug Info
1652 22 May 07 gregory 267       log
1652 22 May 07 gregory 268           .info("ClassChooser::startFTPServer(): server=new FTPserver(port); port = "
1652 22 May 07 gregory 269               + port);
1652 22 May 07 gregory 270       server = new FTPServer(port);
2668 07 May 08 olle 271       server.setTmpDirPath(tmpDirPath);
741 10 Oct 06 olle 272       server.startFTPServer();
1652 22 May 07 gregory 273       if (!server.getPortError()) {
1652 22 May 07 gregory 274         String s_IP = HostInfo.getIPorLocalhost();
741 10 Oct 06 olle 275         showOutput("FTPServer is now running.");
1652 22 May 07 gregory 276         showOutput("Host: " + s_IP);
1652 22 May 07 gregory 277         showOutput("Port: " + server.getPort());
741 10 Oct 06 olle 278
741 10 Oct 06 olle 279         server.start();
1652 22 May 07 gregory 280       } else {
741 10 Oct 06 olle 281         if (server.isPortInUse())
1652 22 May 07 gregory 282           showOutput("Port "
1652 22 May 07 gregory 283               + server.getPort()
1652 22 May 07 gregory 284               + " is already in use.\nPlease use setup to choose another port.");
741 10 Oct 06 olle 285         else
1652 22 May 07 gregory 286           showOutput("Port "
1652 22 May 07 gregory 287               + server.getPort()
1652 22 May 07 gregory 288               + " is an invalid port number.\nPlease use setup to choose another port number.");
741 10 Oct 06 olle 289       }
741 10 Oct 06 olle 290     }
741 10 Oct 06 olle 291   }
741 10 Oct 06 olle 292
1652 22 May 07 gregory 293   private static void startFTPSetup(String[] s) {
1652 22 May 07 gregory 294     FTPSetup setup = null;
1652 22 May 07 gregory 295     boolean b_startRemote = false;
1652 22 May 07 gregory 296     boolean b_startServer = true;
1652 22 May 07 gregory 297     int port = FTPSetup.i_portNr;
1652 22 May 07 gregory 298     b_printOutput = true;
1652 22 May 07 gregory 299     FTPSetup.b_printOutput = true; // Note, we must set this to true as
1652 22 May 07 gregory 300                     // well
741 10 Oct 06 olle 301
1652 22 May 07 gregory 302     if (s.length != 0) {
1652 22 May 07 gregory 303       for (int i = 0; i < s.length; i++) {
1652 22 May 07 gregory 304         if (s[i].equals("-p")) {
1652 22 May 07 gregory 305           try {
1652 22 May 07 gregory 306             port = Integer.parseInt(s[i].substring(2));
1652 22 May 07 gregory 307           } catch (Exception e) {
1652 22 May 07 gregory 308             b_startServer = false;
741 10 Oct 06 olle 309             showOutput("Invalid port.");
741 10 Oct 06 olle 310             showOutput("The flag given should be \"-pXX\" where XX is a port number.");
741 10 Oct 06 olle 311           }
1652 22 May 07 gregory 312         } else if (s[i].equals("-remote") || s[i].equals("-r")) {
1652 22 May 07 gregory 313           b_startRemote = true;
1652 22 May 07 gregory 314         } else // if (s[i].equals("-help") || s[i].equals("-h"))
741 10 Oct 06 olle 315         {
1652 22 May 07 gregory 316           b_startServer = false;
741 10 Oct 06 olle 317           showOutput("Flags:");
741 10 Oct 06 olle 318           showOutput("-h or -help = Show this help text.");
1652 22 May 07 gregory 319           showOutput("-pXX = Start setup on port XX, not "
1652 22 May 07 gregory 320               + FTPSetup.i_portNr + " which is default.");
741 10 Oct 06 olle 321           showOutput("-r or -remote = Allow all IP-addresses to change settings in setup.");
741 10 Oct 06 olle 322         }
741 10 Oct 06 olle 323       }
741 10 Oct 06 olle 324     }
741 10 Oct 06 olle 325
1652 22 May 07 gregory 326     if (b_startServer) {
1652 22 May 07 gregory 327       setup = new FTPSetup(port);
741 10 Oct 06 olle 328
1652 22 May 07 gregory 329       if (!setup.getPortError()) {
1652 22 May 07 gregory 330         String s_IP = HostInfo.getIPorLocalhost();
741 10 Oct 06 olle 331         showOutput("FTPSetup is now running, please visit:");
1652 22 May 07 gregory 332         showOutput("http://" + s_IP + ":" + port + "/");
1652 22 May 07 gregory 333       } else {
741 10 Oct 06 olle 334         if (setup.isPortInUse())
1652 22 May 07 gregory 335           showOutput("Port "
1652 22 May 07 gregory 336               + port
1652 22 May 07 gregory 337               + " is already in use.\nXerver FTP Setup is probably already running.");
741 10 Oct 06 olle 338         else
1652 22 May 07 gregory 339           showOutput("Port "
1652 22 May 07 gregory 340               + port
1652 22 May 07 gregory 341               + " is an invalid port number.\nPlease choose another port number.");
741 10 Oct 06 olle 342       }
741 10 Oct 06 olle 343     }
741 10 Oct 06 olle 344
1652 22 May 07 gregory 345     if (b_startRemote && b_startServer) {
1652 22 May 07 gregory 346       // This inside the other if-statement because here we are 100% sure
1652 22 May 07 gregory 347       // "setup!=null"... (unnecessary?)
1652 22 May 07 gregory 348       if (!setup.getPortError()) {
741 10 Oct 06 olle 349         showOutput("Remote setup has been started.");
741 10 Oct 06 olle 350         setup.startFTPSetup();
741 10 Oct 06 olle 351       }
741 10 Oct 06 olle 352     }
741 10 Oct 06 olle 353
741 10 Oct 06 olle 354     if (b_startServer)
741 10 Oct 06 olle 355       setup.start();
741 10 Oct 06 olle 356   }
741 10 Oct 06 olle 357
1652 22 May 07 gregory 358   private static void showOutput(String txt) {
741 10 Oct 06 olle 359     if (b_printOutput)
741 10 Oct 06 olle 360       System.out.println(txt);
741 10 Oct 06 olle 361   }
741 10 Oct 06 olle 362 }