コード例 #1
0
  public GridFile getFolderListing(String url, int recursiveLevels)
      throws RemoteFileSystemException {

    if (url.equals(ServiceInterface.VIRTUAL_GRID_PROTOCOL_NAME + "://")) {
      final GridFile root = new GridFile(url, -1L);
      root.setVirtual(true);
      root.setPath(url);
      if (recursiveLevels == 0) {
        return root;
      }
      for (final String key : plugins.keySet()) {
        final GridFile vfs =
            new GridFile(ServiceInterface.VIRTUAL_GRID_PROTOCOL_NAME + "://" + key, -1L);
        vfs.setVirtual(true);
        vfs.setPath(url + vfs.getName());
        // vfs.setName(StringUtils.capitalize(key));
        vfs.setName(key);
        root.addChild(vfs);
      }
      return root;
    }
    try {
      return getPlugin(url).createGridFile(url, recursiveLevels);
    } catch (final InvalidPathException e) {
      throw new RemoteFileSystemException(e);
    }
  }
コード例 #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;
  }