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: * @(#)FileListing.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.shared.util;
030:
031: import java.io.File;
032: import java.io.FilenameFilter;
033:
034: import java.util.ArrayList;
035: import java.util.Arrays;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: import java.util.logging.Logger;
040:
041: /**
042: * Helper class for listing files / folders.
043: *
044: * @author Sun Microsystems, Inc.
045: */
046: public final class FileListing {
047: /**
048: * Flag to indicate recursive listing.
049: */
050: private static boolean sRecurse = false;
051:
052: /**
053: * Filter class to be used.
054: */
055: private static FilenameFilter sFilter = null;
056:
057: /**
058: * Logger object.
059: */
060: private static Logger sLog = Logger
061: .getLogger("com.sun.jbi.binding.jms.util");
062:
063: /**
064: * Batch size of files to read.
065: */
066: private static final int BATCH_SIZE = 10;
067:
068: /**
069: * Gets the list of files in a folder.
070: *
071: * @param aDir folder name.
072: * @param bRecurse will recurse if true.
073: * @param aFilter filname filter.
074: *
075: * @return List of file names.
076: */
077: public static synchronized List getFileListing(File aDir,
078: boolean bRecurse, FilenameFilter aFilter) {
079: sRecurse = bRecurse;
080: sFilter = aFilter;
081:
082: if ((aDir == null) || (aDir.getName().trim().equals(""))) {
083: sLog.severe("Directory is null");
084:
085: return null;
086: }
087:
088: return getListing(aDir);
089: }
090:
091: /**
092: * Recursively walk a directory tree and return a List of all Files found; the List
093: * is sorted using File.compareTo.
094: *
095: * @param aDir is a valid directory, which can be read.
096: *
097: * @return array of folder names
098: */
099: public static synchronized String[] getFolderListing(File aDir) {
100: File[] dirs = aDir.listFiles();
101:
102: if (dirs == null) {
103: return null;
104: }
105:
106: List filesDirs = Arrays.asList(dirs);
107:
108: if (filesDirs == null) {
109: return null;
110: }
111:
112: Iterator filesIter = filesDirs.iterator();
113: String[] result = new String[filesDirs.size()];
114: File file = null;
115: int counter = 0;
116:
117: while (filesIter.hasNext()) {
118: file = (File) filesIter.next();
119:
120: if (!file.isFile()) {
121: result[counter] = file.getAbsolutePath();
122: counter++;
123: }
124: }
125:
126: return result;
127: }
128:
129: /**
130: * Util method to get the list of folders.
131: *
132: * @param aDir folder name for which contents have to be listed.
133: *
134: * @return array of folder names.
135: */
136: public static synchronized String[] getFolderNameListing(File aDir) {
137: File[] dirs = aDir.listFiles();
138:
139: if (dirs == null) {
140: return null;
141: }
142:
143: List filesDirs = Arrays.asList(dirs);
144:
145: if (filesDirs == null) {
146: return null;
147: }
148:
149: Iterator filesIter = filesDirs.iterator();
150: String[] result = new String[filesDirs.size()];
151: File file = null;
152: int counter = 0;
153:
154: while (filesIter.hasNext()) {
155: file = (File) filesIter.next();
156:
157: if (!file.isFile()) {
158: result[counter] = file.getName();
159: counter++;
160: }
161: }
162:
163: return result;
164: }
165:
166: /**
167: * Recursively walk a directory tree and return a List of all Files found; the List
168: * is sorted using File.compareTo.
169: *
170: * @param aDir is a valid directory, which can be read.
171: *
172: * @return list of files.
173: */
174: private static List getListing(File aDir) {
175: List result = new ArrayList();
176: File[] filesAndDirs = aDir.listFiles(sFilter);
177:
178: if (filesAndDirs == null) {
179: return null;
180: }
181:
182: List filesDirs = Arrays.asList(filesAndDirs);
183:
184: if (filesDirs == null) {
185: return null;
186: }
187:
188: Iterator filesIter = filesDirs.iterator();
189: File file = null;
190:
191: while (filesIter.hasNext()) {
192: file = (File) filesIter.next();
193:
194: if (!file.isFile()) {
195: //must be a directory
196: //recursive call!
197: if (sRecurse) {
198: List deeperList = getListing(file);
199: result.addAll(deeperList);
200: }
201: } else {
202: result.add(file); //add only files,
203:
204: if (result.size() > BATCH_SIZE) {
205: return result;
206: }
207: }
208: }
209:
210: return result;
211: }
212: }
|