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: }
|