Source Code Cross Referenced for FileBindingUtil.java in  » Workflow-Engines » Dalma » com » sun » jbi » binding » file » util » 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 » Dalma » com.sun.jbi.binding.file.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003:        /*
004:         * FileBindingUtil.java
005:         *
006:         * SUN PROPRIETARY/CONFIDENTIAL.
007:         * This software is the proprietary information of Sun Microsystems, Inc.
008:         * Use is subject to license terms.
009:         *
010:         */
011:        package com.sun.jbi.binding.file.util;
012:
013:        import java.io.File;
014:
015:        import java.util.Hashtable;
016:
017:        /**
018:         * File binding util class. Util class for doing useful task in file binding.
019:         *
020:         * @author Sun Microsystems, Inc.
021:         */
022:        public class FileBindingUtil {
023:            /**
024:             * Tracking id.
025:             */
026:            private static short sTrackingId = 0;
027:
028:            /**
029:             * buffer length
030:             */
031:            private static final short BUF = 5;
032:
033:            /**
034:             * Entries of files that have already been read.
035:             */
036:            private static Hashtable sFileEntries;
037:
038:            /**
039:             * Util method to get the base name of a filename.
040:             *
041:             * @param file File object.
042:             *
043:             * @return base name of the file.
044:             */
045:            public static String getBaseName(File file) {
046:                String fname = file.getName();
047:                int k = fname.lastIndexOf('.');
048:
049:                if (k == -1) {
050:                    return fname;
051:                }
052:
053:                if (k == 0) {
054:                    return "";
055:                }
056:
057:                return fname.substring(0, k);
058:            }
059:
060:            /**
061:             * Util method to get an extension from a file name.
062:             *
063:             * @param file File object
064:             *
065:             * @return the extension of filename.
066:             */
067:            public static String getExtension(File file) {
068:                String fname = file.getName();
069:                int k = fname.lastIndexOf('.');
070:
071:                if (k == -1) {
072:                    return "";
073:                }
074:
075:                return fname.substring(k + 1);
076:            }
077:
078:            /**
079:             * Returns the file path from tracking id.
080:             *
081:             * @param trackid Track id.
082:             *
083:             * @return File path corresponding to track id.
084:             */
085:            public static String getFilePath(String trackid) {
086:                String path = null;
087:
088:                try {
089:                    path = (String) sFileEntries.get(trackid);
090:                } catch (Exception e) {
091:                    ;
092:                }
093:
094:                return path;
095:            }
096:
097:            /**
098:             * Method to generate tracking id.
099:             *
100:             * @return tracking id.
101:             */
102:            public static synchronized String getTrackingId() {
103:                sTrackingId++;
104:
105:                if (sTrackingId >= Short.MAX_VALUE) {
106:                    sTrackingId = 0;
107:                }
108:
109:                return padString(Short.toString(sTrackingId));
110:            }
111:
112:            /**
113:             * Adds an entry to the file entries table.
114:             *
115:             * @param trackid track id.
116:             * @param filepath File path.
117:             */
118:            public static void addEntry(String trackid, String filepath) {
119:                if (sFileEntries == null) {
120:                    sFileEntries = new Hashtable();
121:                }
122:
123:                try {
124:                    sFileEntries.put(trackid, filepath);
125:                } catch (Exception e) {
126:                    ;
127:                }
128:            }
129:
130:            /**
131:             * Checks if the file has already been picked.
132:             *
133:             * @param file File
134:             *
135:             * @return true if not picked.
136:             */
137:            public static boolean canAdd(File file) {
138:                boolean canadd = true;
139:                String filename = file.getAbsolutePath();
140:
141:                try {
142:                    if (sFileEntries.containsValue(filename)) {
143:                        canadd = false;
144:                    }
145:                } catch (Exception e) {
146:                    ;
147:                }
148:
149:                return canadd;
150:            }
151:
152:            /**
153:             * Moves the file to processed folder.
154:             *
155:             * @param trk tracking id to be attached to file name.
156:             * @param destfolder destination folder
157:             * @param file file name
158:             *
159:             * @return true if success.
160:             */
161:            public static boolean moveFile(String trk, String destfolder,
162:                    String file) {
163:                if ((destfolder == null) || (file == null)) {
164:                    return false;
165:                } else if ((destfolder.trim().equals(""))
166:                        || (file.trim().equals(""))) {
167:                    return false;
168:                } else {
169:                    File destFile = new File(destfolder);
170:                    File f = new File(file);
171:
172:                    return moveFile(trk, destFile, f);
173:                }
174:            }
175:
176:            /**
177:             * Moves the file to processed folder.
178:             *
179:             * @param trk tracking id to be attached to file name.
180:             * @param destfolder destination folder
181:             * @param file file name
182:             *
183:             * @return true if success.
184:             */
185:            public static boolean moveFile(String trk, File destfolder,
186:                    File file) {
187:                boolean success = true;
188:
189:                if ((destfolder == null) || (file == null)) {
190:                    return false;
191:                }
192:
193:                if ((!destfolder.exists()) || (!destfolder.canWrite())) {
194:                    file.delete();
195:                    success = false;
196:                } else {
197:                    success = file.renameTo(new File(destfolder
198:                            .getAbsolutePath()
199:                            + File.separatorChar
200:                            + getBaseName(file)
201:                            + ConfigData.SEPARATOR
202:                            + trk
203:                            + "."
204:                            + getExtension(file)));
205:
206:                    if (!success) {
207:                        file.delete();
208:                    }
209:                }
210:
211:                return success;
212:            }
213:
214:            /**
215:             * Removes entry from the files table.
216:             *
217:             * @param trackid Trackid
218:             */
219:            public static void removeEntry(String trackid) {
220:                String path = null;
221:
222:                try {
223:                    path = (String) sFileEntries.remove(trackid);
224:                } catch (Exception e) {
225:                    ;
226:                }
227:            }
228:
229:            /**
230:             * Util method for padding with zeros.
231:             *
232:             * @param s string to be padded.
233:             *
234:             * @return padded string.
235:             */
236:            private static String padString(String s) {
237:                StringBuffer sb = new StringBuffer(s);
238:                int len = sb.length();
239:
240:                for (int i = 0; i < (BUF - len); i++) {
241:                    sb.insert(0, '0');
242:                }
243:
244:                return sb.toString();
245:            }
246:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.