Source Code Cross Referenced for FileDefinition.java in  » Workflow-Engines » jbpm-jpdl-3.2.2 » org » jbpm » file » def » 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 » Workflow Engines » jbpm jpdl 3.2.2 » org.jbpm.file.def 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JBoss, Home of Professional Open Source
003:         * Copyright 2005, JBoss Inc., and individual contributors as indicated
004:         * by the @authors tag. See the copyright.txt in the distribution for a
005:         * full listing of individual contributors.
006:         *
007:         * This is free software; you can redistribute it and/or modify it
008:         * under the terms of the GNU Lesser General Public License as
009:         * published by the Free Software Foundation; either version 2.1 of
010:         * the License, or (at your option) any later version.
011:         *
012:         * This software is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015:         * Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this software; if not, write to the Free
019:         * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021:         */
022:        package org.jbpm.file.def;
023:
024:        import java.io.ByteArrayInputStream;
025:        import java.io.ByteArrayOutputStream;
026:        import java.io.File;
027:        import java.io.FileInputStream;
028:        import java.io.FileNotFoundException;
029:        import java.io.FileOutputStream;
030:        import java.io.IOException;
031:        import java.io.InputStream;
032:        import java.util.ArrayList;
033:        import java.util.Arrays;
034:        import java.util.HashMap;
035:        import java.util.Iterator;
036:        import java.util.List;
037:        import java.util.Map;
038:
039:        import org.apache.commons.logging.Log;
040:        import org.apache.commons.logging.LogFactory;
041:        import org.jbpm.JbpmConfiguration;
042:        import org.jbpm.JbpmException;
043:        import org.jbpm.bytes.ByteArray;
044:        import org.jbpm.module.def.ModuleDefinition;
045:        import org.jbpm.module.exe.ModuleInstance;
046:        import org.jbpm.util.IoUtil;
047:
048:        public class FileDefinition extends ModuleDefinition {
049:
050:            private static final long serialVersionUID = 1L;
051:
052:            static String getRootDir() {
053:                String rootDir = null;
054:                if (JbpmConfiguration.Configs.hasObject("jbpm.files.dir")) {
055:                    rootDir = JbpmConfiguration.Configs
056:                            .getString("jbpm.files.dir");
057:                }
058:                return rootDir;
059:            }
060:
061:            String dir = null;
062:
063:            Map processFiles = null;
064:
065:            public FileDefinition() {
066:            }
067:
068:            public ModuleInstance createInstance() {
069:                return null;
070:            }
071:
072:            // storing files /////////////////////////////////////////////////////////////
073:
074:            /**
075:             * add a file to this definition.
076:             */
077:            public void addFile(String name, byte[] bytes) {
078:                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
079:                addFile(name, bais);
080:            }
081:
082:            /**
083:             * add a file to this definition.
084:             */
085:            public void addFile(String name, InputStream is) {
086:                try {
087:                    if (isStoredOnFileSystem()) {
088:                        storeFileOnFileSystem(name, is);
089:
090:                    } else { // its stored in the database
091:                        storeFileInDb(name, is);
092:                    }
093:                } catch (Exception e) {
094:                    throw new JbpmException("file '" + name
095:                            + "' could not be stored", e);
096:                }
097:            }
098:
099:            void storeFileOnFileSystem(String name, InputStream is)
100:                    throws FileNotFoundException, IOException {
101:                String fileName = getFilePath(name);
102:                log.trace("storing file '" + name + "' on file system to '"
103:                        + fileName + "'");
104:                FileOutputStream fos = new FileOutputStream(fileName);
105:                IoUtil.transfer(is, fos);
106:                fos.close();
107:            }
108:
109:            void storeFileInDb(String name, InputStream is) throws IOException {
110:                if (processFiles == null) {
111:                    processFiles = new HashMap();
112:                }
113:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
114:                log.trace("preparing file '" + name
115:                        + "' for storage in the database");
116:                IoUtil.transfer(is, baos);
117:                processFiles.put(name, new ByteArray(name, baos.toByteArray()));
118:            }
119:
120:            // retrieving files //////////////////////////////////////////////////////////
121:
122:            /**
123:             * retrieve a file of this definition as an inputstream.
124:             */
125:            public InputStream getInputStream(String name) {
126:                InputStream inputStream = null;
127:                try {
128:                    if (isStoredOnFileSystem()) {
129:                        inputStream = getInputStreamFromFileSystem(name);
130:                    } else { // its stored in the database
131:                        inputStream = getInputStreamFromDb(name);
132:                    }
133:                } catch (Exception e) {
134:                    throw new JbpmException(
135:                            "couldn't get inputstream for file '" + name + "'",
136:                            e);
137:                }
138:                return inputStream;
139:            }
140:
141:            public boolean hasFile(String name) {
142:                if (isStoredOnFileSystem()) {
143:                    return new File(getFilePath(name)).exists();
144:                } else {
145:                    return processFiles.containsKey(name);
146:                }
147:            }
148:
149:            public Map getInputStreamMap() {
150:                HashMap result = new HashMap();
151:                if (processFiles != null) {
152:                    Iterator iterator = processFiles.keySet().iterator();
153:                    while (iterator.hasNext()) {
154:                        String name = (String) iterator.next();
155:                        result.put(name, getInputStream(name));
156:                    }
157:                }
158:                return result;
159:            }
160:
161:            public Map getBytesMap() {
162:                HashMap result = new HashMap();
163:                if (processFiles != null) {
164:                    Iterator iterator = processFiles.keySet().iterator();
165:                    while (iterator.hasNext()) {
166:                        String name = (String) iterator.next();
167:                        result.put(name, getBytes(name));
168:                    }
169:                }
170:                return result;
171:            }
172:
173:            private InputStream getInputStreamFromFileSystem(String name)
174:                    throws FileNotFoundException {
175:                InputStream inputStream = null;
176:                String fileName = getFilePath(name);
177:                log.trace("loading file '" + name + "' from file system '"
178:                        + fileName + "'");
179:                inputStream = new FileInputStream(fileName);
180:                return inputStream;
181:            }
182:
183:            private InputStream getInputStreamFromDb(String name) {
184:                InputStream inputStream = null;
185:                log.trace("loading file '" + name + "' from database");
186:                ByteArray byteArray = getByteArray(name);
187:                if (byteArray != null) {
188:                    inputStream = new ByteArrayInputStream(byteArray.getBytes());
189:                }
190:                return inputStream;
191:            }
192:
193:            /**
194:             * retrieve a file of this definition as a byte array.
195:             */
196:            public byte[] getBytes(String name) {
197:                byte[] bytes = null;
198:                try {
199:                    if (isStoredOnFileSystem()) {
200:                        bytes = getBytesFromFileSystem(name);
201:                    } else { // its stored in the database
202:                        bytes = getBytesFromDb(name);
203:                    }
204:                } catch (Exception e) {
205:                    throw new JbpmException("couldn't get value for file '"
206:                            + name + "'", e);
207:                }
208:                return bytes;
209:            }
210:
211:            byte[] getBytesFromFileSystem(String name) throws IOException {
212:                byte[] bytes = null;
213:                InputStream in = getInputStreamFromFileSystem(name);
214:                ByteArrayOutputStream out = new ByteArrayOutputStream();
215:                IoUtil.transfer(in, out);
216:                bytes = out.toByteArray();
217:                return bytes;
218:            }
219:
220:            byte[] getBytesFromDb(String name) {
221:                byte[] bytes;
222:                ByteArray byteArray = getByteArray(name);
223:                bytes = byteArray.getBytes();
224:                return bytes;
225:            }
226:
227:            ByteArray getByteArray(String name) {
228:                return (ByteArray) (processFiles != null ? processFiles
229:                        .get(name) : null);
230:            }
231:
232:            boolean isStoredOnFileSystem() {
233:                String rootDir = getRootDir();
234:                boolean isStoredOnFileSystem = (rootDir != null);
235:                // if files should be stored on the file system and no directory has been
236:                // created yet...
237:                if ((isStoredOnFileSystem) && (dir == null)) {
238:                    // create a new directory
239:                    dir = findNewDirName();
240:                    new File(rootDir + "/" + dir).mkdirs();
241:                }
242:                return isStoredOnFileSystem;
243:            }
244:
245:            String findNewDirName() {
246:                String newDirName = "files-1";
247:
248:                File parentFile = new File(getRootDir());
249:                if (parentFile.exists()) {
250:                    // get the current contents of the directory
251:                    String[] children = parentFile.list();
252:                    List fileNames = new ArrayList();
253:                    if (children != null) {
254:                        fileNames = new ArrayList(Arrays.asList(children));
255:                    }
256:
257:                    // find an unused name for the directory to be created
258:                    int seqNr = 1;
259:                    while (fileNames.contains(newDirName)) {
260:                        seqNr++;
261:                        newDirName = "files-" + seqNr;
262:                    }
263:                }
264:
265:                return newDirName;
266:            }
267:
268:            String getFilePath(String name) {
269:                String filePath = getRootDir() + "/" + dir + "/" + name;
270:                new File(filePath).getParentFile().mkdirs();
271:                return filePath;
272:            }
273:
274:            private static final Log log = LogFactory
275:                    .getLog(FileDefinition.class);
276:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.