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