001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)FileBindingUtil.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.file.util;
030:
031: import java.io.File;
032:
033: import java.util.Hashtable;
034:
035: /**
036: * File binding util class. Util class for doing useful task in file binding.
037: *
038: * @author Sun Microsystems, Inc.
039: */
040: public class FileBindingUtil {
041: /**
042: * Tracking id.
043: */
044: private static short sTrackingId = 0;
045:
046: /**
047: * buffer length
048: */
049: private static final short BUF = 5;
050:
051: /**
052: * Entries of files that have already been read.
053: */
054: private static Hashtable sFileEntries;
055:
056: /**
057: * Util method to get the base name of a filename.
058: *
059: * @param file File object.
060: *
061: * @return base name of the file.
062: */
063: public static String getBaseName(File file) {
064: String fname = file.getName();
065: int k = fname.lastIndexOf('.');
066:
067: if (k == -1) {
068: return fname;
069: }
070:
071: if (k == 0) {
072: return "";
073: }
074:
075: return fname.substring(0, k);
076: }
077:
078: /**
079: * Util method to get an extension from a file name.
080: *
081: * @param file File object
082: *
083: * @return the extension of filename.
084: */
085: public static String getExtension(File file) {
086: String fname = file.getName();
087: int k = fname.lastIndexOf('.');
088:
089: if (k == -1) {
090: return "";
091: }
092:
093: return fname.substring(k + 1);
094: }
095:
096: /**
097: * Returns the file path from tracking id.
098: *
099: * @param trackid Track id.
100: *
101: * @return File path corresponding to track id.
102: */
103: public static String getFilePath(String trackid) {
104: String path = null;
105:
106: try {
107: path = (String) sFileEntries.get(trackid);
108: } catch (Exception e) {
109: ;
110: }
111:
112: return path;
113: }
114:
115: /**
116: * Method to generate tracking id.
117: *
118: * @return tracking id.
119: */
120: public static synchronized String getTrackingId() {
121: sTrackingId++;
122:
123: if (sTrackingId >= Short.MAX_VALUE) {
124: sTrackingId = 0;
125: }
126:
127: return padString(Short.toString(sTrackingId));
128: }
129:
130: /**
131: * Adds an entry to the file entries table.
132: *
133: * @param trackid track id.
134: * @param filepath File path.
135: */
136: public static void addEntry(String trackid, String filepath) {
137: if (sFileEntries == null) {
138: sFileEntries = new Hashtable();
139: }
140:
141: try {
142: sFileEntries.put(trackid, filepath);
143: } catch (Exception e) {
144: ;
145: }
146: }
147:
148: /**
149: * Checks if the file has already been picked.
150: *
151: * @param file File
152: *
153: * @return true if not picked.
154: */
155: public static boolean canAdd(File file) {
156: boolean canadd = true;
157: String filename = file.getAbsolutePath();
158:
159: try {
160: if (sFileEntries.containsValue(filename)) {
161: canadd = false;
162: }
163: } catch (Exception e) {
164: ;
165: }
166:
167: return canadd;
168: }
169:
170: /**
171: * Moves the file to processed folder.
172: *
173: * @param trk tracking id to be attached to file name.
174: * @param destfolder destination folder
175: * @param file file name
176: *
177: * @return moved file name.
178: */
179: public static String moveFile(String trk, String destfolder,
180: String file) {
181: if ((destfolder == null) || (file == null)) {
182: return null;
183: } else if ((destfolder.trim().equals(""))
184: || (file.trim().equals(""))) {
185: return null;
186: } else {
187: File destFile = new File(destfolder);
188: File f = new File(file);
189:
190: return moveFile(trk, destFile, f);
191: }
192: }
193:
194: /**
195: * Moves the file to processed folder.
196: *
197: * @param trk tracking id to be attached to file name.
198: * @param destfolder destination folder
199: * @param file file name
200: *
201: * @return moved file name.
202: */
203: public static String moveFile(String trk, File destfolder, File file) {
204: String movedfilename = null;
205:
206: if ((destfolder == null) || (file == null)) {
207: return null;
208: }
209:
210: if ((!destfolder.exists()) || (!destfolder.canWrite())) {
211: file.delete();
212: } else {
213: movedfilename = destfolder.getAbsolutePath()
214: + File.separatorChar + getBaseName(file)
215: + ConfigData.SEPARATOR + trk + "."
216: + getExtension(file);
217: boolean success = file.renameTo(new File(movedfilename));
218:
219: if (!success) {
220: file.delete();
221: movedfilename = null;
222: }
223: }
224:
225: return movedfilename;
226: }
227:
228: /**
229: * Removes entry from the files table.
230: *
231: * @param trackid Trackid
232: */
233: public static void removeEntry(String trackid) {
234: String path = null;
235:
236: try {
237: path = (String) sFileEntries.remove(trackid);
238: } catch (Exception e) {
239: ;
240: }
241: }
242:
243: /**
244: * Util method for padding with zeros.
245: *
246: * @param s string to be padded.
247: *
248: * @return padded string.
249: */
250: private static String padString(String s) {
251: StringBuffer sb = new StringBuffer(s);
252: int len = sb.length();
253:
254: for (int i = 0; i < (BUF - len); i++) {
255: sb.insert(0, '0');
256: }
257:
258: return sb.toString();
259: }
260: }
|