Exemplo n.º 1
0
  private void initData() {
    try {
      FileSystemManager fsManager = VFS.getManager();
      currentFileObject = fsManager.resolveFile("jar://" + filename);
      textLocation.setText(currentFileObject.getName().getPath());

    } catch (Exception e) {
      e.printStackTrace();
    }
    changeCurrentNode();
  }
Exemplo n.º 2
0
  /** Does a 'cp' command. */
  private void cp(final String[] cmd) throws Exception {
    if (cmd.length < 3) {
      throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);
    if (dest.exists() && dest.getType() == FileType.FOLDER) {
      dest = dest.resolveFile(src.getName().getBaseName());
    }

    dest.copyFrom(src, Selectors.SELECT_ALL);
  }
  private void createFolder(String fUri) throws FileSystemException {

    FileObject fo = fsManager.resolveFile(fUri);
    fo.createFolder();

    logger.debug("Created remote folder: " + fUri);
  }
Exemplo n.º 4
0
  /** Does an 'ls' command. */
  private void ls(final String[] cmd) throws FileSystemException {
    int pos = 1;
    final boolean recursive;
    if (cmd.length > pos && cmd[pos].equals("-R")) {
      recursive = true;
      pos++;
    } else {
      recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
      file = mgr.resolveFile(cwd, cmd[pos]);
    } else {
      file = cwd;
    }

    if (file.getType() == FileType.FOLDER) {
      // List the contents
      System.out.println("Contents of " + file.getName());
      listChildren(file, recursive, "");
    } else {
      // Stat the file
      System.out.println(file.getName());
      final FileContent content = file.getContent();
      System.out.println("Size: " + content.getSize() + " bytes.");
      final DateFormat dateFormat =
          DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
      final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
      System.out.println("Last modified: " + lastMod);
    }
  }
  /**
   * @param job
   * @param inputFolder
   * @return
   * @throws FileSystemException
   */
  protected boolean pushData(Job job, String localInputFolderPath) throws FileSystemException {

    String push_URL = job.getGenericInformations().get(GENERIC_INFO_PUSH_URL_PROPERTY_NAME);

    if ((push_URL == null) || (push_URL.trim().equals(""))) {
      return false;
    } // push inputData

    // TODO - if the copy fails, try to remove the files from the remote
    // folder before throwing an exception
    FileObject remoteFolder = fsManager.resolveFile(push_URL);
    FileObject localfolder = fsManager.resolveFile(localInputFolderPath);
    logger.debug("Pushing files from " + localfolder + " to " + remoteFolder);

    // create the selector
    DSFileSelector fileSelector = new DSFileSelector();

    TaskFlowJob tfj = (TaskFlowJob) job;
    for (Task t : tfj.getTasks()) {
      List<InputSelector> inputFileSelectors = t.getInputFilesList();
      for (InputSelector is : inputFileSelectors) {
        org.ow2.proactive.scheduler.common.task.dataspaces.FileSelector fs = is.getInputFiles();
        if (fs.getIncludes() != null) fileSelector.addIncludes(Arrays.asList(fs.getIncludes()));

        if (fs.getExcludes() != null) fileSelector.addExcludes(Arrays.asList(fs.getExcludes()));
      }
    }

    // We need to check if a pattern exist in both includes and excludes.
    // This may happen if a task one defines, for instance "*.txt" as includes
    // and a task two defines "*.txt" as excludes. In this case we should remove it from the
    // fileSelector's excludes.

    Set<String> includes = fileSelector.getIncludes();
    Set<String> excludes = fileSelector.getExcludes();

    Set<String> intersection = new HashSet<String>(includes);
    intersection.retainAll(excludes);
    excludes.removeAll(intersection);
    fileSelector.setExcludes(excludes);

    remoteFolder.copyFrom(localfolder, fileSelector);

    logger.debug("Finished push operation from " + localfolder + " to " + remoteFolder);
    return true;
  }
Exemplo n.º 6
0
  /** Does an 'rm' command. */
  private void rm(final String[] cmd) throws Exception {
    if (cmd.length < 2) {
      throw new Exception("USAGE: rm <path>");
    }

    final FileObject file = mgr.resolveFile(cwd, cmd[1]);
    file.delete(Selectors.SELECT_SELF);
  }
Exemplo n.º 7
0
 private BlobStoreShell(String uri) throws FileSystemException {
   remoteMgr = new DefaultFileSystemManager();
   remoteMgr.setFilesCache(new SoftRefFilesCache());
   remoteMgr.addProvider("blobstore", new BlobStoreFileProvider());
   remoteMgr.init();
   remoteCwd = remoteMgr.resolveFile(checkNotNull(uri, "uri"));
   mgr = VFS.getManager();
   cwd = mgr.resolveFile(System.getProperty("user.dir"));
   reader = new BufferedReader(new InputStreamReader(System.in));
 }
  /** Returns the base folder for tests. */
  public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    final FileObject baseFolder = config.getBaseTestFolder(manager);

    // Create an empty file system, then link in the base folder
    final FileSystem newFs = manager.createVirtualFileSystem("vfs:").getFileSystem();
    final String junctionPoint = "/some/dir";
    newFs.addJunction(junctionPoint, baseFolder);

    return newFs.resolveFile(junctionPoint);
  }
Exemplo n.º 9
0
 /** Does a 'touch' command. */
 private void touch(final String[] cmd) throws Exception {
   if (cmd.length < 2) {
     throw new Exception("USAGE: touch <path>");
   }
   final FileObject file = mgr.resolveFile(cwd, cmd[1]);
   if (!file.exists()) {
     file.createFile();
   }
   file.getContent().setLastModifiedTime(System.currentTimeMillis());
 }
 public void testDefault() throws Exception {
   Heuristics heuristics = RuleBasedHeuristics.getDefaultInstance();
   FileSystemManager fsm = VFS.getManager();
   FileObject root =
       fsm.resolveFile(
               RuleBasedHeuristicsTest.class.getResource("/heuristics/.root").toExternalForm())
           .getParent();
   for (Reason reason : Reason.values()) {
     for (FileObject file : root.resolveFile(reason.toString().toLowerCase()).getChildren()) {
       InputStream is = file.getContent().getInputStream();
       DeliveryStatus ds = new DeliveryStatus(is);
       is.close();
       assertEquals(
           "Reason for " + file,
           reason,
           heuristics.getReason(ds.getPerRecipientParts()[0].getDiagnostic()));
     }
   }
 }
Exemplo n.º 11
0
  /** Does a 'cat' command. */
  private void cat(final String[] cmd) throws Exception {
    if (cmd.length < 2) {
      throw new Exception("USAGE: cat <path>");
    }

    // Locate the file
    final FileObject file = mgr.resolveFile(cwd, cmd[1]);

    // Dump the contents to System.out
    FileUtil.writeContent(file, System.out);
    System.out.println();
  }
Exemplo n.º 12
0
  protected void setUp() throws Exception {
    String tempDir = System.getProperty("java.io.tmpdir");
    FileSystemManager fsm = VFS.getManager();

    rootDir = fsm.resolveFile(tempDir + "/testfsr");
    rootDir.createFolder();
    tempFile = createFile(rootDir, "fsp.html");

    subdir1 = fsm.resolveFile(rootDir, "SubDir1");
    subdir1.createFolder();

    createFile(subdir1, "toto.html");
    createFile(subdir1, "titi.HTML");
    subdir2 = fsm.resolveFile(rootDir, "SubDir2");
    subdir2.createFolder();

    createFile(subdir2, "toto.html");
    subsubdir = fsm.resolveFile(subdir2, "subsubdir");
    subsubdir.createFolder();

    createFile(subsubdir, "momo.html");
    createFile(subsubdir, "mimi.html");
  }
  public void setPath(String path) {

    FileSystemManager fileSystemManager;
    try {
      fileSystemManager = VFS.getManager();

      FileObject fileObject;
      fileObject = fileSystemManager.resolveFile(path);
      if (fileObject == null) {
        throw new IOException("File cannot be resolved: " + path);
      }
      if (!fileObject.exists()) {
        throw new IOException("File does not exist: " + path);
      }
      repoURL = fileObject.getURL();
      if (repoURL == null) {
        throw new Exception("Cannot load connection repository from path: " + path);
      } else {
        load();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 14
0
  /**
   * Does a 'cd' command. If the taget directory does not exist, a message is printed to <code>
   * System.err</code>.
   */
  private void cd(final String[] cmd) throws Exception {
    final String path;
    if (cmd.length > 1) {
      path = cmd[1];
    } else {
      path = System.getProperty("user.home");
    }

    // Locate and validate the folder
    FileObject tmp = mgr.resolveFile(cwd, path);
    if (tmp.exists()) {
      cwd = tmp;
    } else {
      System.out.println("Folder does not exist: " + tmp.getName());
    }
    System.out.println("Current folder is " + cwd.getName());
  }
Exemplo n.º 15
0
 /** Returns the base folder for read tests. */
 public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
   final File tarFile = AbstractVfsTestCase.getTestResource("test.tar");
   final String uri = "tar:" + tarFile.getAbsolutePath() + "!/";
   return manager.resolveFile(uri);
 }
Exemplo n.º 16
0
 private Shell() throws FileSystemException {
   mgr = VFS.getManager();
   cwd = mgr.resolveFile(System.getProperty("user.dir"));
   reader = new BufferedReader(new InputStreamReader(System.in));
 }
  /**
   * Retrieves the output files produced by the job having the id given as argument. If the transfer
   * finishes successfully it deletes the temporary folders (at push_url and pull_url location) and
   * send notification to the listeners. Otherwise it notifies the listeners of the failure.
   *
   * <p>The transfer data operation is executed by a fixed thread pool executor (see {@link
   * DataTransferProcessor})
   *
   * @param awaitedjob
   * @return
   * @throws FileSystemException
   */
  protected void pullData(AwaitedJob awaitedjob) {
    String localOutFolderPath = awaitedjob.getLocalOutputFolder();
    if (localOutFolderPath == null) {
      logger.warn(
          "The job "
              + awaitedjob.getJobId()
              + " does not define an output folder on local machine. No output data will be retrieved");
      return;
    }

    String jobId = awaitedjob.getJobId();
    String pull_URL = awaitedjob.getPullURL();
    String pushUrl = awaitedjob.getPushURL();

    FileObject remotePullFolder = null;
    FileObject remotePushFolder = null;
    FileObject localfolder = null;

    Set<FileObject> foldersToDelete = new HashSet<FileObject>();

    try {
      remotePullFolder = fsManager.resolveFile(pull_URL);
      remotePushFolder = fsManager.resolveFile(pushUrl);
      localfolder = fsManager.resolveFile(localOutFolderPath);
    } catch (Exception e) {
      logger.error("Could not retrieve data for job " + jobId, e);
      logger.info(
          "Job  "
              + jobId
              + " will be removed from the known job list. The system will not attempt again to retrieve data for this job. You couyld try to manually copy the data from the location  "
              + pull_URL);
      removeAwaitedJob(jobId);
      return;
    }

    try {
      foldersToDelete.add(remotePullFolder.getParent());
      if (!remotePullFolder.getParent().equals(remotePushFolder.getParent()))
        foldersToDelete.add(remotePushFolder.getParent());
    } catch (FileSystemException e) {
      logger.warn(
          "Data in folders "
              + pull_URL
              + " and "
              + pushUrl
              + " cannot be deleted due to an unexpected error ",
          e);
      e.printStackTrace();
    }

    FileSelector fileSelector = Selectors.SELECT_ALL;
    // The code bellow has been commented:
    // We do not need to build a file selector because the files in the temporary folder
    // have been copied by the data space layer which already used a FastFileSelector
    // configured with includes and excludes patterns

    //         DSFileSelector fileSelector  = new DSFileSelector();
    //          try{
    //            JobState jobstate = uischeduler.getJobState(jobId);
    //            for (TaskState ts : jobstate.getTasks() )
    //     	   {
    //         	 List<OutputSelector> of =  ts.getOutputFilesList();
    //         	 for (OutputSelector outputSelector : of) {
    //         		 org.ow2.proactive.scheduler.common.task.dataspaces.FileSelector fs =
    // outputSelector.getOutputFiles();
    //
    //         		fileSelector.addIncludes(fs.getIncludes());
    //         		fileSelector.addExcludes(fs.getExcludes());
    //     		}
    //     	   }
    //          }catch (Exception e)
    //     	   {
    //     		 logger_util.error("An exception occured while computing which output files to download
    // for job "+ jobId+". All available files will be downloaded for this job");
    //         	 e.printStackTrace();
    //     	   }

    DataTransferProcessor dtp =
        new DataTransferProcessor(
            remotePullFolder, localfolder, jobId, foldersToDelete, fileSelector);
    tpe.submit(dtp);
  }
Exemplo n.º 18
0
  public String getFileContent(String fileName) throws Exception {
    FileSystemManager fsManager = null;
    FileObject sftpFile = null;
    FileObject src = null; // used for cleanup in release()
    String fileContent = null;

    try {
      System.out.println("SFTP download");
      FileSystemOptions opts = null;
      // app.initialize();
      try {
        fsManager = VFS.getManager();
      } catch (FileSystemException ex) {
        throw new RuntimeException("failed to get fsManager from VFS", ex);
      }

      UserAuthenticator auth = new StaticUserAuthenticator(null, this.user, this.password);
      opts = new FileSystemOptions();
      try {
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
      } catch (FileSystemException ex) {
        throw new RuntimeException("setUserAuthenticator failed", ex);
      }

      // app.process();
      String startPath =
          "sftp://"
              + this.user
              + ":"
              + this.password
              + "@"
              + this.host
              + ":22"
              + this.remoteDir
              + fileName;

      // Set starting path on remote SFTP server.
      try {
        sftpFile = fsManager.resolveFile(startPath, opts);

        System.out.println("SFTP connection successfully established to " + startPath);
      } catch (FileSystemException ex) {
        throw new RuntimeException("SFTP error parsing path " + this.remoteDir, ex);
      }

      if (sftpFile.exists()) {
        FileContent fc = sftpFile.getContent();
        StringWriter writer = new StringWriter();
        IOUtils.copy(fc.getInputStream(), writer, encoding);
        String theString = writer.toString();
        //	         	System.out.println(theString);
        fileContent = theString;
      }

    } finally {
      // app.release();
      /** Release system resources, close connection to the filesystem. */
      try {
        FileSystem fs = null;
        if (sftpFile != null) {
          fs = sftpFile.getFileSystem(); // This works even if the src is closed.
          fsManager.closeFileSystem(fs);
        } // TODO if sftpFile != null
      } catch (Exception e) {
        System.out.println(e);
      }
    }
    return fileContent;
  }
Exemplo n.º 19
0
 /** Returns the base folder for tests. */
 @Override
 public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
   final String uri = System.getProperty(TEST_URI);
   return manager.resolveFile(uri);
 }