package org.eclipse.debug.internal.core;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationMigrationDelegate;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchDelegate;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.ILaunchMode;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
import org.eclipse.debug.core.sourcelookup.ISourcePathComputer;
import com.ibm.icu.text.MessageFormat;
public class LaunchConfigurationType extends PlatformObject implements ILaunchConfigurationType {
private IConfigurationElement fElement;
private Set<String> fModes = null;
private Set<Set<String>> fModeCombinations = null;
private ISourcePathComputer fSourcePathComputer = null;
private ILaunchConfigurationMigrationDelegate fMigrationDelegate = null;
private String fSourceLocator = null;
private Map<Set<String>, Set<ILaunchDelegate>> fDelegates = null;
private LaunchDelegate fSourceProvider = null;
private Map<Set<String>, ILaunchDelegate> fPreferredDelegates = null;
protected LaunchConfigurationType(IConfigurationElement element) {
fElement = element;
initializePreferredDelegates();
}
@Override
public String getAttribute(String attributeName) {
return fElement.getAttribute(attributeName);
}
@Override
public String getCategory() {
return fElement.getAttribute(IConfigurationElementConstants.CATEGORY);
}
@Override
public ILaunchConfigurationDelegate getDelegate() throws CoreException {
return getDelegate(ILaunchManager.RUN_MODE);
}
@Override
public ILaunchConfigurationDelegate getDelegate(String mode) throws CoreException {
Set<String> modes = new HashSet<>();
modes.add(mode);
ILaunchDelegate[] delegates = getDelegates(modes);
if (delegates.length > 0) {
return delegates[0].getDelegate();
}
IStatus status = null;
ILaunchMode launchMode = DebugPlugin.getDefault().getLaunchManager().getLaunchMode(mode);
if (launchMode == null) {
status = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), MessageFormat.format(DebugCoreMessages.LaunchConfigurationType_7, mode));
} else {
status = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), MessageFormat.format(DebugCoreMessages.LaunchConfigurationType_7, ((LaunchManager) DebugPlugin.getDefault().getLaunchManager()).getLaunchModeName(mode)));
}
throw new CoreException(status);
}
@Override
public ILaunchDelegate[] getDelegates(Set<String> modes) throws CoreException {
initializeDelegates();
Set<ILaunchDelegate> delegates = fDelegates.get(modes);
if (delegates == null) {
delegates = Collections.EMPTY_SET;
}
return delegates.toArray(new ILaunchDelegate[delegates.size()]);
}
@Override
public void setPreferredDelegate(Set<String> modes, ILaunchDelegate delegate) {
if(fPreferredDelegates == null) {
fPreferredDelegates = new HashMap<>();
}
if (delegate == null) {
fPreferredDelegates.remove(modes);
} else {
fPreferredDelegates.put(modes, delegate);
}
((LaunchManager)DebugPlugin.getDefault().getLaunchManager()).persistPreferredLaunchDelegate(this);
}
@Override
public ILaunchDelegate getPreferredDelegate(Set<String> modes) {
initializePreferredDelegates();
return fPreferredDelegates.get(modes);
}
public Map<Set<String>, ILaunchDelegate> getPreferredDelegates() {
initializePreferredDelegates();
return fPreferredDelegates;
}
private synchronized void initializePreferredDelegates() {
if(fPreferredDelegates == null) {
fPreferredDelegates = new HashMap<>();
initializeDelegates();
LaunchManager lm = (LaunchManager) DebugPlugin.getDefault().getLaunchManager();
ILaunchDelegate delegate = null;
for (Set<String> modes : fDelegates.keySet()) {
delegate = lm.getPreferredDelegate(getIdentifier(), modes);
if(delegate != null) {
fPreferredDelegates.put(modes, delegate);
}
}
}
}
private synchronized void initializeDelegates() {
if (fDelegates == null) {
fDelegates = new Hashtable<>();
LaunchDelegate[] launchDelegates = getLaunchDelegateExtensions();
LaunchDelegate delegate = null;
List<Set<String>> modelist = null;
Set<ILaunchDelegate> tmp = null;
for (LaunchDelegate launchDelegate : launchDelegates) {
delegate = launchDelegate;
modelist = delegate.getModes();
for (Set<String> modes : modelist) {
tmp = fDelegates.get(modes);
if (tmp == null) {
tmp = new HashSet<>();
fDelegates.put(modes, tmp);
}
tmp.add(delegate);
}
}
}
}
private LaunchDelegate[] getLaunchDelegateExtensions() {
return ((LaunchManager) DebugPlugin.getDefault().getLaunchManager()).getLaunchDelegates(getIdentifier());
}
@Override
public String getIdentifier() {
return fElement.getAttribute(IConfigurationElementConstants.ID);
}
@Override
public String getName() {
return fElement.getAttribute(IConfigurationElementConstants.NAME);
}
@Override
public String getPluginIdentifier() {
return fElement.getContributor().getName();
}
@Override
public String getSourceLocatorId() {
if(fSourceLocator == null) {
fSourceLocator = getAttribute(IConfigurationElementConstants.SOURCE_LOCATOR);
if(fSourceProvider != null) {
fSourceLocator = fSourceProvider.getSourceLocatorId();
}
if(fSourceLocator == null) {
LaunchDelegate[] delegates = getLaunchDelegateExtensions();
for (LaunchDelegate delegate : delegates) {
fSourceLocator = delegate.getSourceLocatorId();
if (fSourceLocator != null) {
fSourceProvider = delegate;
return fSourceLocator;
}
}
fSourceProvider = null;
}
}
return fSourceLocator;
}
@Override
public ISourcePathComputer getSourcePathComputer() {
if(fSourcePathComputer == null) {
String id = fElement.getAttribute(IConfigurationElementConstants.SOURCE_PATH_COMPUTER);
if(fSourceProvider != null) {
id = fSourceProvider.getSourcePathComputerId();
}
if(id != null) {
fSourcePathComputer = DebugPlugin.getDefault().getLaunchManager().getSourcePathComputer(id);
}
else {
LaunchDelegate[] delegates = getLaunchDelegateExtensions();
for (LaunchDelegate delegate : delegates) {
id = delegate.getSourcePathComputerId();
if (id != null) {
fSourceProvider = delegate;
fSourcePathComputer = DebugPlugin.getDefault().getLaunchManager().getSourcePathComputer(id);
if(fSourcePathComputer != null) {
return fSourcePathComputer;
}
}
}
fSourceProvider = null;
}
}
return fSourcePathComputer;
}
@Override
public Set<String> getSupportedModes() {
if(fModes == null) {
fModes = new HashSet<>();
LaunchDelegate[] delegates = getLaunchDelegateExtensions();
List<Set<String>> modesets = null;
for (LaunchDelegate delegate : delegates) {
modesets = delegate.getModes();
for (Set<String> modes : modesets) {
fModes.addAll(modes);
}
}
}
return fModes;
}
@Override
public Set<Set<String>> getSupportedModeCombinations() {
if(fModeCombinations == null) {
initializeDelegates();
fModeCombinations = fDelegates.keySet();
}
return Collections.unmodifiableSet(fModeCombinations);
}
public boolean isMigrationCandidate(ILaunchConfiguration candidate) throws CoreException {
initializeMigrationDelegate();
if(fMigrationDelegate != null) {
return fMigrationDelegate.isCandidate(candidate);
}
return false;
}
private synchronized void initializeMigrationDelegate() throws CoreException {
if(fElement.getAttribute(IConfigurationElementConstants.MIGRATION_DELEGATE) != null && fMigrationDelegate == null) {
fMigrationDelegate = (ILaunchConfigurationMigrationDelegate) fElement.createExecutableExtension(IConfigurationElementConstants.MIGRATION_DELEGATE);
}
}
@Override
public boolean isPublic() {
String publicString = fElement.getAttribute(IConfigurationElementConstants.PUBLIC);
if (publicString != null) {
if (publicString.equalsIgnoreCase("false")) {
return false;
}
}
return true;
}
public void migrate(ILaunchConfiguration candidate) throws CoreException {
initializeMigrationDelegate();
if(fMigrationDelegate != null) {
fMigrationDelegate.migrate(candidate);
}
}
@Override
public ILaunchConfigurationWorkingCopy newInstance(IContainer container, String name) throws CoreException {
IPath path = new Path(name);
if (container == null) {
if (path.segmentCount() > 1) {
throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugCoreMessages.LaunchConfigurationType_2));
}
}
try {
DebugPlugin.getDefault().getLaunchManager().isValidLaunchConfigurationName(path.lastSegment());
} catch (IllegalArgumentException e) {
throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), e.getMessage(), e));
}
return new LaunchConfigurationWorkingCopy(container, name, this);
}
@Override
public boolean supportsMode(String mode) {
if(fModeCombinations == null) {
getSupportedModeCombinations();
}
for (Set<String> modes : fModeCombinations) {
if(modes.size() == 1 && modes.contains(mode)) {
return true;
}
}
return false;
}
@Override
public String getContributorName() {
return fElement.getContributor().getName();
}
@Override
public boolean supportsModeCombination(Set<String> modes) {
if(fModeCombinations == null) {
getSupportedModeCombinations();
}
return fModeCombinations.contains(modes);
}
void resetPreferredDelegates() {
fPreferredDelegates = null;
}
@Override
public ILaunchConfiguration[] getPrototypes() throws CoreException {
return DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(this, ILaunchConfiguration.PROTOTYPE);
}
@Override
public ILaunchConfigurationWorkingCopy newPrototypeInstance(IContainer container, String name) throws CoreException {
LaunchConfigurationWorkingCopy wc = new LaunchConfigurationWorkingCopy(container, name, this, true);
return wc;
}
@Override
public boolean supportsPrototypes() {
String allowPrototypesString = fElement.getAttribute(IConfigurationElementConstants.ALLOW_PROTOTYPES);
if (allowPrototypesString != null) {
if (allowPrototypesString.equalsIgnoreCase("true")) {
return true;
}
}
return false;
}
@Override
public boolean supportsCommandLine() {
String allowPrototypesString = fElement.getAttribute(IConfigurationElementConstants.ALLOW_COMMANDLINE);
if (allowPrototypesString != null) {
if (allowPrototypesString.equalsIgnoreCase("true")) {
return true;
}
}
return false;
}
@Override
public boolean supportsOutputMerging() {
String allowOutputMergingString = fElement.getAttribute(IConfigurationElementConstants.ALLOW_OUTPUT_MERGING);
if (allowOutputMergingString != null) {
if (allowOutputMergingString.equalsIgnoreCase("true")) {
return true;
}
}
return false;
}
}