Projekt

Obecné

Profil

Stáhnout (5.4 KB) Statistiky
| Větev: | Tag: | Revize:
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 967b6b05 horkmi
/**
19
 * An instance of this class is a thread for converting an input file or SQL
20
 * query to the resulting JSON.
21
 */
22 03a2d08f Michal Horký
public class Converter extends Thread {
23 81ff7da4 @havlijan17
24 967b6b05 horkmi
	/**
25
	 * The user interface that this thread created.
26
	 */
27 81ff7da4 @havlijan17
	private IConversionResults ui;
28 967b6b05 horkmi
29
	/**
30
	 * True, if thread is active, otherwise false. Thread-safe variable.
31
	 */
32 49fd8648 Michal Horký
	private AtomicBoolean active;
33 81ff7da4 @havlijan17
34 967b6b05 horkmi
	/**
35
	 * "Mutex" for the producer–consumer.
36
	 */
37 a5d16f0a horkmi
	private Lock entryLock;
38 967b6b05 horkmi
39
	/**
40
	 * Semaphore for the existence of an input requirement (producer-consumer).
41
	 */
42 49fd8648 Michal Horký
	private Semaphore inputAvailable;
43 81ff7da4 @havlijan17
44 967b6b05 horkmi
	/**
45
	 * Variable for input file. Set by the user.
46
	 */
47 a5d16f0a horkmi
	private File inputFile;
48 967b6b05 horkmi
49
	/**
50
	 * Variable for SQL query. Set by the user.
51
	 */
52 7e0b699a horkmi
	private DB_Messenger query;
53 81ff7da4 @havlijan17
54 967b6b05 horkmi
	/**
55
	 * Initializes instance attributes.
56
	 * 
57
	 * @param ui instance of the Window or CLI.
58
	 */
59 49fd8648 Michal Horký
	public Converter(IConversionResults ui) {
60
		this.ui = ui;
61
		active = new AtomicBoolean(true);
62 a5d16f0a horkmi
		entryLock = new ReentrantLock();
63 49fd8648 Michal Horký
		inputAvailable = new Semaphore(0);
64
	}
65 81ff7da4 @havlijan17
66 967b6b05 horkmi
	/**
67
	 * End of the thread run.
68
	 */
69 49fd8648 Michal Horký
	public void end() {
70
		active.set(false);
71 7e0b699a horkmi
		setInput(null, null);
72 49fd8648 Michal Horký
	}
73 967b6b05 horkmi
74
	/**
75
	 * Sets the input processing request. One of the input parameters should be
76
	 * null.
77
	 * 
78
	 * @param inputFile input file.
79
	 * @param query     SQL query.
80
	 */
81 7e0b699a horkmi
	public void setInput(File inputFile, DB_Messenger query) {
82 a5d16f0a horkmi
		entryLock.lock();
83
		this.inputFile = inputFile;
84 7e0b699a horkmi
		this.query = query;
85 a5d16f0a horkmi
		entryLock.unlock();
86 81ff7da4 @havlijan17
87 a5d16f0a horkmi
		if (!inputAvailable.tryAcquire())
88
			inputAvailable.release(); // MAX VALUE = 1!
89
	}
90 967b6b05 horkmi
91
	/**
92
	 * Get a request to process the input.
93
	 * 
94
	 * @param inputFile variable for input file "passing by reference".
95
	 * @param query     variable for SQL query "passing by reference".
96
	 * 
97
	 * @return true, when there is a request to process the input.
98
	 */
99 7e0b699a horkmi
	public boolean getInput(AtomicReference<File> inputFile, AtomicReference<DB_Messenger> query) {
100 a5d16f0a horkmi
		try {
101
			inputAvailable.acquire();
102
		} catch (InterruptedException e) {
103
			return false;
104
		}
105 81ff7da4 @havlijan17
106 a5d16f0a horkmi
		entryLock.lock();
107
		inputFile.set(this.inputFile);
108 7e0b699a horkmi
		query.set(this.query);
109 967b6b05 horkmi
110 a5d16f0a horkmi
		this.inputFile = null;
111 7e0b699a horkmi
		this.query = null;
112 a5d16f0a horkmi
		entryLock.unlock();
113 81ff7da4 @havlijan17
114 7e0b699a horkmi
		return inputFile.get() != null || query.get() != null;
115 03a2d08f Michal Horký
	}
116 81ff7da4 @havlijan17
117 03a2d08f Michal Horký
	@Override
118
	public void run() {
119
		super.run();
120 49fd8648 Michal Horký
		while (active.get()) {
121 a5d16f0a horkmi
			AtomicReference<File> inputFile = new AtomicReference<File>();
122 7e0b699a horkmi
			AtomicReference<DB_Messenger> query = new AtomicReference<DB_Messenger>();
123 81ff7da4 @havlijan17
124 967b6b05 horkmi
			// Get input data from the user.
125 7e0b699a horkmi
			if (!getInput(inputFile, query)) {
126 967b6b05 horkmi
				// If there is no data, continue.
127 49fd8648 Michal Horký
				continue;
128
			}
129 81ff7da4 @havlijan17
130 967b6b05 horkmi
			// Get the byte array from the input.
131 e608f492 horkmi
			byte array[];
132
			try {
133
				array = getBytes(inputFile.get(), query.get());
134
			} catch (Exception e) {
135 967b6b05 horkmi
				// If an error occurs, it informs the user interface and continues.
136 e608f492 horkmi
				ui.loadingInputFileError();
137
				continue;
138
			}
139 967b6b05 horkmi
140
			// Deserialization of read data.
141
			StringBuilder json = new StringBuilder();
142
			boolean error = false;
143 5ae26965 horkmi
			try {
144 84937a3f @havlijan17
				// Redirection of System.out to jdeserialize log file
145 e608f492 horkmi
				System.setOut(FileWorker.createRedirectStream(false));
146
				System.setErr(FileWorker.createRedirectStream(true));
147
				System.out.println("--- jdeserialize begins ---".toUpperCase());
148 c670fa2f @havlijan17
				convert(array, json);
149 5ae26965 horkmi
			} catch (Exception e) {
150 e608f492 horkmi
				e.printStackTrace();
151
				error = true;
152 5ae26965 horkmi
			} finally {
153 e608f492 horkmi
				System.out.println("--- jdeserialize ends ---".toUpperCase());
154
				System.out.flush();
155
				System.err.flush();
156
				System.setErr(new PrintStream(new FileOutputStream(FileDescriptor.err)));
157 5ae26965 horkmi
				System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
158 a5d16f0a horkmi
			}
159 967b6b05 horkmi
160
			// Pass the deserialization results to the UI.
161 c670fa2f @havlijan17
			ui.completed(error ? null : json.toString(), array == null ? null : new String(array));
162 49fd8648 Michal Horký
		}
163
	}
164 81ff7da4 @havlijan17
165 967b6b05 horkmi
	/**
166
	 * Retrieve data from the file or database. One parameter should always be null.
167
	 * 
168
	 * @param file  input file.
169
	 * @param query input query.
170
	 * 
171
	 * @return read data in a byte array.
172
	 * @throws Exception if an error occurs while retrieving data.
173
	 */
174 e608f492 horkmi
	private byte[] getBytes(File file, DB_Messenger query) throws Exception {
175 49fd8648 Michal Horký
		byte array[];
176 e608f492 horkmi
		if (file != null) {
177
			array = FileWorker.loadFileContent(file);
178
		} else {
179
			array = Database.getBlobBytes(query);
180 49fd8648 Michal Horký
		}
181 e608f492 horkmi
		array = FileWorker.extractFileContent(array);
182 5ae26965 horkmi
		return array;
183 a5d16f0a horkmi
	}
184 967b6b05 horkmi
185 84937a3f @havlijan17
	/**
186 967b6b05 horkmi
	 * Converts the input byte buffer given by parameter into json which is saved
187
	 * using the StringBuilder.
188
	 * 
189
	 * @param buffer byte array buffer for input data of serialized object.
190
	 * @param json   StringBuilder to which will be saved the json result.
191 84937a3f @havlijan17
	 * 
192 967b6b05 horkmi
	 * @throws Exception if an error occurred during deserialization.
193 84937a3f @havlijan17
	 */
194 c670fa2f @havlijan17
	private void convert(byte[] buffer, StringBuilder json) throws Exception {
195 49fd8648 Michal Horký
		jdeserialize deserializer = new jdeserialize(buffer);
196 a5d16f0a horkmi
197 84937a3f @havlijan17
		// gets the "contents" into an array - returns the deserialization of all
198 81ff7da4 @havlijan17
		// 'writes' into serialized object via objectOutputStream
199
		List<content> cntnts = deserializer.getContent();
200
201
		for (content cnt : cntnts) {
202
			if (cnt != null) {
203 8ccb98b8 @havlijan17
				json.append(cnt.toJson("", null, false)).append("\n");
204 e3d5fc53 @havlijan17
			}
205 03a2d08f Michal Horký
		}
206
	}
207
208
}