KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > FTPServer


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintStream JavaDoc;
21 import java.net.SocketException JavaDoc;
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 /**
36  * FTP Server Class
37  *
38  * <p>Create and start the server components required to run the FTP server.
39  *
40  * @author GKSpencer
41  */

42 public class FTPServer implements ApplicationListener
43 {
44     private static final Log logger = LogFactory.getLog("org.alfresco.ftp.server");
45
46     // Server configuration
47

48     private ServerConfiguration filesysConfig;
49
50     // The actual FTP server
51

52     private FTPNetworkServer ftpServer;
53     
54     /**
55      * Class constructor
56      *
57      * @param serverConfig ServerConfiguration
58      */

59     public FTPServer(ServerConfiguration serverConfig)
60     {
61         this.filesysConfig = serverConfig;
62     }
63
64     /**
65      * Return the server configuration
66      *
67      * @return ServerConfiguration
68      */

69     public final ServerConfiguration getConfiguration()
70     {
71         return filesysConfig;
72     }
73
74     /**
75      * @return Returns true if the server started up without any errors
76      */

77     public boolean isStarted()
78     {
79         return (filesysConfig != null && filesysConfig.isFTPServerEnabled());
80     }
81
82     /*
83      * (non-Javadoc)
84      * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
85      */

86     public void onApplicationEvent(ApplicationEvent event)
87     {
88         if (event instanceof ContextRefreshedEvent)
89         {
90             try
91             {
92                 startServer();
93             }
94             catch (SocketException JavaDoc e)
95             {
96                 throw new AlfrescoRuntimeException("Failed to start FTP server", e);
97             }
98             catch (IOException JavaDoc e)
99             {
100                 throw new AlfrescoRuntimeException("Failed to start FTP server", e);
101             }
102         }
103     }
104     
105     /**
106      * Start the FTP server components
107      *
108      * @exception SocketException If a network error occurs
109      * @exception IOException If an I/O error occurs
110      */

111     public final void startServer() throws SocketException JavaDoc, IOException JavaDoc
112     {
113         try
114         {
115             // Create the FTP server, if enabled
116

117             if (filesysConfig.isFTPServerEnabled())
118             {
119                 // Create the FTP server
120

121                 ftpServer = new FTPNetworkServer(filesysConfig);
122                 filesysConfig.addServer(ftpServer);
123             }
124
125
126             // Start the server
127
if(ftpServer != null)
128             {
129                 // Start the FTP server
130
if (logger.isInfoEnabled())
131                     logger.info("Starting server " + ftpServer.getProtocolName() + " ...");
132
133                 ftpServer.startServer();
134             }
135         }
136         catch (Throwable JavaDoc e)
137         {
138             filesysConfig = null;
139             throw new AlfrescoRuntimeException("Failed to start FTP Server", e);
140         }
141         // success
142
}
143
144     /**
145      * Stop the FTP server components
146      */

147     public final void stopServer()
148     {
149         if (filesysConfig == null)
150         {
151             // initialisation failed
152
return;
153         }
154         
155         // Shutdown the FTP server
156

157         if ( ftpServer != null)
158         {
159             if (logger.isInfoEnabled())
160                 logger.info("Shutting server " + ftpServer.getProtocolName() + " ...");
161
162             // Stop the server
163

164             ftpServer.shutdownServer(false);
165             
166             // Remove the server from the global list
167

168             getConfiguration().removeServer(ftpServer.getProtocolName());
169             ftpServer = null;
170         }
171         
172         // Clear the configuration
173

174         filesysConfig = null;
175     }
176
177     /**
178      * Runs the FTP server directly
179      *
180      * @param args String[]
181      */

182     public static void main(String JavaDoc[] args)
183     {
184         PrintStream JavaDoc out = System.out;
185
186         out.println("FTP Server Test");
187         out.println("---------------");
188
189         try
190         {
191             // Create the configuration service in the same way that Spring creates it
192

193             ApplicationContext ctx = new ClassPathXmlApplicationContext("alfresco/application-context.xml");
194
195             // Get the FTP server bean
196

197             FTPServer server = (FTPServer) ctx.getBean("ftpServer");
198             if (server == null)
199             {
200                 throw new AlfrescoRuntimeException("Server bean 'ftpServer' not defined");
201             }
202
203             // Stop the CIFS server components, if running
204

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             // Only wait for shutdown if the FTP server is enabled
214

215             if ( server.getConfiguration().isFTPServerEnabled())
216             {
217                 
218                 // FTP server should have automatically started
219
//
220
// Wait for shutdown via the console
221

222                 out.println("Enter 'x' to shutdown ...");
223                 boolean shutdown = false;
224     
225                 // Wait while the server runs, user may stop the server by typing a key
226

227                 while (shutdown == false)
228                 {
229     
230                     // Wait for the user to enter the shutdown key
231

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                 // Stop the server
244

245                 server.stopServer();
246             }
247         }
248         catch (Exception JavaDoc ex)
249         {
250             ex.printStackTrace();
251         }
252         System.exit(1);
253     }
254 }
255
Popular Tags