Java tutorial
/* * Vulcan Build Manager * Copyright (C) 2005-2012 Chris Eldredge * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package net.sourceforge.vulcan.util; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sourceforge.vulcan.exception.ConfigException; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class JavaCommandBuilder { private static final String JAVA_EXECUTABLE_PATH = "bin" + File.separator + "java"; private static final String EXECUTABLE_EXTENSION = ".exe"; private final static Log log = LogFactory.getLog(JavaCommandBuilder.class); private String javaExecutablePath; private String mainClassName; private String vmParameters; private Map<String, String> systemProperties = new HashMap<String, String>(); private List<String> classPath = new ArrayList<String>(); private List<String> arguments = new ArrayList<String>(); public String[] construct() { final List<String> args = new ArrayList<String>(); args.add(javaExecutablePath); if (vmParameters != null) { Collections.addAll(args, vmParameters.split(" ")); } if (!classPath.isEmpty()) { args.add("-classpath"); args.add(StringUtils.join(classPath.iterator(), File.pathSeparatorChar)); } for (String property : systemProperties.keySet()) { final StringBuilder arg = new StringBuilder("-D"); arg.append(property); arg.append("="); arg.append(systemProperties.get(property)); args.add(arg.toString()); } args.add(mainClassName); for (String arg : arguments) { args.add(arg); } return args.toArray(new String[args.size()]); } public void addClassPathEntry(String path) { if (!classPath.contains(path)) { classPath.add(path); } } public void addSystemProperty(String key, String value) { if (systemProperties.containsKey(key)) { log.debug("Overriding system property " + key); } systemProperties.put(key, value); } public void addArgument(String arg) { arguments.add(arg); } @Override public String toString() { return StringUtils.join(construct(), " "); } @Override public boolean equals(Object obj) { return EqualsBuilder.reflectionEquals(this, obj); } @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); } public String getJavaExecutablePath() { return javaExecutablePath; } public void setJavaExecutablePath(String javaExecutablePath) { this.javaExecutablePath = javaExecutablePath; } public void setJavaHome(String javaHome) throws ConfigException { try { final File javaProgram = getVirtualMachineExecutable(javaHome); setJavaExecutablePath(javaProgram.getCanonicalPath()); } catch (IOException e) { throw new RuntimeException(e); } } public static File getVirtualMachineExecutable(String javaHome) throws ConfigException { File file = new File(javaHome, JAVA_EXECUTABLE_PATH); if (!file.exists()) { file = new File(javaHome, JAVA_EXECUTABLE_PATH + EXECUTABLE_EXTENSION); if (!file.exists()) { throw new ConfigException("java.home.invalid", new String[] { JAVA_EXECUTABLE_PATH, javaHome }); } } return file; } public String getMainClassName() { return mainClassName; } public void setMainClassName(String mainClassName) { this.mainClassName = mainClassName; } public List<String> getClassPath() { return Collections.unmodifiableList(classPath); } public String getVmParameters() { return vmParameters; } public void setVmParameters(String vmParameters) { this.vmParameters = vmParameters; } }