1 19 package org.openharmonise.localfilesystem; 20 21 import java.io.*; 22 import java.net.*; 23 import java.util.*; 24 25 import org.openharmonise.vfs.*; 26 import org.openharmonise.vfs.authentication.*; 27 import org.openharmonise.vfs.metadata.*; 28 import org.openharmonise.vfs.search.*; 29 import org.openharmonise.vfs.status.*; 30 31 32 36 public class LocalFileSystem extends AbstractVirtualFileSystem { 37 38 private VirtualFileCache m_cache = null; 39 40 private VirtualFileSystemView m_vfView = null; 41 42 46 public LocalFileSystem(URI uri) { 47 super(uri); 48 this.setup(); 49 } 50 51 56 public LocalFileSystem(URI uri, AuthInfo authInfo) { 57 super(uri, authInfo); 58 this.setup(); 59 } 60 61 66 public LocalFileSystem( 67 URI uri, 68 AbstractAuthenticationStore authStore) { 69 super(uri, authStore); 70 this.setup(); 71 } 72 73 private void setup() { 74 this.m_sInitialPath = this.m_sInitialPath.substring(1, this.m_sInitialPath.length()); 75 76 this.m_sInitialPath = this.m_sInitialPath.replace('/', '\\'); 77 this.m_cache = new VirtualFileCache(); 78 } 79 80 83 public List getOptions() { 84 return null; 85 } 86 87 90 public ResourceStatusWrapper getVirtualFile(String sFullPath) { 91 VirtualFile vfFile = null; 92 93 String sRealPath = this.getInitialPath() + sFullPath; 94 sRealPath = sRealPath.replace('/', File.separatorChar); 95 96 if (this.m_cache.hasFile(sFullPath)) { 97 vfFile = this.m_cache.getFile(sFullPath); 98 } else { 99 vfFile = new VirtualFile(sFullPath); 100 vfFile.setVFS(this); 101 File fRealFile = new File(sRealPath); 102 this.populateVirtualFile(fRealFile, vfFile); 103 this.m_cache.addFile(vfFile); 104 } 105 106 if (vfFile == null) { 107 this.fireErrorEvent( 108 "FileNotFound", 109 "The file " + sFullPath + " cannot be found."); 110 } 111 112 return new ResourceStatusWrapper(vfFile, new VFSStatus()); 113 } 114 115 private void populateVirtualFile(File fRealFile, VirtualFile vfFile) { 116 vfFile.setIsDirectory(fRealFile.isDirectory()); 117 118 File[] children = fRealFile.listFiles(); 119 if(children!=null) { 120 for (int i = 0; i < children.length; i++) { 121 vfFile.addChild( 122 vfFile.getFullPath() 123 + "/" 124 + children[i].getName()); 125 } 126 } 127 this.setFileChildrenPopulated(vfFile, true); 128 129 this.setFileMetadataPopulated(vfFile, true); 130 } 131 132 135 public ResourceStatusWrapper addVirtualFile(String sPath, VirtualFile content) { 136 VirtualFile vfFile = null; 137 138 String sFullPath = 139 sPath + "/" + content.getFileName(); 140 141 String sRealPath = 142 this.getInitialPath() 143 + sPath 144 + "/" 145 + content.getFileName(); 146 sRealPath = sRealPath.replace('/', File.separatorChar); 147 148 if (this.m_cache.hasFile(sFullPath)) { 149 } else { 151 File fRealFile = new File(sRealPath); 152 try { 153 FileOutputStream fos = new FileOutputStream(fRealFile); 154 fos.write(content.getContent()); 155 } catch (FileNotFoundException e) { 156 this.fireErrorEvent( 157 "FileNotAdded", 158 "The file " 159 + sFullPath 160 + " cannot be added here, this might be because a file already exists here with the same name."); 161 e.printStackTrace(); 162 } catch (IOException e) { 163 this.fireErrorEvent( 164 "FileNotAdded", 165 "The file " 166 + sFullPath 167 + " cannot be added here, this might be because a file already exists here with the same name."); 168 e.printStackTrace(); 169 } 170 } 171 172 if (vfFile == null) { 173 this.fireErrorEvent( 174 "FileNotAdded", 175 "The file " 176 + sFullPath 177 + " cannot be added here, this might be because a file already exists here with the same name."); 178 } 179 180 return new ResourceStatusWrapper(vfFile, new VFSStatus()); 181 } 182 183 186 public StatusData moveVirtualFile(String sFromFullPath, String sToFullPath) { 187 VFSStatus retnStatus = new VFSStatus(); 188 189 boolean bError = false; 190 191 String sRealFromPath = this.getInitialPath() + sFromFullPath; 192 sRealFromPath = sRealFromPath.replace('/', File.separatorChar); 193 String sRealToPath = this.getInitialPath() + sToFullPath; 194 sRealToPath = sRealToPath.replace('/', File.separatorChar); 195 196 VirtualFile vfFromFile = this.getVirtualFile(sFromFullPath).getResource(); 197 if( !vfFromFile.isDirectory() ) { 198 byte[] content = vfFromFile.getContent(); 199 File from = new File(sRealFromPath); 200 File to = new File(sRealToPath); 201 202 FileInputStream fi = null; 203 FileOutputStream fo = null; 204 205 try { 206 Long lLength = new Long (from.length()); 207 byte[] bytes = new byte[lLength.intValue()]; 208 fi = new FileInputStream(from); 209 fo = new FileOutputStream(to); 210 211 fi.read(bytes); 212 fo.write(bytes); 213 214 } catch (Exception e) { 215 e.printStackTrace(System.out); 216 this.fireErrorEvent("FileNotMoved", "The original file " + sFromFullPath + " cannot be removed."); 217 bError = true; 218 } finally { 219 try { 220 fo.close(); 221 fi.close(); 222 } catch (IOException ioe) { 223 this.fireErrorEvent("FileNotMoved", "The original file " + sFromFullPath + " cannot be removed."); 224 bError = true; 225 ioe.printStackTrace(System.out); 226 } 227 } 228 229 if( !bError ) { 230 from.delete(); 231 } 232 } 233 234 if(bError) { 235 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 236 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 237 } 238 239 return retnStatus; 240 } 241 242 245 public StatusData copyVirtualFile(String sFromFullPath, String sToFullPath) { 246 VFSStatus retnStatus = new VFSStatus(); 247 248 boolean bError = false; 249 250 String sRealFromPath = this.getInitialPath() + sFromFullPath; 251 sRealFromPath = sRealFromPath.replace('/', File.separatorChar); 252 String sRealToPath = this.getInitialPath() + sToFullPath; 253 sRealToPath = sRealToPath.replace('/', File.separatorChar); 254 255 VirtualFile vfFromFile = this.getVirtualFile(sFromFullPath).getResource(); 256 if( !vfFromFile.isDirectory() ) { 257 byte[] content = vfFromFile.getContent(); 258 File from = new File(sRealFromPath); 259 File to = new File(sRealToPath); 260 261 FileInputStream fi = null; 262 FileOutputStream fo = null; 263 264 try { 265 Long lLength = new Long (from.length()); 266 byte[] bytes = new byte[lLength.intValue()]; 267 fi = new FileInputStream(from); 268 fo = new FileOutputStream(to); 269 270 fi.read(bytes); 271 fo.write(bytes); 272 273 } catch (Exception e) { 274 e.printStackTrace(System.out); 275 this.fireErrorEvent("FileNotCopied", "The file " + sFromFullPath + " cannot be copied here, this might be because a file already exists here with the same name."); 276 bError = true; 277 } finally { 278 try { 279 fo.close(); 280 fi.close(); 281 } catch (IOException ioe) { 282 this.fireErrorEvent("FileNotCopied", "The file " + sFromFullPath + " cannot be copied here, this might be because a file already exists here with the same name."); 283 bError = true; 284 ioe.printStackTrace(System.out); 285 } 286 } 287 } 288 289 if(bError) { 290 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 291 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 292 } 293 294 return retnStatus; 295 } 296 297 300 public StatusData deleteVirtualFile(String sFullPath) { 301 VFSStatus retnStatus = new VFSStatus(); 302 303 boolean bError = false; 304 305 String sRealPath = this.getInitialPath() + sFullPath; 306 sRealPath = sRealPath.replace('/', File.separatorChar); 307 308 File fRealFile = new File(sRealPath); 309 310 bError = !fRealFile.delete(); 311 312 if(bError) { 313 this.fireErrorEvent("FileNotDeleted", "The file " + sFullPath + " cannot be deleted."); 314 } 315 316 if(bError) { 317 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 318 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 319 } 320 321 return retnStatus; 322 } 323 324 327 public StatusData lockVirtualFile(String sFullPath) { 328 VFSStatus retnStatus = new VFSStatus(); 329 330 String sRealPath = this.getInitialPath() + sFullPath; 331 sRealPath = sRealPath.replace('/', File.separatorChar); 332 this.fireErrorEvent("FileNotLocked", "This server does not support the locking of files."); 333 334 335 return retnStatus; 336 } 337 338 341 public StatusData unlockVirtualFile(String sFullPath) { 342 VFSStatus retnStatus = new VFSStatus(); 343 344 String sRealPath = this.getInitialPath() + sFullPath; 345 sRealPath = sRealPath.replace('/', File.separatorChar); 346 this.fireErrorEvent("FileNotUnlocked", "This server does not support the locking of files."); 347 348 return retnStatus; 349 } 350 351 354 public StatusData createVirtualDirectory(String sFullPath) { 355 VFSStatus retnStatus = new VFSStatus(); 356 357 boolean bError = false; 358 359 String sRealPath = this.getInitialPath() + sFullPath; 360 sRealPath = sRealPath.replace('/', File.separatorChar); 361 362 File fRealDir = new File(sRealPath); 363 364 bError = fRealDir.mkdirs(); 365 366 if(!bError) { 367 this.fireErrorEvent("DirectoryNotCreated", "The directory " + sFullPath + " cannot be created."); 368 } 369 370 if(bError) { 371 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 372 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 373 } 374 375 return retnStatus; 376 } 377 378 381 public StatusData createShortcut(String sFullPath, String sToFullPath) { 382 VFSStatus retnStatus = new VFSStatus(); 383 384 String sRealPath = this.getInitialPath() + sFullPath; 385 sRealPath = sRealPath.replace('/', File.separatorChar); 386 String sRealToPath = this.getInitialPath() + sToFullPath; 387 sRealToPath = sRealToPath.replace('/', File.separatorChar); 388 this.fireErrorEvent("ShortcutNotCreted", "This server does not support shortcuts."); 389 390 return retnStatus; 391 } 392 393 396 public ResourceListStatusWrapper search(Query query) { 397 return null; 398 } 399 400 403 public VirtualFileSystemView getVirtualFileSystemView() { 404 if( this.m_vfView==null ) { 405 this.m_vfView = new LocalFileSystemView(); 406 } 407 408 return this.m_vfView; 409 } 410 411 414 public byte[] getVirtualFileContent(String sFullPath) { 415 boolean bError = false; 416 417 String sRealPath = this.getInitialPath() + sFullPath; 418 sRealPath = sRealPath.replace('/', File.separatorChar); 419 420 byte[] content = null; 421 422 File from = new File(sRealPath); 423 FileInputStream fi = null; 424 425 try { 426 Long lLength = new Long (from.length()); 427 content = new byte[lLength.intValue()]; 428 fi = new FileInputStream(from); 429 430 fi.read(content); 431 this.setFileContentPopulated( this.m_cache.getFile(sFullPath), true ); 432 433 } catch (Exception e) { 434 e.printStackTrace(System.out); 435 this.fireErrorEvent("ContentNotRetrieved", "The content for " + sFullPath + " could not be retrieved."); 436 bError = true; 437 } finally { 438 try { 439 fi.close(); 440 } catch (IOException ioe) { 441 this.fireErrorEvent("ContentNotRetrieved", "The content for " + sFullPath + " could not be retrieved."); 442 bError = true; 443 ioe.printStackTrace(System.out); 444 } 445 } 446 447 return content; 448 } 449 450 453 protected void fullyPopulateFileMetadata(VirtualFile vfFile) { 454 } 456 457 460 protected void fullyPopulateFileChildren(VirtualFile vfFile) { 461 } 463 464 467 public StatusData synchroniseFile(VirtualFile vfFile) { 468 VFSStatus retnStatus = new VFSStatus(); 469 470 boolean bError = false; 471 472 if(vfFile.isChanged()) { 473 String sFullPath = vfFile.getFullPath(); 474 475 String sRealPath = this.getInitialPath() + sFullPath; 476 sRealPath = sRealPath.replace('/', File.separatorChar); 477 478 FileOutputStream fos = null; 479 480 try { 481 fos = new FileOutputStream(sRealPath); 482 fos.write(vfFile.getContent()); 483 } catch (FileNotFoundException e) { 484 bError = true; 485 e.printStackTrace(); 486 } catch (IOException e) { 487 bError = true; 488 e.printStackTrace(); 489 } finally { 490 try { 491 fos.close(); 492 } catch (IOException e1) { 493 e1.printStackTrace(); 494 } 495 } 496 } 497 498 if(bError) { 499 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 500 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 501 } 502 503 return retnStatus; 504 } 505 506 509 public StatusData synchroniseAllFiles() { 510 VFSStatus retnStatus = new VFSStatus(); 511 512 boolean bError = false; 513 514 Iterator itor = this.m_cache.getPaths().iterator(); 515 while(itor.hasNext()) { 516 String sFullPath = (String )itor.next(); 517 VirtualFile vfFile = this.m_cache.getFile(sFullPath); 518 if(vfFile.isChanged()) { 519 StatusData status = this.synchroniseFile(vfFile); 520 retnStatus.addStatusData(status); 521 if( !status.isOK() ) { 522 bError = true; 523 } 524 } 525 } 526 527 if(bError) { 528 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 529 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 530 } 531 532 return retnStatus; 533 } 534 535 538 public boolean exists(String sFullPath) { 539 boolean bExists = true; 540 541 String sRealPath = this.getInitialPath() + sFullPath; 542 sRealPath = sRealPath.replace('/', File.separatorChar); 543 544 File file = new File(sRealPath); 545 bExists = file.exists(); 546 547 return bExists; 548 } 549 550 553 protected void refreshChildren(VirtualFile vfFile) { 554 } 555 556 559 public StatusData orderVirtualFileChildren(List aPaths, VirtualFile vfDir) { 560 VFSStatus retnStatus = new VFSStatus(); 561 return retnStatus; 562 } 563 564 567 public ValueInstance getNewValueInstance(PropertyInstance propInst) { 568 return null; 569 } 570 571 574 public boolean rejectAllChanges() { 575 return false; 576 } 577 578 581 protected void fullyPopulateFileAllowedMethods(VirtualFile vfFile) { 582 } 583 584 587 public String currentUserResourcePath(AuthInfo authInfo) { 588 return null; 589 } 590 591 594 public List getChangedVirtualFiles() { 595 return new ArrayList(); 596 } 597 598 601 public VirtualFile getPropertyVirtualFile(String sPropPath) { 602 return null; 603 } 604 605 } 606
| Popular Tags
|