1 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 ; 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 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 .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 items = new ArrayList (); 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 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 101 public void uploadFile(int remoteFolder, int fileId, String remoteName, int version, File localFile) 102 throws IOException, ClassNotFoundException 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 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 remoteName) 123 throws IOException, ClassNotFoundException 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 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 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 remoteId = new Integer (folderId); 144 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.GET_FOLDER, remoteId); 145 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 146 147 String 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 161 { 162 FileInfo info = null; 163 Integer remoteId = new Integer (fileId); 164 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.GET_FILE, remoteId); 165 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 166 167 String 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 listFolders(int remoteFolder) 180 throws IOException, ClassNotFoundException 181 { 182 Integer remoteId = new Integer (remoteFolder); 183 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.LIST_FOLDERS, remoteId); 184 return getListFromService(action); 185 } 186 187 public ArrayList listFiles(int remoteFolder) 188 throws IOException, ClassNotFoundException 189 { 190 Integer remoteId = new Integer (remoteFolder); 191 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.LIST_FILES, remoteId); 192 return getListFromService(action); 193 } 194 195 public ArrayList listFileVersions(int remoteFileId) 196 throws IOException, ClassNotFoundException 197 { 198 Integer remoteId = new Integer (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 206 { 207 Object [] params = {new Integer (remoteFileId), new Integer (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 )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 cnfe) { 222 cnfe.printStackTrace(); 224 } 225 } 226 227 oc.close(); 228 } 229 230 231 public void updateFolder(FolderInfo folderInfo) 232 throws IOException, ClassNotFoundException 233 { 234 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.UPDATE_FOLDER, folderInfo); 235 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 236 237 String 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 246 { 247 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.UPDATE_FILE, fileInfo); 248 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 249 250 String 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 259 { 260 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.REMOVE_FOLDER, new Integer (folderId)); 261 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 262 263 String 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 272 { 273 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.REMOVE_FILE, new Integer (fileId)); 274 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 275 276 String ack = oc.readString(); 277 oc.close(); 278 279 if(! ack.equals("OK")) 280 throw new IOException(ack); 281 } 282 283 284 286 public AclInfo[] getAcls(String itemId) 287 throws Exception 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 ack = (String )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 (ack); 304 } 305 306 public ArrayList getAllGroups() 307 throws Exception 308 { 309 ArrayList groups = null; 310 311 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.GET_GROUPS); 312 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 313 String ack = (String )oc.read(); 314 315 if(ack.equals("OK")) 316 { 317 groups = (ArrayList )oc.read(); 318 oc.close(); 319 320 return groups; 321 } 322 oc.close(); 323 throw new Exception (ack); 324 } 325 326 public ArrayList getAllUsers() 327 throws Exception 328 { 329 ArrayList users = null; 330 331 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.GET_USERS); 332 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 333 String ack = (String )oc.read(); 334 335 if(ack.equals("OK")) 336 { 337 users = (ArrayList )oc.read(); 338 oc.close(); 339 340 return users; 341 } 342 oc.close(); 343 throw new Exception (ack); 344 } 345 346 public void addAcl(String itemId, AclInfo info) 347 throws Exception 348 { 349 Object [] params = {itemId, info}; 350 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.ADD_ACL, params); 351 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 352 String ack = (String )oc.read(); 353 oc.close(); 354 355 if(!ack.equals("OK")) 356 throw new Exception (ack); 357 } 358 359 public void removeAcl(String itemId, AclInfo info) 360 throws Exception 361 { 362 Object [] params = {itemId, info}; 363 SharedFolderAction action = new SharedFolderAction(SharedFolderAction.REMOVE_ACL, params); 364 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 365 String ack = (String )oc.read(); 366 oc.close(); 367 368 if(!ack.equals("OK")) 369 throw new Exception (ack); 370 } 371 372 public String [] getAccesses() 374 { 375 return new String [] {SharedItem.READ, SharedItem.WRITE}; 376 } 377 378 public String getItemName(String itemId) 379 throws Exception 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 392 private ArrayList getListFromService(SharedFolderAction action) 393 throws IOException, ClassNotFoundException 394 { 395 ArrayList items = null; 396 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 397 398 String ack = oc.readString(); 399 if(ack.equals("OK")) 400 { 401 items = (ArrayList )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 |