KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > sharedfolder > SharedFolderPlugin


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2005 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.applications.sharedfolder;
21
22 import org.lucane.common.ConnectInfo;
23 import org.lucane.common.Logging;
24 import org.lucane.common.acl.AclInfo;
25 import org.lucane.common.acl.AclProducer;
26 import org.lucane.common.net.ObjectConnection;
27 import org.lucane.client.Plugin;
28 import org.lucane.client.StandalonePlugin;
29 import org.lucane.client.Communicator;
30 import org.lucane.client.Client;
31 import org.lucane.client.widgets.DialogBox;
32 import org.lucane.client.widgets.ManagedWindow;
33 import org.lucane.applications.sharedfolder.model.FileInfo;
34 import org.lucane.applications.sharedfolder.model.FolderInfo;
35 import org.lucane.applications.sharedfolder.model.SharedItem;
36 import org.lucane.applications.sharedfolder.gui.FolderTableModel;
37 import org.lucane.applications.sharedfolder.gui.FolderTableListener;
38 import org.lucane.applications.sharedfolder.gui.ActionToolBar;
39 import org.lucane.applications.sharedfolder.gui.FolderTableRenderer;
40
41 import javax.swing.*;
42 import java.io.*;
43 import java.util.ArrayList JavaDoc;
44 import java.awt.*;
45
46 public class SharedFolderPlugin extends StandalonePlugin
47 implements AclProducer
48 {
49     private static final int BUFFER_SIZE = 10240;
50     private ConnectInfo service;
51     private String JavaDoc userName;
52
53     public Plugin newInstance(ConnectInfo[] friends)
54     {
55         return new SharedFolderPlugin();
56     }
57
58     public void start()
59     {
60         this.service = Communicator.getInstance().getConnectInfo(getName());
61         this.userName = Client.getInstance().getMyInfos().getName();
62
63         FolderTableModel model = new FolderTableModel(this);
64
65         JTable table = new JTable(model);
66         table.setDefaultRenderer(String JavaDoc.class, new FolderTableRenderer());
67         table.getColumnModel().getColumn(0).setMinWidth(16);
68         table.getColumnModel().getColumn(0).setMaxWidth(20);
69         table.setRowHeight(18);
70         table.setRowSelectionAllowed(true);
71
72         ActionToolBar toolbar = new ActionToolBar(this, table, false);
73         FolderTableListener listener = new FolderTableListener(this, table, toolbar);
74         table.addMouseListener(listener);
75         table.addKeyListener(listener);
76         table.getSelectionModel().addListSelectionListener(listener);
77         model.addTableModelListener(listener);
78
79         ArrayList JavaDoc items = new ArrayList JavaDoc();
80         try {
81             items.addAll(listFolders(FolderInfo.ROOT_ID));
82             items.addAll(listFiles(FolderInfo.ROOT_ID));
83             model.setItems(this.getFolder(FolderInfo.ROOT_ID), items);
84         } catch(Exception JavaDoc e) {
85             DialogBox.error(e.getMessage());
86             e.printStackTrace();
87         }
88         
89
90         ManagedWindow window = new ManagedWindow(this, getTitle());
91         window.setName("browser");
92         window.setExitPluginOnClose(true);
93         window.getContentPane().add(toolbar, BorderLayout.NORTH);
94         window.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
95         window.setPreferredSize(new Dimension(600, 400));
96         window.show();
97     }
98
99     //--
100

101     public void uploadFile(int remoteFolder, int fileId, String JavaDoc remoteName, int version, File localFile)
102     throws IOException, ClassNotFoundException JavaDoc
103     {
104         FileInfo fileInfo = new FileInfo(fileId, remoteFolder, remoteName, version, userName, localFile.length());
105         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.UPLOAD_FILE, fileInfo);
106         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
107
108         try {
109             sendFileData(oc, localFile);
110         } catch(IOException ioe) {
111             Logging.getLogger().warning("file upload failed");
112             ioe.printStackTrace();
113         }
114
115         String JavaDoc ack = oc.readString();
116         oc.close();
117
118         if(! ack.equals("OK"))
119             throw new IOException(ack);
120     }
121
122     public void createFolder(int remoteFolder, String JavaDoc remoteName)
123     throws IOException, ClassNotFoundException JavaDoc
124     {
125         FolderInfo folderInfo = new FolderInfo(remoteFolder, remoteName, userName);
126         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.CREATE_FOLDER, folderInfo);
127         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
128
129         String JavaDoc ack = oc.readString();
130         oc.close();
131
132         if(! ack.equals("OK"))
133             throw new IOException(ack);
134     }
135
136     public FolderInfo getFolder(int folderId)
137     throws IOException, ClassNotFoundException JavaDoc
138     {
139         if(folderId == FolderInfo.ROOT_ID)
140             return new FolderInfo(FolderInfo.ROOT_ID, FolderInfo.ROOT_ID, "", "", null, null, true, true);
141
142         FolderInfo info = null;
143         Integer JavaDoc remoteId = new Integer JavaDoc(folderId);
144         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.GET_FOLDER, remoteId);
145         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
146
147         String JavaDoc ack = oc.readString();
148         if(ack.equals("OK"))
149             info = (FolderInfo)oc.read();
150
151         oc.close();
152
153         if(! ack.equals("OK"))
154             throw new IOException(ack);
155
156         return info;
157     }
158
159     private FileInfo getFile(int fileId)
160     throws IOException, ClassNotFoundException JavaDoc
161     {
162         FileInfo info = null;
163         Integer JavaDoc remoteId = new Integer JavaDoc(fileId);
164         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.GET_FILE, remoteId);
165         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
166
167         String JavaDoc ack = oc.readString();
168         if(ack.equals("OK"))
169             info = (FileInfo)oc.read();
170
171         oc.close();
172
173         if(! ack.equals("OK"))
174             throw new IOException(ack);
175
176         return info;
177     }
178
179     public ArrayList JavaDoc listFolders(int remoteFolder)
180     throws IOException, ClassNotFoundException JavaDoc
181     {
182         Integer JavaDoc remoteId = new Integer JavaDoc(remoteFolder);
183         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.LIST_FOLDERS, remoteId);
184         return getListFromService(action);
185     }
186
187     public ArrayList JavaDoc listFiles(int remoteFolder)
188     throws IOException, ClassNotFoundException JavaDoc
189     {
190         Integer JavaDoc remoteId = new Integer JavaDoc(remoteFolder);
191         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.LIST_FILES, remoteId);
192         return getListFromService(action);
193     }
194
195     public ArrayList JavaDoc listFileVersions(int remoteFileId)
196     throws IOException, ClassNotFoundException JavaDoc
197     {
198         Integer JavaDoc remoteId = new Integer JavaDoc(remoteFileId);
199         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.LIST_FILE_VERSIONS, remoteId);
200         return getListFromService(action);
201     }
202
203
204     public void downloadFile(int remoteFileId, int version, File destination)
205     throws IOException, ClassNotFoundException JavaDoc
206     {
207         Object JavaDoc[] params = {new Integer JavaDoc(remoteFileId), new Integer JavaDoc(version)};
208         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.DOWNLOAD_FILE, params);
209         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
210
211         DataOutputStream dos = new DataOutputStream(new FileOutputStream(destination));
212
213         long length = ((Long JavaDoc)oc.read()).longValue();
214         long bytes = 0;
215         while(bytes < length)
216         {
217             try {
218                 byte[] buffer = (byte[])oc.read();
219                 dos.write(buffer);
220                 bytes += buffer.length;
221             } catch(ClassNotFoundException JavaDoc cnfe) {
222                 //should never happen, byte[] is well known
223
cnfe.printStackTrace();
224             }
225         }
226
227         oc.close();
228     }
229
230
231     public void updateFolder(FolderInfo folderInfo)
232     throws IOException, ClassNotFoundException JavaDoc
233     {
234         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.UPDATE_FOLDER, folderInfo);
235         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
236
237         String JavaDoc ack = oc.readString();
238         oc.close();
239
240         if(! ack.equals("OK"))
241             throw new IOException(ack);
242     }
243
244     public void updateFile(FileInfo fileInfo)
245     throws IOException, ClassNotFoundException JavaDoc
246     {
247         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.UPDATE_FILE, fileInfo);
248         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
249
250         String JavaDoc ack = oc.readString();
251         oc.close();
252
253         if(! ack.equals("OK"))
254             throw new IOException(ack);
255     }
256
257     public void removeFolder(int folderId)
258     throws IOException, ClassNotFoundException JavaDoc
259     {
260         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.REMOVE_FOLDER, new Integer JavaDoc(folderId));
261         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
262
263         String JavaDoc ack = oc.readString();
264         oc.close();
265
266         if(! ack.equals("OK"))
267             throw new IOException(ack);
268     }
269
270     public void removeFile(int fileId)
271     throws IOException, ClassNotFoundException JavaDoc
272     {
273         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.REMOVE_FILE, new Integer JavaDoc(fileId));
274         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
275
276         String JavaDoc ack = oc.readString();
277         oc.close();
278
279         if(! ack.equals("OK"))
280             throw new IOException(ack);
281     }
282
283
284     //-- ACL
285

286     public AclInfo[] getAcls(String JavaDoc itemId)
287     throws Exception JavaDoc
288     {
289         AclInfo[] acl = null;
290
291         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.GET_ACLS, itemId);
292         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
293         String JavaDoc ack = (String JavaDoc)oc.read();
294
295         if(ack.equals("OK"))
296         {
297             acl = (AclInfo[])oc.read();
298             oc.close();
299
300             return acl;
301         }
302         oc.close();
303         throw new Exception JavaDoc(ack);
304     }
305
306     public ArrayList JavaDoc getAllGroups()
307     throws Exception JavaDoc
308     {
309         ArrayList JavaDoc groups = null;
310
311         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.GET_GROUPS);
312         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
313         String JavaDoc ack = (String JavaDoc)oc.read();
314
315         if(ack.equals("OK"))
316         {
317             groups = (ArrayList JavaDoc)oc.read();
318             oc.close();
319
320             return groups;
321         }
322         oc.close();
323         throw new Exception JavaDoc(ack);
324     }
325
326     public ArrayList JavaDoc getAllUsers()
327     throws Exception JavaDoc
328     {
329         ArrayList JavaDoc users = null;
330
331         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.GET_USERS);
332         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
333         String JavaDoc ack = (String JavaDoc)oc.read();
334
335         if(ack.equals("OK"))
336         {
337             users = (ArrayList JavaDoc)oc.read();
338             oc.close();
339
340             return users;
341         }
342         oc.close();
343         throw new Exception JavaDoc(ack);
344     }
345
346     public void addAcl(String JavaDoc itemId, AclInfo info)
347     throws Exception JavaDoc
348     {
349         Object JavaDoc[] params = {itemId, info};
350         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.ADD_ACL, params);
351         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
352         String JavaDoc ack = (String JavaDoc)oc.read();
353         oc.close();
354
355         if(!ack.equals("OK"))
356             throw new Exception JavaDoc(ack);
357     }
358
359     public void removeAcl(String JavaDoc itemId, AclInfo info)
360     throws Exception JavaDoc
361     {
362         Object JavaDoc[] params = {itemId, info};
363         SharedFolderAction action = new SharedFolderAction(SharedFolderAction.REMOVE_ACL, params);
364         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
365         String JavaDoc ack = (String JavaDoc)oc.read();
366         oc.close();
367
368         if(!ack.equals("OK"))
369             throw new Exception JavaDoc(ack);
370     }
371
372     //introduced for acl webapp
373
public String JavaDoc[] getAccesses()
374     {
375         return new String JavaDoc[] {SharedItem.READ, SharedItem.WRITE};
376     }
377
378     public String JavaDoc getItemName(String JavaDoc itemId)
379     throws Exception JavaDoc
380     {
381         SharedItem item;
382         if(itemId.charAt(0) == 'f')
383             item = getFolder(Integer.parseInt(itemId.substring(1)));
384         else
385             item = getFile(Integer.parseInt(itemId));
386
387         return item.getName();
388     }
389
390     //--
391

392     private ArrayList JavaDoc getListFromService(SharedFolderAction action)
393     throws IOException, ClassNotFoundException JavaDoc
394     {
395         ArrayList JavaDoc items = null;
396         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
397
398         String JavaDoc ack = oc.readString();
399         if(ack.equals("OK"))
400         {
401             items = (ArrayList JavaDoc)oc.read();
402         }
403
404         oc.close();
405
406         if(! ack.equals("OK"))
407             throw new IOException(ack);
408
409         return items;
410     }
411
412     private void sendFileData(ObjectConnection oc, File localFile)
413     throws IOException
414     {
415         DataInputStream dis = new DataInputStream(new FileInputStream(localFile));
416         while(dis.available() > 0)
417         {
418             byte[] buffer = new byte[BUFFER_SIZE];
419             int bytes = dis.read(buffer);
420             if(bytes != buffer.length);
421             {
422                 byte[] temp = new byte[bytes];
423                 System.arraycopy(buffer, 0, temp, 0, bytes);
424                 buffer = temp;
425             }
426             oc.write(buffer);
427         }
428     }
429
430 }
431
Popular Tags