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