package org.eclipse.jdt.launching;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.launching.CompositeId;
import org.eclipse.jdt.internal.launching.DefaultEntryResolver;
import org.eclipse.jdt.internal.launching.DefaultProjectClasspathEntry;
import org.eclipse.jdt.internal.launching.EEVMInstall;
import org.eclipse.jdt.internal.launching.EEVMType;
import org.eclipse.jdt.internal.launching.JREContainerInitializer;
import org.eclipse.jdt.internal.launching.JavaSourceLookupUtil;
import org.eclipse.jdt.internal.launching.LaunchingMessages;
import org.eclipse.jdt.internal.launching.LaunchingPlugin;
import org.eclipse.jdt.internal.launching.RuntimeClasspathEntry;
import org.eclipse.jdt.internal.launching.RuntimeClasspathEntryResolver;
import org.eclipse.jdt.internal.launching.RuntimeClasspathProvider;
import org.eclipse.jdt.internal.launching.SocketAttachConnector;
import org.eclipse.jdt.internal.launching.StandardVMType;
import org.eclipse.jdt.internal.launching.VMDefinitionsContainer;
import org.eclipse.jdt.internal.launching.VMListener;
import org.eclipse.jdt.internal.launching.VariableClasspathEntry;
import org.eclipse.jdt.internal.launching.environments.EnvironmentsManager;
import org.eclipse.jdt.launching.environments.ExecutionEnvironmentDescription;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.ibm.icu.text.MessageFormat;
public final class JavaRuntime {
public static final String JRELIB_VARIABLE= "JRE_LIB";
public static final String JRESRC_VARIABLE= "JRE_SRC";
public static final String JRESRCROOT_VARIABLE= "JRE_SRCROOT";
public static final String EXTENSION_POINT_RUNTIME_CLASSPATH_ENTRY_RESOLVERS= "runtimeClasspathEntryResolvers";
public static final String EXTENSION_POINT_RUNTIME_CLASSPATH_PROVIDERS= "classpathProviders";
public static final String EXTENSION_POINT_EXECUTION_ENVIRONMENTS= "executionEnvironments";
public static final String EXTENSION_POINT_VM_INSTALLS = "vmInstalls";
public static final String JRE_CONTAINER = LaunchingPlugin.getUniqueIdentifier() + ".JRE_CONTAINER";
public static final int ERR_UNABLE_TO_RESOLVE_JRE = 160;
public static final String PREF_CONNECT_TIMEOUT = LaunchingPlugin.getUniqueIdentifier() + ".PREF_CONNECT_TIMEOUT";
public static final String PREF_VM_XML = LaunchingPlugin.getUniqueIdentifier() + ".PREF_VM_XML";
public static final String PREF_STRICTLY_COMPATIBLE_JRE_NOT_AVAILABLE = LaunchingPlugin.getUniqueIdentifier() + ".PREF_STRICTLY_COMPATIBLE_JRE_NOT_AVAILABLE";
public static final String ID_PLUGIN = LaunchingPlugin.ID_PLUGIN;
public static final int DEF_CONNECT_TIMEOUT = 20000;
public final static String ATTR_CMDLINE= LaunchingPlugin.getUniqueIdentifier() + ".launcher.cmdLine";
public static final String CLASSPATH_ATTR_LIBRARY_PATH_ENTRY = LaunchingPlugin.getUniqueIdentifier() + ".CLASSPATH_ATTR_LIBRARY_PATH_ENTRY";
private static Object fgVMLock = new Object();
private static boolean fgInitializingVMs = false;
private static HashSet fgVMTypes = null;
private static String fgDefaultVMId = null;
private static String fgDefaultVMConnectorId = null;
private static Map fgVariableResolvers = null;
private static Map fgContainerResolvers = null;
private static Map fgRuntimeClasspathEntryResolvers = null;
private static Map fgPathProviders = null;
private static IRuntimeClasspathProvider fgDefaultClasspathProvider = new StandardClasspathProvider();
private static IRuntimeClasspathProvider fgDefaultSourcePathProvider = new StandardSourcePathProvider();
private static ListenerList fgVMListeners = new ListenerList();
private static ThreadLocal fgProjects = new ThreadLocal(); private static ThreadLocal fgEntryCount = new ThreadLocal();
private static Set fgContributedVMs = new HashSet();
private JavaRuntime() {
}
private static void initializeVMTypeExtensions() {
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, "vmInstallTypes"); if(extensionPoint != null) {
IConfigurationElement[] configs = extensionPoint.getConfigurationElements();
MultiStatus status = new MultiStatus(LaunchingPlugin.getUniqueIdentifier(), IStatus.OK, "Exceptions occurred", null); fgVMTypes = new HashSet();
for (int i= 0; i < configs.length; i++) {
try {
fgVMTypes.add(configs[i].createExecutableExtension("class")); }
catch (CoreException e) {status.add(e.getStatus());}
}
if (!status.isOK()) {
LaunchingPlugin.log(status);
}
}
else {
LaunchingPlugin.log(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), "VM Install extension point not found", null)); }
}
public static IVMInstall getVMInstall(IJavaProject project) throws CoreException {
IVMInstall vm = null;
IClasspathEntry[] classpath = project.getRawClasspath();
IRuntimeClasspathEntryResolver resolver = null;
IClasspathEntry entry = null;
for (int i = 0; i < classpath.length; i++) {
entry = classpath[i];
switch (entry.getEntryKind()) {
case IClasspathEntry.CPE_VARIABLE:
resolver = getVariableResolver(entry.getPath().segment(0));
if (resolver != null) {
vm = resolver.resolveVMInstall(entry);
}
break;
case IClasspathEntry.CPE_CONTAINER:
resolver = getContainerResolver(entry.getPath().segment(0));
if (resolver != null) {
vm = resolver.resolveVMInstall(entry);
}
break;
}
if (vm != null) {
return vm;
}
}
return null;
}
public static IVMInstallType getVMInstallType(String id) {
IVMInstallType[] vmTypes= getVMInstallTypes();
for (int i= 0; i < vmTypes.length; i++) {
if (vmTypes[i].getId().equals(id)) {
return vmTypes[i];
}
}
return null;
}
public static void setDefaultVMInstall(IVMInstall vm, IProgressMonitor monitor) throws CoreException {
setDefaultVMInstall(vm, monitor, true);
}
public static void setDefaultVMInstall(IVMInstall vm, IProgressMonitor monitor, boolean savePreference) throws CoreException {
IVMInstall previous = null;
if (fgDefaultVMId != null) {
previous = getVMFromCompositeId(fgDefaultVMId);
}
fgDefaultVMId= getCompositeIdFromVM(vm);
if (savePreference) {
saveVMConfiguration();
}
IVMInstall current = null;
if (fgDefaultVMId != null) {
current = getVMFromCompositeId(fgDefaultVMId);
}
if (previous != current) {
notifyDefaultVMChanged(previous, current);
}
}
public static void setDefaultVMConnector(IVMConnector connector, IProgressMonitor monitor) throws CoreException {
fgDefaultVMConnectorId= connector.getIdentifier();
saveVMConfiguration();
}
public static IVMInstall getDefaultVMInstall() {
IVMInstall install= getVMFromCompositeId(getDefaultVMId());
if (install != null) {
File location = install.getInstallLocation();
if (location != null) {
if (location.exists()) {
return install;
}
}
}
if (install != null) {
install.getVMInstallType().disposeVMInstall(install.getId());
}
synchronized (fgVMLock) {
fgDefaultVMId = null;
fgVMTypes = null;
initializeVMs();
}
return getVMFromCompositeId(getDefaultVMId());
}
public static IVMConnector getDefaultVMConnector() {
String id = getDefaultVMConnectorId();
IVMConnector connector = null;
if (id != null) {
connector = getVMConnector(id);
}
if (connector == null) {
connector = new SocketAttachConnector();
}
return connector;
}
public static IVMInstallType[] getVMInstallTypes() {
initializeVMs();
return (IVMInstallType[]) fgVMTypes.toArray(new IVMInstallType[fgVMTypes.size()]);
}
private static String getDefaultVMId() {
initializeVMs();
return fgDefaultVMId;
}
private static String getDefaultVMConnectorId() {
initializeVMs();
return fgDefaultVMConnectorId;
}
public static String getCompositeIdFromVM(IVMInstall vm) {
if (vm == null) {
return null;
}
IVMInstallType vmType = vm.getVMInstallType();
String typeID = vmType.getId();
CompositeId id = new CompositeId(new String[] { typeID, vm.getId() });
return id.toString();
}
public static IVMInstall getVMFromCompositeId(String idString) {
if (idString == null || idString.length() == 0) {
return null;
}
CompositeId id= CompositeId.fromString(idString);
if (id.getPartCount() == 2) {
IVMInstallType vmType= getVMInstallType(id.get(0));
if (vmType != null) {
return vmType.findVMInstall(id.get(1));
}
}
return null;
}
public static IRuntimeClasspathEntry newStringVariableClasspathEntry(String expression) {
return new VariableClasspathEntry(expression);
}
public static IRuntimeClasspathEntry newDefaultProjectClasspathEntry(IJavaProject project) {
return new DefaultProjectClasspathEntry(project);
}
public static IRuntimeClasspathEntry newProjectRuntimeClasspathEntry(IJavaProject project) {
return newRuntimeClasspathEntry(JavaCore.newProjectEntry(project.getProject().getFullPath()));
}
public static IRuntimeClasspathEntry newArchiveRuntimeClasspathEntry(IResource resource) {
return newRuntimeClasspathEntry(JavaCore.newLibraryEntry(resource.getFullPath(), null, null));
}
public static IRuntimeClasspathEntry newArchiveRuntimeClasspathEntry(IPath path) {
return newRuntimeClasspathEntry(JavaCore.newLibraryEntry(path, null, null));
}
public static IRuntimeClasspathEntry newVariableRuntimeClasspathEntry(IPath path) {
return newRuntimeClasspathEntry(JavaCore.newVariableEntry(path, null, null));
}
public static IRuntimeClasspathEntry newRuntimeContainerClasspathEntry(IPath path, int classpathProperty) throws CoreException {
return newRuntimeContainerClasspathEntry(path, classpathProperty, null);
}
public static IRuntimeClasspathEntry newRuntimeContainerClasspathEntry(IPath path, int classpathProperty, IJavaProject project) throws CoreException {
RuntimeClasspathEntry entry = new RuntimeClasspathEntry(JavaCore.newContainerEntry(path), classpathProperty);
entry.setJavaProject(project);
return entry;
}
public static IRuntimeClasspathEntry newRuntimeClasspathEntry(String memento) throws CoreException {
try {
Element root = null;
DocumentBuilder parser = LaunchingPlugin.getParser();
StringReader reader = new StringReader(memento);
InputSource source = new InputSource(reader);
root = parser.parse(source).getDocumentElement();
String id = root.getAttribute("id"); if (id == null || id.length() == 0) {
return new RuntimeClasspathEntry(root);
}
IRuntimeClasspathEntry2 entry = LaunchingPlugin.getDefault().newRuntimeClasspathEntry(id);
NodeList list = root.getChildNodes();
Node node = null;
Element element = null;
for (int i = 0; i < list.getLength(); i++) {
node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
element = (Element)node;
if ("memento".equals(element.getNodeName())) { entry.initializeFrom(element);
}
}
}
return entry;
} catch (SAXException e) {
abort(LaunchingMessages.JavaRuntime_32, e);
} catch (IOException e) {
abort(LaunchingMessages.JavaRuntime_32, e);
}
return null;
}
private static IRuntimeClasspathEntry newRuntimeClasspathEntry(IClasspathEntry entry) {
return new RuntimeClasspathEntry(entry);
}
public static IRuntimeClasspathEntry[] computeUnresolvedRuntimeClasspath(IJavaProject project) throws CoreException {
IClasspathEntry[] entries = project.getRawClasspath();
List classpathEntries = new ArrayList(3);
for (int i = 0; i < entries.length; i++) {
IClasspathEntry entry = entries[i];
switch (entry.getEntryKind()) {
case IClasspathEntry.CPE_CONTAINER:
IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project);
if (container != null) {
switch (container.getKind()) {
case IClasspathContainer.K_APPLICATION:
break;
case IClasspathContainer.K_DEFAULT_SYSTEM:
classpathEntries.add(newRuntimeContainerClasspathEntry(container.getPath(), IRuntimeClasspathEntry.STANDARD_CLASSES, project));
break;
case IClasspathContainer.K_SYSTEM:
classpathEntries.add(newRuntimeContainerClasspathEntry(container.getPath(), IRuntimeClasspathEntry.BOOTSTRAP_CLASSES, project));
break;
}
}
break;
case IClasspathEntry.CPE_VARIABLE:
if (JRELIB_VARIABLE.equals(entry.getPath().segment(0))) {
IRuntimeClasspathEntry jre = newVariableRuntimeClasspathEntry(entry.getPath());
jre.setClasspathProperty(IRuntimeClasspathEntry.STANDARD_CLASSES);
classpathEntries.add(jre);
}
break;
default:
break;
}
}
classpathEntries.add(newDefaultProjectClasspathEntry(project));
return (IRuntimeClasspathEntry[]) classpathEntries.toArray(new IRuntimeClasspathEntry[classpathEntries.size()]);
}
public static IRuntimeClasspathEntry[] computeUnresolvedSourceLookupPath(ILaunchConfiguration configuration) throws CoreException {
return getSourceLookupPathProvider(configuration).computeUnresolvedClasspath(configuration);
}
public static IRuntimeClasspathEntry[] resolveSourceLookupPath(IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration) throws CoreException {
return getSourceLookupPathProvider(configuration).resolveClasspath(entries, configuration);
}
public static IRuntimeClasspathProvider getClasspathProvider(ILaunchConfiguration configuration) throws CoreException {
String providerId = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH_PROVIDER, (String)null);
IRuntimeClasspathProvider provider = null;
if (providerId == null) {
provider = fgDefaultClasspathProvider;
} else {
provider = (IRuntimeClasspathProvider)getClasspathProviders().get(providerId);
if (provider == null) {
abort(MessageFormat.format(LaunchingMessages.JavaRuntime_26, new String[]{providerId}), null);
}
}
return provider;
}
public static IRuntimeClasspathProvider getSourceLookupPathProvider(ILaunchConfiguration configuration) throws CoreException {
String providerId = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, (String)null);
IRuntimeClasspathProvider provider = null;
if (providerId == null) {
provider = fgDefaultSourcePathProvider;
} else {
provider = (IRuntimeClasspathProvider)getClasspathProviders().get(providerId);
if (provider == null) {
abort(MessageFormat.format(LaunchingMessages.JavaRuntime_27, new String[]{providerId}), null);
}
}
return provider;
}
public static IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEntry entry, ILaunchConfiguration configuration) throws CoreException {
switch (entry.getType()) {
case IRuntimeClasspathEntry.PROJECT:
IResource resource = entry.getResource();
if (resource instanceof IProject) {
IProject p = (IProject)resource;
IJavaProject project = JavaCore.create(p);
if (project == null || !p.isOpen() || !project.exists()) {
return new IRuntimeClasspathEntry[0];
}
IRuntimeClasspathEntry[] entries = resolveOutputLocations(project, entry.getClasspathProperty());
if (entries != null) {
return entries;
}
} else {
abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Classpath_references_non_existant_project___0__3, new String[]{entry.getPath().lastSegment()}), null);
}
break;
case IRuntimeClasspathEntry.VARIABLE:
IRuntimeClasspathEntryResolver resolver = getVariableResolver(entry.getVariableName());
if (resolver == null) {
IRuntimeClasspathEntry[] resolved = resolveVariableEntry(entry, null, configuration);
if (resolved != null) {
return resolved;
}
break;
}
return resolver.resolveRuntimeClasspathEntry(entry, configuration);
case IRuntimeClasspathEntry.CONTAINER:
resolver = getContainerResolver(entry.getVariableName());
if (resolver == null) {
return computeDefaultContainerEntries(entry, configuration);
}
return resolver.resolveRuntimeClasspathEntry(entry, configuration);
case IRuntimeClasspathEntry.ARCHIVE:
String location = entry.getLocation();
if (location == null) {
abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Classpath_references_non_existant_archive___0__4, new String[]{entry.getPath().toString()}), null);
}
File file = new File(location);
if (!file.exists()) {
abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Classpath_references_non_existant_archive___0__4, new String[]{entry.getPath().toString()}), null);
}
break;
case IRuntimeClasspathEntry.OTHER:
resolver = getContributedResolver(((IRuntimeClasspathEntry2)entry).getTypeId());
return resolver.resolveRuntimeClasspathEntry(entry, configuration);
default:
break;
}
return new IRuntimeClasspathEntry[] {entry};
}
private static IRuntimeClasspathEntry[] resolveVariableEntry(IRuntimeClasspathEntry entry, IJavaProject project, ILaunchConfiguration configuration) throws CoreException {
IPath archPath = JavaCore.getClasspathVariable(entry.getVariableName());
if (archPath != null) {
if (entry.getPath().segmentCount() > 1) {
archPath = archPath.append(entry.getPath().removeFirstSegments(1));
}
IPath srcPath = null;
IPath srcVar = entry.getSourceAttachmentPath();
IPath srcRootPath = null;
IPath srcRootVar = entry.getSourceAttachmentRootPath();
if (archPath != null && !archPath.isEmpty()) {
if (srcVar != null && !srcVar.isEmpty()) {
srcPath = JavaCore.getClasspathVariable(srcVar.segment(0));
if (srcPath != null) {
if (srcVar.segmentCount() > 1) {
srcPath = srcPath.append(srcVar.removeFirstSegments(1));
}
if (srcRootVar != null && !srcRootVar.isEmpty()) {
srcRootPath = JavaCore.getClasspathVariable(srcRootVar.segment(0));
if (srcRootPath != null) {
if (srcRootVar.segmentCount() > 1) {
srcRootPath = srcRootPath.append(srcRootVar.removeFirstSegments(1));
}
}
}
}
}
IClasspathEntry archEntry = JavaCore.newLibraryEntry(archPath, srcPath, srcRootPath, entry.getClasspathEntry().isExported());
IRuntimeClasspathEntry runtimeArchEntry = newRuntimeClasspathEntry(archEntry);
runtimeArchEntry.setClasspathProperty(entry.getClasspathProperty());
if (configuration == null) {
return resolveRuntimeClasspathEntry(runtimeArchEntry, project);
}
return resolveRuntimeClasspathEntry(runtimeArchEntry, configuration);
}
}
return null;
}
private static IRuntimeClasspathEntry[] resolveOutputLocations(IJavaProject project, int classpathProperty) throws CoreException {
List nonDefault = new ArrayList();
if (project.exists() && project.getProject().isOpen()) {
IClasspathEntry entries[] = project.getRawClasspath();
for (int i = 0; i < entries.length; i++) {
IClasspathEntry classpathEntry = entries[i];
if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
IPath path = classpathEntry.getOutputLocation();
if (path != null) {
nonDefault.add(path);
}
}
}
}
if (nonDefault.isEmpty()) {
return null;
}
IPath def = project.getOutputLocation();
if (!nonDefault.contains(def)) {
nonDefault.add(def);
}
IRuntimeClasspathEntry[] locations = new IRuntimeClasspathEntry[nonDefault.size()];
for (int i = 0; i < locations.length; i++) {
IClasspathEntry newEntry = JavaCore.newLibraryEntry((IPath)nonDefault.get(i), null, null);
locations[i] = new RuntimeClasspathEntry(newEntry);
locations[i].setClasspathProperty(classpathProperty);
}
return locations;
}
public static IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEntry entry, IJavaProject project) throws CoreException {
switch (entry.getType()) {
case IRuntimeClasspathEntry.PROJECT:
IResource resource = entry.getResource();
if (resource instanceof IProject) {
IProject p = (IProject)resource;
IJavaProject jp = JavaCore.create(p);
if (jp != null && p.isOpen() && jp.exists()) {
IRuntimeClasspathEntry[] entries = resolveOutputLocations(jp, entry.getClasspathProperty());
if (entries != null) {
return entries;
}
} else {
return new IRuntimeClasspathEntry[0];
}
}
break;
case IRuntimeClasspathEntry.VARIABLE:
IRuntimeClasspathEntryResolver resolver = getVariableResolver(entry.getVariableName());
if (resolver == null) {
IRuntimeClasspathEntry[] resolved = resolveVariableEntry(entry, project, null);
if (resolved != null) {
return resolved;
}
break;
}
return resolver.resolveRuntimeClasspathEntry(entry, project);
case IRuntimeClasspathEntry.CONTAINER:
resolver = getContainerResolver(entry.getVariableName());
if (resolver == null) {
return computeDefaultContainerEntries(entry, project);
}
return resolver.resolveRuntimeClasspathEntry(entry, project);
case IRuntimeClasspathEntry.OTHER:
resolver = getContributedResolver(((IRuntimeClasspathEntry2)entry).getTypeId());
return resolver.resolveRuntimeClasspathEntry(entry, project);
default:
break;
}
return new IRuntimeClasspathEntry[] {entry};
}
private static IRuntimeClasspathEntry[] computeDefaultContainerEntries(IRuntimeClasspathEntry entry, ILaunchConfiguration config) throws CoreException {
IJavaProject project = entry.getJavaProject();
if (project == null) {
project = getJavaProject(config);
}
return computeDefaultContainerEntries(entry, project);
}
private static IRuntimeClasspathEntry[] computeDefaultContainerEntries(IRuntimeClasspathEntry entry, IJavaProject project) throws CoreException {
if (project == null || entry == null) {
return new IRuntimeClasspathEntry[0];
}
IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project);
if (container == null) {
abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Could_not_resolve_classpath_container___0__1, new String[]{entry.getPath().toString()}), null);
return null;
}
IClasspathEntry[] cpes = container.getClasspathEntries();
int property = -1;
switch (container.getKind()) {
case IClasspathContainer.K_APPLICATION:
property = IRuntimeClasspathEntry.USER_CLASSES;
break;
case IClasspathContainer.K_DEFAULT_SYSTEM:
property = IRuntimeClasspathEntry.STANDARD_CLASSES;
break;
case IClasspathContainer.K_SYSTEM:
property = IRuntimeClasspathEntry.BOOTSTRAP_CLASSES;
break;
}
List resolved = new ArrayList(cpes.length);
List projects = (List) fgProjects.get();
Integer count = (Integer) fgEntryCount.get();
if (projects == null) {
projects = new ArrayList();
fgProjects.set(projects);
count = new Integer(0);
}
int intCount = count.intValue();
intCount++;
fgEntryCount.set(new Integer(intCount));
try {
for (int i = 0; i < cpes.length; i++) {
IClasspathEntry cpe = cpes[i];
if (cpe.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(cpe.getPath().segment(0));
IJavaProject jp = JavaCore.create(p);
if (!projects.contains(jp)) {
projects.add(jp);
IRuntimeClasspathEntry classpath = newDefaultProjectClasspathEntry(jp);
IRuntimeClasspathEntry[] entries = resolveRuntimeClasspathEntry(classpath, jp);
for (int j = 0; j < entries.length; j++) {
IRuntimeClasspathEntry e = entries[j];
if (!resolved.contains(e)) {
resolved.add(entries[j]);
}
}
}
} else {
IRuntimeClasspathEntry e = newRuntimeClasspathEntry(cpe);
if (!resolved.contains(e)) {
resolved.add(e);
}
}
}
} finally {
intCount--;
if (intCount == 0) {
fgProjects.set(null);
fgEntryCount.set(null);
} else {
fgEntryCount.set(new Integer(intCount));
}
}
IRuntimeClasspathEntry[] result = new IRuntimeClasspathEntry[resolved.size()];
for (int i = 0; i < result.length; i++) {
result[i] = (IRuntimeClasspathEntry) resolved.get(i);
result[i].setClasspathProperty(property);
}
return result;
}
public static IRuntimeClasspathEntry[] computeUnresolvedRuntimeClasspath(ILaunchConfiguration configuration) throws CoreException {
return getClasspathProvider(configuration).computeUnresolvedClasspath(configuration);
}
public static IRuntimeClasspathEntry[] resolveRuntimeClasspath(IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration) throws CoreException {
return getClasspathProvider(configuration).resolveClasspath(entries, configuration);
}
public static IJavaProject getJavaProject(ILaunchConfiguration configuration) throws CoreException {
String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String)null);
if ((projectName == null) || (projectName.trim().length() < 1)) {
return null;
}
IJavaProject javaProject = getJavaModel().getJavaProject(projectName);
if (javaProject != null && javaProject.getProject().exists() && !javaProject.getProject().isOpen()) {
abort(MessageFormat.format(LaunchingMessages.JavaRuntime_28, new String[] {configuration.getName(), projectName}), IJavaLaunchConfigurationConstants.ERR_PROJECT_CLOSED, null);
}
if ((javaProject == null) || !javaProject.exists()) {
abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Launch_configuration__0__references_non_existing_project__1___1, new String[] {configuration.getName(), projectName}), IJavaLaunchConfigurationConstants.ERR_NOT_A_JAVA_PROJECT, null);
}
return javaProject;
}
private static IJavaModel getJavaModel() {
return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
}
public static IVMInstall computeVMInstall(ILaunchConfiguration configuration) throws CoreException {
String jreAttr = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String)null);
if (jreAttr == null) {
String type = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String)null);
if (type == null) {
IJavaProject proj = getJavaProject(configuration);
if (proj != null) {
IVMInstall vm = getVMInstall(proj);
if (vm != null) {
return vm;
}
}
} else {
String name = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, (String)null);
return resolveVM(type, name, configuration);
}
} else {
IPath jrePath = Path.fromPortableString(jreAttr);
IClasspathEntry entry = JavaCore.newContainerEntry(jrePath);
IRuntimeClasspathEntryResolver2 resolver = getVariableResolver(jrePath.segment(0));
if (resolver != null) {
return resolver.resolveVMInstall(entry);
} else {
resolver = getContainerResolver(jrePath.segment(0));
if (resolver != null) {
return resolver.resolveVMInstall(entry);
}
}
}
return getDefaultVMInstall();
}
private static IVMInstall resolveVM(String type, String name, ILaunchConfiguration configuration) throws CoreException {
IVMInstallType vt = getVMInstallType(type);
if (vt == null) {
abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Specified_VM_install_type_does_not_exist___0__2, new String[] {type}), null);
}
IVMInstall vm = null;
if (name == null) {
LaunchingPlugin.log(new Status(IStatus.WARNING, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_VM_INSTALL, MessageFormat.format("VM not fully specified in launch configuration {0} - missing VM name. Reverting to default VM.", new String[] {configuration.getName()}), null)); return getDefaultVMInstall();
}
vm = vt.findVMInstallByName(name);
if (vm == null) {
abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Specified_VM_install_not_found__type__0___name__1__2, new String[] {vt.getName(), name}), null);
} else {
return vm;
}
return null;
}
private static void abort(String message, Throwable exception) throws CoreException {
abort(message, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR, exception);
}
private static void abort(String message, int code, Throwable exception) throws CoreException {
throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), code, message, exception));
}
public static String[] computeDefaultRuntimeClassPath(IJavaProject jproject) throws CoreException {
IRuntimeClasspathEntry[] unresolved = computeUnresolvedRuntimeClasspath(jproject);
List resolved = new ArrayList(unresolved.length);
for (int i = 0; i < unresolved.length; i++) {
IRuntimeClasspathEntry entry = unresolved[i];
if (entry.getClasspathProperty() == IRuntimeClasspathEntry.USER_CLASSES) {
IRuntimeClasspathEntry[] entries = resolveRuntimeClasspathEntry(entry, jproject);
for (int j = 0; j < entries.length; j++) {
String location = entries[j].getLocation();
if (location != null) {
resolved.add(location);
}
}
}
}
return (String[])resolved.toArray(new String[resolved.size()]);
}
public static void saveVMConfiguration() throws CoreException {
if (fgVMTypes == null) {
return;
}
String xml = getVMsAsXML();
getPreferences().setValue(PREF_VM_XML, xml);
savePreferences();
}
private static String getVMsAsXML() throws CoreException {
VMDefinitionsContainer container = new VMDefinitionsContainer();
container.setDefaultVMInstallCompositeID(getDefaultVMId());
container.setDefaultVMInstallConnectorTypeID(getDefaultVMConnectorId());
IVMInstallType[] vmTypes = getVMInstallTypes();
IVMInstall[] vms = null;
for (int i = 0; i < vmTypes.length; ++i) {
vms = vmTypes[i].getVMInstalls();
for (int j = 0; j < vms.length; j++) {
container.addVM(vms[j]);
}
}
return container.getAsXML();
}
private static boolean addPersistedVMs(VMDefinitionsContainer vmDefs) throws IOException {
String vmXMLString = getPreferences().getString(PREF_VM_XML);
if (vmXMLString.length() > 0) {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(vmXMLString.getBytes("UTF8")); VMDefinitionsContainer.parseXMLIntoContainer(inputStream, vmDefs);
return false;
} catch (IOException ioe) {
LaunchingPlugin.log(ioe);
}
} else {
IPath stateLocation= LaunchingPlugin.getDefault().getStateLocation();
IPath stateFile= stateLocation.append("vmConfiguration.xml"); File file = new File(stateFile.toOSString());
if (file.exists()) {
InputStream fileInputStream = new BufferedInputStream(new FileInputStream(file));
VMDefinitionsContainer.parseXMLIntoContainer(fileInputStream, vmDefs);
}
}
return true;
}
private static void addVMExtensions(VMDefinitionsContainer vmDefs) {
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, JavaRuntime.EXTENSION_POINT_VM_INSTALLS);
IConfigurationElement[] configs= extensionPoint.getConfigurationElements();
for (int i = 0; i < configs.length; i++) {
IConfigurationElement element = configs[i];
try {
if ("vmInstall".equals(element.getName())) { String vmType = element.getAttribute("vmInstallType"); if (vmType == null) {
abort(MessageFormat.format("Missing required vmInstallType attribute for vmInstall contributed by {0}", new String[]{element.getContributor().getName()}), null);
}
String id = element.getAttribute("id"); if (id == null) {
abort(MessageFormat.format("Missing required id attribute for vmInstall contributed by {0}", new String[]{element.getContributor().getName()}), null);
}
IVMInstallType installType = getVMInstallType(vmType);
if (installType == null) {
abort(MessageFormat.format("vmInstall {0} contributed by {1} references undefined VM install type {2}", new String[]{id, element.getContributor().getName(), vmType}), null);
}
IVMInstall install = installType.findVMInstall(id);
if (install == null) {
String name = element.getAttribute("name"); if (name == null) {
abort(MessageFormat.format("vmInstall {0} contributed by {1} missing required attribute name", new String[]{id, element.getContributor().getName()}), null);
}
String home = element.getAttribute("home"); if (home == null) {
abort(MessageFormat.format("vmInstall {0} contributed by {1} missing required attribute home", new String[]{id, element.getContributor().getName()}), null);
}
String javadoc = element.getAttribute("javadocURL"); String vmArgs = element.getAttribute("vmArgs"); VMStandin standin = null;
home = substitute(home);
File homeDir = new File(home);
if (homeDir.exists()) {
try {
home = homeDir.getCanonicalPath();
homeDir = new File(home);
} catch (IOException e) {
}
}
if (EEVMType.ID_EE_VM_TYPE.equals(installType.getId())) {
standin = createVMFromDefinitionFile(homeDir, name, id);
} else {
standin = new VMStandin(installType, id);
standin.setName(name);
IStatus status = installType.validateInstallLocation(homeDir);
if (!status.isOK()) {
abort(MessageFormat.format("Illegal install location {0} for vmInstall {1} contributed by {2}: {3}", new String[]{home, id, element.getContributor().getName(), status.getMessage()}), null);
}
standin.setInstallLocation(homeDir);
if (javadoc != null) {
try {
standin.setJavadocLocation(new URL(javadoc));
} catch (MalformedURLException e) {
abort(MessageFormat.format("Illegal javadocURL attribute for vmInstall {0} contributed by {1}", new String[]{id, element.getContributor().getName()}), e);
}
}
if (vmArgs == null) {
if (installType instanceof AbstractVMInstallType) {
AbstractVMInstallType type = (AbstractVMInstallType) installType;
vmArgs = type.getDefaultVMArguments(homeDir);
}
}
if (vmArgs != null) {
standin.setVMArgs(vmArgs);
}
IConfigurationElement[] libraries = element.getChildren("library"); LibraryLocation[] locations = null;
if (libraries.length > 0) {
locations = new LibraryLocation[libraries.length];
for (int j = 0; j < libraries.length; j++) {
IConfigurationElement library = libraries[j];
String libPathStr = library.getAttribute("path"); if (libPathStr == null) {
abort(MessageFormat.format("library for vmInstall {0} contributed by {1} missing required attribute libPath", new String[]{id, element.getContributor().getName()}), null);
}
String sourcePathStr = library.getAttribute("sourcePath"); String packageRootStr = library.getAttribute("packageRootPath"); String javadocOverride = library.getAttribute("javadocURL"); URL url = null;
if (javadocOverride != null) {
try {
url = new URL(javadocOverride);
} catch (MalformedURLException e) {
abort(MessageFormat.format("Illegal javadocURL attribute specified for library {0} for vmInstall {1} contributed by {2}" ,new String[]{libPathStr, id, element.getContributor().getName()}), e);
}
}
IPath homePath = new Path(home);
IPath libPath = homePath.append(substitute(libPathStr));
IPath sourcePath = Path.EMPTY;
if (sourcePathStr != null) {
sourcePath = homePath.append(substitute(sourcePathStr));
}
IPath packageRootPath = Path.EMPTY;
if (packageRootStr != null) {
packageRootPath = new Path(substitute(packageRootStr));
}
locations[j] = new LibraryLocation(libPath, sourcePath, packageRootPath, url);
}
}
standin.setLibraryLocations(locations);
}
vmDefs.removeVM(standin);
vmDefs.addVM(standin);
}
fgContributedVMs.add(id);
} else {
abort(MessageFormat.format("Illegal element {0} in vmInstalls extension contributed by {1}", new String[]{element.getName(), element.getContributor().getName()}), null);
}
} catch (CoreException e) {
LaunchingPlugin.log(e);
}
}
}
private static String substitute(String expression) throws CoreException {
return VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(expression);
}
public static boolean isContributedVMInstall(String id) {
getVMInstallTypes(); return fgContributedVMs.contains(id);
}
public static LibraryLocation[] getLibraryLocations(IVMInstall vm) {
IPath[] libraryPaths;
IPath[] sourcePaths;
IPath[] sourceRootPaths;
URL[] javadocLocations;
LibraryLocation[] locations= vm.getLibraryLocations();
if (locations == null) {
URL defJavaDocLocation = vm.getJavadocLocation();
File installLocation = vm.getInstallLocation();
if (installLocation == null) {
return new LibraryLocation[0];
}
LibraryLocation[] dflts= vm.getVMInstallType().getDefaultLibraryLocations(installLocation);
libraryPaths = new IPath[dflts.length];
sourcePaths = new IPath[dflts.length];
sourceRootPaths = new IPath[dflts.length];
javadocLocations= new URL[dflts.length];
for (int i = 0; i < dflts.length; i++) {
libraryPaths[i]= dflts[i].getSystemLibraryPath();
if (defJavaDocLocation == null) {
javadocLocations[i]= dflts[i].getJavadocLocation();
} else {
javadocLocations[i]= defJavaDocLocation;
}
if (!libraryPaths[i].toFile().isFile()) {
libraryPaths[i]= Path.EMPTY;
}
sourcePaths[i]= dflts[i].getSystemLibrarySourcePath();
if (sourcePaths[i].toFile().isFile()) {
sourceRootPaths[i]= dflts[i].getPackageRootPath();
} else {
sourcePaths[i]= Path.EMPTY;
sourceRootPaths[i]= Path.EMPTY;
}
}
} else {
libraryPaths = new IPath[locations.length];
sourcePaths = new IPath[locations.length];
sourceRootPaths = new IPath[locations.length];
javadocLocations= new URL[locations.length];
for (int i = 0; i < locations.length; i++) {
libraryPaths[i]= locations[i].getSystemLibraryPath();
sourcePaths[i]= locations[i].getSystemLibrarySourcePath();
sourceRootPaths[i]= locations[i].getPackageRootPath();
javadocLocations[i]= locations[i].getJavadocLocation();
}
}
locations = new LibraryLocation[sourcePaths.length];
for (int i = 0; i < sourcePaths.length; i++) {
locations[i] = new LibraryLocation(libraryPaths[i], sourcePaths[i], sourceRootPaths[i], javadocLocations[i]);
}
return locations;
}
private static VMStandin detectEclipseRuntime() {
IVMInstallType[] vmTypes= getVMInstallTypes();
for (int i = 0; i < vmTypes.length; i++) {
if (vmTypes[i] instanceof EEVMType){
String eeFileName = System.getProperty("ee.filename"); if (eeFileName != null){
File vmFile = new File(eeFileName);
if (vmFile.isDirectory()){
vmFile = new File(vmFile, "default.ee"); }
if (vmFile.isFile()){
long unique = System.currentTimeMillis();
while (vmTypes[i].findVMInstall(String.valueOf(unique)) != null) {
unique++;
}
String vmID = String.valueOf(unique);
try{
return createVMFromDefinitionFile(vmFile, "", vmID); } catch (CoreException e){
}
}
}
}
}
for (int i = 0; i < vmTypes.length; i++) {
File detectedLocation= vmTypes[i].detectInstallLocation();
if (detectedLocation != null) {
long unique = System.currentTimeMillis();
IVMInstallType vmType = vmTypes[i];
while (vmType.findVMInstall(String.valueOf(unique)) != null) {
unique++;
}
String vmID = String.valueOf(unique);
VMStandin detectedVMStandin = new VMStandin(vmType, vmID);
detectedVMStandin.setInstallLocation(detectedLocation);
detectedVMStandin.setName(generateDetectedVMName(detectedVMStandin));
if (vmType instanceof AbstractVMInstallType) {
AbstractVMInstallType abs = (AbstractVMInstallType)vmType;
URL url = abs.getDefaultJavadocLocation(detectedLocation);
detectedVMStandin.setJavadocLocation(url);
String arguments = abs.getDefaultVMArguments(detectedLocation);
if (arguments != null) {
detectedVMStandin.setVMArgs(arguments);
}
}
return detectedVMStandin;
}
}
return null;
}
private static boolean equals(String optionName, Map defaultOptions, Map options) {
if (defaultOptions.containsKey(optionName)) {
return options.containsKey(optionName) &&
equals(defaultOptions.get(optionName), options.get(optionName));
} else {
return !options.containsKey(optionName);
}
}
private static boolean equals(Object o1, Object o2) {
if (o1 == null) {
return o2 == null;
} else {
return o1.equals(o2);
}
}
private static String generateDetectedVMName(IVMInstall vm) {
String name = vm.getInstallLocation().getName();
name = name.trim();
if (name.length() == 0) {
name = LaunchingMessages.JavaRuntime_25;
}
return name;
}
public static IClasspathEntry getJREVariableEntry() {
return JavaCore.newVariableEntry(
new Path(JRELIB_VARIABLE),
new Path(JRESRC_VARIABLE),
new Path(JRESRCROOT_VARIABLE)
);
}
public static IClasspathEntry getDefaultJREContainerEntry() {
return JavaCore.newContainerEntry(newDefaultJREContainerPath());
}
public static IPath newDefaultJREContainerPath() {
return new Path(JRE_CONTAINER);
}
public static IPath newJREContainerPath(IVMInstall vm) {
return newJREContainerPath(vm.getVMInstallType().getId(), vm.getName());
}
public static IPath newJREContainerPath(String typeId, String name) {
IPath path = newDefaultJREContainerPath();
path = path.append(typeId);
path = path.append(name);
return path;
}
public static IPath newJREContainerPath(IExecutionEnvironment environment) {
IPath path = newDefaultJREContainerPath();
path = path.append(StandardVMType.ID_STANDARD_VM_TYPE);
path = path.append(JREContainerInitializer.encodeEnvironmentId(environment.getId()));
return path;
}
public static IVMInstall getVMInstall(IPath jreContainerPath) {
return JREContainerInitializer.resolveVM(jreContainerPath);
}
public static String getVMInstallTypeId(IPath jreContainerPath) {
if (JREContainerInitializer.isExecutionEnvironment(jreContainerPath)) {
return null;
}
return JREContainerInitializer.getVMTypeId(jreContainerPath);
}
public static String getVMInstallName(IPath jreContainerPath) {
if (JREContainerInitializer.isExecutionEnvironment(jreContainerPath)) {
return null;
}
return JREContainerInitializer.getVMName(jreContainerPath);
}
public static String getExecutionEnvironmentId(IPath jreContainerPath) {
return JREContainerInitializer.getExecutionEnvironmentId(jreContainerPath);
}
public static IRuntimeClasspathEntry computeJREEntry(ILaunchConfiguration configuration) throws CoreException {
String jreAttr = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String)null);
IPath containerPath = null;
if (jreAttr == null) {
String type = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String)null);
if (type == null) {
IJavaProject proj = getJavaProject(configuration);
if (proj == null) {
containerPath = newDefaultJREContainerPath();
} else {
return computeJREEntry(proj);
}
} else {
String name = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, (String)null);
if (name != null) {
containerPath = newDefaultJREContainerPath().append(type).append(name);
}
}
} else {
containerPath = Path.fromPortableString(jreAttr);
}
if (containerPath != null) {
return newRuntimeContainerClasspathEntry(containerPath, IRuntimeClasspathEntry.STANDARD_CLASSES);
}
return null;
}
public static IRuntimeClasspathEntry computeJREEntry(IJavaProject project) throws CoreException {
IClasspathEntry[] rawClasspath = project.getRawClasspath();
IRuntimeClasspathEntryResolver2 resolver = null;
for (int i = 0; i < rawClasspath.length; i++) {
IClasspathEntry entry = rawClasspath[i];
switch (entry.getEntryKind()) {
case IClasspathEntry.CPE_VARIABLE:
resolver = getVariableResolver(entry.getPath().segment(0));
if (resolver != null) {
if (resolver.isVMInstallReference(entry)) {
return newRuntimeClasspathEntry(entry);
}
}
break;
case IClasspathEntry.CPE_CONTAINER:
resolver = getContainerResolver(entry.getPath().segment(0));
if (resolver != null) {
if (resolver.isVMInstallReference(entry)) {
IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project);
if (container != null) {
switch (container.getKind()) {
case IClasspathContainer.K_APPLICATION:
break;
case IClasspathContainer.K_DEFAULT_SYSTEM:
return newRuntimeContainerClasspathEntry(entry.getPath(), IRuntimeClasspathEntry.STANDARD_CLASSES);
case IClasspathContainer.K_SYSTEM:
return newRuntimeContainerClasspathEntry(entry.getPath(), IRuntimeClasspathEntry.BOOTSTRAP_CLASSES);
}
}
}
}
break;
}
}
return null;
}
public static boolean isVMInstallReference(IRuntimeClasspathEntry entry) {
IClasspathEntry classpathEntry = entry.getClasspathEntry();
if (classpathEntry != null) {
switch (classpathEntry.getEntryKind()) {
case IClasspathEntry.CPE_VARIABLE:
IRuntimeClasspathEntryResolver2 resolver = getVariableResolver(classpathEntry.getPath().segment(0));
if (resolver != null) {
return resolver.isVMInstallReference(classpathEntry);
}
break;
case IClasspathEntry.CPE_CONTAINER:
resolver = getContainerResolver(classpathEntry.getPath().segment(0));
if (resolver != null) {
return resolver.isVMInstallReference(classpathEntry);
}
break;
}
}
return false;
}
public static IVMConnector getVMConnector(String id) {
return LaunchingPlugin.getDefault().getVMConnector(id);
}
public static IVMConnector[] getVMConnectors() {
return LaunchingPlugin.getDefault().getVMConnectors();
}
public static Preferences getPreferences() {
return LaunchingPlugin.getDefault().getPluginPreferences();
}
public static void savePreferences() {
LaunchingPlugin.getDefault().savePluginPreferences();
}
public static void addVariableResolver(IRuntimeClasspathEntryResolver resolver, String variableName) {
Map map = getVariableResolvers();
map.put(variableName, resolver);
}
public static void addContainerResolver(IRuntimeClasspathEntryResolver resolver, String containerIdentifier) {
Map map = getContainerResolvers();
map.put(containerIdentifier, resolver);
}
private static Map getVariableResolvers() {
if (fgVariableResolvers == null) {
initializeResolvers();
}
return fgVariableResolvers;
}
private static Map getContainerResolvers() {
if (fgContainerResolvers == null) {
initializeResolvers();
}
return fgContainerResolvers;
}
private static Map getEntryResolvers() {
if (fgRuntimeClasspathEntryResolvers == null) {
initializeResolvers();
}
return fgRuntimeClasspathEntryResolvers;
}
private static void initializeResolvers() {
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, EXTENSION_POINT_RUNTIME_CLASSPATH_ENTRY_RESOLVERS);
IConfigurationElement[] extensions = point.getConfigurationElements();
fgVariableResolvers = new HashMap(extensions.length);
fgContainerResolvers = new HashMap(extensions.length);
fgRuntimeClasspathEntryResolvers = new HashMap(extensions.length);
for (int i = 0; i < extensions.length; i++) {
RuntimeClasspathEntryResolver res = new RuntimeClasspathEntryResolver(extensions[i]);
String variable = res.getVariableName();
String container = res.getContainerId();
String entryId = res.getRuntimeClasspathEntryId();
if (variable != null) {
fgVariableResolvers.put(variable, res);
}
if (container != null) {
fgContainerResolvers.put(container, res);
}
if (entryId != null) {
fgRuntimeClasspathEntryResolvers.put(entryId, res);
}
}
}
private static Map getClasspathProviders() {
if (fgPathProviders == null) {
initializeProviders();
}
return fgPathProviders;
}
private static void initializeProviders() {
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, EXTENSION_POINT_RUNTIME_CLASSPATH_PROVIDERS);
IConfigurationElement[] extensions = point.getConfigurationElements();
fgPathProviders = new HashMap(extensions.length);
for (int i = 0; i < extensions.length; i++) {
RuntimeClasspathProvider res = new RuntimeClasspathProvider(extensions[i]);
fgPathProviders.put(res.getIdentifier(), res);
}
}
private static IRuntimeClasspathEntryResolver2 getVariableResolver(String variableName) {
return (IRuntimeClasspathEntryResolver2)getVariableResolvers().get(variableName);
}
private static IRuntimeClasspathEntryResolver2 getContainerResolver(String containerId) {
return (IRuntimeClasspathEntryResolver2)getContainerResolvers().get(containerId);
}
private static IRuntimeClasspathEntryResolver getContributedResolver(String typeId) {
IRuntimeClasspathEntryResolver resolver = (IRuntimeClasspathEntryResolver)getEntryResolvers().get(typeId);
if (resolver == null) {
return new DefaultEntryResolver();
}
return resolver;
}
public static void addVMInstallChangedListener(IVMInstallChangedListener listener) {
fgVMListeners.add(listener);
}
public static void removeVMInstallChangedListener(IVMInstallChangedListener listener) {
fgVMListeners.remove(listener);
}
private static void notifyDefaultVMChanged(IVMInstall previous, IVMInstall current) {
Object[] listeners = fgVMListeners.getListeners();
for (int i = 0; i < listeners.length; i++) {
IVMInstallChangedListener listener = (IVMInstallChangedListener)listeners[i];
listener.defaultVMInstallChanged(previous, current);
}
}
public static void fireVMChanged(PropertyChangeEvent event) {
Object[] listeners = fgVMListeners.getListeners();
for (int i = 0; i < listeners.length; i++) {
IVMInstallChangedListener listener = (IVMInstallChangedListener)listeners[i];
listener.vmChanged(event);
}
}
public static void fireVMAdded(IVMInstall vm) {
if (!fgInitializingVMs) {
Object[] listeners = fgVMListeners.getListeners();
for (int i = 0; i < listeners.length; i++) {
IVMInstallChangedListener listener = (IVMInstallChangedListener)listeners[i];
listener.vmAdded(vm);
}
}
}
public static void fireVMRemoved(IVMInstall vm) {
Object[] listeners = fgVMListeners.getListeners();
for (int i = 0; i < listeners.length; i++) {
IVMInstallChangedListener listener = (IVMInstallChangedListener)listeners[i];
listener.vmRemoved(vm);
}
}
public static String getProjectOutputDirectory(ILaunchConfiguration config) {
try {
if (config != null) {
IJavaProject javaProject = JavaRuntime.getJavaProject(config);
if (javaProject != null) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath outputLocation = javaProject.getOutputLocation();
IResource resource = root.findMember(outputLocation);
if (resource != null) {
IPath path = resource.getFullPath();
if (path != null) {
return path.makeRelative().toString();
}
}
}
}
} catch (CoreException ce) {
}
return null;
}
public static ISourceContainer[] getSourceContainers(IRuntimeClasspathEntry[] entries) {
return JavaSourceLookupUtil.translate(entries);
}
public static String[] computeJavaLibraryPath(IJavaProject project, boolean requiredProjects) throws CoreException {
Set visited = new HashSet();
List entries = new ArrayList();
gatherJavaLibraryPathEntries(project, requiredProjects, visited, entries);
List resolved = new ArrayList(entries.size());
Iterator iterator = entries.iterator();
IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
while (iterator.hasNext()) {
String entry = (String) iterator.next();
String resolvedEntry = manager.performStringSubstitution(entry);
IPath path = new Path(resolvedEntry);
if (path.isAbsolute()) {
File file = path.toFile();
resolved.add(file.getAbsolutePath());
} else {
IResource resource = root.findMember(path);
if (resource != null) {
IPath location = resource.getLocation();
if (location != null) {
resolved.add(location.toFile().getAbsolutePath());
}
}
}
}
return (String[])resolved.toArray(new String[resolved.size()]);
}
private static void gatherJavaLibraryPathEntries(IJavaProject project, boolean requiredProjects, Set visited, List entries) throws CoreException {
if (visited.contains(project)) {
return;
}
visited.add(project);
IClasspathEntry[] rawClasspath = project.getRawClasspath();
IClasspathEntry[] required = processJavaLibraryPathEntries(project, requiredProjects, rawClasspath, entries);
if (required != null) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
for (int i = 0; i < required.length; i++) {
IClasspathEntry entry = required[i];
String projectName = entry.getPath().segment(0);
IProject p = root.getProject(projectName);
if (p.exists()) {
IJavaProject requiredProject = JavaCore.create(p);
if (requiredProject != null) {
gatherJavaLibraryPathEntries(requiredProject, requiredProjects, visited, entries);
}
}
}
}
}
private static IClasspathEntry[] processJavaLibraryPathEntries(IJavaProject project, boolean collectRequired, IClasspathEntry[] classpathEntries, List entries) throws CoreException {
List req = null;
for (int i = 0; i < classpathEntries.length; i++) {
IClasspathEntry entry = classpathEntries[i];
IClasspathAttribute[] extraAttributes = entry.getExtraAttributes();
for (int j = 0; j < extraAttributes.length; j++) {
String[] paths = getLibraryPaths(extraAttributes[j]);
if (paths != null) {
for (int k = 0; k < paths.length; k++) {
entries.add(paths[k]);
}
}
}
if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project);
if (container != null) {
IClasspathEntry[] requiredProjects = processJavaLibraryPathEntries(project, collectRequired, container.getClasspathEntries(), entries);
if (requiredProjects != null) {
if (req == null) {
req = new ArrayList();
}
for (int j = 0; j < requiredProjects.length; j++) {
req.add(requiredProjects[j]);
}
}
}
} else if (collectRequired && entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
if (req == null) {
req = new ArrayList();
}
req.add(entry);
}
}
if (req != null) {
return (IClasspathEntry[]) req.toArray(new IClasspathEntry[req.size()]);
}
return null;
}
public static IClasspathAttribute newLibraryPathsAttribute(String[] paths) {
StringBuffer value = new StringBuffer();
for (int i = 0; i < paths.length; i++) {
value.append(paths[i]);
if (i < (paths.length - 1)) {
value.append("|"); }
}
return JavaCore.newClasspathAttribute(CLASSPATH_ATTR_LIBRARY_PATH_ENTRY, value.toString());
}
public static String[] getLibraryPaths(IClasspathAttribute attribute) {
if (CLASSPATH_ATTR_LIBRARY_PATH_ENTRY.equals(attribute.getName())) {
String value = attribute.getValue();
return value.split("\\|"); }
return null;
}
public static IExecutionEnvironmentsManager getExecutionEnvironmentsManager() {
return EnvironmentsManager.getDefault();
}
private static void initializeVMs() {
VMDefinitionsContainer vmDefs = null;
boolean setPref = false;
boolean updateCompliance = false;
synchronized (fgVMLock) {
if (fgVMTypes == null) {
try {
fgInitializingVMs = true;
initializeVMTypeExtensions();
try {
vmDefs = new VMDefinitionsContainer();
setPref = addPersistedVMs(vmDefs);
IStatus status = vmDefs.getStatus();
if (status != null) {
if (status.isMultiStatus()) {
MultiStatus multi = (MultiStatus) status;
IStatus[] children = multi.getChildren();
for (int i = 0; i < children.length; i++) {
IStatus child = children[i];
if (!child.isOK()) {
LaunchingPlugin.log(child);
}
}
} else if (!status.isOK()) {
LaunchingPlugin.log(status);
}
}
if (vmDefs.getValidVMList().isEmpty()) {
VMListener listener = new VMListener();
addVMInstallChangedListener(listener);
setPref = true;
VMStandin runtime = detectEclipseRuntime();
removeVMInstallChangedListener(listener);
if (!listener.isChanged()) {
if (runtime != null) {
updateCompliance = true;
vmDefs.addVM(runtime);
vmDefs.setDefaultVMInstallCompositeID(getCompositeIdFromVM(runtime));
}
} else {
addPersistedVMs(vmDefs);
vmDefs.setDefaultVMInstallCompositeID(fgDefaultVMId);
updateCompliance = fgDefaultVMId != null;
}
}
addVMExtensions(vmDefs);
String defId = vmDefs.getDefaultVMInstallCompositeID();
boolean validDef = false;
if (defId != null) {
Iterator iterator = vmDefs.getValidVMList().iterator();
while (iterator.hasNext()) {
IVMInstall vm = (IVMInstall) iterator.next();
if (getCompositeIdFromVM(vm).equals(defId)) {
validDef = true;
break;
}
}
}
if (!validDef) {
setPref = true;
List list = vmDefs.getValidVMList();
if (!list.isEmpty()) {
IVMInstall vm = (IVMInstall) list.get(0);
vmDefs.setDefaultVMInstallCompositeID(getCompositeIdFromVM(vm));
}
}
fgDefaultVMId = vmDefs.getDefaultVMInstallCompositeID();
fgDefaultVMConnectorId = vmDefs.getDefaultVMInstallConnectorTypeID();
List vmList = vmDefs.getValidVMList();
Iterator vmListIterator = vmList.iterator();
while (vmListIterator.hasNext()) {
VMStandin vmStandin = (VMStandin) vmListIterator.next();
vmStandin.convertToRealVM();
}
} catch (IOException e) {
LaunchingPlugin.log(e);
}
} finally {
fgInitializingVMs = false;
}
}
}
if (vmDefs != null) {
IVMInstallType[] installTypes = getVMInstallTypes();
for (int i = 0; i < installTypes.length; i++) {
IVMInstallType type = installTypes[i];
IVMInstall[] installs = type.getVMInstalls();
for (int j = 0; j < installs.length; j++) {
fireVMAdded(installs[j]);
}
}
if (setPref) {
try {
String xml = vmDefs.getAsXML();
LaunchingPlugin.getDefault().getPluginPreferences().setValue(PREF_VM_XML, xml);
} catch (CoreException e) {
LaunchingPlugin.log(e);
}
}
if (updateCompliance) {
updateCompliance(getDefaultVMInstall());
}
}
}
private static void updateCompliance(IVMInstall vm) {
if (vm instanceof IVMInstall2) {
String javaVersion = ((IVMInstall2)vm).getJavaVersion();
if (javaVersion != null) {
String compliance = null;
if (javaVersion.startsWith(JavaCore.VERSION_1_5)) {
compliance = JavaCore.VERSION_1_5;
} else if (javaVersion.startsWith(JavaCore.VERSION_1_6)) {
compliance = JavaCore.VERSION_1_6;
} else if (javaVersion.startsWith(JavaCore.VERSION_1_7)) {
compliance = JavaCore.VERSION_1_6;
}
if (compliance != null) {
Hashtable defaultOptions = JavaCore.getDefaultOptions();
Hashtable options = JavaCore.getOptions();
boolean isDefault =
equals(JavaCore.COMPILER_COMPLIANCE, defaultOptions, options) &&
equals(JavaCore.COMPILER_SOURCE, defaultOptions, options) &&
equals(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, defaultOptions, options) &&
equals(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, defaultOptions, options) &&
equals(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, defaultOptions, options);
if (isDefault) {
JavaCore.setComplianceOptions(compliance, options);
JavaCore.setOptions(options);
}
}
}
}
}
public static VMStandin createVMFromDefinitionFile(File eeFile, String name, String id) throws CoreException {
ExecutionEnvironmentDescription description = new ExecutionEnvironmentDescription(eeFile);
IStatus status = EEVMType.validateDefinitionFile(description);
if (status.isOK()) {
VMStandin standin = new VMStandin(getVMInstallType(EEVMType.ID_EE_VM_TYPE), id);
if (name != null && name.length() > 0){
standin.setName(name);
} else {
name = description.getProperty(ExecutionEnvironmentDescription.EE_NAME);
if (name == null) {
name = eeFile.getName();
}
standin.setName(name);
}
String home = description.getProperty(ExecutionEnvironmentDescription.JAVA_HOME);
standin.setInstallLocation(new File(home));
standin.setLibraryLocations(description.getLibraryLocations());
standin.setVMArgs(description.getVMArguments());
standin.setJavadocLocation(EEVMType.getJavadocLocation(description.getProperties()));
standin.setAttribute(EEVMInstall.ATTR_EXECUTION_ENVIRONMENT_ID, description.getProperty(ExecutionEnvironmentDescription.CLASS_LIB_LEVEL));
File exe = description.getExecutable();
if (exe == null) {
exe = description.getConsoleExecutable();
}
if (exe != null) {
try {
standin.setAttribute(EEVMInstall.ATTR_JAVA_EXE, exe.getCanonicalPath());
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(),
LaunchingMessages.JavaRuntime_24, e));
}
}
standin.setAttribute(EEVMInstall.ATTR_JAVA_VERSION, description.getProperty(ExecutionEnvironmentDescription.LANGUAGE_LEVEL));
standin.setAttribute(EEVMInstall.ATTR_DEFINITION_FILE, eeFile.getPath());
standin.setAttribute(EEVMInstall.ATTR_DEBUG_ARGS, description.getProperty(ExecutionEnvironmentDescription.DEBUG_ARGS));
return standin;
} else {
throw new CoreException(status);
}
}
}