View Javadoc

1   package ch.busyboxes.agoo.dao.impl;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.security.MessageDigest;
8   import java.util.ArrayList;
9   import java.util.List;
10  
11  import org.apache.log4j.Logger;
12  
13  import ch.busyboxes.agoo.dao.FileSystemDAO;
14  
15  /**
16   * Implementation of the file system dao
17   * 
18   * @author julien@busyboxes.ch
19   */
20  public class FileSystemDAOImpl implements FileSystemDAO {
21  	
22  	/** Logger for this file */
23  	private Logger logger = Logger.getLogger(FileSystemDAOImpl.class);
24  
25  	/**
26  	 * @see FileSystemDAO#getFilesInPath(String)
27  	 */
28  	public List<String> getFilesInPath(String path) {
29  		
30  		List<String> result = new ArrayList<String>();
31  		
32  		if (logger.isDebugEnabled()) {
33  			logger.debug("Scanning: " + path + ", for files");
34  		}
35  		
36  		File folder = new File(path);
37  		
38  		if (folder.isDirectory()) {
39  			result = getNewFilesInPath(path, folder, result);
40  		} else {
41  			throw new RuntimeException("Path: " + path + ", is not a directory");
42  		}
43  		
44  		// Filtering out folders
45  		File tempFile;
46  		List<String> folders = new ArrayList<String>();
47  		for (String file : result) {
48  			tempFile = new File(file);
49  			
50  			if (tempFile.isDirectory()) {
51  				folders.add(file);
52  			}
53  		}
54  		
55  		// Removing them
56  		result.removeAll(folders);
57  		
58  		if (logger.isDebugEnabled()) {
59  			logger.debug("Found: " + result.size() + " files in folder: " + path);
60  		}
61  		
62  		return result;
63  	}
64  	
65  	/**
66  	 * Looks for new files or folders in the give File
67  	 * 
68  	 * @param initialPath the prefix present in all paths
69  	 * @param file the file/folder to look at
70  	 * @param result the list of alredy found results
71  	 * @return the list of new file and folder found
72  	 */
73  	private List<String> getNewFilesInPath(String initialPath, File file, List<String> result) {
74  		
75  		if (!result.contains(file.getAbsolutePath())) {
76  			
77  			if (file.isDirectory()) {
78  				if (logger.isDebugEnabled()) {
79  					logger.debug("Found folder: " + file.toString() + ", scanning");
80  				}
81  				File[] subFiles = file.listFiles();
82  				
83  				for(File subFile : subFiles) {
84  					result = getNewFilesInPath(initialPath, subFile, result);
85  				}
86  			} else {
87  				if (logger.isDebugEnabled()) {
88  					logger.debug("Found file: " + file.toString());
89  				}
90  				
91  				String absolutePath = file.getAbsolutePath();
92  				if (!result.contains(absolutePath)) {
93  					if (absolutePath.startsWith(initialPath)) {
94  						result.add(absolutePath.substring(initialPath.length(), absolutePath.length()));
95  					} else {
96  						result.add(absolutePath);
97  					}
98  				}
99  			}
100 		}	
101 		
102 		return result;
103 	}
104 
105 	/**
106 	 * @see FileSystemDAO#computeMd5Hash(String)
107 	 */
108 	@Override
109 	public String computeMd5Hash(String path) {
110 		
111 		if (logger.isDebugEnabled()) {
112 			logger.debug("Computing md5 for file: " + path);
113 		}
114 	
115 		File file = new File(path);
116 		String result = null;
117 
118 		InputStream fin = null;
119 		try {
120 			fin = new FileInputStream(file);
121 			
122 			MessageDigest md5er = MessageDigest.getInstance("MD5");
123 			    
124 			byte[] buffer = new byte[1024];
125 			int read;
126 			
127 			// Reading file
128 			do {
129 				read = fin.read(buffer);
130 				if (read > 0) {
131 					md5er.update(buffer, 0, read);
132 				}
133 			} while (read != -1);
134 			
135 			byte[] digest = md5er.digest();
136 			if (digest != null) {
137 				StringBuffer strDigest = new StringBuffer();
138 				for (int i = 0; i < digest.length; i++) {
139 					strDigest.append(Integer.toString((digest[i] & 0xff) 
140 							+ 0x100, 16).substring(1));
141 				}	
142 				
143 				result = strDigest.toString();
144 			}
145 		} catch (Exception e) {
146 			logger.error("Error while computing md5 on file: " + path, e);
147 		} finally {
148 			if (fin != null) {
149 				try {
150 					fin.close();
151 				} catch(IOException ioe) {
152 					logger.error("Error while closing stream on file: " + path, ioe);
153 				}
154 			}
155 		}
156 		
157 		if(logger.isDebugEnabled()) {
158 			logger.debug("md5 hash is: " + result);
159 		}
160 		
161 		return result;
162 	}
163 	
164 }