Package br.com.ingenieux.launchify.spi.launch4j

Source Code of br.com.ingenieux.launchify.spi.launch4j.Launch4JSPI

package br.com.ingenieux.launchify.spi.launch4j;

import java.io.FileOutputStream;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;

import br.com.ingenieux.launchify.core.ApplicationEntry;
import br.com.ingenieux.launchify.spi.LaunchifySPI;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class Launch4JSPI extends LaunchifySPI {
  private final String EXEC = which(SystemUtils.IS_OS_WINDOWS ? "launch4jc.exe"
      : "launch4j");

  private Collection<String> getClasspathEntries() {
    List<String> result = new ArrayList<String>();

    for (URL url : classpathEntries) {
      result.add(FileUtils.toFile(url).getAbsolutePath());
    }

    return result;
  }

  @Override
  protected void generateAppEntry(ApplicationEntry appEntry) throws Exception {
    Configuration configuration = new Configuration();

    configuration.setClassForTemplateLoading(getClass(), "templates");
    configuration.setObjectWrapper(new DefaultObjectWrapper());

    Template template = configuration.getTemplate("launch4j.xml.ftl");

    StringWriter writer = new StringWriter();

    Map<String, Object> context = new TreeMap<String, Object>();

    String suffix = SystemUtils.IS_OS_WINDOWS ? ".exe" : "";

    if (!this.destinationDirectory.exists())
      this.destinationDirectory.createFolder();

    String radical = FileUtils.toFile(destinationDirectory.getURL()) + "/"
        + appEntry.getName();

    context.put("mainClass", appEntry.getClassName());
    context.put("outputPath", radical + suffix);
    context.put("classpathEntries", getClasspathEntries());

    template.process(context, writer);

    String targetDescriptor = radical + ".l4j.xml";
    IOUtils.write(writer.toString(), new FileOutputStream(targetDescriptor));

    if (null != EXEC) {
      ProcessBuilder processBuilder = new ProcessBuilder(EXEC,
          targetDescriptor);

      Process process = processBuilder.start();

      int errorCode = process.waitFor();

      if (0 != errorCode)
        throw new IllegalStateException("Return code: " + errorCode);
    }
  }
}
TOP

Related Classes of br.com.ingenieux.launchify.spi.launch4j.Launch4JSPI

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.