client/ftpd/src/se/lu/thep/coreftpd/ftp_server/FTPServer.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
1652 22 May 07 gregory 56 package se.lu.thep.coreftpd.ftp_server;
741 10 Oct 06 olle 57
791 23 Oct 06 olle 58 import se.lu.thep.coreftpd.common.HostInfo;
2668 07 May 08 olle 59
2668 07 May 08 olle 60 import java.io.FileInputStream;
2668 07 May 08 olle 61 import java.io.FileWriter;
2668 07 May 08 olle 62 import java.io.IOException;
2668 07 May 08 olle 63 import java.io.InputStream;
741 10 Oct 06 olle 64 import java.net.BindException;
741 10 Oct 06 olle 65 import java.net.ServerSocket;
741 10 Oct 06 olle 66 import java.net.Socket;
741 10 Oct 06 olle 67
741 10 Oct 06 olle 68 /**
1652 22 May 07 gregory 69  * 
1652 22 May 07 gregory 70  * <B>About this class:</B> <BR>
1652 22 May 07 gregory 71  * Run this file when you want to start Xerver FTP server. <BR>
741 10 Oct 06 olle 72  * Note that you can pass arguments to this file when you run its main method.
1652 22 May 07 gregory 73  * 
1652 22 May 07 gregory 74  * 
741 10 Oct 06 olle 75  * @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a>
741 10 Oct 06 olle 76  * @version 1.0
741 10 Oct 06 olle 77  */
741 10 Oct 06 olle 78
1652 22 May 07 gregory 79 final public class FTPServer extends Thread {
1652 22 May 07 gregory 80   private static final boolean b_showErrors = false;
741 10 Oct 06 olle 81
1652 22 May 07 gregory 82   private static FTPSettings FTPS_settings = null;
1652 22 May 07 gregory 83
1652 22 May 07 gregory 84   private static FTPServer FTPS_server = null;
1652 22 May 07 gregory 85
741 10 Oct 06 olle 86   private ServerSocket myServerSocket;
1652 22 May 07 gregory 87
1652 22 May 07 gregory 88   public static boolean isAlive = false;
1652 22 May 07 gregory 89
741 10 Oct 06 olle 90   private int i_portNr;
1652 22 May 07 gregory 91
1652 22 May 07 gregory 92   private boolean b_portError = false;
1652 22 May 07 gregory 93
1652 22 May 07 gregory 94   private boolean b_portInUse = false;
1652 22 May 07 gregory 95
1652 22 May 07 gregory 96   private static boolean b_printOutput = false;
2668 07 May 08 olle 97   
2668 07 May 08 olle 98   private String tmpDirPath = null;
2668 07 May 08 olle 99   
2668 07 May 08 olle 100   public static final String LOCK_FILE_BASENAME = new String("proteios_ftp");
1652 22 May 07 gregory 101
790 23 Oct 06 olle 102   /**
790 23 Oct 06 olle 103    * Logger used. Used to log specific events.
790 23 Oct 06 olle 104    */
790 23 Oct 06 olle 105   private static final org.apache.log4j.Logger log = org.apache.log4j.LogManager
1652 22 May 07 gregory 106       .getLogger("se.lu.thep.coreftpd.ftp_server");
741 10 Oct 06 olle 107
1652 22 May 07 gregory 108   public static void main(String[] s) {
1652 22 May 07 gregory 109     FTPServer server = null;
1652 22 May 07 gregory 110     // boolean b_startRemote=false;
1652 22 May 07 gregory 111     boolean b_startServer = true;
1652 22 May 07 gregory 112     int port = -1;
2668 07 May 08 olle 113     String tmpDirPathArg = null;
1652 22 May 07 gregory 114     b_printOutput = true;
741 10 Oct 06 olle 115
790 23 Oct 06 olle 116     // *** Debug Info
2668 07 May 08 olle 117     //System.out.println("FTPServer::main(): s.length = " + s.length);
2668 07 May 08 olle 118     log.debug("s.length = " + s.length);
1652 22 May 07 gregory 119     if (s.length != 0) {
1652 22 May 07 gregory 120       for (int i = 0; i < s.length; i++) {
790 23 Oct 06 olle 121         // *** Debug Info
2668 07 May 08 olle 122         //System.out.println("FTPServer::main(): s[" + i + "] = \"" + s[i] + "\"");
2668 07 May 08 olle 123         log.debug("s[" + i + "] = \"" + s[i] + "\"");
1652 22 May 07 gregory 124         if (s[i].startsWith("-c") || s[i].startsWith("-commands")) {
1652 22 May 07 gregory 125           FTPNewConnection.b_showAllCommands = true;
1652 22 May 07 gregory 126         } else if (s[i].startsWith("-p")) {
1652 22 May 07 gregory 127           try {
1652 22 May 07 gregory 128             port = Integer.parseInt(s[i].substring(2));
1652 22 May 07 gregory 129             if (port <= 0)
1652 22 May 07 gregory 130               throw new Exception(
1652 22 May 07 gregory 131                   "Please use a positive integer when you set port.");
1652 22 May 07 gregory 132           } catch (Exception e) {
1652 22 May 07 gregory 133             b_startServer = false;
741 10 Oct 06 olle 134             showOutput("Invalid port.");
741 10 Oct 06 olle 135             showOutput("The flag given should be \"-pXX\" where XX is a port number.");
741 10 Oct 06 olle 136           }
2668 07 May 08 olle 137         } else if (s[i].startsWith("-t")) {
2668 07 May 08 olle 138           // Path to directory for temporary files
2668 07 May 08 olle 139           if (s[i].length() > 2)
2668 07 May 08 olle 140           {
2668 07 May 08 olle 141             tmpDirPathArg = s[i].substring(2);
2668 07 May 08 olle 142           }
1652 22 May 07 gregory 143         } else// if (s[i].equals("-help") || s[i].equals("-h"))
741 10 Oct 06 olle 144         {
1652 22 May 07 gregory 145           b_startServer = false;
741 10 Oct 06 olle 146           showOutput("Flags:");
741 10 Oct 06 olle 147           showOutput("-c or -commands = Print all requests and responses to output.");
741 10 Oct 06 olle 148           showOutput("-h or -help = Show this help text.");
741 10 Oct 06 olle 149           showOutput("-pXX = Start FTP server on port XX.");
741 10 Oct 06 olle 150         }
741 10 Oct 06 olle 151       }
741 10 Oct 06 olle 152     }
741 10 Oct 06 olle 153
790 23 Oct 06 olle 154     // *** Debug Info
2668 07 May 08 olle 155     log.info("b_startServer = " + b_startServer);
1652 22 May 07 gregory 156     if (b_startServer) {
1652 22 May 07 gregory 157       showOutput("FTPServer::main(): server = new FTPServer(port); port = "
1652 22 May 07 gregory 158           + port);
1652 22 May 07 gregory 159       server = new FTPServer(port);
2668 07 May 08 olle 160       server.setTmpDirPath(tmpDirPathArg);
741 10 Oct 06 olle 161       server.startFTPServer();
1652 22 May 07 gregory 162       if (!server.getPortError()) {
1652 22 May 07 gregory 163         String s_IP = HostInfo.getIPorLocalhost();
741 10 Oct 06 olle 164         showOutput("FTPServer is now running.");
1652 22 May 07 gregory 165         showOutput("Host: " + s_IP);
1652 22 May 07 gregory 166         showOutput("Port: " + server.i_portNr);
741 10 Oct 06 olle 167
741 10 Oct 06 olle 168         server.start();
1652 22 May 07 gregory 169       } else {
741 10 Oct 06 olle 170         if (server.b_portInUse)
1652 22 May 07 gregory 171           showOutput("Port "
1652 22 May 07 gregory 172               + server.i_portNr
1652 22 May 07 gregory 173               + " is already in use.\nPlease use setup to choose another port.");
741 10 Oct 06 olle 174         else
1652 22 May 07 gregory 175           showOutput("Port "
1652 22 May 07 gregory 176               + server.i_portNr
1652 22 May 07 gregory 177               + " is an invalid port number.\nPlease use setup to choose another port number.");
741 10 Oct 06 olle 178       }
741 10 Oct 06 olle 179     }
741 10 Oct 06 olle 180
741 10 Oct 06 olle 181   }
741 10 Oct 06 olle 182
1652 22 May 07 gregory 183   public FTPServer() {
1652 22 May 07 gregory 184     this(-1);
741 10 Oct 06 olle 185   }
741 10 Oct 06 olle 186
1652 22 May 07 gregory 187   public FTPServer(int i_argPort) {
790 23 Oct 06 olle 188     // *** Debug Info
2668 07 May 08 olle 189     log.info("Start - i_argPort = " + i_argPort);
1652 22 May 07 gregory 190     try {
1652 22 May 07 gregory 191       FTPS_server = this;
1652 22 May 07 gregory 192       FTPS_settings = getFTPSettings();
741 10 Oct 06 olle 193
741 10 Oct 06 olle 194       initSettings();
741 10 Oct 06 olle 195
1652 22 May 07 gregory 196       if (i_argPort != -1) // If we have started with "-pXX" then use
1652 22 May 07 gregory 197                   // this port
1652 22 May 07 gregory 198         i_portNr = i_argPort;
741 10 Oct 06 olle 199
790 23 Oct 06 olle 200       // *** Debug Info
1652 22 May 07 gregory 201       log
2668 07 May 08 olle 202           .debug("Before setting FTPNewConnection.s_serverOuterIP - i_portNr = "
1652 22 May 07 gregory 203               + i_portNr);
1652 22 May 07 gregory 204       FTPNewConnection.s_serverOuterIP = HostInfo.getIP().replace('.',
1652 22 May 07 gregory 205           ',');
790 23 Oct 06 olle 206       // *** Debug Info
1652 22 May 07 gregory 207       log
2668 07 May 08 olle 208           .debug("FTPNewConnection.s_serverOuterIP = \""
1652 22 May 07 gregory 209               + FTPNewConnection.s_serverOuterIP + "\"");
1652 22 May 07 gregory 210       log
2668 07 May 08 olle 211           .debug("Before setting FTPNewConnection.s_serverLocalIP - i_portNr = "
1652 22 May 07 gregory 212               + i_portNr);
1652 22 May 07 gregory 213       FTPNewConnection.s_serverLocalIP = HostInfo.getLocalIPorLocalhost()
1652 22 May 07 gregory 214           .replace('.', ',');
790 23 Oct 06 olle 215       // *** Debug Info
1652 22 May 07 gregory 216       log
2668 07 May 08 olle 217           .debug("FTPNewConnection.s_serverLocalIP = \""
1652 22 May 07 gregory 218               + FTPNewConnection.s_serverLocalIP + "\"");
741 10 Oct 06 olle 219
790 23 Oct 06 olle 220       // *** Debug Info
1652 22 May 07 gregory 221       log
2668 07 May 08 olle 222           .debug("Before listenForConnections - i_portNr = "
1652 22 May 07 gregory 223               + i_portNr);
741 10 Oct 06 olle 224       listenForConnections();
1652 22 May 07 gregory 225     } catch (Exception e) {
790 23 Oct 06 olle 226       // *** Debug Info
2668 07 May 08 olle 227       log.debug("Exception when trying to start FTP server on port " + i_portNr + ": " + e);
741 10 Oct 06 olle 228       showError(e);
741 10 Oct 06 olle 229     }
741 10 Oct 06 olle 230   }
741 10 Oct 06 olle 231
1652 22 May 07 gregory 232   public void listenForConnections() {
1652 22 May 07 gregory 233     b_portError = false;
1652 22 May 07 gregory 234     b_portInUse = false;
1652 22 May 07 gregory 235     try {
1652 22 May 07 gregory 236       try {
1652 22 May 07 gregory 237         if (myServerSocket != null)
741 10 Oct 06 olle 238           myServerSocket.close();
741 10 Oct 06 olle 239
741 10 Oct 06 olle 240         myServerSocket = new ServerSocket(i_portNr);
1652 22 May 07 gregory 241       } catch (BindException be) // port is in use
741 10 Oct 06 olle 242       {
1652 22 May 07 gregory 243         b_portError = true;
1652 22 May 07 gregory 244         b_portInUse = true;
1652 22 May 07 gregory 245       } catch (IllegalArgumentException iae) // port number is invalid,
1652 22 May 07 gregory 246                           // for example if
1652 22 May 07 gregory 247                           // port=999999
741 10 Oct 06 olle 248       {
1652 22 May 07 gregory 249         b_portError = true;
741 10 Oct 06 olle 250       }
1652 22 May 07 gregory 251     } catch (Exception e) {
741 10 Oct 06 olle 252       showError(e);
741 10 Oct 06 olle 253     }
741 10 Oct 06 olle 254   }
741 10 Oct 06 olle 255
1652 22 May 07 gregory 256   public void initSettings() {
1221 18 Feb 07 gregory 257     FTPNewConnection.b_guestAccountExists = FTPSettings.b_guestAccountExists;
1652 22 May 07 gregory 258     FTPNewConnection.i_howToShowAlias = FTPSettings.i_howToShowAlias;
1652 22 May 07 gregory 259     FTPNewConnection.i_maxTimeToIdle = FTPSettings.i_maxTimeToIdle;
1652 22 May 07 gregory 260     FTPNewConnection.i_maxNumberOfNOOP = FTPSettings.i_maxNumberOfNOOP;
1652 22 May 07 gregory 261     FTPDataConnection.i_dataPortNr = FTPSettings.i_dataPortNr;
2703 16 May 08 olle 262     FTPDataConnection.ai_portRange = FTPS_settings.ai_portRange;
741 10 Oct 06 olle 263
1652 22 May 07 gregory 264     i_portNr = FTPSettings.i_portNr; // This is here so that if we change
1652 22 May 07 gregory 265                       // port in setup we will start use
1652 22 May 07 gregory 266                       // the new port immediately, no
1652 22 May 07 gregory 267                       // matter if we have used "-pXX" as
1652 22 May 07 gregory 268                       // a flag.
741 10 Oct 06 olle 269   }
741 10 Oct 06 olle 270
2386 14 Nov 07 gregory 271   @Override
1652 22 May 07 gregory 272   public void run() {
741 10 Oct 06 olle 273     Socket so_userConnection;
1652 22 May 07 gregory 274     if (!b_portInUse && !b_portError) // If [port is not in use] and [port
1652 22 May 07 gregory 275                       // is valid (not like port 999999)]
741 10 Oct 06 olle 276     {
2668 07 May 08 olle 277       // Create lock file for Proteios FTP server
2668 07 May 08 olle 278       createLockFile();
1652 22 May 07 gregory 279       while (true) // IMPORTANT!!! make sure this loop won't go around
1652 22 May 07 gregory 280               // too many times without doing anything or java
1652 22 May 07 gregory 281               // will use up to 100% CPU. Instead let the loop be
1652 22 May 07 gregory 282               // halted by for example an accept call which will
1652 22 May 07 gregory 283               // wait for a network connection to be
1652 22 May 07 gregory 284               // established...
741 10 Oct 06 olle 285       {
1652 22 May 07 gregory 286         try {
1652 22 May 07 gregory 287           // System.out.println("V�ntar p� connection...");
1652 22 May 07 gregory 288           try {
1652 22 May 07 gregory 289             so_userConnection = myServerSocket.accept(); // Wait
1652 22 May 07 gregory 290                                     // until
1652 22 May 07 gregory 291                                     // a
1652 22 May 07 gregory 292                                     // request
1652 22 May 07 gregory 293                                     // is
1652 22 May 07 gregory 294                                     // sent
1652 22 May 07 gregory 295                                     // to
1652 22 May 07 gregory 296                                     // the
1652 22 May 07 gregory 297                                     // server
1652 22 May 07 gregory 298                                     // (with
1652 22 May 07 gregory 299                                     // "myServerSocket.setSoTimeout()"
1652 22 May 07 gregory 300                                     // we
1652 22 May 07 gregory 301                                     // can
1652 22 May 07 gregory 302                                     // limit
1652 22 May 07 gregory 303                                     // the
1652 22 May 07 gregory 304                                     // time
1652 22 May 07 gregory 305                                     // we
1652 22 May 07 gregory 306                                     // wait
1652 22 May 07 gregory 307                                     // for
1652 22 May 07 gregory 308                                     // "accept")
1652 22 May 07 gregory 309           } catch (Exception e) // If the port is already in use...
1652 22 May 07 gregory 310                       // (However, we should never be able
1652 22 May 07 gregory 311                       // to get in here, but just to be
1652 22 May 07 gregory 312                       // safe)
741 10 Oct 06 olle 313           {
741 10 Oct 06 olle 314             stopFTPServer();
741 10 Oct 06 olle 315             break;
741 10 Oct 06 olle 316           }
741 10 Oct 06 olle 317
1652 22 May 07 gregory 318           if (so_userConnection == null) {
741 10 Oct 06 olle 319
2668 07 May 08 olle 320           //} else if (isAlive) {
2668 07 May 08 olle 321           } else if (lockFileExists()) {
1652 22 May 07 gregory 322             so_userConnection
1652 22 May 07 gregory 323                 .setSoTimeout(FTPNewConnection.i_maxTimeToIdle + 1); // "+1"
1652 22 May 07 gregory 324                                             // to
1652 22 May 07 gregory 325                                             // make
1652 22 May 07 gregory 326                                             // sure
1652 22 May 07 gregory 327                                             // that
1652 22 May 07 gregory 328                                             // MAX_IDLE_TIME
1652 22 May 07 gregory 329                                             // seconds
1652 22 May 07 gregory 330                                             // really
1652 22 May 07 gregory 331                                             // has
1652 22 May 07 gregory 332                                             // passed...
1652 22 May 07 gregory 333                                             // (this
1652 22 May 07 gregory 334                                             // might
1652 22 May 07 gregory 335                                             // make
1652 22 May 07 gregory 336                                             // that
1652 22 May 07 gregory 337                                             // we
1652 22 May 07 gregory 338                                             // disconnect
1652 22 May 07 gregory 339                                             // the
1652 22 May 07 gregory 340                                             // user
1652 22 May 07 gregory 341                                             // after
1652 22 May 07 gregory 342                                             // approximately
1652 22 May 07 gregory 343                                             // MAX_IDLE_TIME+1
1652 22 May 07 gregory 344                                             // seconds,
1652 22 May 07 gregory 345                                             // but
1652 22 May 07 gregory 346                                             // that's
1652 22 May 07 gregory 347                                             // better
1652 22 May 07 gregory 348                                             // than
1652 22 May 07 gregory 349                                             // disconnecting
1652 22 May 07 gregory 350                                             // the
1652 22 May 07 gregory 351                                             // user
1652 22 May 07 gregory 352                                             // after
1652 22 May 07 gregory 353                                             // approximately
1652 22 May 07 gregory 354                                             // MAX_IDLE_TIME*2
1652 22 May 07 gregory 355                                             // seconds.
741 10 Oct 06 olle 356             (new FTPNewConnection(so_userConnection)).start();
1652 22 May 07 gregory 357           } else {
741 10 Oct 06 olle 358             so_userConnection.close();
2668 07 May 08 olle 359             // Stop FTP Server
2668 07 May 08 olle 360             stopFTPServer();
2668 07 May 08 olle 361             break;
741 10 Oct 06 olle 362           }
1652 22 May 07 gregory 363         } catch (Exception e) {
1652 22 May 07 gregory 364           showError(e);
1652 22 May 07 gregory 365         }
741 10 Oct 06 olle 366       }
741 10 Oct 06 olle 367     }
741 10 Oct 06 olle 368   }
741 10 Oct 06 olle 369
1652 22 May 07 gregory 370   private void showError(Exception e) {
1652 22 May 07 gregory 371     if (b_showErrors) {
741 10 Oct 06 olle 372       System.out.println("Error: " + e);
741 10 Oct 06 olle 373       e.printStackTrace();
741 10 Oct 06 olle 374     }
741 10 Oct 06 olle 375   }
741 10 Oct 06 olle 376
1652 22 May 07 gregory 377   private static void showOutput(String txt) {
741 10 Oct 06 olle 378     if (b_printOutput)
741 10 Oct 06 olle 379       System.out.println(txt);
741 10 Oct 06 olle 380   }
741 10 Oct 06 olle 381
1652 22 May 07 gregory 382   public void startFTPServer() {
1652 22 May 07 gregory 383     isAlive = true;
2668 07 May 08 olle 384     //System.out.println("FTPServer::startFTPServer(): getTmpDirPath() = \"" + getTmpDirPath() + "\"");
2668 07 May 08 olle 385     log.debug("getTmpDirPath() = \"" + getTmpDirPath() + "\"");
741 10 Oct 06 olle 386   }
741 10 Oct 06 olle 387
1652 22 May 07 gregory 388   public void stopFTPServer() {
1652 22 May 07 gregory 389     isAlive = false;
2668 07 May 08 olle 390     //System.out.println("FTPServer::stopFTPServer(): getTmpDirPath() = \"" + getTmpDirPath() + "\"");
2668 07 May 08 olle 391     log.debug("getTmpDirPath() = \"" + getTmpDirPath() + "\"");
2668 07 May 08 olle 392     deleteLockFile();
2668 07 May 08 olle 393     // Closing down
2668 07 May 08 olle 394     //System.out.println("FTPServer::stopFTPServer(): Calling System.exit(0)");
2668 07 May 08 olle 395     log.debug("Calling System.exit(0)");
2668 07 May 08 olle 396     System.exit(0);
741 10 Oct 06 olle 397   }
741 10 Oct 06 olle 398
1652 22 May 07 gregory 399   public int getPort() {
741 10 Oct 06 olle 400     return i_portNr;
741 10 Oct 06 olle 401   }
741 10 Oct 06 olle 402
1652 22 May 07 gregory 403   public boolean isPortInUse() {
741 10 Oct 06 olle 404     return b_portInUse;
741 10 Oct 06 olle 405   }
741 10 Oct 06 olle 406
1652 22 May 07 gregory 407   public boolean getPortError() {
741 10 Oct 06 olle 408     return b_portError;
741 10 Oct 06 olle 409   }
741 10 Oct 06 olle 410
1652 22 May 07 gregory 411   public static FTPSettings getFTPSettings() throws Exception {
1652 22 May 07 gregory 412     if (FTPS_settings == null)
1652 22 May 07 gregory 413       FTPS_settings = new FTPSettings();
741 10 Oct 06 olle 414     return FTPS_settings;
741 10 Oct 06 olle 415   }
741 10 Oct 06 olle 416
1652 22 May 07 gregory 417   public static FTPServer getFTPServer() {
741 10 Oct 06 olle 418     return FTPS_server;
741 10 Oct 06 olle 419   }
2668 07 May 08 olle 420
2668 07 May 08 olle 421
2668 07 May 08 olle 422   public String getTmpDirPath()
2668 07 May 08 olle 423   {
2668 07 May 08 olle 424     return this.tmpDirPath;
2668 07 May 08 olle 425   }
2668 07 May 08 olle 426
2668 07 May 08 olle 427
2668 07 May 08 olle 428   public void setTmpDirPath(String tmpDirPath)
2668 07 May 08 olle 429   {
2668 07 May 08 olle 430     this.tmpDirPath = tmpDirPath;
2668 07 May 08 olle 431   }
2668 07 May 08 olle 432
2668 07 May 08 olle 433
2668 07 May 08 olle 434   /**
2668 07 May 08 olle 435    * Returns the name of the lock file,
2668 07 May 08 olle 436    * concatenated from the lockfile base name,
2668 07 May 08 olle 437    * an underscore chararcter '_', the
2668 07 May 08 olle 438    * number of the port used by the FTP server,
2668 07 May 08 olle 439    * and file extension ".lck".
2668 07 May 08 olle 440    * 
2668 07 May 08 olle 441    * Example:
2668 07 May 08 olle 442    * Lock file basename = "proteios_ftp"
2668 07 May 08 olle 443    * FTP port used: 8021
2668 07 May 08 olle 444    * Lockfile name: "proteios_ftp_8021.lck"
2668 07 May 08 olle 445    * 
2668 07 May 08 olle 446    * @return String The name of the lock file
2668 07 May 08 olle 447    */
2668 07 May 08 olle 448   public String fetchLockFileName()
2668 07 May 08 olle 449   {
2668 07 May 08 olle 450     String lockFileName = new String(LOCK_FILE_BASENAME + "_" + this.i_portNr + ".lck");
2668 07 May 08 olle 451     return lockFileName;
2668 07 May 08 olle 452   }
2668 07 May 08 olle 453
2668 07 May 08 olle 454
2668 07 May 08 olle 455   private void createLockFile()
2668 07 May 08 olle 456   {
2668 07 May 08 olle 457     String lockFilePath = new String(getTmpDirPath() + java.io.File.separator + fetchLockFileName());
2668 07 May 08 olle 458     //System.out.println("FTPServer::createLockFile(): lockFilePath = \"" + lockFilePath + "\"");
2668 07 May 08 olle 459     log.debug("lockFilePath = \"" + lockFilePath + "\"");
2668 07 May 08 olle 460     java.io.File lockFile = new java.io.File(lockFilePath);
2668 07 May 08 olle 461     try
2668 07 May 08 olle 462     {
2668 07 May 08 olle 463       FileWriter fw = new FileWriter(lockFile);
2668 07 May 08 olle 464       fw.write("Proteios FTP Server lock file for port " + this.i_portNr + "\n");
2668 07 May 08 olle 465       fw.flush();
2668 07 May 08 olle 466       fw.close();
2668 07 May 08 olle 467     }
2668 07 May 08 olle 468     catch (IOException e)
2668 07 May 08 olle 469     {
2668 07 May 08 olle 470       //System.out.println("Couldn't create FTP server lock file \"" + lockFilePath + "\":" + e);
2668 07 May 08 olle 471       log.debug("Couldn't create FTP server lock file \"" + lockFilePath + "\":" + e);
2668 07 May 08 olle 472     }
2668 07 May 08 olle 473   }
2668 07 May 08 olle 474
2668 07 May 08 olle 475
2668 07 May 08 olle 476   private void deleteLockFile()
2668 07 May 08 olle 477   {
2668 07 May 08 olle 478     String lockFilePath = new String(getTmpDirPath() + java.io.File.separator + fetchLockFileName());
2668 07 May 08 olle 479     //System.out.println("FTPServer::deleteLockFile(): lockFilePath = \"" + lockFilePath + "\"");
2668 07 May 08 olle 480     log.debug("lockFilePath = \"" + lockFilePath + "\"");
2668 07 May 08 olle 481     java.io.File lockFile = new java.io.File(lockFilePath);
2668 07 May 08 olle 482     boolean fileDeleted = false;
2668 07 May 08 olle 483     try
2668 07 May 08 olle 484     {
2668 07 May 08 olle 485       fileDeleted = lockFile.delete();
2668 07 May 08 olle 486     }
2668 07 May 08 olle 487     catch (Exception e)
2668 07 May 08 olle 488     {
2668 07 May 08 olle 489       //System.out.println("Couldn't delete FTP server lock file \"" + lockFilePath + "\":" + e);
2668 07 May 08 olle 490       log.debug("Couldn't delete FTP server lock file \"" + lockFilePath + "\":" + e);
2668 07 May 08 olle 491     }
2668 07 May 08 olle 492   }
2668 07 May 08 olle 493
2668 07 May 08 olle 494
2668 07 May 08 olle 495   private boolean lockFileExists()
2668 07 May 08 olle 496   {
2668 07 May 08 olle 497     String lockFilePath = new String(getTmpDirPath() + java.io.File.separator + fetchLockFileName());
2668 07 May 08 olle 498     java.io.File lockFile = new java.io.File(lockFilePath);
2668 07 May 08 olle 499     boolean fileCheckResult = false;
2668 07 May 08 olle 500     try
2668 07 May 08 olle 501     {
2668 07 May 08 olle 502       InputStream inStream = new FileInputStream(lockFilePath);
2668 07 May 08 olle 503       inStream.close();
2668 07 May 08 olle 504       fileCheckResult = true;
2668 07 May 08 olle 505     }
2668 07 May 08 olle 506     catch (Exception e)
2668 07 May 08 olle 507     {
2668 07 May 08 olle 508       log.debug(e.getMessage(), e);
2668 07 May 08 olle 509     }
2668 07 May 08 olle 510     //System.out.println("FTPServer::lockFileExists(): lockFilePath = \"" + lockFilePath + "\", exists = " + fileCheckResult);
2668 07 May 08 olle 511     log.debug("lockFilePath = \"" + lockFilePath + "\", exists = " + fileCheckResult);
2668 07 May 08 olle 512     return fileCheckResult;
2668 07 May 08 olle 513   }
741 10 Oct 06 olle 514 }