1499
|
1 package goodjava.lucene.backup;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.io.IOException;
|
|
5 import java.util.Map;
|
|
6 import java.net.Socket;
|
|
7 import java.net.ServerSocket;
|
|
8 import java.util.concurrent.ThreadPoolExecutor;
|
|
9 import java.util.concurrent.Executors;
|
|
10 import javax.net.ssl.SSLServerSocketFactory;
|
|
11 import javax.net.ssl.SSLServerSocket;
|
|
12 import goodjava.util.SoftCacheMap;
|
1501
|
13 import goodjava.io.IoUtils;
|
1499
|
14 import goodjava.rpc.RpcServer;
|
|
15 import goodjava.rpc.RpcCall;
|
|
16 import goodjava.logging.Logger;
|
|
17 import goodjava.logging.LoggerFactory;
|
|
18
|
|
19
|
|
20 public class BackupServer {
|
|
21 private static final Logger logger = LoggerFactory.getLogger(BackupServer.class);
|
|
22
|
|
23 public static int port = 9101;
|
|
24 public static String[] cipherSuites = new String[] {
|
|
25 "TLS_DH_anon_WITH_AES_128_GCM_SHA256",
|
|
26 "TLS_DH_anon_WITH_AES_128_CBC_SHA256",
|
|
27 "TLS_ECDH_anon_WITH_AES_128_CBC_SHA",
|
|
28 "TLS_DH_anon_WITH_AES_128_CBC_SHA",
|
|
29 "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA",
|
|
30 "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",
|
|
31 "TLS_ECDH_anon_WITH_RC4_128_SHA",
|
|
32 "SSL_DH_anon_WITH_RC4_128_MD5",
|
|
33 "SSL_DH_anon_WITH_DES_CBC_SHA",
|
|
34 "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",
|
|
35 "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",
|
|
36 };
|
|
37 static {
|
|
38 cipherSuites = null; // for now, until I figure out disgusting java security
|
|
39 }
|
|
40
|
|
41 private final File backupDir;
|
|
42 private static final ThreadPoolExecutor threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();
|
|
43 private static final Map<String,Backup> backups = new SoftCacheMap<String,Backup>();
|
|
44
|
1501
|
45 public BackupServer(File backupDir) throws IOException {
|
1499
|
46 this.backupDir = backupDir;
|
1501
|
47 IoUtils.mkdirs(backupDir);
|
1499
|
48 }
|
|
49
|
|
50 public synchronized void start() throws IOException {
|
|
51 final ServerSocket ss;
|
|
52 if( cipherSuites == null ) {
|
|
53 ss = new ServerSocket(port);
|
|
54 } else {
|
|
55 ss = SSLServerSocketFactory.getDefault().createServerSocket(port);
|
|
56 ((SSLServerSocket)ss).setEnabledCipherSuites(cipherSuites);
|
|
57 }
|
|
58 threadPool.execute(new Runnable(){public void run() {
|
|
59 try {
|
|
60 while(!threadPool.isShutdown()) {
|
|
61 final Socket socket = ss.accept();
|
|
62 threadPool.execute(new Runnable(){public void run() {
|
|
63 handle(socket);
|
|
64 }});
|
|
65 }
|
|
66 } catch(IOException e) {
|
|
67 logger.error("",e);
|
|
68 }
|
|
69 }});
|
|
70 logger.info("started server on port "+port);
|
|
71 }
|
|
72
|
|
73 private void handle(Socket socket) {
|
|
74 RpcServer rpc = new RpcServer(socket);
|
|
75 while( !rpc.isClosed() ) {
|
|
76 RpcCall call = rpc.read();
|
|
77 if( call == null )
|
|
78 break;
|
|
79 String name = (String)call.args[0];
|
|
80 Backup backup;
|
|
81 synchronized(backups) {
|
|
82 backup = backups.get(name);
|
|
83 if( backup == null ) {
|
|
84 backup = new Backup(new File(backupDir,name));
|
|
85 backups.put(name,backup);
|
|
86 }
|
|
87 }
|
|
88 backup.handle(rpc,call);
|
|
89 }
|
|
90 }
|
|
91
|
|
92 }
|