001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * FileListing.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: import java.io.FilenameFilter;
015:
016: import java.util.ArrayList;
017: import java.util.Arrays;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.logging.Logger;
021:
022: /**
023: * Helper class for listing files / folders.
024: *
025: * @author Sun Microsystems, Inc.
026: */
027: public final class FileListing {
028: /**
029: * Flag to indicate recursive listing.
030: */
031: private static boolean sRecurse = false;
032:
033: /**
034: * Filter class to be used.
035: */
036: private static FilenameFilter sFilter = null;
037:
038: /**
039: * Logger object.
040: */
041: private static Logger sLog = Logger
042: .getLogger("com.sun.jbi.binding.file.util");
043:
044: /**
045: * Gets the list of files in a folder.
046: *
047: * @param aDir folder name.
048: * @param bRecurse will recurse if true.
049: * @param aFilter filname filter.
050: *
051: * @return List of file names.
052: */
053: public static synchronized List getFileListing(File aDir,
054: boolean bRecurse, FilenameFilter aFilter) {
055: sRecurse = bRecurse;
056: sFilter = aFilter;
057:
058: if ((aDir == null) || (aDir.getName().trim().equals(""))) {
059: sLog.severe("Directory is null");
060:
061: return null;
062: }
063:
064: return getListing(aDir);
065: }
066:
067: /**
068: * Recursively walk a directory tree and return a List of all Files found;
069: * the List is sorted using File.compareTo.
070: *
071: * @param aDir is a valid directory, which can be read.
072: *
073: * @return array of folder names
074: */
075: public static synchronized String[] getFolderListing(File aDir) {
076: File[] dirs = aDir.listFiles();
077:
078: if (dirs == null) {
079: return null;
080: }
081:
082: List filesDirs = Arrays.asList(dirs);
083:
084: if (filesDirs == null) {
085: return null;
086: }
087:
088: Iterator filesIter = filesDirs.iterator();
089: String[] result = new String[filesDirs.size()];
090: File file = null;
091: int counter = 0;
092:
093: while (filesIter.hasNext()) {
094: file = (File) filesIter.next();
095:
096: if (!file.isFile()) {
097: result[counter] = file.getAbsolutePath();
098: counter++;
099: }
100: }
101:
102: return result;
103: }
104:
105: /**
106: * Util method to get the list of folders.
107: *
108: * @param aDir folder name for which contents have to be listed.
109: *
110: * @return array of folder names.
111: */
112: public static synchronized String[] getFolderNameListing(File aDir) {
113: File[] dirs = aDir.listFiles();
114:
115: if (dirs == null) {
116: return null;
117: }
118:
119: List filesDirs = Arrays.asList(dirs);
120:
121: if (filesDirs == null) {
122: return null;
123: }
124:
125: Iterator filesIter = filesDirs.iterator();
126: String[] result = new String[filesDirs.size()];
127: File file = null;
128: int counter = 0;
129:
130: while (filesIter.hasNext()) {
131: file = (File) filesIter.next();
132:
133: if (!file.isFile()) {
134: result[counter] = file.getName();
135: counter++;
136: }
137: }
138:
139: return result;
140: }
141:
142: /**
143: * Returns a WSDL file from the deployment artifact.
144: *
145: * @param path Path of the ASA
146: *
147: * @return WSDL file path
148: */
149: public static String getWSDLFile(String path) {
150: InputFileFilter myFilter = new InputFileFilter();
151: myFilter.setFilterexpression(".*\\.wsdl");
152:
153: String wsdlfile = null;
154:
155: try {
156: File dir = new File(path);
157: File[] filesAndDirs = dir.listFiles(myFilter);
158:
159: if (filesAndDirs == null) {
160: wsdlfile = null;
161: } else {
162: if (filesAndDirs.length > 0) {
163: wsdlfile = filesAndDirs[0].getAbsolutePath();
164: }
165: }
166: } catch (Exception e) {
167: ;
168: }
169:
170: return wsdlfile;
171: }
172:
173: /**
174: * Returns an XML file path from the deployment artifact.
175: *
176: * @param path path of the deployment artifact.
177: *
178: * @return XML file path.
179: */
180: public static String getXMLFile(String path) {
181: InputFileFilter myFilter = new InputFileFilter();
182: myFilter.setFilterexpression(".*\\.xml");
183:
184: String xmlfile = null;
185:
186: try {
187: File dir = new File(path);
188: File[] filesAndDirs = dir.listFiles(myFilter);
189:
190: if (filesAndDirs == null) {
191: xmlfile = null;
192: } else {
193: if (filesAndDirs.length > 0) {
194: xmlfile = filesAndDirs[0].getAbsolutePath();
195: }
196: }
197: } catch (Exception e) {
198: ;
199: }
200:
201: return xmlfile;
202: }
203:
204: /**
205: * Recursively walk a directory tree and return a List of all Files found;
206: * the List is sorted using File.compareTo.
207: *
208: * @param aDir is a valid directory, which can be read.
209: *
210: * @return list of files.
211: */
212: private static List getListing(File aDir) {
213: List result = new ArrayList();
214: File[] filesAndDirs = aDir.listFiles(sFilter);
215:
216: if (filesAndDirs == null) {
217: return null;
218: }
219:
220: List filesDirs = Arrays.asList(filesAndDirs);
221:
222: if (filesDirs == null) {
223: return null;
224: }
225:
226: Iterator filesIter = filesDirs.iterator();
227: File file = null;
228:
229: while (filesIter.hasNext()) {
230: file = (File) filesIter.next();
231:
232: if (!file.isFile()) {
233: if (sRecurse) {
234: List deeperList = getListing(file);
235: result.addAll(deeperList);
236: }
237: } else {
238: if (FileBindingUtil.canAdd(file)) {
239: result.add(file);
240: }
241:
242: if (result.size() >= ConfigData.BATCH_SIZE) {
243: return result;
244: }
245: }
246: }
247:
248: return result;
249: }
250: }
|