1
|
import java.io.File;
|
2
|
import java.io.FileDescriptor;
|
3
|
import java.io.FileOutputStream;
|
4
|
import java.io.PrintStream;
|
5
|
import java.util.List;
|
6
|
import java.util.concurrent.Semaphore;
|
7
|
import java.util.concurrent.atomic.AtomicBoolean;
|
8
|
import java.util.concurrent.atomic.AtomicReference;
|
9
|
import java.util.concurrent.locks.Lock;
|
10
|
import java.util.concurrent.locks.ReentrantLock;
|
11
|
|
12
|
import jdeserialize.content;
|
13
|
import jdeserialize.jdeserialize;
|
14
|
import io.Database.DB_Messenger;
|
15
|
import io.Database;
|
16
|
import io.FileWorker;
|
17
|
|
18
|
public class Converter extends Thread {
|
19
|
|
20
|
private IConversionResults ui;
|
21
|
private AtomicBoolean active;
|
22
|
|
23
|
// P-K without E semaphore.
|
24
|
private Lock entryLock;
|
25
|
private Semaphore inputAvailable;
|
26
|
|
27
|
// Input.
|
28
|
private File inputFile;
|
29
|
private DB_Messenger query;
|
30
|
|
31
|
public Converter(IConversionResults ui) {
|
32
|
this.ui = ui;
|
33
|
active = new AtomicBoolean(true);
|
34
|
entryLock = new ReentrantLock();
|
35
|
inputAvailable = new Semaphore(0);
|
36
|
}
|
37
|
|
38
|
public void end() {
|
39
|
active.set(false);
|
40
|
setInput(null, null);
|
41
|
}
|
42
|
|
43
|
public void setInput(File inputFile, DB_Messenger query) {
|
44
|
entryLock.lock();
|
45
|
this.inputFile = inputFile;
|
46
|
this.query = query;
|
47
|
entryLock.unlock();
|
48
|
|
49
|
if (!inputAvailable.tryAcquire())
|
50
|
inputAvailable.release(); // MAX VALUE = 1!
|
51
|
}
|
52
|
|
53
|
public boolean getInput(AtomicReference<File> inputFile, AtomicReference<DB_Messenger> query) {
|
54
|
try {
|
55
|
inputAvailable.acquire();
|
56
|
} catch (InterruptedException e) {
|
57
|
return false;
|
58
|
}
|
59
|
|
60
|
entryLock.lock();
|
61
|
inputFile.set(this.inputFile);
|
62
|
query.set(this.query);
|
63
|
|
64
|
this.inputFile = null;
|
65
|
this.query = null;
|
66
|
entryLock.unlock();
|
67
|
|
68
|
return inputFile.get() != null || query.get() != null;
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
public void run() {
|
73
|
super.run();
|
74
|
while (active.get()) {
|
75
|
AtomicReference<File> inputFile = new AtomicReference<File>();
|
76
|
AtomicReference<DB_Messenger> query = new AtomicReference<DB_Messenger>();
|
77
|
|
78
|
if (!getInput(inputFile, query)) {
|
79
|
continue;
|
80
|
}
|
81
|
|
82
|
byte array[];
|
83
|
try {
|
84
|
array = getBytes(inputFile.get(), query.get());
|
85
|
} catch (Exception e) {
|
86
|
ui.loadingInputFileError();
|
87
|
continue;
|
88
|
}
|
89
|
|
90
|
StringBuilder json = new StringBuilder();
|
91
|
|
92
|
boolean error = false;
|
93
|
try {
|
94
|
// Redirectovany system.out do null streamu. Mozno redirect do souboru
|
95
|
System.setOut(FileWorker.createRedirectStream(false));
|
96
|
System.setErr(FileWorker.createRedirectStream(true));
|
97
|
System.out.println("--- jdeserialize begins ---".toUpperCase());
|
98
|
convert(array, json);
|
99
|
} catch (Exception e) {
|
100
|
e.printStackTrace();
|
101
|
error = true;
|
102
|
} finally {
|
103
|
System.out.println("--- jdeserialize ends ---".toUpperCase());
|
104
|
System.out.flush();
|
105
|
System.err.flush();
|
106
|
System.setErr(new PrintStream(new FileOutputStream(FileDescriptor.err)));
|
107
|
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
|
108
|
}
|
109
|
|
110
|
ui.completed(error ? null : json.toString(), array == null ? null : new String(array));
|
111
|
}
|
112
|
}
|
113
|
|
114
|
private byte[] getBytes(File file, DB_Messenger query) throws Exception {
|
115
|
byte array[];
|
116
|
if (file != null) {
|
117
|
array = FileWorker.loadFileContent(file);
|
118
|
} else {
|
119
|
array = Database.getBlobBytes(query);
|
120
|
}
|
121
|
array = FileWorker.extractFileContent(array);
|
122
|
return array;
|
123
|
}
|
124
|
|
125
|
private void convert(byte[] buffer, StringBuilder json) throws Exception {
|
126
|
jdeserialize deserializer = new jdeserialize(buffer);
|
127
|
|
128
|
// gets the "contents" into an array - returnes the deserialization of all
|
129
|
// 'writes' into serialized object via objectOutputStream
|
130
|
List<content> cntnts = deserializer.getContent();
|
131
|
|
132
|
for (content cnt : cntnts) {
|
133
|
if (cnt != null) {
|
134
|
json.append(cnt.toJson("", null, false)).append("\n");
|
135
|
}
|
136
|
}
|
137
|
}
|
138
|
|
139
|
}
|