001: package org.enhydra.dm.business;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.FileOutputStream;
008: import java.io.IOException;
009: import java.util.Calendar;
010: import java.util.Properties;
011:
012: import org.enhydra.dm.api.DocumentStore;
013: import org.enhydra.dm.api.exceptions.BaseException;
014: import org.enhydra.dm.api.loggers.Log;
015: import org.enhydra.dm.business.exceptions.ConfigurationException;
016: import org.enhydra.dm.business.exceptions.DocumentStoreException;
017:
018: /**
019: * Default implementation of DocumentStore interface. Document are stored in docstore
020: * like: doctore/yyyy/mm/dd/timestamp.extension
021: *
022: * @author Svjetlana Milidrag
023: */
024: public class DocumentStoreImpl implements DocumentStore {
025:
026: private String docStorePath = null;
027:
028: private Log logger = null;
029:
030: public DocumentStoreImpl() {
031:
032: }
033:
034: /*
035: * (non-Javadoc)
036: *
037: * @see org.enhydra.dm.api.DocumentStore#saveDocumentContent(java.lang.String, byte[])
038: */
039: public String saveDocumentContent(String documentName,
040: byte[] documentContent) throws BaseException {
041:
042: if (null != logger) {
043: logger
044: .log(
045: Log.DEBUG,
046: "Saving document content for document for the first time.)",
047: documentName);
048: }
049: FileOutputStream fos = null;
050: try {
051:
052: File documentFile = new File(
053: createDocumentStorePath(documentName));
054: if (!documentFile.exists()) {
055: documentFile.createNewFile();
056: }
057: fos = new FileOutputStream(documentFile);
058: fos.write(documentContent);
059: fos.flush();
060:
061: return documentFile.getCanonicalPath();
062: } catch (IOException e) {
063:
064: throw new DocumentStoreException("IOException :"
065: + e.getMessage());
066: } finally {
067: try {
068: if (null != fos) {
069: fos.close();
070: }
071: } catch (IOException ex) {
072: throw new DocumentStoreException("IOException :"
073: + ex.getMessage());
074: }
075: }
076: }
077:
078: /*
079: * (non-Javadoc)
080: *
081: * @see org.enhydra.dm.api.DocumentStore#saveDocumentContent(java.lang.String, byte[],
082: * java.lang.String)
083: */
084: public String saveDocumentContent(String documentName,
085: byte[] documentContent, String path) throws BaseException {
086: if (null != logger) {
087: logger.log(Log.DEBUG,
088: "Saving document content for document)",
089: documentName);
090: }
091: FileOutputStream fos = null;
092: try {
093:
094: File documentFile = new File(path);
095: if (!documentFile.exists()) {
096: documentFile.createNewFile();
097: }
098: fos = new FileOutputStream(documentFile);
099: fos.write(documentContent);
100: fos.flush();
101:
102: return documentFile.getCanonicalPath();
103: } catch (IOException e) {
104: throw new DocumentStoreException("IOException :"
105: + e.getMessage());
106: } finally {
107: try {
108: if (null != fos) {
109: fos.close();
110: }
111: } catch (IOException ex) {
112: throw new DocumentStoreException("IOException :"
113: + ex.getMessage());
114: }
115: }
116: }
117:
118: /*
119: * (non-Javadoc)
120: *
121: * @see org.enhydra.dm.api.DocumentStore#loadDocumentContent(java.lang.String)
122: */
123: public byte[] loadDocumentContent(String documentFilePath)
124: throws BaseException {
125: if (null != logger) {
126: logger.log(Log.DEBUG,
127: "Loading document content from document store)");
128: }
129: byte[] buf = new byte[1024];
130: ByteArrayOutputStream baos = null;
131: BufferedOutputStream bos = null;
132: FileInputStream fis = null;
133:
134: File documentFile = new File(documentFilePath);
135: if (!documentFile.exists()) {
136: throw new DocumentStoreException(
137: "Document content is physically deleted for document "
138: + documentFilePath + "!");
139: }
140:
141: try {
142: baos = new ByteArrayOutputStream();
143: bos = new BufferedOutputStream(baos);
144: fis = new FileInputStream(documentFile);
145:
146: int b = 0;
147: while ((b = fis.read(buf, 0, buf.length)) != -1) {
148: bos.write(buf, 0, b);
149: }
150: bos.flush();
151:
152: return baos.toByteArray();
153: } catch (Exception ex) {
154: throw new DocumentStoreException(
155: "Error while loading document content!");
156: } finally {
157: if (baos != null)
158: try {
159: baos.close();
160: } catch (IOException e) {
161: throw new DocumentStoreException("IOException :"
162: + e.getMessage());
163: }
164: if (bos != null)
165: try {
166: bos.close();
167: } catch (IOException e) {
168: throw new DocumentStoreException("IOException :"
169: + e.getMessage());
170: }
171: if (fis != null) {
172: try {
173: fis.close();
174: } catch (IOException e) {
175: throw new DocumentStoreException("IOException :"
176: + e.getMessage());
177: }
178: }
179: }
180:
181: }
182:
183: /**
184: * Method for create docstore path : doctore/yyyy/mm/dd/timestamp.extension
185: *
186: * @param documentName
187: * @return
188: */
189: protected String createDocumentStorePath(String documentName) {
190: Calendar calendar = Calendar.getInstance();
191:
192: String ret = docStorePath + File.separator
193: + String.valueOf(calendar.get(Calendar.YEAR))
194: + File.separator;
195:
196: int month = calendar.get(Calendar.MONTH) + 1;
197: ret += ((month >= 10) ? String.valueOf(month) : "0"
198: + String.valueOf(month));
199:
200: int day = calendar.get(Calendar.DAY_OF_MONTH);
201: ret += File.separator
202: + ((day >= 10) ? String.valueOf(day) : "0"
203: + String.valueOf(day));
204: File filePath = new File(ret);
205: filePath.mkdirs();
206: String extension = "";
207: if (documentName.indexOf(".") != -1) {
208: extension = documentName.substring(documentName
209: .lastIndexOf("."));
210: }
211: documentName = calendar.getTimeInMillis() + extension;
212: return ret + File.separator + documentName;
213: }
214:
215: /*
216: * (non-Javadoc)
217: *
218: * @see org.enhydra.dm.api.DocumentStore#getLogger()
219: */
220: public Log getLogger() {
221: return logger;
222: }
223:
224: /*
225: * (non-Javadoc)
226: *
227: * @see org.enhydra.dm.api.DocumentStore#setLogger(org.enhydra.dm.api.loggers.Log)
228: */
229: public void setLogger(Log logger) {
230: this .logger = logger;
231: }
232:
233: /*
234: * (non-Javadoc)
235: *
236: * @see org.enhydra.dm.api.DocumentStore#getDocStorePath()
237: */
238: public String getDocStorePath() {
239: return docStorePath;
240: }
241:
242: /*
243: * (non-Javadoc)
244: *
245: * @see org.enhydra.dm.api.DocumentStore#setDocStorePath(java.lang.String)
246: */
247: public void setDocStorePath(String docStorePath) {
248: this .docStorePath = docStorePath;
249: }
250:
251: /*
252: * (non-Javadoc)
253: *
254: * @see org.enhydra.dm.api.DocumentStore#configure(java.util.Properties)
255: */
256: public void configure(Properties properties) throws BaseException {
257:
258: String documentStoreDefaultPath = System
259: .getProperty("java.io.tmpdir");
260:
261: try {
262: this .docStorePath = properties.getProperty(
263: "DocumentStore.path", documentStoreDefaultPath);
264: if (null == logger) {
265: logger = Log.getInstance();
266: }
267: } catch (Exception e) {
268: throw new ConfigurationException(e.getMessage());
269: }
270: }
271:
272: }
|