View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.util;
22  
23  import java.io.BufferedReader;
24  import java.io.File;
25  import java.io.FileReader;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  import java.net.MalformedURLException;
30  import java.net.URL;
31  import java.util.Arrays;
32  import java.util.Comparator;
33  import java.util.HashSet;
34  
35  import net.sf.jour.log.Logger;
36  
37  public class FileUtil {
38  
39  	protected static final Logger log = Logger.getLogger();
40  
41  	public static URL getFileLocation(String resource) {
42  		return getFileLocation(resource, null);
43  	}
44  
45  	public static URL getFileLocation(String resource, ClassLoader loader) {
46  		URL url = null;
47  		if (loader != null) {
48  			url = loader.getResource(resource);
49  		}
50  		if (url == null) {
51  			ClassLoader loader2 = Thread.currentThread().getContextClassLoader();
52  			if (loader2 != null) {
53  				url = loader2.getResource(resource);
54  			}
55  		}
56  		if (url == null) {
57  			ClassLoader loader3 = FileUtil.class.getClassLoader();
58  			url = loader3.getResource(resource);
59  		}
60  		if (url == null) {
61  			url = FileUtil.class.getResource(resource);
62  		}
63  		if (url != null) {
64  			log.debug("rc[" + resource + "] -> " + url.getFile());
65  		}
66  		return url;
67  	}
68  
69  	public static URL getFile(String fileName) {
70  		return getFile(fileName, FileUtil.class.getClassLoader());
71  	}
72  
73  	public static URL getFile(String fileName, Object owner) {
74  		URL url = owner.getClass().getResource(fileName);
75  		if (url != null) {
76  			return url;
77  		}
78  		return getFile(fileName, owner.getClass().getClassLoader());
79  	}
80  
81  	public static URL getFile(String fileName, ClassLoader loader) {
82  		File file = new File(fileName);
83  		if (!file.canRead()) {
84  			URL location = getFileLocation(fileName, loader);
85  			if (location != null) {
86  				return location;
87  			} else {
88  				log.debug("[" + fileName + "] -> not found");
89  				return null;
90  			}
91  		} else {
92  			log.debug(fileName + "->" + file.getAbsolutePath());
93  			try {
94  				return new URL("file:" + file.getAbsolutePath());
95  			} catch (MalformedURLException e) {
96  				log.error("Error", e);
97  				return null;
98  			}
99  		}
100 	}
101 
102 	public static boolean deleteDir(File dir, boolean delSelf) {
103 		if (dir.isDirectory()) {
104 			String[] children = dir.list();
105 			for (int i = 0; i < children.length; i++) {
106 				boolean success = deleteDir(new File(dir, children[i]), true);
107 				if (!success)
108 					return false;
109 			}
110 		}
111 		if (delSelf) {
112 			return dir.delete();
113 		} else {
114 			return true;
115 		}
116 	}
117 
118 	public static void closeQuietly(InputStream input) {
119         try {
120             if (input != null) {
121                 input.close();
122             }
123         } catch (IOException ioe) {
124             // ignore
125         }
126     }
127 
128 	public static void closeQuietly(OutputStream out) {
129         try {
130             if (out != null) {
131                 out.close();
132             }
133         } catch (IOException ioe) {
134             // ignore
135         }
136     }
137 	
138 	public static HashSet readTextFile(File file) {
139 		HashSet list = new HashSet();
140 		if (!readTextFile(file, list)) {
141 			return null;
142 		}
143 		return list;
144 	}
145 
146 	public static boolean readTextFile(File file, HashSet list) {
147 		String filename = file.getName();
148 		try {
149 			final String directiveInclude = "#include";
150 
151 			BufferedReader in = new BufferedReader(new FileReader(file));
152 			while (true) {
153 				String l = in.readLine();
154 				if (l == null)
155 					break;
156 				l = l.trim();
157 				if ((l.length() == 0) || (l.startsWith("#"))) {
158 
159 					if (l.startsWith(directiveInclude)) {
160 						String fileName = l.substring(directiveInclude.length()).trim();
161 						if (!readTextFile(new File(fileName), list)) {
162 							return false;
163 						}
164 					}
165 
166 					continue;
167 				}
168 				if (!list.contains(l)) {
169 					list.add(l);
170 				}
171 			}
172 			in.close();
173 
174 		} catch (Exception e) {
175 			log.error("Read error " + filename, e);
176 			return false;
177 		}
178 		log.debug("Read list of " + list.size() + " classes in " + filename);
179 		return true;
180 	}
181 
182 	static public File[] sortFileListByDate(File[] children) {
183 		Arrays.sort(children, new FileListByDateComparator());
184 		return children;
185 	}
186 
187 	static public File[] sortFileListByName(File[] children) {
188 		Arrays.sort(children, new FileListByNameComparator());
189 		return children;
190 	}
191 
192 	public static class FileListByDateComparator implements Comparator {
193 
194 		public FileListByDateComparator() {
195 		}
196 
197 		public int compare(Object o1, Object o2) {
198 			File f1 = (File) o1;
199 			File f2 = (File) o2;
200 			// keep folders at the top of the list
201 			if (f1.isDirectory() && !f2.isDirectory()) {
202 				return -1;
203 			} else if (!f1.isDirectory() && f2.isDirectory()) {
204 				return 1;
205 			}
206 
207 			if (f1.lastModified() < f2.lastModified()) {
208 				return -1;
209 			} else if (f1.lastModified() > f2.lastModified()) {
210 				return 1;
211 			} else {
212 				return 0;
213 			}
214 		}
215 	}
216 
217 	public static class FileListByNameComparator implements Comparator {
218 
219 		public FileListByNameComparator() {
220 		}
221 
222 		public int compare(Object o1, Object o2) {
223 			File f1 = (File) o1;
224 			File f2 = (File) o2;
225 			// keep folders at the top of the list
226 			if (f1.isDirectory() && !f2.isDirectory()) {
227 				return -1;
228 			} else if (!f1.isDirectory() && f2.isDirectory()) {
229 				return 1;
230 			}
231 
232 			return f1.getName().compareTo(f2.getName());
233 		}
234 	}
235 
236 }