Mercurial Hosting > luan
annotate src/goodjava/lucene/backup/BackupIndexWriter.java @ 1506:d80395468b4e
ssl security in code
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 15 May 2020 18:29:47 -0600 |
| parents | f443542d8650 |
| children | 86c5e7000ecf |
| rev | line source |
|---|---|
| 1488 | 1 package goodjava.lucene.backup; |
| 2 | |
| 3 import java.io.File; | |
| 1499 | 4 import java.io.InputStream; |
| 1488 | 5 import java.io.IOException; |
| 1499 | 6 import java.net.Socket; |
| 1488 | 7 import java.util.List; |
| 8 import java.util.ArrayList; | |
| 1499 | 9 import java.util.Map; |
| 10 import java.util.HashMap; | |
| 11 import java.util.Arrays; | |
| 1504 | 12 import java.util.concurrent.Executors; |
| 13 import java.util.concurrent.ExecutorService; | |
| 1499 | 14 import javax.net.ssl.SSLSocket; |
| 1488 | 15 import goodjava.io.IoUtils; |
| 1499 | 16 import goodjava.rpc.RpcClient; |
| 17 import goodjava.rpc.RpcCall; | |
| 18 import goodjava.rpc.RpcResult; | |
| 19 import goodjava.rpc.RpcException; | |
| 1488 | 20 import goodjava.lucene.api.LuceneIndexWriter; |
| 1499 | 21 import goodjava.logging.Logger; |
| 22 import goodjava.logging.LoggerFactory; | |
| 1488 | 23 import goodjava.lucene.logging.LoggingIndexWriter; |
| 24 import goodjava.lucene.logging.LogFile; | |
| 25 | |
| 26 | |
| 27 public class BackupIndexWriter extends LoggingIndexWriter { | |
| 1499 | 28 private static final Logger logger = LoggerFactory.getLogger(BackupIndexWriter.class); |
| 29 public static String[] backupDomains; | |
| 1488 | 30 private final String name; |
| 31 private final File dir; | |
| 1499 | 32 private boolean isSyncPending = false; |
| 1504 | 33 private final ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 1488 | 34 |
| 35 public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,String name) throws IOException { | |
| 36 super(indexWriter,logDir); | |
| 1499 | 37 if( backupDomains == null ) |
| 38 throw new RuntimeException("must set backupDomains"); | |
| 1488 | 39 this.name = name; |
| 40 File f = new File(System.getProperty("java.io.tmpdir")); | |
| 41 dir = new File(f,"goodjava.lucene/"+name); | |
| 1501 | 42 IoUtils.mkdirs(dir); |
| 1488 | 43 } |
| 44 | |
| 1504 | 45 public synchronized void close() throws IOException { |
| 46 super.close(); | |
| 47 exec.shutdown(); | |
| 48 } | |
| 49 | |
| 1488 | 50 public synchronized void commit() throws IOException { |
| 51 super.commit(); | |
| 1499 | 52 //sync(); |
| 53 if( !isSyncPending ) { | |
| 1504 | 54 exec.execute(sync); |
| 1499 | 55 isSyncPending = true; |
| 1488 | 56 } |
| 57 } | |
| 58 | |
| 1499 | 59 public void runSync() { |
| 1504 | 60 try { |
| 61 exec.submit(sync).get(); | |
| 62 } catch(Exception e) { | |
| 63 throw new RuntimeException(e); | |
| 64 } | |
| 1499 | 65 } |
| 66 | |
| 67 private final Runnable sync = new Runnable() { | |
| 1504 | 68 public void run() { |
| 1499 | 69 try { |
| 70 sync(); | |
| 71 } catch(IOException e) { | |
| 72 throw new RuntimeException(e); | |
| 73 } | |
| 74 } | |
| 75 }; | |
| 76 | |
| 77 private void sync() throws IOException { | |
| 78 List<LogFile> logs = new ArrayList<LogFile>(); | |
| 79 synchronized(this) { | |
| 80 isSyncPending = false; | |
| 1500 | 81 clearDir(); |
| 1499 | 82 for( LogFile log : this.logs ) { |
| 83 File f = new File(dir,log.file.getName()); | |
| 84 IoUtils.link(log.file,f); | |
| 85 logs.add( new LogFile(f) ); | |
| 86 } | |
| 87 } | |
| 88 List logInfo = new ArrayList(); | |
| 89 Map<String,LogFile> logMap = new HashMap<String,LogFile>(); | |
| 90 for( LogFile log : logs ) { | |
| 91 Map fileInfo = new HashMap(); | |
| 92 fileInfo.put("name",log.file.getName()); | |
| 93 fileInfo.put("end",log.end()); | |
| 94 logInfo.add(fileInfo); | |
| 95 logMap.put(log.file.getName(),log); | |
| 96 } | |
| 97 for( String backupDomain : backupDomains ) { | |
| 98 RpcClient rpc = rpcClient(backupDomain); | |
| 99 RpcCall call = new RpcCall("check",name,logInfo); | |
| 100 try { | |
| 101 while(true) { | |
| 102 rpc.write(call); | |
| 103 RpcResult result = rpc.read(); | |
| 104 logger.info(Arrays.asList(result.returnValues).toString()); | |
| 105 String status = (String)result.returnValues[0]; | |
| 106 if( status.equals("ok") ) { | |
| 107 break; | |
| 108 } else if( status.equals("missing") ) { | |
| 109 String fileName = (String)result.returnValues[1]; | |
| 110 LogFile log = logMap.get(fileName); | |
| 111 long len = log.end() - 8; | |
| 112 InputStream in = log.input(); | |
| 113 call = new RpcCall(in,len,"add",name,logInfo,fileName); | |
| 114 } else if( status.equals("incomplete") ) { | |
| 115 String fileName = (String)result.returnValues[1]; | |
| 116 long logEnd = (Long)result.returnValues[2]; | |
| 117 LogFile log = logMap.get(fileName); | |
| 118 long len = log.end() - logEnd; | |
| 119 InputStream in = log.input(); | |
| 120 in.skip(logEnd-8); | |
| 121 call = new RpcCall(in,len,"append",name,logInfo,fileName); | |
| 122 } else | |
| 123 throw new RuntimeException("status "+status); | |
| 124 } | |
| 125 } catch(RpcException e) { | |
| 126 logger.warn("",e); | |
| 127 } | |
| 128 rpc.close(); | |
| 129 } | |
| 1500 | 130 clearDir(); |
| 131 } | |
| 132 | |
| 133 private void clearDir() throws IOException { | |
| 134 for( File f : dir.listFiles() ) { | |
| 135 IoUtils.delete(f); | |
| 136 } | |
| 1499 | 137 } |
| 138 | |
| 139 static RpcClient rpcClient(String backupDomain) throws IOException { | |
| 140 Socket socket; | |
| 141 if( BackupServer.cipherSuites == null ) { | |
| 142 socket = new Socket(backupDomain,BackupServer.port); | |
| 143 } else { | |
|
1506
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1504
diff
changeset
|
144 socket = IoUtils.getSSLSocketFactory().createSocket(backupDomain,BackupServer.port); |
| 1499 | 145 ((SSLSocket)socket).setEnabledCipherSuites(BackupServer.cipherSuites); |
| 146 } | |
| 147 return new RpcClient(socket); | |
| 148 } | |
| 1488 | 149 } |
