1 17 package org.alfresco.filesys; 18 19 import java.io.IOException ; 20 import java.io.PrintStream ; 21 import java.net.SocketException ; 22 23 import org.alfresco.error.AlfrescoRuntimeException; 24 import org.alfresco.filesys.ftp.FTPNetworkServer; 25 import org.alfresco.filesys.server.NetworkServer; 26 import org.alfresco.filesys.server.config.ServerConfiguration; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.springframework.context.ApplicationContext; 30 import org.springframework.context.ApplicationEvent; 31 import org.springframework.context.ApplicationListener; 32 import org.springframework.context.event.ContextRefreshedEvent; 33 import org.springframework.context.support.ClassPathXmlApplicationContext; 34 35 42 public class FTPServer implements ApplicationListener 43 { 44 private static final Log logger = LogFactory.getLog("org.alfresco.ftp.server"); 45 46 48 private ServerConfiguration filesysConfig; 49 50 52 private FTPNetworkServer ftpServer; 53 54 59 public FTPServer(ServerConfiguration serverConfig) 60 { 61 this.filesysConfig = serverConfig; 62 } 63 64 69 public final ServerConfiguration getConfiguration() 70 { 71 return filesysConfig; 72 } 73 74 77 public boolean isStarted() 78 { 79 return (filesysConfig != null && filesysConfig.isFTPServerEnabled()); 80 } 81 82 86 public void onApplicationEvent(ApplicationEvent event) 87 { 88 if (event instanceof ContextRefreshedEvent) 89 { 90 try 91 { 92 startServer(); 93 } 94 catch (SocketException e) 95 { 96 throw new AlfrescoRuntimeException("Failed to start FTP server", e); 97 } 98 catch (IOException e) 99 { 100 throw new AlfrescoRuntimeException("Failed to start FTP server", e); 101 } 102 } 103 } 104 105 111 public final void startServer() throws SocketException , IOException 112 { 113 try 114 { 115 117 if (filesysConfig.isFTPServerEnabled()) 118 { 119 121 ftpServer = new FTPNetworkServer(filesysConfig); 122 filesysConfig.addServer(ftpServer); 123 } 124 125 126 if(ftpServer != null) 128 { 129 if (logger.isInfoEnabled()) 131 logger.info("Starting server " + ftpServer.getProtocolName() + " ..."); 132 133 ftpServer.startServer(); 134 } 135 } 136 catch (Throwable e) 137 { 138 filesysConfig = null; 139 throw new AlfrescoRuntimeException("Failed to start FTP Server", e); 140 } 141 } 143 144 147 public final void stopServer() 148 { 149 if (filesysConfig == null) 150 { 151 return; 153 } 154 155 157 if ( ftpServer != null) 158 { 159 if (logger.isInfoEnabled()) 160 logger.info("Shutting server " + ftpServer.getProtocolName() + " ..."); 161 162 164 ftpServer.shutdownServer(false); 165 166 168 getConfiguration().removeServer(ftpServer.getProtocolName()); 169 ftpServer = null; 170 } 171 172 174 filesysConfig = null; 175 } 176 177 182 public static void main(String [] args) 183 { 184 PrintStream out = System.out; 185 186 out.println("FTP Server Test"); 187 out.println("---------------"); 188 189 try 190 { 191 193 ApplicationContext ctx = new ClassPathXmlApplicationContext("alfresco/application-context.xml"); 194 195 197 FTPServer server = (FTPServer) ctx.getBean("ftpServer"); 198 if (server == null) 199 { 200 throw new AlfrescoRuntimeException("Server bean 'ftpServer' not defined"); 201 } 202 203 205 NetworkServer srv = server.getConfiguration().findServer("SMB"); 206 if ( srv != null) 207 srv.shutdownServer(true); 208 209 srv = server.getConfiguration().findServer("NetBIOS"); 210 if ( srv != null) 211 srv.shutdownServer(true); 212 213 215 if ( server.getConfiguration().isFTPServerEnabled()) 216 { 217 218 222 out.println("Enter 'x' to shutdown ..."); 223 boolean shutdown = false; 224 225 227 while (shutdown == false) 228 { 229 230 232 int ch = System.in.read(); 233 234 if (ch == 'x' || ch == 'X') 235 shutdown = true; 236 237 synchronized (server) 238 { 239 server.wait(20); 240 } 241 } 242 243 245 server.stopServer(); 246 } 247 } 248 catch (Exception ex) 249 { 250 ex.printStackTrace(); 251 } 252 System.exit(1); 253 } 254 } 255 | Popular Tags |