package org.eclipse.jdt.launching;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.Launch;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.IStreamsProxy;
import org.eclipse.jdt.internal.launching.LaunchingMessages;
import org.eclipse.jdt.internal.launching.LaunchingPlugin;
import org.eclipse.jdt.internal.launching.Standard11xVM;
import org.eclipse.jdt.internal.launching.StandardVMType;
import org.eclipse.osgi.util.NLS;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public abstract class AbstractVMInstall implements IVMInstall, IVMInstall2, IVMInstall3 {
private IVMInstallType fType;
private String fId;
private String fName;
private File fInstallLocation;
private LibraryLocation[] fSystemLibraryDescriptions;
private URL fJavadocLocation;
private String fVMArgs;
private Map<String, String> fAttributeMap = new HashMap<>();
private static final String PREF_VM_INSTALL_SYSTEM_PROPERTY = "PREF_VM_INSTALL_SYSTEM_PROPERTY";
private boolean fNotify = true;
public AbstractVMInstall(IVMInstallType type, String id) {
if (type == null) {
throw new IllegalArgumentException(LaunchingMessages.vmInstall_assert_typeNotNull);
}
if (id == null) {
throw new IllegalArgumentException(LaunchingMessages.vmInstall_assert_idNotNull);
}
fType= type;
fId= id;
}
@Override
public String getId() {
return fId;
}
@Override
public String getName() {
return fName;
}
@Override
public void setName(String name) {
if (!name.equals(fName)) {
PropertyChangeEvent event = new PropertyChangeEvent(this, IVMInstallChangedListener.PROPERTY_NAME, fName, name);
fName= name;
if (fNotify) {
JavaRuntime.fireVMChanged(event);
}
}
}
@Override
public File getInstallLocation() {
return fInstallLocation;
}
@Override
public void setInstallLocation(File installLocation) {
if (!installLocation.equals(fInstallLocation)) {
PropertyChangeEvent event = new PropertyChangeEvent(this, IVMInstallChangedListener.PROPERTY_INSTALL_LOCATION, fInstallLocation, installLocation);
fInstallLocation= installLocation;
if (fNotify) {
JavaRuntime.fireVMChanged(event);
}
}
}
@Override
public IVMInstallType getVMInstallType() {
return fType;
}
@Override
public IVMRunner getVMRunner(String mode) {
return null;
}
@Override
public LibraryLocation[] getLibraryLocations() {
return fSystemLibraryDescriptions;
}
@Override
public void setLibraryLocations(LibraryLocation[] locations) {
if (locations == fSystemLibraryDescriptions) {
return;
}
LibraryLocation[] newLocations = locations;
if (newLocations == null) {
newLocations = getVMInstallType().getDefaultLibraryLocations(getInstallLocation());
}
LibraryLocation[] prevLocations = fSystemLibraryDescriptions;
if (prevLocations == null) {
prevLocations = getVMInstallType().getDefaultLibraryLocations(getInstallLocation());
}
if (newLocations.length == prevLocations.length) {
int i = 0;
boolean equal = true;
while (i < newLocations.length && equal) {
equal = newLocations[i].equals(prevLocations[i]);
i++;
}
if (equal) {
return;
}
}
PropertyChangeEvent event = new PropertyChangeEvent(this, IVMInstallChangedListener.PROPERTY_LIBRARY_LOCATIONS, prevLocations, newLocations);
fSystemLibraryDescriptions = locations;
if (fNotify) {
JavaRuntime.fireVMChanged(event);
}
}
@Override
public URL getJavadocLocation() {
return fJavadocLocation;
}
@Override
public void setJavadocLocation(URL url) {
if (url == fJavadocLocation) {
return;
}
if (url != null && fJavadocLocation != null) {
if (url.toExternalForm().equals(fJavadocLocation.toExternalForm())) {
return;
}
}
PropertyChangeEvent event = new PropertyChangeEvent(this, IVMInstallChangedListener.PROPERTY_JAVADOC_LOCATION, fJavadocLocation, url);
fJavadocLocation = url;
if (fNotify) {
JavaRuntime.fireVMChanged(event);
}
}
protected void setNotify(boolean notify) {
fNotify = notify;
}
@Override
public boolean equals(Object object) {
if (object instanceof IVMInstall) {
IVMInstall vm = (IVMInstall)object;
return getVMInstallType().equals(vm.getVMInstallType()) &&
getId().equals(vm.getId());
}
return false;
}
@Override
public int hashCode() {
return getVMInstallType().hashCode() + getId().hashCode();
}
@Override
public String[] getVMArguments() {
String args = getVMArgs();
if (args == null) {
return null;
}
ExecutionArguments ex = new ExecutionArguments(args, "");
return ex.getVMArgumentsArray();
}
@Override
public void setVMArguments(String[] vmArgs) {
if (vmArgs == null) {
setVMArgs(null);
} else {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < vmArgs.length; i++) {
String string = vmArgs[i];
buf.append(string);
buf.append(" ");
}
setVMArgs(buf.toString().trim());
}
}
@Override
public String getVMArgs() {
return fVMArgs;
}
@Override
public void setVMArgs(String vmArgs) {
if (fVMArgs == null) {
if (vmArgs == null) {
return;
}
} else if (fVMArgs.equals(vmArgs)) {
return;
}
PropertyChangeEvent event = new PropertyChangeEvent(this, IVMInstallChangedListener.PROPERTY_VM_ARGUMENTS, fVMArgs, vmArgs);
fVMArgs = vmArgs;
if (fNotify) {
JavaRuntime.fireVMChanged(event);
}
}
@Override
public String getJavaVersion() {
return null;
}
@Override
public Map<String, String> evaluateSystemProperties(String[] properties, IProgressMonitor monitor) throws CoreException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
Map<String, String> map = new HashMap<>();
boolean cached = true;
IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(LaunchingPlugin.ID_PLUGIN);
if(prefs != null) {
for (int i = 0; i < properties.length; i++) {
String property = properties[i];
String key = getSystemPropertyKey(property);
String value = prefs.get(key, null);
if (value != null) {
map.put(property, value);
} else {
map.clear();
cached = false;
break;
}
}
}
if (!cached) {
File file = LaunchingPlugin.getFileInPlugin(new Path("lib/launchingsupport.jar"));
if (file != null && file.exists()) {
VMRunnerConfiguration config = new VMRunnerConfiguration("org.eclipse.jdt.internal.launching.support.LegacySystemProperties", new String[] { file.getAbsolutePath() });
IVMRunner runner = getVMRunner(ILaunchManager.RUN_MODE);
if (runner == null) {
abort(NLS.bind(LaunchingMessages.AbstractVMInstall_0, ""), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
}
if (!(this instanceof Standard11xVM)) {
config.setVMArguments(new String[] { StandardVMType.MIN_VM_SIZE });
}
config.setProgramArguments(properties);
Launch launch = new Launch(null, ILaunchManager.RUN_MODE, null);
if (monitor.isCanceled()) {
return map;
}
monitor.beginTask(LaunchingMessages.AbstractVMInstall_1, 2);
runner.run(config, launch, monitor);
IProcess[] processes = launch.getProcesses();
if (processes.length != 1) {
abort(NLS.bind(LaunchingMessages.AbstractVMInstall_0, runner), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
}
IProcess process = processes[0];
try {
int total = 0;
int max = Platform.getPreferencesService().getInt(
LaunchingPlugin.ID_PLUGIN,
JavaRuntime.PREF_CONNECT_TIMEOUT,
JavaRuntime.DEF_CONNECT_TIMEOUT,
null);
while (!process.isTerminated()) {
try {
if (total > max) {
break;
}
Thread.sleep(50);
total+=50;
} catch (InterruptedException e) {
}
}
} finally {
if (!launch.isTerminated()) {
launch.terminate();
}
}
monitor.worked(1);
if (monitor.isCanceled()) {
return map;
}
monitor.subTask(LaunchingMessages.AbstractVMInstall_3);
IStreamsProxy streamsProxy = process.getStreamsProxy();
String text = null;
if (streamsProxy != null) {
text = streamsProxy.getOutputStreamMonitor().getContents();
}
if (text != null && text.length() > 0) {
try {
DocumentBuilder parser = LaunchingPlugin.getParser();
Document document = parser.parse(new ByteArrayInputStream(text.getBytes()));
Element envs = document.getDocumentElement();
NodeList list = envs.getChildNodes();
int length = list.getLength();
for (int i = 0; i < length; ++i) {
Node node = list.item(i);
short type = node.getNodeType();
if (type == Node.ELEMENT_NODE) {
Element element = (Element) node;
if (element.getNodeName().equals("property")) {
String name = element.getAttribute("name");
String value = element.getAttribute("value");
map.put(name, value);
}
}
}
} catch (SAXException e) {
String commandLine = process.getAttribute(IProcess.ATTR_CMDLINE);
abort(NLS.bind(LaunchingMessages.AbstractVMInstall_4, commandLine), e, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
} catch (IOException e) {
String commandLine = process.getAttribute(IProcess.ATTR_CMDLINE);
abort(NLS.bind(LaunchingMessages.AbstractVMInstall_4, commandLine), e, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
}
} else {
String commandLine = process.getAttribute(IProcess.ATTR_CMDLINE);
abort(NLS.bind(LaunchingMessages.AbstractVMInstall_0, commandLine), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
}
monitor.worked(1);
} else {
abort(NLS.bind(LaunchingMessages.AbstractVMInstall_0, file), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
}
Iterator<String> keys = map.keySet().iterator();
while (keys.hasNext()) {
String property = keys.next();
String value = map.get(property);
String key = getSystemPropertyKey(property);
prefs.put(key, value);
}
}
monitor.done();
return map;
}
private String getSystemPropertyKey(String property) {
StringBuilder buffer = new StringBuilder();
buffer.append(PREF_VM_INSTALL_SYSTEM_PROPERTY);
buffer.append(".");
buffer.append(getVMInstallType().getId());
buffer.append(".");
buffer.append(getId());
buffer.append(".");
buffer.append(property);
return buffer.toString();
}
protected void abort(String message, Throwable exception, int code) throws CoreException {
throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin
.getUniqueIdentifier(), code, message, exception));
}
public void setAttribute(String key, String value) {
String prevValue = fAttributeMap.remove(key);
boolean notify = false;
if (value == null) {
if (prevValue != null && fNotify) {
notify = true;
}
} else {
fAttributeMap.put(key, value);
if (fNotify && (prevValue == null || !prevValue.equals(value))) {
notify = true;
}
}
if (notify) {
PropertyChangeEvent event = new PropertyChangeEvent(this, key, prevValue, value);
JavaRuntime.fireVMChanged(event);
}
}
public String getAttribute(String key) {
return fAttributeMap.get(key);
}
public Map<String, String> getAttributes() {
return new HashMap<>(fAttributeMap);
}
}