Projekt

Obecné

Profil

Stáhnout (3.81 KB) Statistiky
| Větev: | Tag: | Revize:
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
			StringBuilder json = new StringBuilder();
78
			boolean error = false;
79

    
80
			if (!getInput(inputFile, query)) {
81
				continue;
82
			}
83

    
84
			byte array[];
85
			try {
86
				array = getBytes(inputFile.get(), query.get());
87
			} catch (Exception e) {
88
				ui.loadingInputFileError();
89
				continue;
90
			}
91
			
92
			try {
93
				// Redirection of System.out to jdeserialize log file
94
				System.setOut(FileWorker.createRedirectStream(false));
95
				System.setErr(FileWorker.createRedirectStream(true));
96
				System.out.println("--- jdeserialize begins ---".toUpperCase());
97
				convert(array, json);
98
			} catch (Exception e) {
99
				e.printStackTrace();
100
				error = true;
101
			} finally {
102
				System.out.println("--- jdeserialize ends ---".toUpperCase());
103
				System.out.flush();
104
				System.err.flush();
105
				System.setErr(new PrintStream(new FileOutputStream(FileDescriptor.err)));
106
				System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
107
			}
108
			
109
			ui.completed(error ? null : json.toString(), array == null ? null : new String(array));
110
		}
111
	}
112

    
113
	private byte[] getBytes(File file, DB_Messenger query) throws Exception {
114
		byte array[];
115
		if (file != null) {
116
			array = FileWorker.loadFileContent(file);
117
		} else {
118
			array = Database.getBlobBytes(query);
119
		}
120
		array = FileWorker.extractFileContent(array);
121
		return array;
122
	}
123
	
124
	/**
125
	 * Converts the input byte buffer given by parameter into json
126
	 * which is saved using the StringBuilder.
127
	 * 
128
	 * @param buffer  byte array buffer for input data of serialized object
129
	 * @param json	  StringBuilder to which will be saved the json result
130
	 */
131
	private void convert(byte[] buffer, StringBuilder json) throws Exception {
132
		jdeserialize deserializer = new jdeserialize(buffer);
133

    
134
		// gets the "contents" into an array - returns the deserialization of all
135
		// 'writes' into serialized object via objectOutputStream
136
		List<content> cntnts = deserializer.getContent();
137

    
138
		for (content cnt : cntnts) {
139
			if (cnt != null) {
140
				json.append(cnt.toJson("", null, false)).append("\n");
141
			}
142
		}
143
	}
144

    
145
}
(2-2/6)