+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package org.argeo.slc.detached.drivers;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
+import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.argeo.slc.detached.DetachedAnswer;
import org.argeo.slc.detached.DetachedClient;
import org.argeo.slc.detached.DetachedCommunication;
+import org.argeo.slc.detached.DetachedDriver;
import org.argeo.slc.detached.DetachedException;
import org.argeo.slc.detached.DetachedRequest;
+import org.argeo.slc.detached.DetachedXmlConverter;
import org.springframework.beans.factory.InitializingBean;
-public class FileDriver extends AbstractDriver implements DetachedClient,
+/**
+ * Implements both <code>DetachedClient</code> and <code>DetachedDriver</code>
+ * using File protocol
+ */
+public class FileDriver implements DetachedClient, DetachedDriver,
InitializingBean {
private final static Log log = LogFactory.getLog(FileDriver.class);
private final static SimpleDateFormat sdf = new SimpleDateFormat(
"yyMMdd_HHmmss_SSS");
+ private final static MessageFormat mf = new MessageFormat("{0,number,000}");
private File baseDir;
private File requestsDir;
private FileFilter notLockFileFilter = new NotFileFilter(
new SuffixFileFilter("." + lockFileExt));
+ // Counters to avoid naming files with same prefix
+ private long lastSentTime = 0;
+ private int counter = 0;
+
+ private DetachedXmlConverter xmlConverter = null;
+
+ private long receiveAnswerTimeout = 10000l;
+
+ private boolean active = true;
+
public synchronized DetachedRequest receiveRequest() throws Exception {
DetachedRequest request = (DetachedRequest) receiveFile(requestsDir,
processedRequestsDir, 0);
if (request != null)
if (log.isTraceEnabled())
log.trace("Received detached request #" + request.getUuid()
- + " for ref '" + request.getRef() + "', path="
- + request.getPath());
+ + " for ref '" + request.getRef());
return request;
}
- public void sendAnswer(DetachedAnswer answer) throws Exception {
+ public synchronized void sendAnswer(DetachedAnswer answer) throws Exception {
sendFile(answersDir, answer);
if (log.isTraceEnabled())
log.trace("Sent detached answer #" + answer.getUuid());
}
- public DetachedAnswer receiveAnswer() throws Exception {
+ public synchronized DetachedAnswer receiveAnswer() throws Exception {
DetachedAnswer answer = (DetachedAnswer) receiveFile(answersDir,
processedAnswersDir, getReceiveAnswerTimeout());
if (answer != null)
return answer;
}
- public void sendRequest(DetachedRequest request) throws Exception {
+ public synchronized void sendRequest(DetachedRequest request)
+ throws Exception {
sendFile(requestsDir, request);
if (log.isTraceEnabled())
log.trace("Sent detached request #" + request.getUuid()
- + " for ref '" + request.getRef() + "', path="
- + request.getPath());
+ + " for ref '" + request.getRef());
}
- protected void sendFile(File dir, DetachedCommunication detCom)
+ protected synchronized void sendFile(File dir, DetachedCommunication detCom)
throws Exception {
- final File file;
+ final String ext;
if (getXmlConverter() != null)
- file = new File(dir.getPath() + File.separator
- + sdf.format(new Date()) + '-' + detCom.getUuid() + ".xml");
+ ext = ".xml";
else
- file = new File(dir.getPath() + File.separator + detCom.getUuid());
+ ext = "";
+
+ // Check counters
+ Date nowDate = new Date();
+ long nowMs = nowDate.getTime();
+ if (nowMs == lastSentTime) {
+ counter++;
+ } else {
+ counter = 0;
+ }
+
+ // Create file path
+ StringBuffer filePath = new StringBuffer(dir.getPath());
+ filePath.append(File.separatorChar).append(sdf.format(nowDate))
+ .append('-');
+ filePath.append(mf.format(new Object[] { new Long(counter) })).append(
+ '-');
+ filePath.append(detCom.getUuid()).append(ext);
+ File file = new File(filePath.toString());
File lockFile = createLockFile(file);
- if (getXmlConverter() != null) {
+ if (getXmlConverter() != null) {// xml
FileOutputStream outFile = new FileOutputStream(file);
try {
StreamResult result = new StreamResult(outFile);
} finally {
IOUtils.closeQuietly(outFile);
}
- } else {
+ } else {// serialize
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(file));
try {
}
}
- if (!isActive())
+ if (!isActive()) {
+ log.debug("DetachedDriver is not active. Leaving receiveFile");
return null;
+ }
File lockFile = nameLockFile(file);
while (lockFile.exists())
return detCom;
}
+ public synchronized void stop() {
+ log.debug("Stopping Detached Driver");
+ active = false;
+ notifyAll();
+ }
+
+ private synchronized boolean isActive() {
+ return active;
+ }
+
protected File createLockFile(File file) {
File lockFile = nameLockFile(file);
try {
createIfNotExist(processedAnswersDir);
createIfNotExist(cleanedRequestsDir);
createIfNotExist(cleanedAnswersDir);
- log.info("Detached File Driver initialized on " + baseDir);
+ if (log.isDebugEnabled())
+ log.debug("Detached File Driver initialized on " + baseDir);
}
public void cleanPreviousRuns() throws Exception {
}
+ public long getReceiveAnswerTimeout() {
+ return receiveAnswerTimeout;
+ }
+
+ public void setReceiveAnswerTimeout(long receiveAnswerTimeout) {
+ this.receiveAnswerTimeout = receiveAnswerTimeout;
+ }
+
+ public DetachedXmlConverter getXmlConverter() {
+ return xmlConverter;
+ }
+
+ public void setXmlConverter(DetachedXmlConverter xmlConverter) {
+ this.xmlConverter = xmlConverter;
+ }
}