View Javadoc

1   package twcsckernel.projectbase.io;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.rmi.RemoteException;
6   import java.util.Collection;
7   import java.util.Collections;
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import twcsckernel.projectbase.common.FileFactory;
12  
13  public class FileFactoryImpl implements FileFactory {
14  
15  	private Collection<LocalFsChangeListener> fsListeners = null;
16  
17  	private Map<Integer, File> fileMap = Collections
18  			.synchronizedMap(new HashMap<Integer, File>());
19  
20  	private Map<Integer, FileSecurityManager> securityMap = new HashMap<Integer, FileSecurityManager>();
21  
22  	private static final SecurityException illegalAccess = new SecurityException();
23  
24  	private static final ItemNotFoundException fileNotFound = new ItemNotFoundException();
25  
26  	private Object idPointerMutex = new Object();
27  
28  	private int idPointer = 0;
29  
30  	private FileSecurityManager defaultSecManager = null;
31  
32  	public FileFactoryImpl(FileSecurityManager defaultSecManager,
33  			Collection<LocalFsChangeListener> listeners)
34  			throws NullPointerException {
35  		if (defaultSecManager == null || listeners == null)
36  			throw new NullPointerException();
37  		this.defaultSecManager = defaultSecManager;
38  		fsListeners = listeners;
39  	}
40  
41  	public RemoteFile newRemoteFile(String path) throws SecurityException,
42  			RemoteException, IOException {
43  		return newRemoteFile(path, defaultSecManager);
44  	}
45  
46  	public RemoteFile newRemoteFile(String path, FileSecurityManager secManager)
47  			throws SecurityException, IOException {
48  		File localFile = secManager.getUserRootPathManager().getFile(path);
49  		if (secManager.checkRootPath(localFile) == FileSecurityManager.ACC_ILLEGAL)
50  			throw illegalAccess;
51  		RemoteFile rFile = null;
52  
53  		synchronized (idPointerMutex) {
54  			fileMap.put(idPointer, localFile);
55  			securityMap.put(idPointer, secManager);
56  			rFile = new RemoteFile(idPointer, this);
57  			idPointer++;
58  		}
59  		return rFile;
60  	}
61  
62  	public boolean disposeFileHandle(int fileID) throws RemoteException {
63  		boolean result = fileMap.remove(fileID) != null;
64  		securityMap.remove(fileID);
65  		return result;
66  	}
67  
68  	public char getSeparatorChar() throws RemoteException {
69  		return File.separatorChar;
70  	}
71  
72  	public String getSeparator() throws RemoteException {
73  		return File.separator;
74  	}
75  
76  	public char getPathSeparatorChar() throws RemoteException {
77  		return File.pathSeparatorChar;
78  	}
79  
80  	public String getPathSeparator() throws RemoteException {
81  		return File.pathSeparator;
82  	}
83  
84  	public String getName(int fileID) throws RemoteException,
85  			SecurityException, ItemNotFoundException {
86  		File file = getFile(fileID);
87  		return file.getName();
88  	}
89  
90  	public String getParent(int fileID) throws RemoteException,
91  			SecurityException, ItemNotFoundException {
92  		File file = getFile(fileID);
93  		FileSecurityManager secManager = securityMap.get(fileID);
94  		File parentFile = file.getParentFile();
95  		if (secManager.checkRootPath(parentFile) == FileSecurityManager.ACC_ILLEGAL)
96  			throw illegalAccess;
97  		return secManager.getUserRootPathManager().getUserAbsolutePath(
98  				parentFile.getAbsolutePath());
99  	}
100 
101 	public String getPath(int fileID) throws RemoteException,
102 			SecurityException, ItemNotFoundException {
103 		File file = getFile(fileID);
104 		FileSecurityManager secManager = securityMap.get(fileID);
105 		String result = secManager.getUserRootPathManager()
106 				.getUserAbsolutePath(file.getAbsolutePath());
107 		return result;
108 	}
109 
110 	public boolean canRead(int fileID) throws RemoteException,
111 			SecurityException, ItemNotFoundException {
112 		File file = getFile(fileID);
113 		FileSecurityManager secManager = securityMap.get(fileID);
114 		if (secManager.checkReadAccess(file) == FileSecurityManager.ACC_ILLEGAL)
115 			return false;
116 		else
117 			return file.canRead();
118 	}
119 
120 	public boolean canWrite(int fileID) throws RemoteException,
121 			SecurityException, ItemNotFoundException {
122 		File file = getFile(fileID);
123 		FileSecurityManager secManager = securityMap.get(fileID);
124 		if (secManager.checkWriteAccess(file) == FileSecurityManager.ACC_ILLEGAL)
125 			return false;
126 		else
127 			return file.canWrite();
128 	}
129 
130 	public boolean exists(int fileID) throws RemoteException,
131 			SecurityException, ItemNotFoundException {
132 		File file = getFile(fileID);
133 		FileSecurityManager secManager = securityMap.get(fileID);
134 		if (secManager.checkReadAccess(file) == FileSecurityManager.ACC_ILLEGAL)
135 			throw illegalAccess;
136 		else
137 			return file.exists();
138 	}
139 
140 	public boolean isDirectory(int fileID) throws RemoteException,
141 			SecurityException, ItemNotFoundException {
142 		File file = getFile(fileID);
143 		FileSecurityManager secManager = securityMap.get(fileID);
144 		if (secManager.checkReadAccess(file) == FileSecurityManager.ACC_ILLEGAL)
145 			throw illegalAccess;
146 		else
147 			return file.isDirectory();
148 	}
149 
150 	public boolean isFile(int fileID) throws RemoteException,
151 			SecurityException, ItemNotFoundException {
152 		File file = getFile(fileID);
153 		FileSecurityManager secManager = securityMap.get(fileID);
154 		if (secManager.checkReadAccess(file) == FileSecurityManager.ACC_ILLEGAL)
155 			throw illegalAccess;
156 		else
157 			return file.isFile();
158 	}
159 
160 	public boolean isHidden(int fileID) throws RemoteException,
161 			SecurityException, ItemNotFoundException {
162 		File file = getFile(fileID);
163 		FileSecurityManager secManager = securityMap.get(fileID);
164 		if (secManager.checkReadAccess(file) == FileSecurityManager.ACC_ILLEGAL)
165 			throw illegalAccess;
166 		else
167 			return file.isHidden();
168 	}
169 
170 	public long lastModified(int fileID) throws RemoteException,
171 			SecurityException, ItemNotFoundException {
172 		File file = getFile(fileID);
173 		FileSecurityManager secManager = securityMap.get(fileID);
174 		if (secManager.checkReadAccess(file) == FileSecurityManager.ACC_ILLEGAL)
175 			throw illegalAccess;
176 		else
177 			return file.lastModified();
178 	}
179 
180 	public long length(int fileID) throws RemoteException, SecurityException,
181 			ItemNotFoundException {
182 		File file = getFile(fileID);
183 		FileSecurityManager secManager = securityMap.get(fileID);
184 		if (secManager.checkReadAccess(file) == FileSecurityManager.ACC_ILLEGAL)
185 			throw illegalAccess;
186 		else
187 			return file.length();
188 	}
189 
190 	public boolean createNewFile(int fileID) throws RemoteException,
191 			SecurityException, ItemNotFoundException, IOException {
192 		File file = getFile(fileID);
193 		FileSecurityManager secManager = securityMap.get(fileID);
194 		if (secManager.checkWriteAccess(file) == FileSecurityManager.ACC_ILLEGAL)
195 			throw illegalAccess;
196 		else {
197 			boolean result = false;
198 			try {
199 				result = file.createNewFile();
200 				if (result) {
201 					FsChangeDescriptor fd = new FsChangeDescriptor(
202 							FsChangeDescriptor.FILE_CREATE);
203 					fd.setChangeInfo(getFileDescriptor(file));
204 					fireFsListeners(fd);
205 				}
206 			} catch (IOException e) {
207 				throw e;
208 			}
209 
210 			return result;
211 		}
212 	}
213 
214 	public boolean delete(int fileID) throws RemoteException,
215 			SecurityException, ItemNotFoundException {
216 		File file = getFile(fileID);
217 		FileSecurityManager secManager = securityMap.get(fileID);
218 		if (secManager.checkWriteAccess(file) == FileSecurityManager.ACC_ILLEGAL)
219 			throw illegalAccess;
220 		else {
221 			boolean result = false;
222 			result = file.delete();
223 			if (result) {
224 				FsChangeDescriptor fd = new FsChangeDescriptor(
225 						FsChangeDescriptor.FILE_DELETE);
226 				fd.setSource(file.getAbsolutePath());
227 				fireFsListeners(fd);
228 			}
229 			return result;
230 		}
231 	}
232 
233 	public FileDescriptor[] list(int fileID) throws RemoteException,
234 			SecurityException, ItemNotFoundException {
235 		File file = getFile(fileID);
236 		FileSecurityManager secManager = securityMap.get(fileID);
237 		if (secManager.checkReadAccess(file) == FileSecurityManager.ACC_ILLEGAL)
238 			throw illegalAccess;
239 		else {
240 			File[] fileList = file.listFiles();
241 			FileDescriptor fileDsc[] = new FileDescriptor[fileList.length];
242 			for (int i = 0; i < fileList.length; i++) {
243 				fileDsc[i] = getFileDescriptor(fileList[i]);
244 				fileDsc[i].path = secManager.getUserRootPathManager()
245 						.getUserAbsolutePath(fileDsc[i].path);
246 			}
247 			return fileDsc;
248 		}
249 	}
250 
251 	public boolean mkdir(int fileID) throws RemoteException, SecurityException,
252 			ItemNotFoundException {
253 		File file = getFile(fileID);
254 		FileSecurityManager secManager = securityMap.get(fileID);
255 		if (secManager.checkWriteAccess(file) == FileSecurityManager.ACC_ILLEGAL)
256 			throw illegalAccess;
257 		else {
258 			boolean result = false;
259 			result = file.mkdir();
260 			if (result) {
261 				FsChangeDescriptor fd = new FsChangeDescriptor(
262 						FsChangeDescriptor.FILE_CREATE);
263 				fd.setChangeInfo(getFileDescriptor(file));
264 				fireFsListeners(fd);
265 			}
266 			return result;
267 		}
268 	}
269 
270 	public boolean mkdirs(int fileID) throws RemoteException,
271 			SecurityException, ItemNotFoundException {
272 		File file = getFile(fileID);
273 		FileSecurityManager secManager = securityMap.get(fileID);
274 		if (secManager.checkWriteAccess(file) == FileSecurityManager.ACC_ILLEGAL)
275 			throw illegalAccess;
276 		else {
277 			boolean result = false;
278 			result = file.mkdirs();
279 			if (result) {
280 				FsChangeDescriptor fd = new FsChangeDescriptor(
281 						FsChangeDescriptor.FILE_MKDIRS);
282 				fd.setChangeInfo(getFileDescriptor(file));
283 				fireFsListeners(fd);
284 			}
285 			return result;
286 		}
287 	}
288 
289 	public boolean renameTo(int fileID, String newName) throws RemoteException,
290 			SecurityException, ItemNotFoundException {
291 		File file = getFile(fileID);
292 		FileSecurityManager secManager = securityMap.get(fileID);
293 		File destFile = new File(file.getParent() + "/" + newName);
294 		if (secManager.checkWriteAccess(file) == FileSecurityManager.ACC_ILLEGAL
295 				|| secManager.checkWriteAccess(destFile) == FileSecurityManager.ACC_ILLEGAL)
296 			throw illegalAccess;
297 		else {
298 			boolean result = false;
299 			String srcPath = file.getAbsolutePath();
300 			result = file.renameTo(destFile);
301 			if (result) {
302 				FsChangeDescriptor fd = new FsChangeDescriptor(
303 						FsChangeDescriptor.FILE_RENAME);
304 				fd.setSource(srcPath);
305 				fd.setTarget(newName);
306 				fireFsListeners(fd);
307 			}
308 			return result;
309 		}
310 	}
311 
312 	public boolean setLastModified(int fileID, long time)
313 			throws RemoteException, SecurityException, ItemNotFoundException {
314 		File file = getFile(fileID);
315 		FileSecurityManager secManager = securityMap.get(fileID);
316 		if (secManager.checkWriteAccess(file) == FileSecurityManager.ACC_ILLEGAL)
317 			throw illegalAccess;
318 		else {
319 			boolean result = false;
320 			result = file.setLastModified(time);
321 			if (result) {
322 				FsChangeDescriptor fd = new FsChangeDescriptor(
323 						FsChangeDescriptor.FILE_REFRESH);
324 				fd.setChangeInfo(getFileDescriptor(file));
325 				fireFsListeners(fd);
326 			}
327 			return result;
328 		}
329 	}
330 
331 	public boolean setReadOnly(int fileID) throws RemoteException,
332 			SecurityException, ItemNotFoundException {
333 		File file = getFile(fileID);
334 		FileSecurityManager secManager = securityMap.get(fileID);
335 		if (secManager.checkWriteAccess(file) == FileSecurityManager.ACC_ILLEGAL)
336 			throw illegalAccess;
337 		else {
338 			boolean result = false;
339 			result = file.setReadOnly();
340 			if (result) {
341 				FsChangeDescriptor fd = new FsChangeDescriptor(
342 						FsChangeDescriptor.FILE_REFRESH);
343 				fd.setChangeInfo(getFileDescriptor(file));
344 				fireFsListeners(fd);
345 			}
346 			return result;
347 		}
348 	}
349 
350 	public FileDescriptor getDescriptor(int fileID) throws RemoteException,
351 			SecurityException, ItemNotFoundException {
352 		File file = getFile(fileID);
353 		FileSecurityManager secManager = securityMap.get(fileID);
354 		FileDescriptor result = getFileDescriptor(file);
355 		result.path = secManager.getUserRootPathManager().getUserAbsolutePath(
356 				result.path);
357 		if (result.path == null) {
358 			result.path = "";
359 			result.name = "/";
360 		}
361 		return result;
362 	}
363 
364 	public void releaseAllResources() {
365 		fileMap.clear();
366 	}
367 
368 	private File getFile(int fileID) throws ItemNotFoundException {
369 		File file = fileMap.get(fileID);
370 		if (file == null)
371 			throw fileNotFound;
372 		return file;
373 	}
374 
375 	private void fireFsListeners(FsChangeDescriptor desc) {
376 		for (LocalFsChangeListener fsL : fsListeners)
377 			fsL.fsChangeNotify(desc);
378 	}
379 
380 	public static FileDescriptor getFileDescriptor(File file) {
381 		FileDescriptor result = new FileDescriptor();
382 		result.name = file.getName();
383 		result.path = file.getParent();
384 		result.isFile = file.isFile();
385 		result.isHidden = file.isHidden();
386 		result.lastModified = file.lastModified();
387 		result.length = file.length();
388 		return result;
389 	}
390 
391 }