001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2005 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package org.lucane.applications.sharedfolder;
021:
022: import org.lucane.common.ConnectInfo;
023: import org.lucane.common.Logging;
024: import org.lucane.common.acl.AclInfo;
025: import org.lucane.common.acl.AclProducer;
026: import org.lucane.common.net.ObjectConnection;
027: import org.lucane.client.Plugin;
028: import org.lucane.client.StandalonePlugin;
029: import org.lucane.client.Communicator;
030: import org.lucane.client.Client;
031: import org.lucane.client.widgets.DialogBox;
032: import org.lucane.client.widgets.ManagedWindow;
033: import org.lucane.applications.sharedfolder.model.FileInfo;
034: import org.lucane.applications.sharedfolder.model.FolderInfo;
035: import org.lucane.applications.sharedfolder.model.SharedItem;
036: import org.lucane.applications.sharedfolder.gui.FolderTableModel;
037: import org.lucane.applications.sharedfolder.gui.FolderTableListener;
038: import org.lucane.applications.sharedfolder.gui.ActionToolBar;
039: import org.lucane.applications.sharedfolder.gui.FolderTableRenderer;
040:
041: import javax.swing.*;
042: import java.io.*;
043: import java.util.ArrayList;
044: import java.awt.*;
045:
046: public class SharedFolderPlugin extends StandalonePlugin implements
047: AclProducer {
048: private static final int BUFFER_SIZE = 10240;
049: private ConnectInfo service;
050: private String userName;
051:
052: public Plugin newInstance(ConnectInfo[] friends) {
053: return new SharedFolderPlugin();
054: }
055:
056: public void start() {
057: this .service = Communicator.getInstance().getConnectInfo(
058: getName());
059: this .userName = Client.getInstance().getMyInfos().getName();
060:
061: FolderTableModel model = new FolderTableModel(this );
062:
063: JTable table = new JTable(model);
064: table.setDefaultRenderer(String.class,
065: new FolderTableRenderer());
066: table.getColumnModel().getColumn(0).setMinWidth(16);
067: table.getColumnModel().getColumn(0).setMaxWidth(20);
068: table.setRowHeight(18);
069: table.setRowSelectionAllowed(true);
070:
071: ActionToolBar toolbar = new ActionToolBar(this , table, false);
072: FolderTableListener listener = new FolderTableListener(this ,
073: table, toolbar);
074: table.addMouseListener(listener);
075: table.addKeyListener(listener);
076: table.getSelectionModel().addListSelectionListener(listener);
077: model.addTableModelListener(listener);
078:
079: ArrayList items = new ArrayList();
080: try {
081: items.addAll(listFolders(FolderInfo.ROOT_ID));
082: items.addAll(listFiles(FolderInfo.ROOT_ID));
083: model.setItems(this .getFolder(FolderInfo.ROOT_ID), items);
084: } catch (Exception e) {
085: DialogBox.error(e.getMessage());
086: e.printStackTrace();
087: }
088:
089: ManagedWindow window = new ManagedWindow(this , getTitle());
090: window.setName("browser");
091: window.setExitPluginOnClose(true);
092: window.getContentPane().add(toolbar, BorderLayout.NORTH);
093: window.getContentPane().add(new JScrollPane(table),
094: BorderLayout.CENTER);
095: window.setPreferredSize(new Dimension(600, 400));
096: window.show();
097: }
098:
099: //--
100:
101: public void uploadFile(int remoteFolder, int fileId,
102: String remoteName, int version, File localFile)
103: throws IOException, ClassNotFoundException {
104: FileInfo fileInfo = new FileInfo(fileId, remoteFolder,
105: remoteName, version, userName, localFile.length());
106: SharedFolderAction action = new SharedFolderAction(
107: SharedFolderAction.UPLOAD_FILE, fileInfo);
108: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
109: service, getName(), action);
110:
111: try {
112: sendFileData(oc, localFile);
113: } catch (IOException ioe) {
114: Logging.getLogger().warning("file upload failed");
115: ioe.printStackTrace();
116: }
117:
118: String ack = oc.readString();
119: oc.close();
120:
121: if (!ack.equals("OK"))
122: throw new IOException(ack);
123: }
124:
125: public void createFolder(int remoteFolder, String remoteName)
126: throws IOException, ClassNotFoundException {
127: FolderInfo folderInfo = new FolderInfo(remoteFolder,
128: remoteName, userName);
129: SharedFolderAction action = new SharedFolderAction(
130: SharedFolderAction.CREATE_FOLDER, folderInfo);
131: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
132: service, getName(), action);
133:
134: String ack = oc.readString();
135: oc.close();
136:
137: if (!ack.equals("OK"))
138: throw new IOException(ack);
139: }
140:
141: public FolderInfo getFolder(int folderId) throws IOException,
142: ClassNotFoundException {
143: if (folderId == FolderInfo.ROOT_ID)
144: return new FolderInfo(FolderInfo.ROOT_ID,
145: FolderInfo.ROOT_ID, "", "", null, null, true, true);
146:
147: FolderInfo info = null;
148: Integer remoteId = new Integer(folderId);
149: SharedFolderAction action = new SharedFolderAction(
150: SharedFolderAction.GET_FOLDER, remoteId);
151: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
152: service, getName(), action);
153:
154: String ack = oc.readString();
155: if (ack.equals("OK"))
156: info = (FolderInfo) oc.read();
157:
158: oc.close();
159:
160: if (!ack.equals("OK"))
161: throw new IOException(ack);
162:
163: return info;
164: }
165:
166: private FileInfo getFile(int fileId) throws IOException,
167: ClassNotFoundException {
168: FileInfo info = null;
169: Integer remoteId = new Integer(fileId);
170: SharedFolderAction action = new SharedFolderAction(
171: SharedFolderAction.GET_FILE, remoteId);
172: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
173: service, getName(), action);
174:
175: String ack = oc.readString();
176: if (ack.equals("OK"))
177: info = (FileInfo) oc.read();
178:
179: oc.close();
180:
181: if (!ack.equals("OK"))
182: throw new IOException(ack);
183:
184: return info;
185: }
186:
187: public ArrayList listFolders(int remoteFolder) throws IOException,
188: ClassNotFoundException {
189: Integer remoteId = new Integer(remoteFolder);
190: SharedFolderAction action = new SharedFolderAction(
191: SharedFolderAction.LIST_FOLDERS, remoteId);
192: return getListFromService(action);
193: }
194:
195: public ArrayList listFiles(int remoteFolder) throws IOException,
196: ClassNotFoundException {
197: Integer remoteId = new Integer(remoteFolder);
198: SharedFolderAction action = new SharedFolderAction(
199: SharedFolderAction.LIST_FILES, remoteId);
200: return getListFromService(action);
201: }
202:
203: public ArrayList listFileVersions(int remoteFileId)
204: throws IOException, ClassNotFoundException {
205: Integer remoteId = new Integer(remoteFileId);
206: SharedFolderAction action = new SharedFolderAction(
207: SharedFolderAction.LIST_FILE_VERSIONS, remoteId);
208: return getListFromService(action);
209: }
210:
211: public void downloadFile(int remoteFileId, int version,
212: File destination) throws IOException,
213: ClassNotFoundException {
214: Object[] params = { new Integer(remoteFileId),
215: new Integer(version) };
216: SharedFolderAction action = new SharedFolderAction(
217: SharedFolderAction.DOWNLOAD_FILE, params);
218: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
219: service, getName(), action);
220:
221: DataOutputStream dos = new DataOutputStream(
222: new FileOutputStream(destination));
223:
224: long length = ((Long) oc.read()).longValue();
225: long bytes = 0;
226: while (bytes < length) {
227: try {
228: byte[] buffer = (byte[]) oc.read();
229: dos.write(buffer);
230: bytes += buffer.length;
231: } catch (ClassNotFoundException cnfe) {
232: //should never happen, byte[] is well known
233: cnfe.printStackTrace();
234: }
235: }
236:
237: oc.close();
238: }
239:
240: public void updateFolder(FolderInfo folderInfo) throws IOException,
241: ClassNotFoundException {
242: SharedFolderAction action = new SharedFolderAction(
243: SharedFolderAction.UPDATE_FOLDER, folderInfo);
244: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
245: service, getName(), action);
246:
247: String ack = oc.readString();
248: oc.close();
249:
250: if (!ack.equals("OK"))
251: throw new IOException(ack);
252: }
253:
254: public void updateFile(FileInfo fileInfo) throws IOException,
255: ClassNotFoundException {
256: SharedFolderAction action = new SharedFolderAction(
257: SharedFolderAction.UPDATE_FILE, fileInfo);
258: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
259: service, getName(), action);
260:
261: String ack = oc.readString();
262: oc.close();
263:
264: if (!ack.equals("OK"))
265: throw new IOException(ack);
266: }
267:
268: public void removeFolder(int folderId) throws IOException,
269: ClassNotFoundException {
270: SharedFolderAction action = new SharedFolderAction(
271: SharedFolderAction.REMOVE_FOLDER, new Integer(folderId));
272: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
273: service, getName(), action);
274:
275: String ack = oc.readString();
276: oc.close();
277:
278: if (!ack.equals("OK"))
279: throw new IOException(ack);
280: }
281:
282: public void removeFile(int fileId) throws IOException,
283: ClassNotFoundException {
284: SharedFolderAction action = new SharedFolderAction(
285: SharedFolderAction.REMOVE_FILE, new Integer(fileId));
286: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
287: service, getName(), action);
288:
289: String ack = oc.readString();
290: oc.close();
291:
292: if (!ack.equals("OK"))
293: throw new IOException(ack);
294: }
295:
296: //-- ACL
297:
298: public AclInfo[] getAcls(String itemId) throws Exception {
299: AclInfo[] acl = null;
300:
301: SharedFolderAction action = new SharedFolderAction(
302: SharedFolderAction.GET_ACLS, itemId);
303: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
304: service, getName(), action);
305: String ack = (String) oc.read();
306:
307: if (ack.equals("OK")) {
308: acl = (AclInfo[]) oc.read();
309: oc.close();
310:
311: return acl;
312: }
313: oc.close();
314: throw new Exception(ack);
315: }
316:
317: public ArrayList getAllGroups() throws Exception {
318: ArrayList groups = null;
319:
320: SharedFolderAction action = new SharedFolderAction(
321: SharedFolderAction.GET_GROUPS);
322: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
323: service, getName(), action);
324: String ack = (String) oc.read();
325:
326: if (ack.equals("OK")) {
327: groups = (ArrayList) oc.read();
328: oc.close();
329:
330: return groups;
331: }
332: oc.close();
333: throw new Exception(ack);
334: }
335:
336: public ArrayList getAllUsers() throws Exception {
337: ArrayList users = null;
338:
339: SharedFolderAction action = new SharedFolderAction(
340: SharedFolderAction.GET_USERS);
341: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
342: service, getName(), action);
343: String ack = (String) oc.read();
344:
345: if (ack.equals("OK")) {
346: users = (ArrayList) oc.read();
347: oc.close();
348:
349: return users;
350: }
351: oc.close();
352: throw new Exception(ack);
353: }
354:
355: public void addAcl(String itemId, AclInfo info) throws Exception {
356: Object[] params = { itemId, info };
357: SharedFolderAction action = new SharedFolderAction(
358: SharedFolderAction.ADD_ACL, params);
359: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
360: service, getName(), action);
361: String ack = (String) oc.read();
362: oc.close();
363:
364: if (!ack.equals("OK"))
365: throw new Exception(ack);
366: }
367:
368: public void removeAcl(String itemId, AclInfo info) throws Exception {
369: Object[] params = { itemId, info };
370: SharedFolderAction action = new SharedFolderAction(
371: SharedFolderAction.REMOVE_ACL, params);
372: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
373: service, getName(), action);
374: String ack = (String) oc.read();
375: oc.close();
376:
377: if (!ack.equals("OK"))
378: throw new Exception(ack);
379: }
380:
381: //introduced for acl webapp
382: public String[] getAccesses() {
383: return new String[] { SharedItem.READ, SharedItem.WRITE };
384: }
385:
386: public String getItemName(String itemId) throws Exception {
387: SharedItem item;
388: if (itemId.charAt(0) == 'f')
389: item = getFolder(Integer.parseInt(itemId.substring(1)));
390: else
391: item = getFile(Integer.parseInt(itemId));
392:
393: return item.getName();
394: }
395:
396: //--
397:
398: private ArrayList getListFromService(SharedFolderAction action)
399: throws IOException, ClassNotFoundException {
400: ArrayList items = null;
401: ObjectConnection oc = Communicator.getInstance().sendMessageTo(
402: service, getName(), action);
403:
404: String ack = oc.readString();
405: if (ack.equals("OK")) {
406: items = (ArrayList) oc.read();
407: }
408:
409: oc.close();
410:
411: if (!ack.equals("OK"))
412: throw new IOException(ack);
413:
414: return items;
415: }
416:
417: private void sendFileData(ObjectConnection oc, File localFile)
418: throws IOException {
419: DataInputStream dis = new DataInputStream(new FileInputStream(
420: localFile));
421: while (dis.available() > 0) {
422: byte[] buffer = new byte[BUFFER_SIZE];
423: int bytes = dis.read(buffer);
424: if (bytes != buffer.length)
425: ;
426: {
427: byte[] temp = new byte[bytes];
428: System.arraycopy(buffer, 0, temp, 0, bytes);
429: buffer = temp;
430: }
431: oc.write(buffer);
432: }
433: }
434:
435: }
|