Source code: org/linpoch/kde/dcop/LinPochFileService.java
1 package org.linpoch.kde.dcop;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.DataInputStream;
6 import java.io.DataOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.kde.dcop.Marchaller;
15 import org.kde.koala.DCOPAnswer;
16 import org.kde.koala.DCOPObject;
17 import plp.presentation.fileclient.Directory;
18 import plp.presentation.fileclient.EpocFile;
19 import plp.presentation.fileclient.FileClient;
20 import plp.presentation.fileclient.RegularFile;
21
22 /**
23 * This object serves as a DCOP object in KDE.
24 *
25 * @author Gert-Jan van der Heiden
26 */
27 public class LinPochFileService extends DCOPObject{
28 private Map openFiles;
29 private Map openInputStreams;
30 private Map openOutputStreams;
31 private FileClient fileClient;
32 public LinPochFileService(FileClient fileClient) {
33 super("LinPochFileService");
34 this.setFileClient(fileClient);
35 }
36 private void setFileClient(FileClient client) {
37 this.fileClient = client;
38 }
39 public FileClient getFileClient() {
40 return this.fileClient;
41 }
42 public ArrayList functions() {
43 ArrayList operations = new ArrayList();
44 operations.add("QString myOperation()");
45 operations.add("DirectoryAnswer getDirectory(QString url)");
46 operations.add("FileAnswer getFile(QString url)");
47 operations.add("int openFile(QString url)");
48 operations.add("FileContentAnswer read(int fileReference, int size)");
49 operations.add("void close(int fileReference)");
50 operations.add("QString deleteFile(String url)");
51 operations.add("QString write(int fileReference, QByteArray data)");
52 operations.add("int write(int fileReference, QByteArray data)");
53 operations.add("QString deleteDirectory(QString url)");
54 operations.add("QString makeDirectory(QString url)");
55 operations.add("QString rename(QString sourceUrl, QString destUrl, bool overwrite)");
56 operations.add("bool isConnected()");
57 return operations;
58 }
59 public ArrayList interfaces() {
60 ArrayList list = new ArrayList();
61 list.add("LinPochFileService");
62 return list;
63 }
64 /**
65 * This operation is the java variant to the pocess used in c++
66 *
67 * This operation SHOULD be generated!
68 * @param fun
69 * @param data
70 * @return
71 */
72 public DCOPAnswer javaProcess(String fun, byte[] data) {
73 DCOPAnswer answer = new DCOPAnswer();
74 try {
75 if ("myOperation()".equals(fun)) {
76 answer.setReplyType("QString");
77 answer.setSucces(true);
78
79 ByteArrayOutputStream stream = new ByteArrayOutputStream();
80 DataOutputStream os = new DataOutputStream(stream);
81 Marchaller.write_QString(os, "test from java!");
82 answer.setReplyData(stream.toByteArray());
83 return answer;
84 } else if ("getDirectory()".equals(fun)) {
85 // deserialize the parameter
86 ByteArrayInputStream is = new ByteArrayInputStream(data);
87 DataInputStream dis = new DataInputStream(is);
88 String url = Marchaller.read_QString(dis);
89 ByteArrayOutputStream stream = new ByteArrayOutputStream();
90 DataOutputStream os = new DataOutputStream(stream);
91 // call the operation
92 DirectoryAnswer operationReply = this.getDirectory(url);
93 // serialize the answer
94 operationReply.write(os);
95 // set answer object
96 answer.setReplyType("DirectoryAnswer");
97 answer.setReplyData(stream.toByteArray());
98 answer.setSucces(true);
99 return answer;
100 } else if ("getFile()".equals(fun)) {
101 // deserialize the parameter
102 ByteArrayInputStream is = new ByteArrayInputStream(data);
103 DataInputStream dis = new DataInputStream(is);
104 String url = Marchaller.read_QString(dis);
105 ByteArrayOutputStream stream = new ByteArrayOutputStream();
106 DataOutputStream os = new DataOutputStream(stream);
107 // call the operation
108 FileAnswer operationReply = this.getFile(url);
109 // serialize the answer
110 operationReply.write(os);
111 // set answer object
112 answer.setReplyType("FileAnswer");
113 answer.setReplyData(stream.toByteArray());
114 answer.setSucces(true);
115 return answer;
116 } else if ("openFile()".equals(fun)) {
117 // deserialize the parameter
118 ByteArrayInputStream is = new ByteArrayInputStream(data);
119 DataInputStream dis = new DataInputStream(is);
120 String url = Marchaller.read_QString(dis);
121 ByteArrayOutputStream stream = new ByteArrayOutputStream();
122 DataOutputStream os = new DataOutputStream(stream);
123 // call the operation
124 int operationReply = this.openFile(url);
125 // serialize the answer
126 Marchaller.write_int(os, operationReply);
127 // set answer object
128 answer.setReplyType("int");
129 answer.setReplyData(stream.toByteArray());
130 answer.setSucces(true);
131 return answer;
132 } else if ("read()".equals(fun)) {
133 // deserialize the parameter
134 ByteArrayInputStream is = new ByteArrayInputStream(data);
135 DataInputStream dis = new DataInputStream(is);
136 int ref = Marchaller.read_int(dis);
137 int length = Marchaller.read_int(dis);
138 ByteArrayOutputStream stream = new ByteArrayOutputStream();
139 DataOutputStream os = new DataOutputStream(stream);
140 // call the operation
141 FileContentAnswer operationReply = this.read(ref, length);
142 // serialize the answer
143 operationReply.write(os);
144 // set answer object
145 answer.setReplyType("FileContentAnswer");
146 answer.setReplyData(stream.toByteArray());
147 answer.setSucces(true);
148 return answer;
149 } else if ("closeFile()".equals(fun)) {
150 // deserialize the parameter
151 ByteArrayInputStream is = new ByteArrayInputStream(data);
152 DataInputStream dis = new DataInputStream(is);
153 int ref = Marchaller.read_int(dis);
154 // call the operation
155 this.closeFile(ref);
156 // set answer object
157 answer.setReplyType("FileContentAnswer");
158 // answer.setReplyData(byte[0]);
159 answer.setSucces(true);
160 return answer;
161 } else if ("deleteFile()".equals(fun)) {
162 // deserialize the parameter
163 ByteArrayInputStream is = new ByteArrayInputStream(data);
164 DataInputStream dis = new DataInputStream(is);
165 String url = Marchaller.read_QString(dis);
166 // call the operation
167 String errorMessage = this.deleteFile(url);
168 // serialize the answer
169 ByteArrayOutputStream stream = new ByteArrayOutputStream();
170 DataOutputStream os = new DataOutputStream(stream);
171 Marchaller.write_QString(os, errorMessage);
172 // set answer object
173 answer.setReplyType("QString");
174 answer.setReplyData(stream.toByteArray());
175 answer.setSucces(true);
176 return answer;
177 } else if ("write()".equals(fun)) {
178 // deserialize the parameter
179 ByteArrayInputStream is = new ByteArrayInputStream(data);
180 DataInputStream dis = new DataInputStream(is);
181 int fileReference = Marchaller.read_int(dis);
182 byte[] dataToWrite = Marchaller.read_QByteArray(dis);
183 // call the operation
184 String errorMessage = this.write(fileReference, dataToWrite);
185 // serialize the answer
186 ByteArrayOutputStream stream = new ByteArrayOutputStream();
187 DataOutputStream os = new DataOutputStream(stream);
188 Marchaller.write_QString(os, errorMessage);
189 // set answer object
190 answer.setReplyType("QString");
191 answer.setReplyData(stream.toByteArray());
192 answer.setSucces(true);
193 return answer;
194 } else if ("createFile()".equals(fun)) {
195 // deserialize the parameter
196 ByteArrayInputStream is = new ByteArrayInputStream(data);
197 DataInputStream dis = new DataInputStream(is);
198 String url = Marchaller.read_QString(dis);
199 // call the operation
200 int fileReference = this.createFile(url);
201 // serialize the answer
202 ByteArrayOutputStream stream = new ByteArrayOutputStream();
203 DataOutputStream os = new DataOutputStream(stream);
204 Marchaller.write_int(os, fileReference);
205 // set answer object
206 answer.setReplyType("int");
207 answer.setReplyData(stream.toByteArray());
208 answer.setSucces(true);
209 return answer;
210 } else if ("deleteDirectory()".equals(fun)) {
211 // deserialize the parameter
212 ByteArrayInputStream is = new ByteArrayInputStream(data);
213 DataInputStream dis = new DataInputStream(is);
214 String url = Marchaller.read_QString(dis);
215 // call the operation
216 String errorMessage = this.deleteDirectory(url);
217 // serialize the answer
218 ByteArrayOutputStream stream = new ByteArrayOutputStream();
219 DataOutputStream os = new DataOutputStream(stream);
220 Marchaller.write_QString(os, errorMessage);
221 // set answer object
222 answer.setReplyType("QString");
223 answer.setReplyData(stream.toByteArray());
224 answer.setSucces(true);
225 return answer;
226 } else if ("makeDirectory()".equals(fun)) {
227 // deserialize the parameter
228 ByteArrayInputStream is = new ByteArrayInputStream(data);
229 DataInputStream dis = new DataInputStream(is);
230 String url = Marchaller.read_QString(dis);
231 // call the operation
232 String errorMessage = this.makeDirectory(url);
233 // serialize the answer
234 ByteArrayOutputStream stream = new ByteArrayOutputStream();
235 DataOutputStream os = new DataOutputStream(stream);
236 Marchaller.write_QString(os, errorMessage);
237 // set answer object
238 answer.setReplyType("QString");
239 answer.setReplyData(stream.toByteArray());
240 answer.setSucces(true);
241 return answer;
242 }else if ("rename()".equals(fun)) {
243 // deserialize the parameter
244 ByteArrayInputStream is = new ByteArrayInputStream(data);
245 DataInputStream dis = new DataInputStream(is);
246 String urlSource = Marchaller.read_QString(dis);
247 String urlDestination = Marchaller.read_QString(dis);
248 boolean overwrite =Marchaller.read_bool(dis);
249 // call the operation
250 String errorMessage = this.rename(urlSource, urlDestination, overwrite);
251 // serialize the answer
252 ByteArrayOutputStream stream = new ByteArrayOutputStream();
253 DataOutputStream os = new DataOutputStream(stream);
254 Marchaller.write_QString(os, errorMessage);
255 // set answer object
256 answer.setReplyType("QString");
257 answer.setReplyData(stream.toByteArray());
258 answer.setSucces(true);
259 return answer;
260 }else if ("isConnected()".equals(fun)) {
261 // deserialize the parameter
262 ByteArrayInputStream is = new ByteArrayInputStream(data);
263 DataInputStream dis = new DataInputStream(is);
264 // call the operation
265 boolean isConnected= this.isConnected();
266 // serialize the answer
267 ByteArrayOutputStream stream = new ByteArrayOutputStream();
268 DataOutputStream os = new DataOutputStream(stream);
269 Marchaller.write_bool(os, isConnected);
270 // set answer object
271 answer.setReplyType("bool");
272 answer.setReplyData(stream.toByteArray());
273 answer.setSucces(true);
274 return answer;
275 }else {
276 return answer;
277 }
278 } catch (IOException ioe) {
279 ioe.printStackTrace();
280 return answer;
281 }
282 }
283
284 /*------------------*/
285 private String makeEpocUrl(String url) {
286 String epocUrl;
287 if (url != null && url.length() > 1) {
288 epocUrl = url.replace('/', '\\');
289 if (epocUrl.charAt(0) == '\\') {
290 epocUrl = epocUrl.substring(1);
291 }
292
293 } else {
294 epocUrl = "";
295 }
296 return epocUrl;
297 }
298 private void addOpenFile(RegularFile file) {
299 this.getOpenFiles().put(file.getHandle(), file);
300 }
301 private void removeOpenFile(RegularFile file) {
302 this.getOpenFiles().remove(file.getHandle());
303 }
304 private RegularFile getOpenFile(int refNumber) {
305 return (RegularFile) this.getOpenFiles().get(new Integer(refNumber));
306
307 }
308 private Map getOpenFiles() {
309 if (this.openFiles == null)
310 this.openFiles = new HashMap();
311 return this.openFiles;
312 }
313 private Map getOpenInputStreams() {
314 if (this.openInputStreams == null)
315 this.openInputStreams = new HashMap();
316 return this.openInputStreams;
317 }
318 private void addOpenInputStream(RegularFile file, InputStream stream) {
319 this.getOpenInputStreams().put(file.getHandle(), stream);
320 }
321 private void removeOpenStream(RegularFile file) {
322 this.getOpenInputStreams().remove(file.getHandle());
323 }
324 private InputStream getOpenInputStream(RegularFile file) throws IOException {
325 InputStream stream =
326 (InputStream) this.getOpenInputStreams().get(file.getHandle());
327 if (stream == null) {
328 stream = file.openInputStream();
329 this.addOpenInputStream(file, stream);
330 }
331 return stream;
332 }
333 private Map getOpenOutputStreams() {
334 if (this.openOutputStreams == null)
335 this.openOutputStreams = new HashMap();
336 return this.openOutputStreams;
337 }
338 private void addOpenOutputStream(RegularFile file, OutputStream stream) {
339 this.getOpenOutputStreams().put(file.getHandle(), stream);
340 }
341 private void removeOpenOutputStream(RegularFile file) {
342 this.getOpenOutputStreams().remove(file.getHandle());
343 }
344 private OutputStream getOpenOutputStream(RegularFile file)
345 throws IOException {
346 OutputStream stream =
347 (OutputStream) this.getOpenOutputStreams().get(file.getHandle());
348 if (stream == null) {
349 stream = file.openOutputStream();
350 this.addOpenOutputStream(file, stream);
351 }
352 return stream;
353 }
354
355 public DirectoryAnswer getDirectory(String url) {
356 String epocUrl = this.makeEpocUrl(url);
357 if (epocUrl.endsWith("\\"))
358 epocUrl += "*";
359 else
360 epocUrl += "\\*";
361
362 DirectoryAnswer dir = new DirectoryAnswer("", "");
363
364 if ("\\*".equals(epocUrl)) {
365 dir.addFile(new DCOPFile("C:", true));
366 dir.addFile(new DCOPFile("D:", true));
367 } else {
368 Directory epocDir = this.getFileClient().openDir(epocUrl);
369 if (epocDir != null) {
370 for (int i = 0; i < epocDir.getNumberOfFiles(); i++) {
371 EpocFile file = epocDir.getFileAt(i);
372 dir.addFile(
373 new DCOPFile(file.getName(), file.isDirectory(), file.getSize()));
374 }
375 } else {
376 dir.setErrorMessage("not a directory");
377 }
378 }
379 return dir;
380 }
381 public FileAnswer getFile(String url) {
382 String epocUrl = this.makeEpocUrl(url);
383 if (epocUrl.endsWith("\\"))
384 epocUrl = epocUrl.substring(0, epocUrl.length() - 1);
385 if ("C:".equals(epocUrl) || "D:".equals(epocUrl) || "Z:".equals(epocUrl))
386 return new FileAnswer(epocUrl, true, 0);
387 EpocFile epocFile = this.getFileClient().getFile(epocUrl);
388 if (epocFile != null) {
389 FileAnswer answer = new FileAnswer(epocFile);
390 return answer;
391 } else{
392 return new FileAnswer("file doesn't exist");
393 }
394 }
395 public int openFile(String url) {
396 String myUrl = this.makeEpocUrl(url);
397 try {
398 RegularFile file = this.getFileClient().openFile(myUrl);
399 this.addOpenFile(file);
400 return file.getHandle().intValue();
401 } catch (IOException ioe) {
402 return -1;
403 }
404 }
405 public String write(int fileReference, byte[] data) {
406 RegularFile file = this.getOpenFile(fileReference);
407 int bytesWritten = 0;
408 final int blockSize = 1024;
409 if (file != null) {
410 try {
411 OutputStream stream = this.getOpenOutputStream(file);
412 while (bytesWritten < data.length) {
413 int length = Math.min(blockSize, data.length - bytesWritten);
414 stream.write(data, bytesWritten, length);
415 bytesWritten += length;
416 }
417 return "";
418 } catch (IOException ioe) {
419 return ioe.getMessage();
420 }
421 } else {
422 return "File is not opened";
423 }
424 }
425 public FileContentAnswer read(int fileReference, int length) {
426 RegularFile file = this.getOpenFile(fileReference);
427 if (file != null) {
428 try {
429 InputStream stream = this.getOpenInputStream(file);
430 byte[] data = new byte[length];
431 int numberOfBytesRead = stream.read(data);
432 if (numberOfBytesRead < length) {
433 byte[] shorterData = new byte[numberOfBytesRead];
434 System.arraycopy(data, 0, shorterData, 0, numberOfBytesRead);
435 return new FileContentAnswer(shorterData);
436 } else
437 return new FileContentAnswer(data);
438 } catch (IOException ioe) {
439 return new FileContentAnswer(ioe.getMessage());
440 }
441 } else {
442 return new FileContentAnswer("File is not opened");
443 }
444 }
445 public void closeFile(int fileReference) {
446 RegularFile file = this.getOpenFile(fileReference);
447 if (file != null) {
448 this.removeOpenStream(file);
449 this.removeOpenOutputStream(file);
450 this.removeOpenFile(file);
451 file.close();
452 }
453 }
454 public String deleteFile(String url) {
455 String epocUrl = this.makeEpocUrl(url);
456 if (epocUrl.endsWith("\\"))
457 epocUrl = epocUrl.substring(0, epocUrl.length() - 1);
458 try {
459 this.getFileClient().deleteFile(epocUrl);
460 return "";
461 } catch (IOException ioe) {
462 return ioe.getMessage();
463 }
464 }
465 public int createFile(String url) {
466 String epocUrl = this.makeEpocUrl(url);
467 if (epocUrl.endsWith("\\"))
468 epocUrl = epocUrl.substring(0, epocUrl.length() - 1);
469 try {
470 RegularFile file = this.getFileClient().createFile(epocUrl);
471 this.addOpenFile(file);
472 return file.getHandle().intValue();
473 } catch (IOException ioe) {
474 System.out.println(ioe.getMessage());
475 return -1;
476 }
477 }
478 public String deleteDirectory(String url) {
479 String epocUrl = this.makeEpocUrl(url);
480 if (!epocUrl.endsWith("\\"))
481 epocUrl += "\\";
482 try {
483 this.getFileClient().deleteDirectory(epocUrl);
484 return "";
485 } catch (IOException ioe) {
486 System.out.println("error deleting dir: " + ioe.getMessage());
487 return ioe.getMessage();
488 }
489 }
490 public String makeDirectory(String url) {
491 String epocUrl = this.makeEpocUrl(url);
492 epocUrl += "\\";
493 try {
494 this.getFileClient().makeDirectory(epocUrl);
495 return "";
496 } catch (IOException ioe) {
497 return ioe.getMessage();
498 }
499 }
500 public boolean isConnected(){
501 return this.getFileClient().isConnected();
502 }
503 public String rename(String sourceUrl, String destinationUrl, boolean overwrite){
504 String src = this.makeEpocUrl(sourceUrl);
505 String dst = this.makeEpocUrl(destinationUrl);
506 if(overwrite)
507 return "NYI";
508 try{
509 this.getFileClient().rename(src,dst);
510 return "";
511 }catch(IOException ioe){
512 return ioe.getMessage();
513 }
514
515 }
516 }