annotate src/goodjava/lucene/backup/BackupServer.java @ 1679:39287902fb0c

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 31 May 2022 15:39:58 -0600
parents 445048f9eca7
children 973d3039c421
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.Executors;
1502
8a7b6b32c691 minor threads
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
9 import java.util.concurrent.ExecutorService;
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
10 import javax.net.ssl.SSLSocket;
1499
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;
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
13 import goodjava.io.IoUtils;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 import goodjava.rpc.RpcServer;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 import goodjava.rpc.RpcCall;
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
16 import goodjava.rpc.RpcClient;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
17 import goodjava.rpc.RpcResult;
1512
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
18 import goodjava.rpc.Rpc;
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
19 import goodjava.rpc.RpcException;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 import goodjava.logging.Logger;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 import goodjava.logging.LoggerFactory;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
1672
8dd8c556c449 backup work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1512
diff changeset
24 public final class BackupServer {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 private static final Logger logger = LoggerFactory.getLogger(BackupServer.class);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
1675
Franklin Schmidt <fschmidt@gmail.com>
parents: 1674
diff changeset
27 public static int port = 9102;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 public static String[] cipherSuites = new String[] {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 "TLS_DH_anon_WITH_AES_128_GCM_SHA256",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 "TLS_DH_anon_WITH_AES_128_CBC_SHA256",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 "TLS_ECDH_anon_WITH_AES_128_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 "TLS_DH_anon_WITH_AES_128_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 "TLS_ECDH_anon_WITH_RC4_128_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 "SSL_DH_anon_WITH_RC4_128_MD5",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 "SSL_DH_anon_WITH_DES_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 };
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 private final File backupDir;
1502
8a7b6b32c691 minor threads
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
43 private static final ExecutorService threadPool = Executors.newCachedThreadPool();
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 private static final Map<String,Backup> backups = new SoftCacheMap<String,Backup>();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
46 public BackupServer(File backupDir) throws IOException {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 this.backupDir = backupDir;
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
48 IoUtils.mkdirs(backupDir);
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 public synchronized void start() throws IOException {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 final ServerSocket ss;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 if( cipherSuites == null ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 ss = new ServerSocket(port);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 } else {
1506
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1502
diff changeset
56 ss = IoUtils.getSSLServerSocketFactory().createServerSocket(port);
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 ((SSLServerSocket)ss).setEnabledCipherSuites(cipherSuites);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 threadPool.execute(new Runnable(){public void run() {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 try {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 while(!threadPool.isShutdown()) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 final Socket socket = ss.accept();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 threadPool.execute(new Runnable(){public void run() {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 handle(socket);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 }});
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 } catch(IOException e) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 logger.error("",e);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 }});
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 logger.info("started server on port "+port);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 private void handle(Socket socket) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 RpcServer rpc = new RpcServer(socket);
1674
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
76 Backup backup = null;
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
77 while( !rpc.isClosed() ) {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 RpcCall call = rpc.read();
1674
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
79 if( call == null )
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
80 break;
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
81 if( call.cmd.equals("exists") ) {
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
82 String name = (String)call.args[0];
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
83 rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) );
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
84 } else if( call.cmd.equals("login") ) {
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
85 String name = (String)call.args[0];
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
86 synchronized(backups) {
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
87 backup = backups.get(name);
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
88 if( backup == null ) {
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
89 backup = new Backup(new File(backupDir,name));
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
90 backups.put(name,backup);
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
91 }
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
92 }
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
93 rpc.write(Rpc.OK);
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
94 } else if( backup != null ) {
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
95 backup.handle(rpc,call);
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
96 } else {
1512
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
97 rpc.write( new RpcException("login expected") );
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
98 rpc.close();
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
99 return;
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
100 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
104
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
105 // for client
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
106
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
107 public static RpcClient rpcClient(String backupDomain) throws IOException {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
108 Socket socket;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
109 if( BackupServer.cipherSuites == null ) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
110 socket = new Socket(backupDomain,BackupServer.port);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
111 } else {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
112 socket = IoUtils.getSSLSocketFactory().createSocket(backupDomain,BackupServer.port);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
113 ((SSLSocket)socket).setEnabledCipherSuites(BackupServer.cipherSuites);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
114 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
115 return new RpcClient(socket);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
116 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1506
diff changeset
117
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 }