Package com.subhajit.eclipse.util

Source Code of com.subhajit.eclipse.util.GiantJarMaker

package com.subhajit.eclipse.util;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.taskdefs.ManifestException;

import com.subhajit.build.CombineJars;
import com.subhajit.common.util.IConstants;
import com.subhajit.common.util.IProgress;

public class GiantJarMaker {
  // private static final String META_INF_MANIFEST_MF =
  // "META-INF/MANIFEST.MF";
  // private final URLClassLoader classLoader;
  // private final URL[] urls;
  // private final Map<String, byte[]> allResources;

  private final File[] files;

  public GiantJarMaker(URLClassLoader classLoader, IProgress... progresses) {
    super();
    // this.classLoader = classLoader;
    List<File> files = new ArrayList<File>();
    for (URL url : classLoader.getURLs()) {
      if (url.getProtocol().equals("file")) {
        files.add(new File(url.getFile()));
      }
    }
    this.files = files.toArray(IConstants.ZERO_LENGTH_FILE_ARRAY);
    // allResources = new ConcurrentHashMap<String, byte[]>();
  }

  // private void init(IProgress... progresses) {
  // ExecutorService executor = Executors.newCachedThreadPool();
  // try {
  // List<Future<Map<String, byte[]>>> futures = new
  // ArrayList<Future<Map<String, byte[]>>>();
  // for (IProgress progress : progresses) {
  // progress.setRange(0, urls.length);
  // }
  // for (final URL url : urls) {
  // futures.add(executor
  // .submit(new Callable<Map<String, byte[]>>() {
  // public Map<String, byte[]> call() throws Exception {
  // if (url.getProtocol().equals("file")) {
  // File file = new File(url.getFile());
  // if (file.isDirectory()) {
  // return parseDirectory(file);
  // } else if (file.isFile()
  // & file.getName().endsWith(".jar")) {
  // return parseJarFile(file);
  // } else {
  // throw new UnsupportedOperationException(
  // "Only directories and JAR file class path elements are supported at this time.");
  // }
  // } else {
  // throw new UnsupportedOperationException(
  // "Only file URLs are supported at this time.");
  // }
  // }
  // }));
  // }
  // for (Future<Map<String, byte[]>> future : futures) {
  // try {
  // allResources.putAll(future.get());
  // for (IProgress progress : progresses) {
  // progress.increment(1, "Parsed classpath element");
  // }
  // } catch (InterruptedException e) {
  // e.printStackTrace();
  // Thread.currentThread().interrupt();
  // return;
  // } catch (ExecutionException e) {
  // e.printStackTrace();
  // }
  // }
  // } finally {
  // executor.shutdown();
  // }
  // }
  //
  // private Map<String, byte[]> parseDirectory(File dir) throws IOException {
  // assert dir.isDirectory();
  // Map<String, byte[]> ret = new ConcurrentHashMap<String, byte[]>();
  // for (File file : FileUtils.listAllContentsUnder(dir)) {
  // if (file.isFile()) {
  // ret.put(
  // file.getCanonicalPath().substring(
  // dir.getCanonicalPath().length() + 1).replace(
  // '\\', '/'), FileUtils.loadFile(file));
  // }
  // }
  // return ret;
  // }
  //
  // private Map<String, byte[]> parseJarFile(File file) throws IOException {
  // assert file.isFile();
  // assert file.getName().endsWith(".jar");
  // Map<String, byte[]> ret = new ConcurrentHashMap<String, byte[]>();
  // ZipFile zipFile = null;
  // try {
  // zipFile = new ZipFile(file);
  // Enumeration<? extends ZipEntry> entries = zipFile.entries();
  // while (entries.hasMoreElements()) {
  // ZipEntry entry = entries.nextElement();
  // if (!entry.isDirectory()) {
  // InputStream in = zipFile.getInputStream(entry);
  // try {
  // ret.put(entry.getName(), StreamUtils.readFully(in));
  // } finally {
  // if (in != null) {
  // in.close();
  // }
  // }
  // }
  // }
  // } finally {
  // if (zipFile != null) {
  // zipFile.close();
  // }
  // }
  // return ret;
  // }

//  public int getClassCount() throws IOException {
//    return allResources.keySet().size();
//  }

  public void generate(File targetFile, IProgress... progresses)
      throws IOException, ManifestException {
    new CombineJars()
        .createCombinedJar(null, files, targetFile, progresses);
  }

  // private void saveResources(ZipOutputStream zOut, IProgress... progresses)
  // throws IOException {
  // for (Map.Entry<String, byte[]> entry : allResources.entrySet()) {
  // for (IProgress progress : progresses) {
  // progress.increment(1, "Adding " + entry.getKey());
  // }
  // if (entry.getKey().equals(META_INF_MANIFEST_MF)) {
  // continue;
  // }
  // ZipEntry zipEntry = new ZipEntry(entry.getKey());
  // zOut.putNextEntry(zipEntry);
  // zOut.write(entry.getValue());
  // zOut.closeEntry();
  // }
  // }

  // private void saveMergedManifestOfJarFiles(ZipOutputStream zOut)
  // throws ZipException, IOException, ManifestException {
  // List<File> jarFiles = new ArrayList<File>();
  // for (URL url : urls) {
  // if (url.getProtocol().equals("file")) {
  // File urlFile = new File(url.getFile());
  // if (urlFile.isFile() && urlFile.getName().endsWith(".jar")) {
  // jarFiles.add(urlFile);
  // }
  // }
  // }
  //
  // Manifest manifest = ManifestUtils.getMergedManifest(jarFiles
  // .toArray(IConstants.ZERO_LENGTH_FILE_ARRAY));
  // ByteArrayOutputStream bOut = null;
  // try {
  // bOut = new ByteArrayOutputStream();
  // PrintWriter pw = null;
  // try {
  // pw = new PrintWriter(bOut);
  // manifest.write(pw);
  // pw.flush();
  //
  // ZipEntry manifestEntry = new ZipEntry(META_INF_MANIFEST_MF);
  // zOut.putNextEntry(manifestEntry);
  // zOut.write(bOut.toByteArray());
  // zOut.closeEntry();
  // } finally {
  // if (pw != null) {
  // pw.close();
  // }
  // }
  // } finally {
  // if (bOut != null) {
  // bOut.close();
  // }
  // }
  // }
}
TOP

Related Classes of com.subhajit.eclipse.util.GiantJarMaker

TOP
Copyright © 2018 www.massapi.com. 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.