Package ch.cmbntr.modulizer.bootstrap.util.Resources

Examples of ch.cmbntr.modulizer.bootstrap.util.Resources.Pool


      throws MojoExecutionException {

    final Map<Artifact, File> fromTo = compute(artifacts, resourceTargetFile(moduleDirectory));
    final Map<File, Future<String>> targetHashes = sha1async(fromTo.values());

    final Pool pool = Resources.getPoolHandle();
    final ExecutorService executor = pool.aquireExec();
    try {
      return collect(ImmutableList.copyOf(transform(fromTo.entrySet(),
          new Function<Entry<Artifact, File>, Future<String>>() {
            @Override
            public Future<String> apply(final Entry<Artifact, File> e) {
              final Artifact a = e.getKey();
              final File src = a.getFile();
              final File target = e.getValue();
              final Future<String> targetHash = targetHashes.get(target);
              checkState(src != null, "missing file for %s", a);
              return executor.submit(conditionalCopy(src, target, targetHash));
            }
          })));
    } catch (final ComputationException e) {
      throw new MojoExecutionException("failed to copy artifacts", e.getCause());
    } finally {
      pool.releaseExec(executor);
    }

  }
View Full Code Here


      throws ComputationException {
    return Maps.transformValues(submitCompute(items, f), ModulizerUtil.<T> futureCollector());
  }

  private static <F, T> Map<F, Future<T>> submitCompute(final Iterable<F> items, final Function<? super F, T> f) {
    final Pool pool = Resources.getPoolHandle();
    final ExecutorService exec = pool.aquireBlockableExec();
    try {
      final ImmutableMap.Builder<F, Future<T>> submits = ImmutableMap.builder();
      for (final F i : items) {
        submits.put(i, exec.submit(new Callable<T>() {
          @Override
          public T call() throws Exception {
            return f.apply(i);
          }
        }));
      }
      return submits.build();
    } finally {
      pool.releaseBlockableExec(exec);
    }
  }
View Full Code Here

      throws MojoExecutionException {

    final Map<Artifact, File> fromTo = compute(artifacts, resourceTargetFile(moduleDirectory));
    final Map<File, Future<String>> targetHashes = sha1async(fromTo.values());

    final Pool pool = Resources.getPoolHandle();
    final ExecutorService executor = pool.aquireExec();
    try {
      return collect(ImmutableList.copyOf(transform(fromTo.entrySet(),
          new Function<Entry<Artifact, File>, Future<String>>() {
            @Override
            public Future<String> apply(final Entry<Artifact, File> e) {
              final Artifact a = e.getKey();
              final File src = a.getFile();
              final File target = e.getValue();
              final Future<String> targetHash = targetHashes.get(target);
              checkState(src != null, "missing file for %s", a);
              return executor.submit(conditionalCopy(src, target, targetHash));
            }
          })));
    } catch (final ComputationException e) {
      throw new MojoExecutionException("failed to copy artifacts", e.getCause());
    } finally {
      pool.releaseExec(executor);
    }

  }
View Full Code Here

      throws MojoExecutionException {

    final Map<Artifact, File> fromTo = compute(artifacts, resourceTargetFile(moduleDirectory));
    final Map<File, Future<String>> targetHashes = sha1async(fromTo.values());

    final Pool pool = Resources.getPoolHandle();
    final ExecutorService executor = pool.aquireExec();
    try {
      return collect(ImmutableList.copyOf(transform(fromTo.entrySet(),
          new Function<Entry<Artifact, File>, Future<String>>() {
            @Override
            public Future<String> apply(final Entry<Artifact, File> e) {
              final Artifact a = e.getKey();
              final File src = a.getFile();
              final File target = e.getValue();
              final Future<String> targetHash = targetHashes.get(target);
              checkState(src != null, "missing file for %s", a);
              return executor.submit(conditionalCopy(src, target, targetHash));
            }
          })));
    } catch (final ComputationException e) {
      throw new MojoExecutionException("failed to copy artifacts", e.getCause());
    } finally {
      pool.releaseExec(executor);
    }

  }
View Full Code Here

  /**
   * {@inheritDoc}
   */
  @Override
  public void run() {
    final Pool handle = Resources.getPoolHandle();
    try {
      establishContext();
      performSecuritySettings();
      initializeLogging();

View Full Code Here

  }

  private static void copy(final InputStream input, final OutputStream output) throws IOException {
    final ReadableByteChannel in = inChannel(input);
    final WritableByteChannel out = outChannel(output);
    final Pool pool = Resources.getPoolHandle();
    final ByteBuffer buffer = pool.aquireBuffer();
    try {
      while (true) {
        if (in.read(buffer) == -1) {
          break;
        }
        buffer.flip();
        while (buffer.hasRemaining()) {
          final int n = out.write(buffer);
          if (n < 1) {
            throw new IOException("write stalled");
          }
        }
        buffer.clear();
      }
    } finally {
      pool.releaseBuffer(buffer);
    }
  }
View Full Code Here

    return sha1 == null ? null : sha1.append('.').append(extension(f.getName(), "dat")).toString();
  }

  public static LinkedHashMap<File, Future<String>> sha1async(final Iterable<File> files) {
    final LinkedHashMap<File, Future<String>> result = new LinkedHashMap<File, Future<String>>();
    final Pool pool = Resources.getPoolHandle();
    final ExecutorService exec = pool.aquireExec();
    try {
      for (final File f : files) {
        result.put(f, exec.submit(new Callable<String>() {
          @Override
          public String call() throws Exception {
            final StringBuilder sha1 = sha1(f);
            return sha1 == null ? null : sha1.toString();
          }
        }));
      }
      return result;
    } finally {
      pool.releaseExec(exec);
    }
  }
View Full Code Here

  public static StringBuilder sha1(final File f) throws IOException {
    if (f == null || !f.exists()) {
      return null;
    }
    final Pool pool = Resources.getPoolHandle();
    final ByteBuffer buf = pool.aquireBuffer();
    try {
      final FileChannel fc = new FileInputStream(f).getChannel();
      final MessageDigest digest = pool.aquireDigest();
      try {
        int cnt = 0;
        while (cnt >= 0) {
          cnt = fc.read(buf);
          buf.flip();
          digest.update(buf);
          buf.clear();
        }
        return formatAsHex(digest.digest());
      } finally {
        pool.releaseDigest(digest);
        closeQuietly(fc);
      }
    } finally {
      pool.releaseBuffer(buf);
    }
  }
View Full Code Here

  public static void preloadClasses() {
    execute(new Preloader());
  }

  public static List<Module> loadModules(final Iterable<String> moduleIdentifiers, final File... repoRoots) {
    final Pool pool = Resources.getPoolHandle();
    final ExecutorService exec = pool.aquireExec();
    final Properties origProps = snapshotProps();
    try {
      final LocalModuleLoader loader = new LocalModuleLoader(repoRoots);
      final List<Future<Module>> asyncModules = new LinkedList<Future<Module>>();
      for (final String moduleIdentifier : moduleIdentifiers) {
        asyncModules.add(exec.submit(new Callable<Module>() {
          @Override
          public Module call() throws Exception {
            return loader.loadModule(ModuleIdentifier.fromString(moduleIdentifier));
          }
        }));
      }

      final List<Module> modules = new ArrayList<Module>(asyncModules.size());
      for (final Future<Module> m : asyncModules) {
        modules.add(Resources.get(m, "failed to load module"));
      }
      return modules;
    } finally {
      restoreProps(origProps);
      pool.releaseExec(exec);
    }
  }
View Full Code Here

      throws MojoExecutionException {

    final Map<Artifact, File> fromTo = compute(artifacts, resourceTargetFile(moduleDirectory));
    final Map<File, Future<String>> targetHashes = sha1async(fromTo.values());

    final Pool pool = Resources.getPoolHandle();
    final ExecutorService executor = pool.aquireExec();
    try {
      return collect(ImmutableList.copyOf(transform(fromTo.entrySet(),
          new Function<Entry<Artifact, File>, Future<String>>() {
            @Override
            public Future<String> apply(final Entry<Artifact, File> e) {
              final Artifact a = e.getKey();
              final File src = a.getFile();
              final File target = e.getValue();
              final Future<String> targetHash = targetHashes.get(target);
              checkState(src != null, "missing file for %s", a);
              return executor.submit(conditionalCopy(src, target, targetHash));
            }
          })));
    } catch (final ComputationException e) {
      throw new MojoExecutionException("failed to copy artifacts", e.getCause());
    } finally {
      pool.releaseExec(executor);
    }

  }
View Full Code Here

TOP

Related Classes of ch.cmbntr.modulizer.bootstrap.util.Resources.Pool

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.