KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Vector JavaDoc;
23
24 import org.alfresco.error.AlfrescoRuntimeException;
25 import org.alfresco.filesys.netbios.server.NetBIOSNameServer;
26 import org.alfresco.filesys.server.NetworkServer;
27 import org.alfresco.filesys.server.config.ServerConfiguration;
28 import org.alfresco.filesys.smb.server.SMBServer;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.springframework.context.ApplicationContext;
32 import org.springframework.context.ApplicationEvent;
33 import org.springframework.context.ApplicationListener;
34 import org.springframework.context.event.ContextRefreshedEvent;
35 import org.springframework.context.support.ClassPathXmlApplicationContext;
36
37 /**
38  * CIFS Server Class
39  *
40  * <p>Create and start the various server components required to run the CIFS server.
41  *
42  * @author GKSpencer
43  */

44 public class CIFSServer implements ApplicationListener
45 {
46     private static final Log logger = LogFactory.getLog("org.alfresco.smb.server");
47
48     // Server configuration
49

50     private ServerConfiguration filesysConfig;
51
52     // List of CIFS server components
53

54     private Vector JavaDoc<NetworkServer> serverList = new Vector JavaDoc<NetworkServer>();
55     
56     /**
57      * Class constructor
58      *
59      * @param serverConfig ServerConfiguration
60      */

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

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

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

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

113     public final void startServer() throws SocketException JavaDoc, IOException JavaDoc
114     {
115         try
116         {
117             // Create the SMB server and NetBIOS name server, if enabled
118

119             if (filesysConfig.isSMBServerEnabled())
120             {
121                 // Create the NetBIOS name server if NetBIOS SMB is enabled
122

123                 if (filesysConfig.hasNetBIOSSMB())
124                     serverList.add(new NetBIOSNameServer(filesysConfig));
125
126                 // Create the SMB server
127

128                 serverList.add(new SMBServer(filesysConfig));
129                 
130                 // Add the servers to the configuration
131

132                 for (NetworkServer server : serverList)
133                 {
134                     filesysConfig.addServer(server);
135                 }
136             }
137
138             // Start the CIFS server(s)
139

140             for (NetworkServer server : serverList)
141             {
142                 if (logger.isInfoEnabled())
143                     logger.info("Starting server " + server.getProtocolName() + " ...");
144
145                 // Start the server
146

147                 String JavaDoc serverName = server.getConfiguration().getServerName();
148                 server.startServer();
149             }
150         }
151         catch (Throwable JavaDoc e)
152         {
153             filesysConfig = null;
154             throw new AlfrescoRuntimeException("Failed to start CIFS Server", e);
155         }
156         // success
157
}
158
159     /**
160      * Stop the CIFS server components
161      */

162     public final void stopServer()
163     {
164         if (filesysConfig == null)
165         {
166             // initialisation failed
167
return;
168         }
169         
170         // Shutdown the CIFs server components
171

172         for ( NetworkServer server : serverList)
173         {
174             if (logger.isInfoEnabled())
175                 logger.info("Shutting server " + server.getProtocolName() + " ...");
176
177             // Stop the server
178

179             server.shutdownServer(false);
180             
181             // Remove the server from the global list
182

183             getConfiguration().removeServer(server.getProtocolName());
184         }
185         
186         // Clear the server list and configuration
187

188         serverList.clear();
189         filesysConfig = null;
190     }
191
192     /**
193      * Runs the CIFS server directly
194      *
195      * @param args String[]
196      */

197     public static void main(String JavaDoc[] args)
198     {
199         PrintStream JavaDoc out = System.out;
200
201         out.println("CIFS Server Test");
202         out.println("----------------");
203
204         try
205         {
206             // Create the configuration service in the same way that Spring creates it
207

208             ApplicationContext ctx = new ClassPathXmlApplicationContext("alfresco/application-context.xml");
209
210             // Get the CIFS server bean
211

212             CIFSServer server = (CIFSServer) ctx.getBean("cifsServer");
213             if (server == null)
214             {
215                 throw new AlfrescoRuntimeException("Server bean 'cifsServer' not defined");
216             }
217
218             // Stop the FTP server, if running
219

220             server.getConfiguration().setFTPServerEnabled(false);
221             
222             NetworkServer srv = server.getConfiguration().findServer("FTP");
223             if ( srv != null)
224                 srv.shutdownServer(true);
225             
226             // Only wait for shutdown if the SMB/CIFS server is enabled
227

228             if ( server.getConfiguration().isSMBServerEnabled())
229             {
230                 
231                 // SMB/CIFS server should have automatically started
232
// Wait for shutdown via the console
233

234                 out.println("Enter 'x' to shutdown ...");
235                 boolean shutdown = false;
236     
237                 // Wait while the server runs, user may stop the server by typing a key
238

239                 while (shutdown == false)
240                 {
241     
242                     // Wait for the user to enter the shutdown key
243

244                     int ch = System.in.read();
245     
246                     if (ch == 'x' || ch == 'X')
247                         shutdown = true;
248     
249                     synchronized (server)
250                     {
251                         server.wait(20);
252                     }
253                 }
254     
255                 // Stop the server
256

257                 server.stopServer();
258             }
259         }
260         catch (Exception JavaDoc ex)
261         {
262             ex.printStackTrace();
263         }
264         System.exit(1);
265     }
266
267
268 }
269
Popular Tags