comparison src/goodjava/lucene/backup/BackupServer.java @ 1499:22e15cf73040

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