Example #1
0
  public int complete(String arg0, int arg1, List arg2) {

    final GricliEnvironment env = Gricli.completionCache.getEnvironment();

    try {
      final List<String> files = (List<String>) env.getVariable("files").get();
      final Set<String> names = new HashSet<String>();
      for (final String file : files) {
        final String filename = FileManager.getFilename(file);
        names.add(filename);
      }

      return new SimpleCompletor(names.toArray(new String[] {})).complete(arg0, arg1, arg2);
    } catch (final GricliRuntimeException e) {
      myLogger.error(e.getLocalizedMessage(), e);
      return -1;
    }
  }
Example #2
0
  public GridFile createGridFile(String path, int recursiveLevels)
      throws RemoteFileSystemException {

    path = FileManager.removeTrailingSlash(path);

    // Thread.dumpStack();
    if (recursiveLevels > 1) {
      throw new RuntimeException("Recursion levels greater than 1 not supported yet");
    }

    final String rightPart = path.substring(BASE.length());
    if (rightPart.contains("//")) {
      GridFile result = null;

      // that means we everything before the // is the fqan and everything
      // after is the path

      final int i = rightPart.indexOf("//");
      String fqanT = rightPart.substring(0, i);
      final String restPath = rightPart.substring(i + 2);
      if (!fqanT.startsWith("/")) {
        fqanT = "/" + fqanT;
      }

      final Set<String> urls = resolveUrls(restPath, fqanT, true);

      if (urls.size() == 0) {
        // TODO not sure what to do
        throw new RuntimeException("No real url found for virtual url.");
      } else if (urls.size() == 1) {
        result = new GridFile(urls.iterator().next(), -1L);
        result.setVirtual(false);
        result.setPath(path);
      } else {
        result = new GridFile(path, -1L);
        result.setVirtual(true);
        result.setPath(path);
      }

      if (recursiveLevels == 0) {
        return result;
      }

      final Set<GridFile> childs = listGroup(fqanT, restPath, true);

      for (final GridFile file : childs) {

        if (file.isInaccessable()) {
          result.addChild(file);
        } else {
          result.addChildren(file.getChildren());
        }
      }

      final Map<String, Set<String>> temp = findDirectChildFqans(fqanT);
      final Set<String> childFqans = temp.keySet();
      for (final String fqan : childFqans) {

        final Set<MountPoint> mps = user.getMountPoints(fqan);
        // we need to remove all volatile mountpoints first, users are
        // not interested in those
        final Iterator<MountPoint> it = mps.iterator();
        while (it.hasNext()) {
          final MountPoint mp = it.next();
          if (mp.isVolatileFileSystem()) {
            it.remove();
          }
        }

        if (mps.size() == 1) {
          final GridFile file = new GridFile(mps.iterator().next());
          file.setName(FileManager.getFilename(fqan));
          final String pathNew =
              (path + "/" + file.getName()).replace("///", "/").replace("//", "/") + "//";
          file.setPath(pathNew);
          file.setVirtual(true);
          file.addSites(temp.get(fqan));
          result.addChild(file);
        } else {
          final GridFile file =
              new GridFile((BASE + fqan).replace("///", "/").replace("//", "/") + "//", fqan);
          final String pathNew =
              (path + "/" + file.getName()).replace("///", "/").replace("//", "/") + "//";
          // String pathNew = file.getUrl();
          file.setPath(pathNew);
          for (final MountPoint mp : mps) {
            // X.p("Add" + mp.getRootUrl());
            file.addUrl(mp.getRootUrl(), GridFile.FILETYPE_MOUNTPOINT_PRIORITY);
          }
          file.setVirtual(true);
          file.addSites(temp.get(fqan));
          result.addChild(file);
        }
      }

      return result;
    }

    final int index = BASE.length();
    final String importantUrlPart = path.substring(index);
    final String[] tokens = StringUtils.split(importantUrlPart, '/');

    GridFile result = null;

    if (tokens.length == 0) {
      // means root of the groupfilesystem

      result = new GridFile(path, -1L);
      result.setVirtual(true);
      result.setPath(path);

      if (recursiveLevels == 0) {
        return result;
      }

      for (final VO vo : new TreeSet<VO>(user.getFqans().values())) {
        final GridFile f = new GridFile(BASE + "/" + vo.getVoName(), -1L);
        f.setVirtual(true);
        f.setPath(path + "/" + vo.getVoName());

        for (final MountPoint mp : user.getMountPoints("/" + vo.getVoName())) {
          f.addSite(mp.getSite());
          f.addUrl(mp.getRootUrl(), GridFile.FILETYPE_MOUNTPOINT_PRIORITY);
        }

        result.addChild(f);
      }

    } else if (tokens.length == 1) {
      // means is root of VO so we need to list potential files on all
      // sites that support this vo
      // and also all child vos

      final String parentfqan = "/" + tokens[0];
      final Set<String> urls = resolveUrls(path, parentfqan, true);

      if (urls.size() == 1) {
        result = new GridFile(urls.iterator().next(), -1L);
        result.setVirtual(false);
        result.setPath(path);
      } else {
        result = new GridFile(path, -1L);
        result.setVirtual(true);
        result.setPath(path);
        for (final String u : urls) {
          result.addUrl(u, GridFile.FILETYPE_MOUNTPOINT_PRIORITY);
        }
      }

      if (recursiveLevels == 0) {
        return result;
      }

      final Map<String, Set<String>> temp = findDirectChildFqans(parentfqan);
      final Set<String> childFqans = temp.keySet();

      for (final String fqan : childFqans) {

        final Set<MountPoint> mps = Sets.newTreeSet(user.getMountPoints(fqan));

        // we need to remove volatile mountpoints
        final Iterator<MountPoint> it = mps.iterator();
        while (it.hasNext()) {
          final MountPoint mp = it.next();
          if (mp.isVolatileFileSystem()) {
            it.remove();
          }
        }

        if (mps.size() == 1) {
          final GridFile file = new GridFile(mps.iterator().next());
          file.setName(FileManager.getFilename(fqan));
          file.setPath(path + "/" + file.getName());
          file.setVirtual(true);
          file.addSites(temp.get(fqan));
          result.addChild(file);
        } else {
          final GridFile file = new GridFile(BASE + fqan, fqan);
          file.setPath(path + "/" + file.getName());
          file.setVirtual(true);
          file.addSites(temp.get(fqan));
          for (final MountPoint mp : mps) {
            file.addUrl(mp.getRootUrl(), GridFile.FILETYPE_MOUNTPOINT_PRIORITY);
          }
          file.addFqan(fqan);
          result.addChild(file);
        }
      }

      final Set<GridFile> files = listGroup("/" + tokens[0], "", true);
      for (final GridFile file : files) {
        if (file.isInaccessable()) {
          result.addChild(file);
        } else {
          result.addChildren(file.getChildren());
        }
      }

    } else {

      String currentUrl = BASE;
      String potentialFqan = "";

      final Set<String> parentUrls = new HashSet<String>();
      final Set<GridFile> children = new TreeSet<GridFile>();

      for (final String token : tokens) {

        currentUrl = currentUrl + "/" + token;
        potentialFqan = potentialFqan + "/" + token;

        if (!user.getFqans().keySet().contains(potentialFqan)) {
          continue;
        }

        final String rest = path.substring(currentUrl.length());

        final Set<String> urls = resolveUrls(rest, potentialFqan, true);
        parentUrls.addAll(urls);

        if (recursiveLevels == 1) {

          final Set<GridFile> files = listGroup(potentialFqan, rest, false);
          for (final GridFile file : files) {

            if (file.isInaccessable()) {
              children.add(file);
            } else {
              children.addAll(file.getChildren());
            }
          }
        }
      }

      if (recursiveLevels == 1) {

        final Map<String, Set<String>> temp = findDirectChildFqans(potentialFqan);
        final Set<String> childFqans = temp.keySet();
        for (final String fqan : childFqans) {
          final Set<MountPoint> mps = user.getMountPoints(fqan);

          // we need to remove volatile mountpoints
          final Iterator<MountPoint> it = mps.iterator();
          while (it.hasNext()) {
            final MountPoint mp = it.next();
            if (mp.isVolatileFileSystem()) {
              it.remove();
            }
          }
          if (mps.size() == 0) {
            continue;
          }
          if (mps.size() == 1) {
            final GridFile file = new GridFile(mps.iterator().next());
            file.setName(FileManager.getFilename(fqan));
            file.setPath(path + "/" + file.getName() + "//");
            file.setVirtual(true);
            file.addSites(temp.get(fqan));
            children.add(file);
          } else {
            final GridFile file = new GridFile(BASE + fqan, fqan);
            file.setPath(path + "/" + file.getName() + "//");
            file.setVirtual(true);
            file.addSites(temp.get(fqan));
            children.add(file);
          }
        }
      }

      if (recursiveLevels == 0) {

        myLogger.debug(
            "Checking whether any of the possible real urls for virtual url " + path + " exist...");
        // test whether files actually exist

        if (parentUrls.size() > 0) {
          final NamedThreadFactory tf = new NamedThreadFactory("realUrlCheckForVfs");
          final ExecutorService executor = Executors.newFixedThreadPool(parentUrls.size(), tf);

          // let's sort using last modified date, so that we can give
          // the
          // last file the highest priority
          final Map<Long, String> temp = Collections.synchronizedMap(new TreeMap<Long, String>());

          for (final String url : parentUrls) {
            final Thread t =
                new Thread() {
                  @Override
                  public void run() {
                    try {
                      final long ts = user.getFileManager().lastModified(url);

                      temp.put(ts, url);
                      myLogger.debug("File exists: " + url);
                      // boolean exists =
                      // user.getFileSystemManager()
                      // .fileExists(url);
                      // if (exists) {
                      // myLogger.debug("File exists: " + url);
                      // temp.add(url);
                      // } else {
                      // myLogger.debug("File does not exit: " +
                      // url);
                      // }
                    } catch (final Exception e) {
                      myLogger.debug(
                          "File does not exit: " + url + " - " + e.getLocalizedMessage());
                    }
                  }
                };
            executor.execute(t);
          }

          executor.shutdown();
          try {
            executor.awaitTermination(60, TimeUnit.SECONDS);
          } catch (final InterruptedException e) {
          }

          if (temp.size() == 1) {
            result = new GridFile(temp.values().iterator().next(), -1L);
            result.setVirtual(false);
            result.setPath(path);
          } else {
            result = new GridFile(path, -1L);
            result.setVirtual(true);
            result.setPath(path);
            int i = 0;
            for (final Long lm : temp.keySet()) {
              result.addUrl(temp.get(lm), GridFile.FILETYPE_FOLDER_PRIORITY + i);
              i = i + 1;
            }
          }

          return result;
        }
      }

      // xxx
      if (parentUrls.size() == 1) {
        final String url = parentUrls.iterator().next();
        try {
          final boolean isFolder = user.getFileManager().isFolder(url);
          if (isFolder) {
            result = new GridFile(url, -1L);
          } else {
            result = user.ls(url, 0);
          }
          result.setVirtual(false);
          result.setPath(path);
        } catch (Exception e) {
          result = new GridFile(url, -1L);
          GridFile error = new GridFile(url, false, e);
          result.addChild(error);
        }

      } else {
        result = new GridFile(path, -1L);
        result.setVirtual(true);
        result.setPath(path);
        for (final String u : parentUrls) {
          result.addUrl(u, GridFile.FILETYPE_FOLDER_PRIORITY);
        }
      }

      result.addChildren(children);
    }

    return result;
  }
Example #3
0
 public void setFastaFile(String value) throws RemoteFileSystemException {
   // System.out.println("SetFastaFile: " + value);
   GridFile file = fm.createGridFile(value);
   setFastaFile(file);
 }
Example #4
0
  public void execute(GricliEnvironment env) throws GricliRuntimeException {

    if ((job_or_filenames == null) || (job_or_filenames.length == 0)) {
      throw new GricliRuntimeException("No jobname and/or file provided.");
    }

    String filename = null;
    String jobname = null;
    if (job_or_filenames.length == 1) {
      filename = job_or_filenames[0];
    } else if (job_or_filenames.length == 2) {
      filename = job_or_filenames[1];
      jobname = job_or_filenames[0];
    } else {
      throw new GricliRuntimeException("Too many arguments.");
    }

    String fileToView = null;
    GrisuJob jobToView = null;

    if (jobname != null) {

      DtoJob job = null;
      for (DtoJob jobTemp : Gricli.completionCache.getCurrentJobs(false)) {
        if (jobTemp.jobname().equals(jobname)) {
          job = jobTemp;
          break;
        }
      }

      if (job != null) {
        try {
          job = env.getServiceInterface().getJob(jobname);
        } catch (NoSuchJobException e) {
          throw new GricliRuntimeException("No job with name \"" + jobname + "\"");
        }
      }

      String jobdir = DtoJob.getProperty(job, Constants.JOBDIRECTORY_KEY);
      jobdir = FileManager.ensureTrailingSlash(jobdir);

      fileToView = jobdir + filename;

    } else {
      DtoJob job = null;
      for (DtoJob jobTemp : Gricli.completionCache.getCurrentJobs(false)) {
        if (jobTemp.jobname().equals(filename)) {
          job = jobTemp;
          break;
        }
      }

      if (job != null) {
        try {
          jobToView = new GrisuJob(env.getServiceInterface(), job);
        } catch (NoSuchJobException e) {
          throw new GricliRuntimeException("No job with name \"" + jobname + "\"");
        }
      } else {
        fileToView = filename;
      }
    }

    if (jobToView == null) {

      final FileManager fm = env.getGrisuRegistry().getFileManager();

      File cacheFile = null;
      try {
        cacheFile = fm.downloadFile(fileToView, false);
        // System.out.println("Cache: " + cacheFile.getAbsolutePath());
      } catch (final FileTransactionException e) {
        if (e.getCause() == null) {
          // means threshold bigger
          env.printError("File bigger than configured download threshold. Not downloading.");
        }
      }

      try {
        for (final String line : Files.readLines(cacheFile, Charsets.UTF_8)) {
          env.printMessage(line);
        }
      } catch (final IOException e) {
        env.printError("Can't read file: " + e.getLocalizedMessage());
      }

    } else {
      // view stdout & stderr

      env.printMessage("");
      env.printMessage("Reading stdout & stderr for job: '" + jobToView.getJobname() + "'...");
      env.printMessage("");
      try {
        env.printMessage("========================================");
        env.printMessage("Stdout:");
        env.printMessage("");
        String stdout = jobToView.getStdOutContent();
        env.printMessage(stdout);
      } catch (Exception e) {
        env.printError("Can't display stdout: " + e.getLocalizedMessage());
      }
      try {
        env.printMessage("");
        env.printMessage("========================================");
        env.printMessage("Stderr:");
        env.printMessage("");
        String stderr = jobToView.getStdErrContent();
        env.printMessage(stderr);
      } catch (Exception e) {
        env.printError("Can't display stderr: " + e.getLocalizedMessage());
      }
      env.printMessage("");
      env.printMessage("========================================");
      env.printMessage("");
    }
  }