1 17 package org.alfresco.filesys.ftp; 18 19 import java.io.IOException ; 20 import java.net.InetAddress ; 21 import java.net.ServerSocket ; 22 import java.net.Socket ; 23 import java.net.SocketException ; 24 import java.util.Enumeration ; 25 26 import org.alfresco.filesys.server.ServerListener; 27 import org.alfresco.filesys.server.SrvSession; 28 import org.alfresco.filesys.server.config.ServerConfiguration; 29 import org.alfresco.filesys.server.core.SharedDeviceList; 30 import org.alfresco.filesys.server.filesys.NetworkFileServer; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 40 public class FTPNetworkServer extends NetworkFileServer implements Runnable 41 { 42 43 45 private static final Log logger = LogFactory.getLog("org.alfresco.ftp.protocol"); 46 47 51 private static final String ServerVersion = "3.5.0"; 52 53 55 protected static final int LISTEN_BACKLOG = 10; 56 57 59 protected static final int SERVER_PORT = 21; 60 61 63 private ServerSocket m_srvSock; 64 65 67 private FTPSessionList m_sessions; 68 69 71 private SharedDeviceList m_shares; 72 73 75 private int m_sessId; 76 77 79 private FTPPath m_rootPath; 80 81 83 private Thread m_srvThread; 84 85 87 private String m_localFTPaddress; 88 89 95 public FTPNetworkServer(ServerConfiguration config) 96 { 97 super("FTP", config); 98 99 101 setVersion(ServerVersion); 102 103 105 m_sessions = new FTPSessionList(); 106 107 109 if (getConfiguration().getFTPDebug() != 0) 110 setDebug(true); 111 112 114 if (getConfiguration().hasFTPRootPath()) 115 { 116 117 try 118 { 119 120 122 m_rootPath = new FTPPath(getConfiguration().getFTPRootPath()); 123 } 124 catch (InvalidPathException ex) 125 { 126 logger.error(ex); 127 } 128 } 129 } 130 131 136 protected final void addSession(FTPSrvSession sess) 137 { 138 139 141 m_sessions.addSession(sess); 142 143 145 if (hasDebug()) 146 { 147 148 150 sess.setDebug(getConfiguration().getFTPDebug()); 151 } 152 } 153 154 159 protected final void removeSession(FTPSrvSession sess) 160 { 161 162 164 if (m_sessions.removeSession(sess) != null) 165 { 166 167 169 fireSessionClosedEvent(sess); 170 } 171 } 172 173 182 protected final FTPDataSession allocateDataSession(FTPSrvSession sess, InetAddress remAddr, int remPort) 183 throws IOException 184 { 185 187 FTPDataSession dataSess = null; 188 if (remAddr != null) 189 { 190 191 193 dataSess = new FTPDataSession(sess, remAddr, remPort); 194 } 195 else 196 { 197 198 200 dataSess = new FTPDataSession(sess, getBindAddress()); 201 } 202 203 205 return dataSess; 206 } 207 208 213 protected final void releaseDataSession(FTPDataSession dataSess) 214 { 215 216 218 dataSess.closeSession(); 219 } 220 221 226 public final SharedDeviceList getShareList() 227 { 228 229 231 if (m_shares == null) 232 m_shares = getConfiguration().getShareMapper() 233 .getShareList(getConfiguration().getServerName(), null, false); 234 235 237 return m_shares; 238 } 239 240 245 public final boolean hasBindAddress() 246 { 247 return getConfiguration().getFTPBindAddress() != null ? true : false; 248 } 249 250 255 public final InetAddress getBindAddress() 256 { 257 return getConfiguration().getFTPBindAddress(); 258 } 259 260 265 public final boolean hasRootPath() 266 { 267 return m_rootPath != null ? true : false; 268 } 269 270 275 public final boolean allowAnonymous() 276 { 277 return getConfiguration().allowAnonymousFTP(); 278 } 279 280 285 public final String getAnonymousAccount() 286 { 287 return getConfiguration().getAnonymousFTPAccount(); 288 } 289 290 295 public final String getLocalFTPAddressString() 296 { 297 return m_localFTPaddress; 298 } 299 300 305 protected final synchronized int getNextSessionId() 306 { 307 return m_sessId++; 308 } 309 310 315 public final int getPort() 316 { 317 return getConfiguration().getFTPPort(); 318 } 319 320 325 protected final ServerSocket getSocket() 326 { 327 return m_srvSock; 328 } 329 330 335 public final FTPPath getRootPath() 336 { 337 return m_rootPath; 338 } 339 340 345 protected final void sessionLoggedOn(SrvSession sess) 346 { 347 348 350 fireSessionLoggedOnEvent(sess); 351 } 352 353 356 public void run() 357 { 358 359 361 if (logger.isDebugEnabled() && hasDebug()) 362 { 363 logger.debug("FTP Server starting on port " + getPort()); 364 logger.debug("Version " + isVersion()); 365 } 366 367 369 try 370 { 371 372 374 if (hasBindAddress()) 375 m_srvSock = new ServerSocket (getPort(), LISTEN_BACKLOG, getBindAddress()); 376 else 377 m_srvSock = new ServerSocket (getPort(), LISTEN_BACKLOG); 378 379 381 if (logger.isDebugEnabled() && hasDebug()) 382 { 383 String ftpAddr = "ALL"; 384 385 if (hasBindAddress()) 386 ftpAddr = getBindAddress().getHostAddress(); 387 logger.debug("FTP Binding to local address " + ftpAddr); 388 } 389 390 392 if (hasBindAddress()) 393 m_localFTPaddress = getBindAddress().getHostAddress().replace('.', ','); 394 395 397 setActive(true); 398 fireServerEvent(ServerListener.ServerActive); 399 400 402 while (hasShutdown() == false) 403 { 404 405 407 Socket sessSock = getSocket().accept(); 408 409 411 if (m_localFTPaddress == null) 412 { 413 if (sessSock.getLocalAddress() != null) 414 m_localFTPaddress = sessSock.getLocalAddress().getHostAddress().replace('.', ','); 415 } 416 417 419 sessSock.setTcpNoDelay(true); 420 421 423 if (logger.isDebugEnabled() && hasDebug()) 424 logger.debug("FTP session request received from " 425 + sessSock.getInetAddress().getHostAddress()); 426 427 429 FTPSrvSession srvSess = new FTPSrvSession(sessSock, this); 430 srvSess.setSessionId(getNextSessionId()); 431 srvSess.setUniqueId("FTP" + srvSess.getSessionId()); 432 srvSess.setDebugPrefix("[FTP" + srvSess.getSessionId() + "] "); 433 434 436 if (hasRootPath()) 437 srvSess.setRootPath(getRootPath()); 438 439 441 addSession(srvSess); 442 443 445 fireSessionOpenEvent(srvSess); 446 447 449 Thread srvThread = new Thread (srvSess); 450 srvThread.setDaemon(true); 451 srvThread.setName("Sess_FTP" + srvSess.getSessionId() + "_" 452 + sessSock.getInetAddress().getHostAddress()); 453 srvThread.start(); 454 455 457 try 458 { 459 Thread.sleep(1000L); 460 } 461 catch (InterruptedException ex) 462 { 463 } 464 } 465 } 466 catch (SocketException ex) 467 { 468 469 472 if (hasShutdown() == false) 473 { 474 logger.error("FTP Socket error", ex); 475 476 478 setException(ex); 479 fireServerEvent(ServerListener.ServerError); 480 } 481 } 482 catch (Exception ex) 483 { 484 485 488 if (hasShutdown() == false) 489 { 490 logger.error("FTP Server error", ex); 491 } 492 493 495 setException(ex); 496 fireServerEvent(ServerListener.ServerError); 497 } 498 499 501 Enumeration enm = m_sessions.enumerate(); 502 503 while (enm.hasMoreElements()) 504 { 505 506 508 Integer sessId = (Integer ) enm.nextElement(); 509 FTPSrvSession sess = m_sessions.findSession(sessId); 510 511 513 if (logger.isDebugEnabled() && hasDebug()) 514 logger.debug("FTP Close session, id = " + sess.getSessionId()); 515 516 518 sess.closeSession(); 519 } 520 521 523 if (logger.isDebugEnabled() && hasDebug()) 524 logger.debug("FTP Server shutting down ..."); 525 526 528 setActive(false); 529 fireServerEvent(ServerListener.ServerShutdown); 530 } 531 532 537 public void shutdownServer(boolean immediate) 538 { 539 540 542 setShutdown(true); 543 544 546 try 547 { 548 if (getSocket() != null) 549 getSocket().close(); 550 } 551 catch (IOException ex) 552 { 553 } 554 555 557 if (m_srvThread != null) 558 { 559 560 try 561 { 562 m_srvThread.join(3000); 563 } 564 catch (Exception ex) 565 { 566 } 567 } 568 569 571 fireServerEvent(ServerListener.ServerShutdown); 572 } 573 574 577 public void startServer() 578 { 579 580 582 m_srvThread = new Thread (this); 583 m_srvThread.setName("FTP Server"); 584 m_srvThread.start(); 585 586 588 fireServerEvent(ServerListener.ServerStartup); 589 } 590 } 591 | Popular Tags |