package br.com.ingenieux.launchify.spi.hashdot;
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 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 HashdotSPI extends LaunchifySPI {
private final String CHMOD_EXEC = which("chmod");
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("hashdot.ftl");
StringWriter writer = new StringWriter();
Map<String, Object> context = new TreeMap<String, Object>();
if (!this.destinationDirectory.exists())
this.destinationDirectory.createFolder();
String radical = FileUtils.toFile(destinationDirectory.getURL()) + "/"
+ appEntry.getName();
context.put("mainClass", appEntry.getClassName());
context.put("outputPath", radical);
context.put("classpathEntries", getClasspathEntries());
template.process(context, writer);
IOUtils.write(writer.toString(), new FileOutputStream(radical));
if (null != CHMOD_EXEC) {
ProcessBuilder processBuilder = new ProcessBuilder(CHMOD_EXEC,
"755", radical);
Process process = processBuilder.start();
int errorCode = process.waitFor();
if (0 != errorCode)
throw new IllegalStateException("Return code: " + errorCode);
}
}
}