]> git.argeo.org Git - gpl/argeo-slc.git/blob - FileDriver.java
fafa3ebf69506b58df812521734d44bc0a37e8e1
[gpl/argeo-slc.git] / FileDriver.java
1 package org.argeo.slc.detached.drivers;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.ObjectInputStream;
8 import java.io.ObjectOutputStream;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.slc.detached.DetachedAnswer;
13 import org.argeo.slc.detached.DetachedClient;
14 import org.argeo.slc.detached.DetachedCommunication;
15 import org.argeo.slc.detached.DetachedException;
16 import org.argeo.slc.detached.DetachedRequest;
17 import org.springframework.beans.factory.InitializingBean;
18
19 public class FileDriver extends AbstractDriver implements DetachedClient,
20 InitializingBean {
21 private final static Log log = LogFactory.getLog(FileDriver.class);
22
23 private File baseDir;
24 private File requestsDir;
25 private File answersDir;
26 private File processedRequestsDir;
27 private File processedAnswersDir;
28
29 public synchronized DetachedRequest receiveRequest() throws Exception {
30 DetachedRequest request = (DetachedRequest) receiveFile(requestsDir,
31 processedRequestsDir);
32 if (request != null)
33 log.debug("Received detached request #" + request.getUuid()
34 + " for ref '" + request.getRef() + "', path="
35 + request.getPath());
36 return request;
37 }
38
39 public void sendAnswer(DetachedAnswer answer) throws Exception {
40 sendFile(answersDir, answer);
41 log.debug("Sent detached answer #" + answer.getUuid());
42 }
43
44 public DetachedAnswer receiveAnswer() throws Exception {
45 DetachedAnswer answer = (DetachedAnswer) receiveFile(answersDir,
46 processedAnswersDir);
47 if (answer != null)
48 log.debug("Received detached answer #" + answer.getUuid());
49 return answer;
50 }
51
52 public void sendRequest(DetachedRequest request) throws Exception {
53 sendFile(requestsDir, request);
54 log.debug("Sent detached request #" + request.getUuid()
55 + " for ref '" + request.getRef() + "', path="
56 + request.getPath());
57 }
58
59 protected void sendFile(File dir, DetachedCommunication detCom)
60 throws Exception {
61 File file = new File(dir.getPath() + File.separator + detCom.getUuid());
62 File lockFile = createLockFile(file);
63 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
64 file));
65 out.writeObject(detCom);
66 out.close();
67 lockFile.delete();
68 }
69
70 protected synchronized DetachedCommunication receiveFile(File dir,
71 File processedDir) throws Exception {
72 File file = null;
73 while (file == null && isActive()) {
74 if (!dir.exists())
75 throw new DetachedException("Dir " + dir + " does not exist.");
76
77 File[] files = dir.listFiles();
78 if (files.length > 0)
79 file = files[0];
80 else {
81 try {
82 wait(100);
83 } catch (InterruptedException e) {
84 // silent
85 }
86 }
87 }
88
89 if (!isActive())
90 return null;
91
92 File lockFile = nameLockFile(file);
93 while (lockFile.exists())
94 // TODO: implements time out
95 Thread.sleep(100);
96
97 ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
98 DetachedCommunication detCom = (DetachedCommunication) in.readObject();
99 in.close();
100
101 // Move to processed dir
102 file.renameTo(new File(processedDir.getAbsolutePath() + File.separator
103 + file.getName()));
104 return detCom;
105
106 }
107
108 protected File createLockFile(File file) {
109 File lockFile = nameLockFile(file);
110 try {
111 lockFile.createNewFile();
112 } catch (IOException e) {
113 throw new DetachedException("Cannot create lock file " + lockFile);
114 }
115 return lockFile;
116 }
117
118 protected File nameLockFile(File file) {
119 return new File(file.getAbsolutePath() + ".lck");
120 }
121
122 public void setBaseDir(File baseDir) {
123 this.baseDir = baseDir;
124 }
125
126 private void createIfNotExist(File dir) {
127 if (!dir.exists()) {
128 log.warn("Dir " + dir.getAbsolutePath()
129 + " does not exist. Creating it...");
130 dir.mkdirs();
131 }
132 }
133
134 public void afterPropertiesSet() throws Exception {
135 this.requestsDir = new File(baseDir.getAbsolutePath() + File.separator
136 + "requests");
137 this.answersDir = new File(baseDir.getAbsolutePath() + File.separator
138 + "answers");
139 this.processedRequestsDir = new File(baseDir.getAbsolutePath()
140 + File.separator + "processed" + File.separator + "requests");
141 this.processedAnswersDir = new File(baseDir.getAbsolutePath()
142 + File.separator + "processed" + File.separator + "answers");
143
144 createIfNotExist(requestsDir);
145 createIfNotExist(answersDir);
146 createIfNotExist(processedRequestsDir);
147 createIfNotExist(processedAnswersDir);
148 }
149
150 }