@Override
  public void download(String remote, Path local, Collection<Option> options) throws IOException {
    local = ValidateUtils.checkNotNull(local, "Invalid argument local: %s", local);
    remote = ValidateUtils.checkNotNullAndNotEmpty(remote, "Invalid argument remote: %s", remote);

    LinkOption[] opts = IoUtils.getLinkOptions(false);
    if (Files.isDirectory(local, opts)) {
      options = addTargetIsDirectory(options);
    }

    if (options.contains(Option.TargetIsDirectory)) {
      Boolean status = IoUtils.checkFileExists(local, opts);
      if (status == null) {
        throw new SshException("Target directory " + local.toString() + " is probaly inaccesible");
      }

      if (!status.booleanValue()) {
        throw new SshException("Target directory " + local.toString() + " does not exist");
      }

      if (!Files.isDirectory(local, opts)) {
        throw new SshException("Target directory " + local.toString() + " is not a directory");
      }
    }

    download(remote, local.getFileSystem(), local, options);
  }
  /**
   * Loads up a set of {@link IridaWorkflow}s from the given directory.
   *
   * @param workflowDirectory The directory containing the different workflow implementations and
   *     files.
   * @return A set of {@link IridaWorkflow}s for all implementations.
   * @throws IOException If there was an issue reading one of the workflow files.
   * @throws IridaWorkflowLoadException If there was an issue when loading up the workflows.
   */
  public Set<IridaWorkflow> loadAllWorkflowImplementations(Path workflowDirectory)
      throws IOException, IridaWorkflowLoadException {
    checkNotNull(workflowDirectory, "workflowDirectory is null");
    checkArgument(Files.isDirectory(workflowDirectory), "workflowDirectory is not a directory");

    Set<IridaWorkflow> workflowImplementations = new HashSet<>();

    DirectoryStream<Path> stream = Files.newDirectoryStream(workflowDirectory);

    for (Path implementationDirectory : stream) {
      if (!Files.isDirectory(implementationDirectory)) {
        logger.warn(
            "Workflow directory "
                + workflowDirectory
                + " contains a file "
                + implementationDirectory
                + " that is not a proper workflow directory");
      } else {
        IridaWorkflow iridaWorkflow = loadIridaWorkflowFromDirectory(implementationDirectory);

        workflowImplementations.add(iridaWorkflow);
      }
    }

    return workflowImplementations;
  }
  /**
   * Collects the files from given directory if it exists. Files will be sorted by name.
   *
   * @param migrationPath Path
   * @return Sorted files
   * @throws IOException If directory stream fails
   */
  private List<Path> getMigrationFiles(Path migrationPath) throws IOException {
    if ((null == migrationPath)
        || !Files.exists(migrationPath)
        || !Files.isDirectory(migrationPath)) {
      return Collections.emptyList();
    }

    // get directory stream and add all files
    DirectoryStream<Path> directoryStream = Files.newDirectoryStream(migrationPath);
    List<Path> migrationsFiles = new ArrayList<>();
    for (Path migrationFile : directoryStream) {
      if (!Files.isDirectory(migrationFile)) {
        migrationsFiles.add(migrationFile);
      }
    }
    // sort by name
    Collections.sort(
        migrationsFiles,
        new Comparator<Path>() {
          @Override
          public int compare(Path o1, Path o2) {
            return o1.getFileName().toString().compareTo(o2.getFileName().toString());
          }
        });
    return migrationsFiles;
  }
 private void initHomeDir() throws IOException {
   String[] filesToCheck = new String[] {"logs", "tmp", "log4j.xml", "templates"};
   if (!Files.exists(config.getHomeDir())) {
     Files.createDirectories(config.getHomeDir());
   }
   if (Files.exists(config.getHomeDir()) && Files.isDirectory(config.getHomeDir())) {
     for (String fileToCheck : filesToCheck) {
       Path sourceFile = config.getResourceFetcher().getFile(fileToCheck);
       if (sourceFile != null && Files.exists(sourceFile)) {
         Path destFile = config.getHomeDir().resolve(fileToCheck);
         if (!Files.exists(destFile)) {
           if (Files.isDirectory(sourceFile)) {
             Files.createDirectories(destFile);
             for (Path f : PathUtils.list(sourceFile)) {
               if (Files.isDirectory(f)) {
                 Path destDir2 = destFile.resolve(f.getFileName().toString());
                 for (Path x : PathUtils.list(f)) {
                   FileUtils.copyFile(
                       x.toFile(), destDir2.resolve(x.getFileName().toString()).toFile());
                 }
               } else if (Files.isDirectory(f)) {
                 FileUtils.copyFile(
                     f.toFile(), destFile.resolve(f.getFileName().toString()).toFile());
               }
             }
           } else {
             FileUtils.copyFile(sourceFile.toFile(), destFile.toFile());
           }
         }
       }
     }
   }
 }
 @Override
 public Stream<Entry> entries() {
   List<Entry> entries = new ArrayList<>();
   try {
     /*
      * This code should be revisited to avoid buffering of the entries.
      * 1) Do we really need sorting classes? This force buffering of entries.
      *    libs, cmds and configs are not sorted.
      * 2) I/O streams should be concatenated instead of buffering into
      *    entries list.
      * 3) Close I/O streams in a close handler.
      */
     if (classes != null) {
       try (Stream<Path> stream = Files.walk(classes)) {
         entries.addAll(
             stream
                 .filter(
                     p ->
                         !Files.isDirectory(p)
                             && !classes.relativize(p).toString().startsWith("_the.")
                             && !classes.relativize(p).toString().equals("javac_state"))
                 .sorted()
                 .map(p -> toEntry(p, classes, EntryType.CLASS_OR_RESOURCE))
                 .collect(Collectors.toList()));
       }
     }
     if (cmds != null) {
       try (Stream<Path> stream = Files.walk(cmds)) {
         entries.addAll(
             stream
                 .filter(p -> !Files.isDirectory(p))
                 .map(p -> toEntry(p, cmds, EntryType.NATIVE_CMD))
                 .collect(Collectors.toList()));
       }
     }
     if (libs != null) {
       try (Stream<Path> stream = Files.walk(libs)) {
         entries.addAll(
             stream
                 .filter(p -> !Files.isDirectory(p))
                 .map(p -> toEntry(p, libs, EntryType.NATIVE_LIB))
                 .collect(Collectors.toList()));
       }
     }
     if (configs != null) {
       try (Stream<Path> stream = Files.walk(configs)) {
         entries.addAll(
             stream
                 .filter(p -> !Files.isDirectory(p))
                 .map(p -> toEntry(p, configs, EntryType.CONFIG))
                 .collect(Collectors.toList()));
       }
     }
   } catch (IOException ioe) {
     throw new UncheckedIOException(ioe);
   }
   return entries.stream();
 }
Exemple #6
0
 private void restoreBackup(Path copy, Path original) throws IOException {
   if (Files.exists(copy) && Files.isDirectory(copy)) {
     FileUtils.deleteQuietly(original.toFile());
     FileUtils.moveDirectory(copy.toFile(), original.toFile());
   } else if (Files.exists(copy) && !Files.isDirectory(copy)) {
     FileUtils.deleteQuietly(original.toFile());
     FileUtils.moveFile(copy.toFile(), original.toFile());
   }
 }
  @Override
  public void onWebSocketConnect(Session sess) {

    connected = true;
    final String spath = getFirstValue(sess, "path");
    final String sset = getFirstValue(sess, "dataset");
    final boolean writing = Boolean.parseBoolean(getFirstValue(sess, "writingExpected"));
    final Path path = Paths.get(spath);
    try {
      WatchService myWatcher = path.getFileSystem().newWatchService();

      QueueReader fileWatcher = new QueueReader(myWatcher, sess, spath, sset, writing);

      // We may only monitor a directory
      if (Files.isDirectory(path)) {
        path.register(myWatcher, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
      } else {
        path.getParent().register(myWatcher, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
      }

      Thread th = new Thread(fileWatcher, path.getFileName() + " Watcher");
      th.setDaemon(true);
      th.setPriority(Thread.MAX_PRIORITY - 2);
      th.start();
      if (diagInfo != null) diagInfo.record("Start Thread", th.getName());

    } catch (Exception ne) {
      logger.error("Cannot watch " + path, ne);
      try {
        sess.getRemote().sendString(ne.getMessage());
      } catch (IOException e) {
        logger.warn("Cannot write to remote " + sess, e);
      }
    }
  }
  private Toolbox loadToolbox() throws IOException, URISyntaxException {

    Path foundPath = findToolsDir();

    Toolbox box;
    if (Files.isDirectory(foundPath)) {
      box = new Toolbox(foundPath);

      Path tempDir = Files.createTempDirectory(TOOLS_DIR_NAME);
      Path tempZipFile = tempDir.resolve(TOOLS_ZIP_NAME);

      dirToZip(foundPath, tempZipFile);
      byte[] zipContents = Files.readAllBytes(tempZipFile);
      box.setZipContents(zipContents);
      Files.delete(tempZipFile);
      Files.delete(tempDir);
    }

    // found tools zip
    else {
      FileSystem fs = FileSystems.newFileSystem(foundPath, null);
      Path toolsPath = fs.getPath(TOOLS_DIR_NAME);
      box = new Toolbox(toolsPath);

      byte[] zipContents = Files.readAllBytes(foundPath);
      box.setZipContents(zipContents);
    }

    return box;
  }
  protected void renameFile(SyncWatchEvent syncWatchEvent) throws Exception {
    Path sourceFilePath = Paths.get(syncWatchEvent.getPreviousFilePathName());

    SyncFile syncFile = SyncFileService.fetchSyncFile(sourceFilePath.toString());

    Path targetFilePath = Paths.get(syncWatchEvent.getFilePathName());

    if (sanitizeFileName(targetFilePath)) {
      return;
    }

    if (syncFile == null) {
      if (Files.isDirectory(targetFilePath)) {
        addFolder(syncWatchEvent);
      } else {
        addFile(syncWatchEvent);
      }

      return;
    } else if (isPendingTypePK(syncFile)) {
      queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

      return;
    }

    String fileType = syncFile.getType();

    if (fileType.equals(SyncFile.TYPE_FILE)) {
      SyncFileService.renameFileSyncFile(targetFilePath, _syncAccountId, syncFile);
    } else {
      SyncFileService.renameFolderSyncFile(targetFilePath, _syncAccountId, syncFile);
    }
  }
Exemple #10
0
  private ClientDatabase getConfigDatabase() {
    try {
      Path configDir = databaseFile.toAbsolutePath().getParent();
      if (!Files.isDirectory(configDir)) {
        Files.createDirectory(configDir);
      }
      connection = DriverManager.getConnection(getSqliteConnectionString());

      try {
        try (Statement statement = connection.createStatement()) {
          statement.execute("PRAGMA FOREIGN_KEYS = ON");
        }
        ClientDatabase clientDatabase = new ChatClientDatabase(connection);
        clientDatabase.migrate();

        return clientDatabase;
      } catch (Exception e) {
        try {
          connection.close();
        } catch (Exception ignored) {
        }
        throw e;
      }

    } catch (Exception e) {
      throw new IllegalStateException(
          "failed to initialize or migrate config database:" + e.getMessage(), e);
    }
  }
Exemple #11
0
  public static FileInfo pathToFileInfo(Path filePath, Path start) {
    FileInfo fileInfo = new FileInfo();
    if (!Files.exists(filePath)) {
      fileInfo.setName("Unknown");
      return fileInfo;
    }

    fileInfo.setName(start.relativize(filePath).toString());

    if (Files.isDirectory(filePath)) {
      fileInfo.setDir(true);
    } else {
      fileInfo.setDir(false);
      try {
        fileInfo.setSize(Files.size(filePath));
      } catch (IOException e) {
        fileInfo.setSize(0);
      }
      try {
        fileInfo.setMimeType(Files.probeContentType(filePath));
      } catch (IOException e) {
        fileInfo.setMimeType("application/octet-stream");
      }
      if (fileInfo.getMimeType() == null) {
        fileInfo.setMimeType("application/octet-stream");
      }
    }

    return fileInfo;
  }
  @GenerateLink(rel = Constants.LINK_REL_GET_CURRENT_RECIPE)
  @GET
  @Path("/recipe")
  @Produces(MediaType.APPLICATION_JSON)
  public Response getRecipe(@QueryParam("id") String id) throws Exception {
    java.nio.file.Path dockerParentPath = Paths.get(dockerfilesRepository);
    if (dockerfilesRepository == null
        || !java.nio.file.Files.exists(dockerParentPath)
        || !java.nio.file.Files.isDirectory(dockerParentPath)) {
      throw new NotFoundException(
          "The configuration of docker repository wasn't found or some problem with configuration was found.");
    }

    final String path = id.replace(SYSTEM_PREFIX, "") + DOCKER_FILE_NAME;

    java.nio.file.Path dockerFile = Paths.get(dockerParentPath.toAbsolutePath().toString(), path);
    if (!java.nio.file.Files.exists(dockerFile)) {
      throw new NotFoundException("The docker file with given id wasn't found.");
    }

    return Response.ok(
            "{ \"recipe\":\"" + new String(java.nio.file.Files.readAllBytes(dockerFile)) + "\" }",
            MediaType.APPLICATION_JSON_TYPE)
        .build();
  }
  @Test
  public void test() throws Exception {
    Module module =
        new Module(
            "test",
            new Version("1.0.0", new LiquibaseMigration(), new AntMigration("test-ant_1.0.0.xml")));

    Connection conn = DriverManager.getConnection("jdbc:h2:mem:test", "sa", "sa");
    Path path = Paths.get("solidbase-test-dir");

    try {
      Solidbase solidbase = new Solidbase();
      solidbase.migrate(
          conn, Thread.currentThread().getContextClassLoader(), new H2Database(), module);

      Integer count = selectIntFromDatabase(conn, "SELECT COUNT(*) FROM PERSON");
      assertEquals(0, count.intValue());

      String version =
          selectStringFromDatabase(conn, "SELECT VERSION FROM VERSIONS WHERE MODULE_ID='test'");
      assertEquals("1.0.0", version);

      assertTrue(Files.exists(path));
      assertTrue(Files.isDirectory(path));

    } finally {
      ignoreException(() -> conn.close());
      ignoreException(() -> Files.delete(path));
    }
  }
  public static void saveData(String file, EnumMap<PlayerStats, Float> map, boolean overwrite) {
    FileSystem fileSystem = FileSystems.getDefault();
    Path path = fileSystem.getPath(file);
    if (Files.isDirectory(path))
      throw new IllegalArgumentException("This method is not supposed to read a directory");

    if (Files.exists(path) && Files.isReadable(path)) {
      if (overwrite == false) return;
    } else
      try {
        Files.createFile(path);
      } catch (IOException e) {
        e
            .printStackTrace(); // To change body of catch statement use File | Settings | File
                                // Templates.
      }

    try (ObjectOutputStream objectOutputStream =
        new ObjectOutputStream(Files.newOutputStream(path))) {
      objectOutputStream.writeObject(map);
    } catch (IOException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
  }
  /**
   * Marshals the object to the given path that must represent a path to the file.
   *
   * <p>This method is capable of setting the schema version to the object being marshalled.
   *
   * @param path Path to file
   * @param object Object to marshal
   * @param noNamespaceSchemaLocation NoNamespaceSchemaLocation to set. If it's <code>null</code> no
   *     location will be set.
   * @param schemaVersion If schema version is set and object to marshall is instance of {@link
   *     ISchemaVersionAware} then the given schema version will be set to the object. Use <code>0
   *     </code> to ignore setting of schema version.
   * @throws JAXBException If {@link JAXBException} occurs.
   * @throws IOException If {@link IOException} occurs.
   */
  public void marshall(
      Path path, Object object, String noNamespaceSchemaLocation, int schemaVersion)
      throws JAXBException, IOException {
    if (Files.isDirectory(path)) {
      throw new IOException("Can not marshal object to the path that represents the directory");
    }
    Files.deleteIfExists(path);
    Files.createDirectories(path.getParent());

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    if (null != noNamespaceSchemaLocation) {
      marshaller.setProperty(
          Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, noNamespaceSchemaLocation);
    }

    // set schema version if needed
    if ((object instanceof ISchemaVersionAware) && (0 != schemaVersion)) {
      ((ISchemaVersionAware) object).setSchemaVersion(schemaVersion);
    }

    try (OutputStream outputStream = Files.newOutputStream(path, StandardOpenOption.CREATE_NEW)) {
      marshaller.marshal(object, outputStream);
    }
  }
 /**
  * This is a best effort to ensure that we actually have write permissions to write in all our
  * data directories. This prevents disasters if nodes are started under the wrong username etc.
  */
 private void assertCanWrite() throws IOException {
   for (Path path : nodeDataPaths()) { // check node-paths are writable
     tryWriteTempFile(path);
   }
   for (String indexFolderName : this.availableIndexFolders()) {
     for (Path indexPath :
         this.resolveIndexFolder(indexFolderName)) { // check index paths are writable
       Path indexStatePath = indexPath.resolve(MetaDataStateFormat.STATE_DIR_NAME);
       tryWriteTempFile(indexStatePath);
       tryWriteTempFile(indexPath);
       try (DirectoryStream<Path> stream = Files.newDirectoryStream(indexPath)) {
         for (Path shardPath : stream) {
           String fileName = shardPath.getFileName().toString();
           if (Files.isDirectory(shardPath) && fileName.chars().allMatch(Character::isDigit)) {
             Path indexDir = shardPath.resolve(ShardPath.INDEX_FOLDER_NAME);
             Path statePath = shardPath.resolve(MetaDataStateFormat.STATE_DIR_NAME);
             Path translogDir = shardPath.resolve(ShardPath.TRANSLOG_FOLDER_NAME);
             tryWriteTempFile(indexDir);
             tryWriteTempFile(translogDir);
             tryWriteTempFile(statePath);
             tryWriteTempFile(shardPath);
           }
         }
       }
     }
   }
 }
Exemple #17
0
  private static Stream<String> apps() {
    if (cl instanceof URLClassLoader) {
      URLClassLoader ucl = (URLClassLoader) cl;

      return Stream.of(ucl.getURLs())
          .map(propagating(url -> Paths.get(url.toURI())))
          .flatMap(
              propagating(
                  path -> {
                    if (Files.isRegularFile(path)) {
                      return zipContents(path);
                    } else if (Files.isDirectory(path)) {
                      return Files.walk(path)
                          .map(subpath -> path.relativize(subpath))
                          .map(subpath -> subpath.toString())
                          .filter(subpath -> subpath.endsWith(".class"))
                          .map(Scanner::toClassName);
                    } else {
                      return Stream.empty();
                    }
                  }))
          .filter(x -> !x.startsWith("com.cakemanny.app."))
          .filter(implementsInterface(App.class));
    } else {
      return Stream.empty();
    }
  }
  /**
   * Returns list of files paths with given extension for a storage in HTTP form.
   *
   * @param storageData Storage.
   * @param extension Files extension.
   * @return Returns the map containing pair with file name with given extension for a storage in
   *     HTTP form and size of each file.
   * @throws IOException If {@link IOException} occurs.
   */
  public Map<String, Long> getFilesHttpLocation(StorageData storageData, final String extension)
      throws IOException {
    Path storagePath = getStoragePath(storageData);
    if (storagePath == null || !Files.isDirectory(storagePath)) {
      return Collections.emptyMap();
    }

    final List<Path> filesPaths = new ArrayList<Path>();
    Files.walkFileTree(
        storagePath,
        new SimpleFileVisitor<Path>() {
          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
              throws IOException {
            if (file.toString().endsWith(extension)) {
              filesPaths.add(file);
            }
            return super.visitFile(file, attrs);
          }
        });

    Map<String, Long> result = new HashMap<String, Long>();
    for (Path path : filesPaths) {
      result.put(getPathAsHttp(path), Files.size(path));
    }

    return result;
  }
  public static Collection<ProfileDef> getAvailableProfiles(Path rootDir) {
    Path profileDir = getProfileDirectory(rootDir);
    if (!Files.isDirectory(profileDir)) {
      return Collections.emptySet();
    }

    List<ProfileDef> result = new LinkedList<>();
    try (DirectoryStream<Path> profileDirContent = Files.newDirectoryStream(profileDir)) {
      int suffixLength = PROFILE_FILE_NAME_SUFFIX.length();
      for (Path file : profileDirContent) {
        if (!Files.isRegularFile(file)) {
          continue;
        }

        String fileName = file.getFileName().toString();
        String normFileName = fileName.toLowerCase(Locale.ROOT);
        if (!normFileName.endsWith(PROFILE_FILE_NAME_SUFFIX)) {
          continue;
        }

        // This should hold, but check it just in case I don't know
        // something about weird case issues.
        if (fileName.length() >= suffixLength) {
          String profileName = fileName.substring(0, fileName.length() - suffixLength);
          result.add(new ProfileDef(null, fileName, profileName));
        }
      }
    } catch (IOException ex) {
      LOGGER.log(Level.INFO, "Cannot list profile directory: " + profileDir, ex);
    }
    return result;
  }
  /**
   * Reads input files from input directory and sorts them in ascending order.
   *
   * @param inputDirPath input directory
   * @return sorted list of input files
   */
  private static List<Path> getInputFiles(final Path inputDirPath) {
    final List<Path> result = new ArrayList<>(10);

    try (final DirectoryStream<Path> files = Files.newDirectoryStream(inputDirPath)) {
      final Iterator<Path> filesIterator = files.iterator();

      while (filesIterator.hasNext()) {
        final Path file = filesIterator.next();

        if (!Files.isDirectory(file)) {
          result.add(file);
        }
      }
    } catch (final IOException ex) {
      throw new RuntimeException("Failed to read content of input directory", ex);
    }

    Collections.sort(
        result,
        new Comparator<Path>() {
          @Override
          public int compare(final Path o1, final Path o2) {
            return o1.getFileName().toString().compareTo(o2.getFileName().toString());
          }
        });

    return result;
  }
 public void testPlatformBinPermissions() throws Exception {
   assumeTrue("posix filesystem", isPosix);
   Tuple<Path, Environment> env = createEnv(fs, temp);
   Path pluginDir = createPluginDir(temp);
   Path platformDir = pluginDir.resolve("platform");
   Path platformNameDir = platformDir.resolve("linux-x86_64");
   Path platformBinDir = platformNameDir.resolve("bin");
   Files.createDirectories(platformBinDir);
   Path programFile = Files.createFile(platformBinDir.resolve("someprogram"));
   // a file created with Files.createFile() should not have execute permissions
   Set<PosixFilePermission> sourcePerms = Files.getPosixFilePermissions(programFile);
   assertFalse(sourcePerms.contains(PosixFilePermission.OWNER_EXECUTE));
   assertFalse(sourcePerms.contains(PosixFilePermission.GROUP_EXECUTE));
   assertFalse(sourcePerms.contains(PosixFilePermission.OTHERS_EXECUTE));
   String pluginZip = createPlugin("fake", pluginDir);
   installPlugin(pluginZip, env.v1());
   assertPlugin("fake", pluginDir, env.v2());
   // check that the installed program has execute permissions, even though the one added to the
   // plugin didn't
   Path installedPlatformBinDir =
       env.v2()
           .pluginsFile()
           .resolve("fake")
           .resolve("platform")
           .resolve("linux-x86_64")
           .resolve("bin");
   assertTrue(Files.isDirectory(installedPlatformBinDir));
   Path installedProgramFile = installedPlatformBinDir.resolve("someprogram");
   assertTrue(Files.isRegularFile(installedProgramFile));
   Set<PosixFilePermission> installedPerms = Files.getPosixFilePermissions(installedProgramFile);
   assertTrue(installedPerms.contains(PosixFilePermission.OWNER_EXECUTE));
   assertTrue(installedPerms.contains(PosixFilePermission.GROUP_EXECUTE));
   assertTrue(installedPerms.contains(PosixFilePermission.OTHERS_EXECUTE));
 }
Exemple #22
0
  @Test
  public void testFileResources() throws Exception {
    String someResource = "/org/node/resource.txt";

    File file = new File("files/node-1.0-SNAPSHOT.jar");
    URL url1 = file.getAbsoluteFile().toURI().toURL();
    URL url2 = new File("files/invoke-1.0-SNAPSHOT.jar").getAbsoluteFile().toURI().toURL();

    URLClassLoader loader = new URLClassLoader(new URL[] {url1, url2});

    final List<String> resourcePaths = Classpaths.listFromClassLoader(loader, someResource);

    int fileCount = 0;
    int dirCount = 0;
    for (String path : resourcePaths) {
      if (!Files.isDirectory(IO.path(path))) {
        fileCount++;
      } else {
        dirCount++;
      }
    }

    boolean ok = true;

    ok |= dirCount == 0 || die();

    //        ok |= fileCount == 2 || die();

  }
Exemple #23
0
  /** Process all events for keys queued to the watcher */
  void processEvents() {
    while (true) {

      // wait for key to be signalled
      WatchKey key;
      try {
        key = watcher.take();
      } catch (InterruptedException x) {
        return;
      }

      Path dir = keys.get(key);
      if (dir == null) {
        System.err.println("WatchKey not recognized!!");
        continue;
      }

      List<WatchEvent<?>> events = key.pollEvents();

      for (WatchEvent<?> event : events) {
        Kind<?> kind = event.kind();

        // TBD - provide example of how OVERFLOW event is handled
        if (kind == OVERFLOW) {
          continue;
        }

        // Context for directory entry event is the file name of entry
        WatchEvent<Path> ev = cast(event);
        Path name = ev.context();
        Path child = dir.resolve(name);

        // print out event
        onEvent.accept(ev, child);

        // if directory is created, and watching recursively, then
        // register it and its sub-directories
        if (recursive && (kind == ENTRY_CREATE)) {
          try {
            if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
              registerAll(child);
            }
          } catch (IOException x) {
            // ignore to keep sample readbale
          }
        }
      }

      // reset key and remove from set if directory no longer accessible
      boolean valid = key.reset();
      if (!valid) {
        keys.remove(key);

        // all directories are inaccessible
        if (keys.isEmpty()) {
          break;
        }
      }
    }
  }
  static boolean unpackZipTo(Path zipfile, Path destDirectory) throws IOException {
    boolean ret = true;

    byte[] bytebuffer = new byte[BUFFERSIZE];
    ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream(zipfile.toFile()));

    ZipEntry zipentry;
    while ((zipentry = zipinputstream.getNextEntry()) != null) {
      Path newFile = destDirectory.resolve(zipentry.getName());

      if (!Files.exists(newFile.getParent(), LinkOption.NOFOLLOW_LINKS)) {
        Files.createDirectories(newFile.getParent());
      }

      if (!Files.isDirectory(newFile, LinkOption.NOFOLLOW_LINKS)) {
        FileOutputStream fileoutputstream = new FileOutputStream(newFile.toFile());

        int bytes;
        while ((bytes = zipinputstream.read(bytebuffer)) > -1) {
          fileoutputstream.write(bytebuffer, 0, bytes);
        }
        fileoutputstream.close();
      }
      zipinputstream.closeEntry();
    }

    zipinputstream.close();

    return ret;
  }
  /**
   * キー情報から実ファイルデータを取得し、それらからメタデータを生成して返します.
   *
   * @param key ファイルデータのキー情報
   * @return キーから生成したメタデータ
   */
  @Override
  public UrlTreeMetaData<InputStream> generateMetaDataFromReal(String key, UrlTreeContext ctx)
      throws BadContentException {
    if (!this.canLoad(key, ctx)) {
      throw new IllegalArgumentException(
          "Cannot Load it. check before can it load with canLoad(key): " + key);
    }
    Path p = this.generateFileObj(key);
    File f = p.toFile();
    long lastModified = f.lastModified();

    logger.trace("[デバッグ]ファイル名とファイルのlastModified: " + p.toString() + ":" + lastModified);

    UrlTreeMetaData<InputStream> md = new UrlTreeMetaData<>();
    md.setDirectory(Files.isDirectory(p, LinkOption.NOFOLLOW_LINKS));
    md.setFilename(key);
    md.setOwnerId(ctx.getUserName());
    md.setGroupId(ctx.getPrimaryGroup());
    md.setCreatedTime(lastModified);
    md.setUpdatedTime(lastModified);
    md.setPermission(ctx.getDefaultPermission());
    String contentType = new MimetypesFileTypeMap().getContentType(p.getFileName().toString());
    md.setContentType(contentType);

    return md;
  }
Exemple #26
0
 public Boolean isCollectorFullBase() throws BotException {
   final int[] attackableElixirs = {0};
   final BufferedImage image = platform.screenshot(ENEMY_BASE);
   try {
     final URI uri = getClass().getResource("elixirs").toURI();
     Utils.withClasspathFolder(
         uri,
         (path) -> {
           final List<Rectangle> matchedElixirs = new ArrayList<>();
           try (Stream<Path> walk = Files.walk(path, 1)) {
             for (final Iterator<Path> it = walk.iterator(); it.hasNext(); ) {
               final Path next = it.next();
               if (Files.isDirectory(next)) {
                 continue;
               }
               final BufferedImage tar =
                   ImageIO.read(Files.newInputStream(next, StandardOpenOption.READ));
               final List<RegionMatch> doFindAll =
                   TemplateMatcher.findMatchesByGrayscaleAtOriginalResolution(image, tar, 7, 0.8);
               attackableElixirs[0] += countAttackableElixirs(doFindAll, matchedElixirs, next);
             }
           } catch (final IOException e) {
             logger.log(Level.SEVERE, e.getMessage(), e);
           }
         });
   } catch (final URISyntaxException e) {
     logger.log(Level.SEVERE, e.getMessage(), e);
   }
   return attackableElixirs[0] >= 0;
 }
  /**
   * Try to locate tools dir or tools zip
   *
   * @return path to tolls dir or tools zip
   * @throws FileNotFoundException if tools dir or zip not found
   */
  private Path findToolsDir() throws FileNotFoundException {

    for (String location : TOOLS_SEARCH_LOCATIONS) {

      Path path;

      // search tools dir
      path = Paths.get(location, TOOLS_DIR_NAME);
      logger.info("looking for " + path);
      if (Files.isDirectory(path)) {
        logger.info("tools directory " + path + " found");
        return path;
      }

      // search tools zip
      else {
        path = Paths.get(location, TOOLS_ZIP_NAME);
        logger.info("looking for " + path);
        if (Files.exists(path)) {
          logger.info("tools zip " + path + " found");
          return path;
        }
      }
    }

    logger.warn("tools not found");
    throw new FileNotFoundException("tools not found");
  }
 public Plugin(Settings settings) {
   String priorPath = settings.get(PRIOR_PATH_KEY);
   enabled =
       settings.getAsBoolean(PRIOR_STORE_ENABLED_KEY, true)
           && null != priorPath
           && Files.isDirectory(Paths.get(priorPath));
 }
  public static void reconstructTurtle(File partFolder, File reconstructed) throws IOException {
    Path tmpOut = Files.createTempFile(partFolder.toPath(), "reconstr", ".tmp");
    FileOutputStream dstOut = new FileOutputStream(tmpOut.toFile());
    FileChannel dstOutChannel = dstOut.getChannel();
    try {
      if (!Files.isDirectory(partFolder.toPath()))
        throw new IOException("Not a directory: " + partFolder);
      File[] fileList =
          FileUtils.listFiles(partFolder, new PrefixFileFilter("part"), TrueFileFilter.TRUE)
              .toArray(new File[0]);
      Arrays.sort(fileList);
      RandomAccessFile inputFile;

      inputFile = new RandomAccessFile(fileList[0], "r");
      inputFile.getChannel().transferTo(0, inputFile.length(), dstOutChannel);
      inputFile.close();
      for (int i = 1; i < fileList.length; i++) {
        inputFile = new RandomAccessFile(fileList[i], "r");
        long lastPrefix = findTurtlePrefixEnd(inputFile);
        inputFile
            .getChannel()
            .transferTo(lastPrefix, inputFile.length() - lastPrefix, dstOutChannel);
        inputFile.close();
      }
    } finally {
      dstOut.close();
    }
    Files.move(
        tmpOut,
        reconstructed.toPath(),
        StandardCopyOption.ATOMIC_MOVE,
        StandardCopyOption.REPLACE_EXISTING);
    FileUtils.deleteQuietly(tmpOut.toFile());
    FileUtils.deleteQuietly(partFolder);
  }
 private static Set<ShardId> findAllShardsForIndex(Path indexPath) throws IOException {
   Set<ShardId> shardIds = new HashSet<>();
   if (Files.isDirectory(indexPath)) {
     try (DirectoryStream<Path> stream = Files.newDirectoryStream(indexPath)) {
       String currentIndex = indexPath.getFileName().toString();
       for (Path shardPath : stream) {
         String fileName = shardPath.getFileName().toString();
         if (Files.isDirectory(shardPath) && fileName.chars().allMatch(Character::isDigit)) {
           int shardId = Integer.parseInt(fileName);
           ShardId id = new ShardId(currentIndex, shardId);
           shardIds.add(id);
         }
       }
     }
   }
   return shardIds;
 }