1 17 package org.alfresco.filesys; 18 19 import java.io.IOException ; 20 import java.io.PrintStream ; 21 import java.net.SocketException ; 22 import java.util.Vector ; 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 44 public class CIFSServer implements ApplicationListener 45 { 46 private static final Log logger = LogFactory.getLog("org.alfresco.smb.server"); 47 48 50 private ServerConfiguration filesysConfig; 51 52 54 private Vector <NetworkServer> serverList = new Vector <NetworkServer>(); 55 56 61 public CIFSServer(ServerConfiguration serverConfig) 62 { 63 this.filesysConfig = serverConfig; 64 } 65 66 71 public final ServerConfiguration getConfiguration() 72 { 73 return filesysConfig; 74 } 75 76 79 public boolean isStarted() 80 { 81 return (filesysConfig != null && filesysConfig.isSMBServerEnabled()); 82 } 83 84 88 public void onApplicationEvent(ApplicationEvent event) 89 { 90 if (event instanceof ContextRefreshedEvent) 91 { 92 try 93 { 94 startServer(); 95 } 96 catch (SocketException e) 97 { 98 throw new AlfrescoRuntimeException("Failed to start CIFS server", e); 99 } 100 catch (IOException e) 101 { 102 throw new AlfrescoRuntimeException("Failed to start CIFS server", e); 103 } 104 } 105 } 106 107 113 public final void startServer() throws SocketException , IOException 114 { 115 try 116 { 117 119 if (filesysConfig.isSMBServerEnabled()) 120 { 121 123 if (filesysConfig.hasNetBIOSSMB()) 124 serverList.add(new NetBIOSNameServer(filesysConfig)); 125 126 128 serverList.add(new SMBServer(filesysConfig)); 129 130 132 for (NetworkServer server : serverList) 133 { 134 filesysConfig.addServer(server); 135 } 136 } 137 138 140 for (NetworkServer server : serverList) 141 { 142 if (logger.isInfoEnabled()) 143 logger.info("Starting server " + server.getProtocolName() + " ..."); 144 145 147 String serverName = server.getConfiguration().getServerName(); 148 server.startServer(); 149 } 150 } 151 catch (Throwable e) 152 { 153 filesysConfig = null; 154 throw new AlfrescoRuntimeException("Failed to start CIFS Server", e); 155 } 156 } 158 159 162 public final void stopServer() 163 { 164 if (filesysConfig == null) 165 { 166 return; 168 } 169 170 172 for ( NetworkServer server : serverList) 173 { 174 if (logger.isInfoEnabled()) 175 logger.info("Shutting server " + server.getProtocolName() + " ..."); 176 177 179 server.shutdownServer(false); 180 181 183 getConfiguration().removeServer(server.getProtocolName()); 184 } 185 186 188 serverList.clear(); 189 filesysConfig = null; 190 } 191 192 197 public static void main(String [] args) 198 { 199 PrintStream out = System.out; 200 201 out.println("CIFS Server Test"); 202 out.println("----------------"); 203 204 try 205 { 206 208 ApplicationContext ctx = new ClassPathXmlApplicationContext("alfresco/application-context.xml"); 209 210 212 CIFSServer server = (CIFSServer) ctx.getBean("cifsServer"); 213 if (server == null) 214 { 215 throw new AlfrescoRuntimeException("Server bean 'cifsServer' not defined"); 216 } 217 218 220 server.getConfiguration().setFTPServerEnabled(false); 221 222 NetworkServer srv = server.getConfiguration().findServer("FTP"); 223 if ( srv != null) 224 srv.shutdownServer(true); 225 226 228 if ( server.getConfiguration().isSMBServerEnabled()) 229 { 230 231 234 out.println("Enter 'x' to shutdown ..."); 235 boolean shutdown = false; 236 237 239 while (shutdown == false) 240 { 241 242 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 257 server.stopServer(); 258 } 259 } 260 catch (Exception ex) 261 { 262 ex.printStackTrace(); 263 } 264 System.exit(1); 265 } 266 267 268 } 269 | Popular Tags |