001 /*
002 $Id: ShareManager.java,v 1.17 2003/05/02 20:29:48 culdesac Exp $
003 */
004
005 package sharpster.daemon.sharemanagement;
006
007 import sharpster.common.*;
008 import sharpster.daemon.usermanagement.UserManager;
009 import sharpster.daemon.filemanagement.FileManager;
010 import sharpster.daemon.groupmanagement.GroupController;
011 import java.util.Hashtable;
012 import net.jxta.id.*;
013 import net.jxta.document.*;
014 import java.io.File;
015 import java.io.FileInputStream;
016 import java.io.FileOutputStream;
017 import java.io.StringWriter;
018 import java.util.*;
019
020 /**
021 * Class responsible for the overall management of file shares.
022 */
023 public class ShareManager {
024
025 private FileManager fileManager;
026 private UserManager userManager;
027 private GroupController groupController;
028 private ShareMap shareMap;
029 private String configFile;
030
031
032 /**
033 *
034 */
035 public void initialize(FileManager fm, UserManager um, GroupController gc) {
036 fileManager = fm;
037 userManager = um;
038 shareMap = new ShareMap();
039 configFile = new String();
040 groupController = gc;
041 }
042
043 /**
044 * Store the specified file share. If recursive is true, the share
045 * operates recursively.
046 */
047 public ResponseCollection shareFiles(FileCollection files,
048 boolean recursive) {
049
050 ResponseCollection responses = userManager.checkUsersExistance(files);
051 if (responses.hasError()) {
052 return responses;
053 }
054
055 ResponseCollection missingFiles = fileManager.checkFilesExistance(files);
056 if (missingFiles.hasError()) {
057 return missingFiles;
058 }
059
060 SharedFilesResponse sharedResponse = new SharedFilesResponse();
061
062 for (int i = 0; i < files.getFileCount(); i++) {
063 SharedFile file = files.getFile(i);
064
065 String users[] = file.getSharedToUsers();
066 for (int j = 0; j < users.length; j++) {
067 ID uid = userManager.getKey(users[j]);
068 ID fid = fileManager.getFileID(file.getPathInCVS() + "/" +
069 file.getFileName());
070
071 boolean added = shareMap.add(uid, fid,
072 file.getAccessRights(),
073 file.getPluginData());
074 }
075 }
076
077 sharedResponse.setOrigin("ShareManager");
078 sharedResponse.setError(false);
079 sharedResponse.setUser(sharpster.daemon.SharpsterDaemon.getPeerName());
080 sharedResponse.setFiles(files);
081 sharedResponse.setAction(1);
082
083 responses.addResponse(sharedResponse);
084
085 saveToFile(null);
086
087 return responses;
088 }
089
090 /**
091 *
092 */
093 public ResponseCollection unshareFiles(FileCollection files,
094 boolean recursive) {
095 ResponseCollection responses = userManager.checkUsersExistance(files);
096 if (responses.hasError()) {
097 return responses;
098 }
099
100 ResponseCollection missingFiles = fileManager.checkFilesExistance(files);
101 if (missingFiles.hasError()) {
102 return missingFiles;
103 }
104
105 SharedFilesResponse sharedResponse = new SharedFilesResponse();
106
107 for (int i = 0; i < files.getFileCount(); i++) {
108 SharedFile file = files.getFile(i);
109 String users[] = file.getSharedToUsers();
110 for (int j = 0; j < users.length; j++) {
111 ID uid = userManager.getKey(users[j]);
112 ID fid = fileManager.getFileID(file.getFullPath());
113
114 boolean removed = shareMap.remove(uid, fid);
115 }
116 }
117
118 sharedResponse.setOrigin("ShareManager");
119 sharedResponse.setError(false);
120 sharedResponse.setUser(sharpster.daemon.SharpsterDaemon.getPeerName());
121 sharedResponse.setFiles(files);
122 sharedResponse.setAction(2);
123
124 responses.addResponse(sharedResponse);
125
126 saveToFile(null);
127
128 return responses;
129 }
130
131 /**
132 *
133 */
134 public void removeSharesWithFile(ID fileID) {
135 shareMap.removeAllFiles(fileID);
136 }
137
138 public void synchronizeShares() {
139 shareMap.synchronizeShares(fileManager);
140 }
141
142
143 public ResponseCollection getSharedFiles(String user) {
144 LinkedList list = groupController.getGroups(user);
145 System.out.println(list);
146 ResponseCollection responses = new ResponseCollection();
147
148 for (int i=0; i < list.size(); i++) {
149 responses.addResponse(getSharedFilesLoop((String)list.get(i)));
150
151 }
152 return responses;
153 }
154
155
156
157 /**
158 *
159 */
160 private SharedFilesResponse getSharedFilesLoop(String user) {
161 FileCollection sharedFiles = new FileCollection();
162 // ResponseCollection responses = new ResponseCollection();
163 SharedFilesResponse sharedFilesResponse = new SharedFilesResponse();
164
165 ID uid = userManager.getKey(user);
166 ID[] files = shareMap.getAllShares(uid);
167
168 System.out.println("SM::Looking for shares to "+user);
169 System.out.println(uid.toString());
170 System.out.println(files.length);
171
172 if (files != null && uid != null) {
173 for (int j = 0; j < files.length; j++) {
174 String filepath = fileManager.getFilePath(files[j]);
175 SharedFile file = new SharedFile();
176 file.setFileName(filepath);
177 sharedFiles.addFile(file);
178 }
179 }
180
181 sharedFilesResponse.setFiles(sharedFiles);
182 sharedFilesResponse.setOrigin("ShareManager");
183 sharedFilesResponse.setUser(sharpster.daemon.SharpsterDaemon.getPeerName());
184 sharedFilesResponse.setError(false);
185 sharedFilesResponse.addSharedToUsers(user);
186
187
188
189 return sharedFilesResponse;
190 }
191
192 /**
193 *
194 */
195 public ResponseCollection viewFileSharing() {
196 ResponseCollection responses = new ResponseCollection();
197
198 String[] users = userManager.getAllUsers();
199 for (int i = 0; i < users.length; i++) {
200 SharedFilesResponse sharedFilesResponse = new SharedFilesResponse();
201 FileCollection sharedFiles = new FileCollection();
202 ID[] files = shareMap.getAllShares(userManager.getKey(users[i]));
203 if (files != null) {
204 for (int j = 0; j < files.length; j++) {
205 String filepath = fileManager.getFilePath(files[j]);
206 SharedFile file = new SharedFile();
207 file.setFileName(filepath);
208 ID userid = userManager.getKey(users[i]);
209 file.setPluginData(shareMap.getSharePluginData(userid, files[j]));
210 file.setAccessRights(shareMap.getShareAccess(userid, files[j]));
211 sharedFiles.addFile(file);
212 }
213 }
214 sharedFilesResponse.setFiles(sharedFiles);
215 sharedFilesResponse.setOrigin("ShareManager");
216 sharedFilesResponse.setUser(sharpster.daemon.SharpsterDaemon.getPeerName());
217 sharedFilesResponse.setError(false);
218 sharedFilesResponse.addSharedToUsers(new String(users[i]));
219 responses.addResponse(sharedFilesResponse);
220 }
221
222 return responses;
223 }
224
225 private boolean checkAccess(SharedFile file, String user, int access) {
226 LinkedList list = groupController.getGroups(user);
227
228 for (int i=0; i < list.size(); i++) {
229 if(checkAccessInLoop(file, (String)list.get(i), access)) {
230 return true;
231 }
232 }
233 return false;
234 }
235
236 private boolean checkAccessInLoop(SharedFile file, String user, int access) {
237 ID userid = userManager.getKey(user);
238 if (userid == null) {
239 return false;
240 }
241
242 ID fileid = fileManager.getFileID(file.getFullPath());
243 if (shareMap.shareExists(userid, fileid)) {
244 int a = shareMap.getShareAccess(userid, fileid);
245 if ( (a & access) == access) {
246 file.setPluginData(shareMap.getSharePluginData(userid, fileid));
247 return true;
248 }
249 }
250 else {
251 String[] dirList = file.getPathInCVS().split("(/)");
252 String path = new String("");
253 for (int i = 0; i < dirList.length; i++) {
254 path += dirList[i];
255 fileid = fileManager.getFileID(path);
256 if (fileid != null && shareMap.shareExists(userid, fileid)) {
257 int a = shareMap.getShareAccess(userid, fileid);
258 if ( (a & access) == access) {
259 file.setPluginData(shareMap.getSharePluginData(userid, fileid));
260 return true;
261 }
262 }
263 path += "/";
264 }
265 }
266 return false;
267 }
268
269 public ResponseCollection checkAccess(FileCollection files, String user,
270 int access) {
271 FileCollection failedFiles = new FileCollection();
272 ResponseCollection responses = new ResponseCollection();
273
274 for (int i = 0; i < files.getFileCount(); i++) {
275 SharedFile file = files.getFile(i);
276 if (!checkAccess(file, user, access)) {
277 failedFiles.addFile(file);
278 }
279 }
280
281 if (failedFiles.getFileCount() > 0) {
282 MissingAccessResponse resp = new MissingAccessResponse();
283 resp.setOrigin("ShareManager");
284 resp.setUser(sharpster.daemon.SharpsterDaemon.getPeerName());
285 resp.setError(true);
286 resp.setFiles(failedFiles);
287 resp.setAccess(access);
288 responses.addResponse(resp);
289 }
290
291 return responses;
292 }
293
294 /**
295 * Check if a user has read access to the specified files.
296 */
297 public boolean hasReadAccess(FileCollection files, String user) {
298 for (int i = 0; i < files.getFileCount(); i++) {
299 SharedFile file = files.getFile(i);
300 if (!file.hasReadAccess(user)) {
301 return false;
302 }
303 }
304 return true;
305 }
306
307 /**
308 * Check if a user has write access to the specified files.
309 */
310 public boolean hasWriteAccess(FileCollection files, String user) {
311 for (int i = 0; i < files.getFileCount(); i++) {
312 SharedFile file = files.getFile(i);
313 if (!file.hasWriteAccess(user)) {
314 return false;
315 }
316 }
317 return true;
318 }
319
320 /**
321 * Check if a user has remove access to the specified files.
322 */
323 public boolean hasDeleteAccess(FileCollection files, String user) {
324 for (int i = 0; i < files.getFileCount(); i++) {
325 SharedFile file = files.getFile(i);
326 if (!file.hasDeleteAccess(user)) {
327 return false;
328 }
329 }
330 return true;
331 }
332
333 public boolean loadFromFile(String filepath) {
334 if (filepath == null) {
335 filepath = configFile;
336 }
337 else {
338 configFile = new String(filepath);
339 }
340
341 return shareMap.loadFromFile(configFile);
342 }
343
344 public boolean saveToFile(String filepath) {
345 if (filepath == null) {
346 filepath = configFile;
347 }
348 else {
349 configFile = new String(filepath);
350 }
351
352 return shareMap.saveToFile(configFile);
353 }
354 }
355
356
357
358
359