Beispiel #1
1
  private void unzipPlugin(Path zip, Path target) throws IOException {
    Files.createDirectories(target);

    try (ZipInputStream zipInput = new ZipInputStream(Files.newInputStream(zip))) {
      ZipEntry entry;
      byte[] buffer = new byte[8192];
      while ((entry = zipInput.getNextEntry()) != null) {
        Path targetFile = target.resolve(entry.getName());

        // be on the safe side: do not rely on that directories are always extracted
        // before their children (although this makes sense, but is it guaranteed?)
        Files.createDirectories(targetFile.getParent());
        if (entry.isDirectory() == false) {
          try (OutputStream out = Files.newOutputStream(targetFile)) {
            int len;
            while ((len = zipInput.read(buffer)) >= 0) {
              out.write(buffer, 0, len);
            }
          }
        }
        zipInput.closeEntry();
      }
    }
  }
Beispiel #2
0
  @Override
  public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
    checkCreate(qc);

    final Path path = toPath(0, qc);
    final B64 archive = toB64(exprs[1], qc, false);
    final TokenSet hs = entries(2, qc);

    try (ArchiveIn in = ArchiveIn.get(archive.input(info), info)) {
      while (in.more()) {
        final ZipEntry ze = in.entry();
        final String name = ze.getName();
        if (hs == null || hs.delete(token(name)) != 0) {
          final Path file = path.resolve(name);
          if (ze.isDirectory()) {
            Files.createDirectories(file);
          } else {
            Files.createDirectories(file.getParent());
            Files.write(file, in.read());
          }
        }
      }
    } catch (final IOException ex) {
      throw ARCH_FAIL_X.get(info, ex);
    }
    return null;
  }
Beispiel #3
0
  /**
   * 以阻塞的方式立即下载一个文件,该方法会覆盖已经存在的文件
   *
   * @param task
   * @return "ok" if download success (else return errmessage);
   */
  static String download(DownloadTask task) {
    if (!openedStatus && show) openStatus();

    URL url;
    HttpURLConnection conn;
    try {
      url = new URL(task.getOrigin());
      conn = (HttpURLConnection) url.openConnection();
      conn.setConnectTimeout(30000);
      conn.setReadTimeout(30000);
      conn.setRequestProperty(
          "User-Agent",
          "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/"
              + Math.random());
      if ("www.imgjav.com".equals(url.getHost())) { // 该网站需要带cookie请求
        conn.setRequestProperty(
            "User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36");
        // conn.setRequestProperty("Cookie", "__cfduid=d219ea333c7a9b5743b572697b631925a1446093229;
        // cf_clearance=6ae62d843f5d09acf393f9e4eb130d9366840c82-1446093303-28800");
        conn.setRequestProperty(
            "Cookie",
            "__cfduid=d6ee846b378bb7d5d173a05541f8a2b6a1446090548; cf_clearance=ea10e8db31f8b6ee51570b118dd89b7e616d7b62-1446099714-28800");
        conn.setRequestProperty("Host", "www.imgjav.com");
      }
      Path directory = Paths.get(task.getDest()).getParent();
      if (!Files.exists(directory)) Files.createDirectories(directory);
    } catch (Exception e) {
      e.printStackTrace();
      return e.getMessage();
    }

    try (InputStream is = conn.getInputStream();
        BufferedInputStream in = new BufferedInputStream(is);
        FileOutputStream fos = new FileOutputStream(task.getDest());
        OutputStream out = new BufferedOutputStream(fos); ) {
      int length = conn.getContentLength();
      if (length < 1) throw new IOException("length<1");
      byte[] binary = new byte[length];
      byte[] buff = new byte[65536];
      int len;
      int index = 0;
      while ((len = in.read(buff)) != -1) {
        System.arraycopy(buff, 0, binary, index, len);
        index += len;
        allLen += len; // allLen有线程安全的问题 ,可能会不正确。无需精确数据,所以不同步了
        task.setReceivePercent(String.format("%.2f", ((float) index / length) * 100) + "%");
      }
      out.write(binary);
    } catch (IOException e) {
      e.printStackTrace();
      return e.getMessage();
    }
    return "ok";
  }
 private static void write(ContextFactory factory, SourceFile file) {
   try {
     RuntimeModel model = factory.create(file.getInputPath()).getModel();
     String source = generator.generate(file, model);
     Files.createDirectories(file.getOutputPath().getParent());
     Files.write(
         file.getOutputPath(),
         source.getBytes(Charset.forName("UTF-8")),
         StandardOpenOption.CREATE,
         StandardOpenOption.TRUNCATE_EXISTING);
   } catch (Throwable t) {
     throw new CodeGeneratorException(t);
   }
 }
  /**
   * Saves the file to disk along with corresponding expiration date file.
   *
   * @param fileName the name of the file to save
   * @param content the content of the file
   * @param expirationDate the file expiration date
   * @throws Exception if an error occurs
   */
  public static final void save(
      Path fileName, byte[] content, ConfigurationPartMetadata expirationDate) throws Exception {
    if (fileName == null) {
      return;
    }

    Path parent = fileName.getParent();
    if (parent != null) {
      Files.createDirectories(parent);
    }

    log.info("Saving content to file {}", fileName);

    // save the content to disk
    AtomicSave.execute(fileName.toString(), "conf", content, StandardCopyOption.ATOMIC_MOVE);

    // save the content metadata date to disk
    saveMetadata(fileName, expirationDate);
  }
Beispiel #6
0
 private static void extractAndLoadNativeLibs() throws IOException {
   Path target = Paths.get(ioTmpDir, "/tklib");
   if (!target.toFile().exists()) {
     Files.createDirectories(target);
   }
   final boolean windows = System.getProperty("os.name").equalsIgnoreCase("windows");
   String fileExtension = windows ? "dll" : "so";
   String prefix = windows ? "" : "lib";
   String libPattern = fileExtension.equals("dll") ? "-windows" : "-linux" + "-x86";
   if (System.getProperty("sun.arch.data.model").equals("64")) {
     libPattern += "_64";
   }
   libPattern += "." + fileExtension;
   //		System.err.println(libPattern);
   System.setProperty("java.library.path", target.toString());
   //		System.err.println(System.getProperty("java.library.path"));
   extractAndLoadNativeLib(prefix + "JCudaDriver" + libPattern, target);
   extractAndLoadNativeLib(prefix + "JCudaRuntime" + libPattern, target);
   extractAndLoadNativeLib(prefix + "JCurand" + libPattern, target);
 }
Beispiel #7
0
 /**
  * Searches for the directory 'dirPath'; if not found, tries to create it, then returns a File
  * object for that directory.
  */
 public static File ensureDirExists(final String dirPath) {
   File dirpath = new File(dirPath);
   if (!dirpath.isDirectory()) {
     if (!dirpath.exists()) {
       printDebug("[Meta] " + dirpath + " does not exist: creating it...");
       try {
         Files.createDirectories(Paths.get(dirpath.getPath()));
         return dirpath;
       } catch (IOException e) {
         printDebug("[Meta] Exception while creating directory:");
         e.printStackTrace();
         return null;
       }
     } else {
       printDebug(
           "[Meta] Error: path `"
               + dirpath
               + "' is not a valid directory path, and could not create it.");
       return null;
     }
   } else return dirpath;
 }
Beispiel #8
0
  private void copyBinDirectory(
      Path sourcePluginBinDirectory,
      Path destPluginBinDirectory,
      String pluginName,
      Terminal terminal)
      throws IOException {
    boolean canCopyFromSource =
        Files.exists(sourcePluginBinDirectory)
            && Files.isReadable(sourcePluginBinDirectory)
            && Files.isDirectory(sourcePluginBinDirectory);
    if (canCopyFromSource) {
      terminal.println(VERBOSE, "Found bin, moving to %s", destPluginBinDirectory.toAbsolutePath());
      if (Files.exists(destPluginBinDirectory)) {
        IOUtils.rm(destPluginBinDirectory);
      }
      try {
        Files.createDirectories(destPluginBinDirectory.getParent());
        FileSystemUtils.move(sourcePluginBinDirectory, destPluginBinDirectory);
      } catch (IOException e) {
        throw new IOException(
            "Could not move [" + sourcePluginBinDirectory + "] to [" + destPluginBinDirectory + "]",
            e);
      }
      if (Environment.getFileStore(destPluginBinDirectory)
          .supportsFileAttributeView(PosixFileAttributeView.class)) {
        final PosixFileAttributes parentDirAttributes =
            Files.getFileAttributeView(
                    destPluginBinDirectory.getParent(), PosixFileAttributeView.class)
                .readAttributes();
        // copy permissions from parent bin directory
        final Set<PosixFilePermission> filePermissions = new HashSet<>();
        for (PosixFilePermission posixFilePermission : parentDirAttributes.permissions()) {
          switch (posixFilePermission) {
            case OWNER_EXECUTE:
            case GROUP_EXECUTE:
            case OTHERS_EXECUTE:
              break;
            default:
              filePermissions.add(posixFilePermission);
          }
        }
        // add file execute permissions to existing perms, so execution will work.
        filePermissions.add(PosixFilePermission.OWNER_EXECUTE);
        filePermissions.add(PosixFilePermission.GROUP_EXECUTE);
        filePermissions.add(PosixFilePermission.OTHERS_EXECUTE);
        Files.walkFileTree(
            destPluginBinDirectory,
            new SimpleFileVisitor<Path>() {
              @Override
              public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                  throws IOException {
                if (attrs.isRegularFile()) {
                  setPosixFileAttributes(
                      file,
                      parentDirAttributes.owner(),
                      parentDirAttributes.group(),
                      filePermissions);
                }
                return FileVisitResult.CONTINUE;
              }

              @Override
              public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                  throws IOException {
                setPosixFileAttributes(
                    dir,
                    parentDirAttributes.owner(),
                    parentDirAttributes.group(),
                    parentDirAttributes.permissions());
                return FileVisitResult.CONTINUE;
              }
            });
      } else {
        terminal.println(
            VERBOSE, "Skipping posix permissions - filestore doesn't support posix permission");
      }
      terminal.println(
          VERBOSE, "Installed %s into %s", pluginName, destPluginBinDirectory.toAbsolutePath());
    }
  }
  @Inject
  @SuppressForbidden(reason = "System.out.*")
  public NodeEnvironment(Settings settings, Environment environment) throws IOException {
    super(settings);

    this.addNodeId = settings.getAsBoolean(ADD_NODE_ID_TO_CUSTOM_PATH, true);
    this.customPathsEnabled = settings.getAsBoolean(SETTING_CUSTOM_DATA_PATH_ENABLED, false);

    if (!DiscoveryNode.nodeRequiresLocalStorage(settings)) {
      nodePaths = null;
      locks = null;
      localNodeId = -1;
      return;
    }

    final NodePath[] nodePaths = new NodePath[environment.dataWithClusterFiles().length];
    final Lock[] locks = new Lock[nodePaths.length];

    int localNodeId = -1;
    IOException lastException = null;
    int maxLocalStorageNodes = settings.getAsInt("node.max_local_storage_nodes", 50);
    for (int possibleLockId = 0; possibleLockId < maxLocalStorageNodes; possibleLockId++) {
      for (int dirIndex = 0; dirIndex < environment.dataWithClusterFiles().length; dirIndex++) {
        Path dir =
            environment
                .dataWithClusterFiles()[dirIndex]
                .resolve(NODES_FOLDER)
                .resolve(Integer.toString(possibleLockId));
        Files.createDirectories(dir);

        try (Directory luceneDir = FSDirectory.open(dir, NativeFSLockFactory.INSTANCE)) {
          logger.trace("obtaining node lock on {} ...", dir.toAbsolutePath());
          try {
            locks[dirIndex] = Lucene.acquireLock(luceneDir, NODE_LOCK_FILENAME, 0);
            nodePaths[dirIndex] = new NodePath(dir, environment);
            localNodeId = possibleLockId;
          } catch (LockObtainFailedException ex) {
            logger.trace("failed to obtain node lock on {}", dir.toAbsolutePath());
            // release all the ones that were obtained up until now
            releaseAndNullLocks(locks);
            break;
          }

        } catch (IOException e) {
          logger.trace("failed to obtain node lock on {}", e, dir.toAbsolutePath());
          lastException = new IOException("failed to obtain lock on " + dir.toAbsolutePath(), e);
          // release all the ones that were obtained up until now
          releaseAndNullLocks(locks);
          break;
        }
      }
      if (locks[0] != null) {
        // we found a lock, break
        break;
      }
    }

    if (locks[0] == null) {
      throw new IllegalStateException(
          "Failed to obtain node lock, is the following location writable?: "
              + Arrays.toString(environment.dataWithClusterFiles()),
          lastException);
    }

    this.localNodeId = localNodeId;
    this.locks = locks;
    this.nodePaths = nodePaths;

    if (logger.isDebugEnabled()) {
      logger.debug("using node location [{}], local_node_id [{}]", nodePaths, localNodeId);
    }

    maybeLogPathDetails();

    if (settings.getAsBoolean(SETTING_ENABLE_LUCENE_SEGMENT_INFOS_TRACE, false)) {
      SegmentInfos.setInfoStream(System.out);
    }
  }
Beispiel #10
0
  @Test
  public void testJavaClassCondition()
      throws IOException, InstantiationException, IllegalAccessException {
    try (GraphContext context = factory.create(getDefaultPath())) {
      final String inputDir = "src/test/resources/org/jboss/windup/rules/java";

      final Path outputPath =
          Paths.get(
              FileUtils.getTempDirectory().toString(),
              "windup_" + RandomStringUtils.randomAlphanumeric(6));
      FileUtils.deleteDirectory(outputPath.toFile());
      Files.createDirectories(outputPath);

      // Fill the graph with test data.
      ProjectModel pm = context.getFramed().addVertex(null, ProjectModel.class);
      pm.setName("Main Project");

      // Create FileModel for $inputDir
      FileModel inputPathFrame = context.getFramed().addVertex(null, FileModel.class);
      inputPathFrame.setFilePath(inputDir);
      inputPathFrame.setProjectModel(pm);
      pm.addFileModel(inputPathFrame);

      // Set project.rootFileModel to inputPath
      pm.setRootFileModel(inputPathFrame);

      // Create FileModel for $inputDir/HintsClassificationsTest.java
      FileModel fileModel = context.getFramed().addVertex(null, FileModel.class);
      fileModel.setFilePath(inputDir + "/JavaHintsClassificationsTest.java");
      fileModel.setProjectModel(pm);
      pm.addFileModel(fileModel);

      // Create FileModel for $inputDir/JavaClassTest.java
      fileModel = context.getFramed().addVertex(null, FileModel.class);
      fileModel.setFilePath(inputDir + "/JavaClassTest.java");
      fileModel.setProjectModel(pm);
      pm.addFileModel(fileModel);

      context.getGraph().getBaseGraph().commit();

      final WindupConfiguration processorConfig =
          new WindupConfiguration().setOutputDirectory(outputPath);
      processorConfig.setRuleProviderFilter(
          new RuleProviderWithDependenciesPredicate(TestJavaClassTestRuleProvider.class));
      processorConfig
          .setGraphContext(context)
          .setRuleProviderFilter(
              new RuleProviderWithDependenciesPredicate(TestJavaClassTestRuleProvider.class));
      processorConfig.setInputPath(Paths.get(inputDir));
      processorConfig.setOutputDirectory(outputPath);
      processorConfig.setOptionValue(ScanPackagesOption.NAME, Collections.singletonList(""));

      processor.execute(processorConfig);

      GraphService<JavaTypeReferenceModel> typeRefService =
          new GraphService<>(context, JavaTypeReferenceModel.class);
      Iterable<JavaTypeReferenceModel> typeReferences = typeRefService.findAll();
      Assert.assertTrue(typeReferences.iterator().hasNext());

      Assert.assertEquals(3, provider.getFirstRuleMatchCount());
      Assert.assertEquals(1, provider.getSecondRuleMatchCount());
    }
  }