Exemple #1
0
 private static void delete(FileObject file, boolean recursive) throws FileSystemException {
   if (file.exists()) {
     if (file.getType() == FileType.FILE) {
       file.delete();
     } else if (file.getType() == FileType.FOLDER) {
       if (file.getChildren().length == 0) {
         file.delete();
       } else if (recursive) {
         file.delete();
       }
     }
   }
 }
  /**
   * Take specified action to either move or delete the processed file, depending on the outcome
   *
   * @param entry the PollTableEntry for the file that has been processed
   * @param fileObject the FileObject representing the file to be moved or deleted
   */
  private void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObject fileObject)
      throws AxisFault {

    String moveToDirectoryURI = null;
    try {
      switch (entry.getLastPollState()) {
        case PollTableEntry.SUCCSESSFUL:
          if (entry.getActionAfterProcess() == PollTableEntry.MOVE) {
            moveToDirectoryURI = entry.getMoveAfterProcess();
          }
          break;

        case PollTableEntry.FAILED:
          if (entry.getActionAfterFailure() == PollTableEntry.MOVE) {
            moveToDirectoryURI = entry.getMoveAfterFailure();
          }
          break;

        default:
          return;
      }

      if (moveToDirectoryURI != null) {
        FileObject moveToDirectory = fsManager.resolveFile(moveToDirectoryURI);
        String prefix;
        if (entry.getMoveTimestampFormat() != null) {
          prefix = entry.getMoveTimestampFormat().format(new Date());
        } else {
          prefix = "";
        }
        FileObject dest = moveToDirectory.resolveFile(prefix + fileObject.getName().getBaseName());
        if (log.isDebugEnabled()) {
          log.debug("Moving to file :" + dest.getName().getURI());
        }
        try {
          fileObject.moveTo(dest);
        } catch (FileSystemException e) {
          handleException("Error moving file : " + fileObject + " to " + moveToDirectoryURI, e);
        }
      } else {
        try {
          if (log.isDebugEnabled()) {
            log.debug("Deleting file :" + fileObject);
          }
          fileObject.close();
          if (!fileObject.delete()) {
            String msg = "Cannot delete file : " + fileObject;
            log.error(msg);
            throw new AxisFault(msg);
          }
        } catch (FileSystemException e) {
          log.error("Error deleting file : " + fileObject, e);
        }
      }

    } catch (FileSystemException e) {
      handleException(
          "Error resolving directory to move after processing : " + moveToDirectoryURI, e);
    }
  }
  public void testPreferencesEvents() throws Exception {
    storage.toFolder(true);
    FileUtil.getConfigRoot().addRecursiveListener(new FileListener());
    pref.addNodeChangeListener(new NodeListener());

    String newPath = "a/b/c";
    String[] paths = newPath.split("/");
    FileObject fo = null;
    FileObject fo0 = null;
    for (int i = 0; i < paths.length; i++) {
      String path = paths[i];
      fo = FileUtil.createFolder((fo == null) ? FileUtil.getConfigRoot() : fo, path);
      if (i == 0) {
        fo0 = fo;
      }
      nodeAddedEvent.await();
      assertEquals("Missing node added event", 0, nodeAddedEvent.getCount());
      nodeAddedEvent = new CountDownLatch(1);

      Preferences pref2 = pref.node(fo.getPath());
      pref2.addNodeChangeListener(new NodeListener());
    }

    FileObject fo1 = FileUtil.createData(fo, "a.properties");
    nodeAddedEvent.await();
    assertEquals("Missing node added event", 0, nodeAddedEvent.getCount());

    nodeRemovedEvent = new CountDownLatch(paths.length + 1);
    fo0.delete();
    nodeRemovedEvent.await();
    assertEquals("Missing node removed event", 0, nodeRemovedEvent.getCount());
  }
 @Override
 protected FileSystem createFS(String... resources) throws IOException {
   for (String s : resources) {
     FileObject fo = FileUtil.getConfigFile(s.replaceAll("/.*", ""));
     if (fo != null) {
       fo.delete();
     }
   }
   FileSystem sfs = FileUtil.getConfigRoot().getFileSystem();
   for (String s : resources) {
     assertNotNull("creating: " + s, FileUtil.createData(sfs.getRoot(), s));
   }
   return sfs;
 }