1
2
3
4
5
6
7
8
9
10 package org.wiztools.jenkryptor;
11
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.security.InvalidKeyException;
19 import java.security.NoSuchAlgorithmException;
20 import java.util.concurrent.Executor;
21 import java.util.concurrent.ExecutorService;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.TimeUnit;
24 import java.util.logging.Logger;
25 import javax.crypto.NoSuchPaddingException;
26 import static org.wiztools.jenkryptor.Globals.*;
27 import org.wiztools.wizcrypt.Callback;
28 import org.wiztools.wizcrypt.CipherKey;
29 import org.wiztools.wizcrypt.CipherKeyGen;
30 import org.wiztools.wizcrypt.PasswordMismatchException;
31 import org.wiztools.wizcrypt.WizCrypt;
32
33 /***
34 *
35 * @author subhash
36 */
37 public class Processor {
38
39 private static final Processor processor = new Processor();
40 private static final Logger LOG = Logger.getLogger(Processor.class.getName());
41
42 /*** Creates a new instance of Processor */
43 private Processor() {
44 }
45
46 public static Processor getInstance(){
47 return processor;
48 }
49
50 public void process(){
51 Runnable process = new Runnable(){
52 public void run(){
53 Globals.isRunning = true;
54 Globals.MAIN_FRAME.freeze();
55 msgDisplayer.setStatus("Processing. . .");
56
57 ExecutorService exec = Executors.newFixedThreadPool(Globals.THREAD_SIZE);
58 for(int i = 0; i < files.length; i++){
59 final File file = files[i];
60
61 Runnable r = new ProcessThread(file);
62 exec.execute(r);
63 }
64 LOG.finest("Awaiting shutdown of threadpool. . .");
65 exec.shutdown();
66 LOG.finest("Shutdown complete, awaiting termination. . .");
67
68
69 try{
70 exec.awaitTermination(999999999, TimeUnit.DAYS);
71 LOG.finest("Termination successful!");
72 }
73 catch(InterruptedException ie){
74 ie.printStackTrace();
75 }
76 Globals.MAIN_FRAME.unfreeze();
77 Globals.isRunning = false;
78 msgDisplayer.setStatus(TITLE);
79 }
80 };
81
82 new Thread(process).start();
83 }
84
85 class ProcessThread implements Runnable{
86
87 private File file;
88 private String filePath;
89
90 ProcessThread(File file){
91 this.file = file;
92 this.filePath = file.getAbsolutePath();
93 }
94
95 public void run(){
96 boolean gotLPE = false;
97 LabelProgressbarEnsc lpe = null;
98
99 try{
100
101 lpe = PBPM.getLPE();
102 gotLPE = true;
103
104 Callback cb = new WizCryptCallback(file, lpe);
105
106 long fileSize = file.length();
107
108 try{
109 InputStream is = new FileInputStream(file);
110
111 if(mode == MODE_ENCRYPT){
112 File outFile = new File(filePath + ".wiz");
113 if(!outFile.exists() ||
114 (outFile.exists() && Preferences.overwriteDestination_pref)){
115 OutputStream os = new FileOutputStream(outFile);
116
117 CipherKey ck = CipherKeyGen.getCipherKeyForEncrypt(password);
118
119 WizCrypt.encrypt(is, os, ck, cb, fileSize);
120
121 Globals.msgDisplayer.appendMessage("Done: " + outFile.getAbsolutePath());
122
123 if(Preferences.deleteSource_pref){
124 file.delete();
125 }
126 }
127 else{
128 Globals.msgDisplayer.appendMessage("SKIPPING: "
129 + outFile.getAbsolutePath() + " exists!");
130 }
131 }
132 else{
133 String outPath = filePath.replaceFirst(".wiz$", "");
134 File outFile = new File(outPath);
135 OutputStream os = new FileOutputStream(outPath);
136
137 if(!outFile.exists() ||
138 (outFile.exists() && Preferences.overwriteDestination_pref)){
139 CipherKey ck = CipherKeyGen.getCipherKeyForDecrypt(password);
140
141 WizCrypt.decrypt(is, os, ck, cb, fileSize);
142
143 Globals.msgDisplayer.appendMessage("Done: " + outPath);
144
145 if(Preferences.deleteSource_pref){
146 file.delete();
147 }
148 }
149 else{
150 Globals.msgDisplayer.appendMessage("SKIPPING: "
151 + outFile.getAbsolutePath() + " exists!");
152 }
153 }
154 }
155 catch(PasswordMismatchException e){
156 Globals.msgDisplayer.appendMessage("ERROR: " +
157 e.getMessage() + filePath);
158 }
159 catch(IOException e){
160 Globals.msgDisplayer.appendMessage("ERROR: " +
161 e.getMessage() + filePath);
162 }
163 }
164 catch(NoSuchAlgorithmException e){
165 Globals.msgDisplayer.appendMessage("ERROR: "+e.getMessage());
166 }
167 catch(InvalidKeyException e){
168 Globals.msgDisplayer.appendMessage("ERROR: "+e.getMessage());
169 }
170 catch(NoSuchPaddingException e){
171 Globals.msgDisplayer.appendMessage("ERROR: "+e.getMessage());
172 }
173 finally{
174 if(gotLPE){
175 PBPM.returnLPE(lpe);
176 }
177 }
178 }
179 }
180
181 }