Source Code Cross Referenced for DocumentStoreImpl.java in  » J2EE » Enhydra-Demos » org » enhydra » dm » business » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Enhydra Demos » org.enhydra.dm.business 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.