1 /*
2 * Copyright 2011 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.ole.sys;
17
18 import java.io.File;
19 import java.io.FilenameFilter;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.TreeSet;
25
26 import org.apache.commons.io.FileUtils;
27
28 /**
29 * This class provides a set of facilities that can be used to work with files
30 */
31 public class FileUtil {
32 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FileUtil.class);
33
34 private static Set<String> createdDirectory = new TreeSet<String>();
35
36 /**
37 * In directory looks for a pattern matching filenameFilter and returns the filename with the highest lastModified()
38 * @param directory
39 * @param filenameFilter to filter filenames in batchFileDirectoryName for
40 * @return File with highest lastModified()
41 */
42 public static File getNewestFile(File directory, FilenameFilter filenameFilter) {
43 File newestFile = null;
44
45 File[] directoryListing = directory.listFiles(filenameFilter);
46 if (directoryListing == null || directoryListing.length == 0) {
47 return null;
48 } else {
49 for (int i = 0; i < directoryListing.length; i++) {
50 File file = directoryListing[i];
51 if (newestFile == null) {
52 newestFile = file;
53 } else {
54 if (newestFile.lastModified() < file.lastModified()){
55 newestFile = file;
56 }
57 }
58 }
59 }
60
61 return newestFile;
62 }
63
64 /**
65 * Check and create (if not exists) each of the directory path from param
66 * This is for creating single directory
67 *
68 * @param directoryPath
69 */
70 public static void createDirectory(final String directoryPath) {
71 createDirectories(new ArrayList<String>(){{add(directoryPath);}});
72 }
73
74 /**
75 * Check and create (if not exists) each of the directory path from param
76 *
77 * @param directoryPathList
78 */
79 public static void createDirectories(List<String> directoryPathList) {
80 File directoryToCheck;
81 for (String path : directoryPathList){
82 if (path != null) {
83 directoryToCheck = new File(path);
84 if (!directoryToCheck.isDirectory() && !createdDirectory.contains(path)) {
85 try {
86 FileUtils.forceMkdir(directoryToCheck);
87 LOG.debug("[" + path + "] has been created successfully");
88
89 //store locally to avoid future redundant IO check
90 createdDirectory.add(path);
91 }
92 catch (IOException ex) {
93 LOG.warn("Unable to create directory [" + path + "]", ex);
94 }
95 }
96 }
97 }
98 }
99 }