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.net.MalformedURLException;
24  import java.net.URL;
25  import java.io.*;
26  import java.util.*;
27  
28  import org.apache.log4j.Logger;
29  
30  /***
31   */
32  public class FileUtil extends RuntimeException {
33  
34  	protected static final Logger log = Logger.getLogger(FileUtil.class);
35  
36      public static URL getFileLocation(String resource) {
37          URL url = null;
38          final ClassLoader loader = ClassLoader.getSystemClassLoader();
39          if (loader != null) {
40              url = (URL) loader.getResource(resource);
41              if (url != null) {
42                  log.debug("rc[" + resource + "] -> " + url.getFile());
43              }
44          }
45          return url;
46      }
47      
48  	public static URL getFile(String fileName) {
49  		File file = new File(fileName);
50  		boolean isResource = false;
51  		if (!file.canRead()) {
52  		    URL location = getFileLocation(fileName);
53  			if (location != null) {
54  				return location;
55  			} else {
56  			    log.debug("[" + fileName + "] -> not found");
57  			    return null;
58  			}
59  		} else {
60  		    log.debug(fileName + "->" + file.getAbsolutePath());
61  			try {
62                  return new URL("file:" + file.getAbsolutePath());
63              } catch (MalformedURLException e) {
64                  log.error("Error", e);
65                  return null;
66              }
67  		} 
68  	}
69  	
70  	public boolean deleteDir(File dir, boolean delSelf) {
71  		if (dir.isDirectory()) {
72  			String[] children = dir.list();
73  			for (int i = 0; i < children.length; i++) {
74  				boolean success = deleteDir(new File(dir, children[i]), true);
75  				if (!success)
76  					return false;
77  			}
78  		}
79  		if (delSelf) {
80  			return dir.delete();
81  		} else {
82  			return true;
83  		}
84  	}
85  	
86  	public static HashSet readTextFile(File file) {
87  		HashSet list = new HashSet();
88  		if (!readTextFile(file, list)) {
89  		    return null;
90  		}
91  		return list;
92  	}
93  	
94  	public static boolean readTextFile(File file, HashSet list) {
95  		String filename = file.getName();
96  		try {
97  		    final String directiveInclude = "#include";
98  				    
99  			BufferedReader in = new BufferedReader(new FileReader(file));
100 			while (true) {
101 				String l = in.readLine();
102 				if (l == null)
103 					break;
104 				l = l.trim();
105 				if ((l.length() == 0) || (l.startsWith("#"))) {
106 					
107 				    if (l.startsWith(directiveInclude)) {
108 					    String fileName = l.substring(directiveInclude.length()).trim();
109 					    if (!readTextFile(new File(fileName), list)) {
110 					        return false;
111 					    }
112 					}
113 					    
114 					continue;
115 				}
116 				if (!list.contains(l)) {
117 				    list.add(l);
118 				}
119 			}
120 			in.close();
121 
122 		} catch (Exception e) {
123 			log.error("Read error " + filename, e);
124 			return false;
125 		}
126 		log.debug("Read list of " + list.size() + " classes in " + filename);
127 		return true;
128 	}
129 	
130 	static public File[] sortFileListByDate(File[] children) {
131 		Arrays.sort(children, new FileListByDateComparator());
132 		return children;
133 	}
134 	
135 	public static class FileListByDateComparator implements Comparator {
136 		
137 	    public FileListByDateComparator() {
138 		}
139 
140 		public int compare(Object o1, Object o2) {
141 			File f1 = (File) o1;
142 			File f2 = (File) o2;
143 			// keep folders at the top of the list
144 			if (f1.isDirectory() && !f2.isDirectory()) {
145 				return -1;
146 			} else if (!f1.isDirectory() && f2.isDirectory()) {
147 				return 1;
148 			}
149 
150 			if (f1.lastModified() < f2.lastModified()) {
151 				return -1;
152 			} else if (f1.lastModified() > f2.lastModified()) {
153 				return 1;
154 			} else {
155 				return 0;
156 			}
157 		}
158 	}
159 	
160 }