View Javadoc

1   package twcsckernel.projectbase.io;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileNotFoundException;
6   import java.io.IOException;
7   import java.rmi.RemoteException;
8   import java.util.Collection;
9   import java.util.Collections;
10  import java.util.HashMap;
11  import java.util.Map;
12  
13  import twcsckernel.projectbase.common.ReaderFactory;
14  
15  public class ReaderFactoryImpl implements ReaderFactory {
16  	private Map<Integer, FileInputStream> streamMap = Collections
17  			.synchronizedMap(new HashMap<Integer, FileInputStream>());
18  
19  	private static final FileNotFoundException fileNotFound = new FileNotFoundException();
20  
21  	private static final SecurityException illegalAccess = new SecurityException();
22  
23  	private static final ItemNotFoundException streamNotFound = new ItemNotFoundException();
24  
25  	private Object idPointerMutex = new Object();
26  
27  	private int idPointer = 0;
28  
29  	private FileSecurityManager defaultSecManager = null;
30  
31  	public ReaderFactoryImpl(FileSecurityManager defaultSecManager)
32  			throws NullPointerException {
33  		if (defaultSecManager == null)
34  			throw new NullPointerException("FileSecurityManager==null");
35  
36  		this.defaultSecManager = defaultSecManager;
37  	}
38  
39  	public RemoteFileInputStream newFileInputStream(String filePath)
40  			throws RemoteException, FileNotFoundException, SecurityException {
41  		return newFileInputStream(filePath, defaultSecManager);
42  	}
43  
44  	public RemoteFileInputStream newFileInputStream(String filePath,
45  			FileSecurityManager secManager) throws RemoteException,
46  			FileNotFoundException, SecurityException {
47  
48  		File file;
49  		try {
50  			file = secManager.getUserRootPathManager().getFile(filePath);
51  		} catch (IOException e1) {
52  			throw new FileNotFoundException();
53  		}
54  		switch (secManager.checkReadAccess(file)) {
55  		case FileSecurityManager.ACC_NOT_FOUND:
56  			throw fileNotFound;
57  		case FileSecurityManager.ACC_ILLEGAL:
58  			throw illegalAccess;
59  		}
60  
61  		RemoteFileInputStream rfis = null;
62  		try {
63  			FileInputStream fis = new FileInputStream(file);
64  			synchronized (idPointerMutex) {
65  				streamMap.put(idPointer, fis);
66  				rfis = new RemoteFileInputStream(idPointer, this);
67  				idPointer++;
68  			}
69  		} catch (FileNotFoundException e) {
70  			throw e;
71  		}
72  		return rfis;
73  	}
74  
75  	public ReadResult read(int streamID) throws RemoteException, IOException,
76  			ItemNotFoundException {
77  		FileInputStream is = getStream(streamID);
78  		Integer intResult = null;
79  		synchronized (is) {
80  			intResult = is.read();
81  		}
82  		return new ReadResult(intResult, null);
83  	}
84  
85  	public ReadResult read(int streamID, int len) throws RemoteException,
86  			IOException, ItemNotFoundException {
87  		FileInputStream is = getStream(streamID);
88  		byte[] buffer = new byte[len];
89  		Integer intResult = null;
90  		synchronized (is) {
91  			intResult = is.read(buffer, 0, len);
92  		}
93  		return new ReadResult(intResult, buffer);
94  	}
95  
96  	public long skip(int streamID, long off) throws RemoteException,
97  			IOException, ItemNotFoundException {
98  		FileInputStream is = getStream(streamID);
99  		long skipResult = 0;
100 		synchronized (is) {
101 			skipResult = is.skip(off);
102 		}
103 		return skipResult;
104 	}
105 
106 	public int available(int streamID) throws RemoteException, IOException,
107 			ItemNotFoundException {
108 		FileInputStream is = getStream(streamID);
109 		int avResult = 0;
110 		synchronized (is) {
111 			avResult = is.available();
112 		}
113 		return avResult;
114 	}
115 
116 	public void close(int streamID) throws RemoteException, IOException,
117 			ItemNotFoundException {
118 		FileInputStream is = getStream(streamID);
119 		synchronized (is) {
120 			is.close();
121 			streamMap.remove(streamID);
122 		}
123 
124 	}
125 
126 	public void releaseAllResources() {
127 		Collection<FileInputStream> streams = streamMap.values();
128 		for (FileInputStream fStream : streams)
129 			try {
130 				synchronized (fStream) {
131 					fStream.close();
132 				}
133 			} catch (IOException e) {
134 			}
135 		streamMap.clear();
136 	}
137 
138 	private FileInputStream getStream(int streamID)
139 			throws ItemNotFoundException {
140 		FileInputStream is = streamMap.get(streamID);
141 		if (is == null)
142 			throw streamNotFound;
143 		return is;
144 	}
145 
146 }