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