ISaveParticipant
, allowing it to participate in
* workspace persistence life-cycles.
*
* @see ISaveParticipant
* @see org.eclipse.debug.ui.ILaunchShortcut
* @see ILaunchGroup
* @see ILaunchListener
* @see ILaunchHistoryChangedListener
* @see DebugUIPlugin
* @see LaunchHistory
*/
public class LaunchConfigurationManager implements ILaunchListener, ISaveParticipant {
/**
* A comparator for the ordering of launch shortcut extensions
* @since 3.3
*/
class ShortcutComparator implements ComparatorILaunchHistoryChangedListener
*/
protected ListIlaunchDeleagtes
that does not contain any delegates from disabled activities
* @param type the type to get the delegates from
* @param modes the set of launch modes to get delegates for
* @return the filtered listing of ILaunchDelegate
s or an empty array, never null
.
* @throws CoreException if an exception occurs
* @since 3.3
*/
public static ILaunchDelegate[] filterLaunchDelegates(ILaunchConfigurationType type, Setnull
* if none. This method does not include any filtering for the returned launch configuration.
*
* This method is exposed via DebugTools.getLastLaunch
* @param groupId the identifier of the {@link ILaunchGroup} to get the last launch from
*
* @return the last launch, or null
if none
*/
public ILaunchConfiguration getLastLaunch(String groupId) {
LaunchHistory history = getLaunchHistory(groupId);
if (history != null) {
return history.getRecentLaunch();
}
return null;
}
/**
* Returns the most recent launch for the given group taking launch configuration
* filters into account, or null
if none.
*
* @param groupId launch group
* @return the most recent, un-filtered launch
*/
public ILaunchConfiguration getFilteredLastLaunch(String groupId) {
LaunchHistory history = getLaunchHistory(groupId);
if (history != null) {
ILaunchConfiguration[] filterConfigs = history.getCompleteLaunchHistory();
if (filterConfigs.length > 0) {
return filterConfigs[0];
}
}
return null;
}
/**
* Add the specified listener to the list of listeners that will be notified when the
* launch history changes.
* @param listener the listener to add - adding a duplicate listener has no effect
*/
public void addLaunchHistoryListener(ILaunchHistoryChangedListener listener) {
if (!fLaunchHistoryChangedListeners.contains(listener)) {
fLaunchHistoryChangedListeners.add(listener);
}
}
/**
* Remove the specified listener from the list of listeners that will be notified when the
* launch history changes.
* @param listener the listener to remove
*/
public void removeLaunchHistoryListener(ILaunchHistoryChangedListener listener) {
fLaunchHistoryChangedListeners.remove(listener);
}
/**
* Notify all launch history listeners that the launch history has changed in some way.
*/
protected void fireLaunchHistoryChanged() {
for (ILaunchHistoryChangedListener listener : fLaunchHistoryChangedListeners) {
listener.launchHistoryChanged();
}
}
/**
* Returns the history listing as XML
* @return the history listing as XML
* @throws CoreException if an exception occurs
* @throws ParserConfigurationException if there is a problem creating the XML for the launch history
*/
protected String getHistoryAsXML() throws CoreException, ParserConfigurationException {
Document doc = DebugUIPlugin.getDocument();
Element historyRootElement = doc.createElement(IConfigurationElementConstants.LAUNCH_HISTORY);
doc.appendChild(historyRootElement);
for (LaunchHistory history : fLaunchHistories.values()) {
Element groupElement = doc.createElement(IConfigurationElementConstants.LAUNCH_GROUP);
groupElement.setAttribute(IConfigurationElementConstants.ID, history.getLaunchGroup().getIdentifier());
historyRootElement.appendChild(groupElement);
Element historyElement = doc.createElement(IConfigurationElementConstants.MRU_HISTORY);
groupElement.appendChild(historyElement);
createEntry(doc, historyElement, history.getCompleteLaunchHistory());
Element favs = doc.createElement(IConfigurationElementConstants.FAVORITES);
groupElement.appendChild(favs);
createEntry(doc, favs, history.getFavorites());
history.setSaved(true);
}
return DebugPlugin.serializeDocument(doc);
}
/**
* Creates a new launch history element and adds it to the specified Document
* @param doc the Document
to add the new element to
* @param historyRootElement the root element
* @param configurations the configurations to create entries for
* @throws CoreException is an exception occurs
*/
protected void createEntry(Document doc, Element historyRootElement, ILaunchConfiguration[] configurations) throws CoreException {
for (int i = 0; i < configurations.length; i++) {
ILaunchConfiguration configuration = configurations[i];
if (configuration.exists()) {
Element launch = doc.createElement(IConfigurationElementConstants.LAUNCH);
launch.setAttribute(IConfigurationElementConstants.MEMENTO, configuration.getMemento());
historyRootElement.appendChild(launch);
}
}
}
/**
* Returns the path to the local file for the launch history
* @return the file path for the launch history file
*/
protected IPath getHistoryFilePath() {
return DebugUIPlugin.getDefault().getStateLocation().append(LAUNCH_CONFIGURATION_HISTORY_FILENAME);
}
/**
* Write out an XML file indicating the entries on the run & debug history lists and
* the most recent launch.
* @throws IOException if writing the history file fails
* @throws CoreException is an exception occurs
* @throws ParserConfigurationException if there is a problem reading the XML
*/
protected void persistLaunchHistory() throws IOException, CoreException, ParserConfigurationException {
synchronized (this) {
if (fLaunchHistories == null || fRestoring) {
return;
}
}
boolean shouldsave = false;
for (LaunchHistory history : fLaunchHistories.values()) {
shouldsave |= history.needsSaving();
}
if(shouldsave) {
IPath historyPath = getHistoryFilePath();
String osHistoryPath = historyPath.toOSString();
String xml = getHistoryAsXML();
File file = new File(osHistoryPath);
file.createNewFile();
try (FileOutputStream stream = new FileOutputStream(file)) {
stream.write(xml.getBytes("UTF8")); //$NON-NLS-1$
}
}
}
/**
* Find the XML history file and parse it. Place the corresponding configurations
* in the appropriate history, and set the most recent launch.
*/
private void restoreLaunchHistory() {
// Find the history file
IPath historyPath = getHistoryFilePath();
String osHistoryPath = historyPath.toOSString();
File file = new File(osHistoryPath);
// If no history file, nothing to do
if (!file.exists()) {
return;
}
Element rootHistoryElement= null;
try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) {
// Parse the history file
try {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
parser.setErrorHandler(new DefaultHandler());
rootHistoryElement = parser.parse(new InputSource(stream)).getDocumentElement();
} catch (SAXException e) {
DebugUIPlugin.log(e);
return;
} catch (ParserConfigurationException e) {
DebugUIPlugin.log(e);
return;
}
} catch (IOException exception) {
DebugUIPlugin.log(exception);
return;
}
// If root node isn't what we expect, return
if (!rootHistoryElement.getNodeName().equalsIgnoreCase(IConfigurationElementConstants.LAUNCH_HISTORY)) {
return;
}
// For each child of the root node, construct a launch config handle and add it to
// the appropriate history, or set the most recent launch
LaunchHistory[] histories = fLaunchHistories.values().toArray(new LaunchHistory[fLaunchHistories.size()]);
NodeList list = rootHistoryElement.getChildNodes();
int length = list.getLength();
Node node = null;
Element entry = null;
for (int i = 0; i < length; ++i) {
node = list.item(i);
short type = node.getNodeType();
if (type == Node.ELEMENT_NODE) {
entry = (Element) node;
if (entry.getNodeName().equalsIgnoreCase(IConfigurationElementConstants.LAUNCH)) {
createHistoryElement(entry, histories, false);
} else if (entry.getNodeName().equalsIgnoreCase(IConfigurationElementConstants.LAST_LAUNCH)) {
createHistoryElement(entry, histories, true);
} else if (entry.getNodeName().equals(IConfigurationElementConstants.LAUNCH_GROUP)) {
String id = entry.getAttribute(IConfigurationElementConstants.ID);
if (id != null) {
LaunchHistory history = getLaunchHistory(id);
if (history != null) {
restoreHistory(entry, history);
}
}
}
}
}
}
/**
* Restores the given launch history.
*
* @param groupElement launch group history
* @param history associated history cache
*/
private void restoreHistory(Element groupElement, LaunchHistory history) {
NodeList nodes = groupElement.getChildNodes();
int length = nodes.getLength();
for (int i = 0; i < length; i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element)node;
if (element.getNodeName().equals(IConfigurationElementConstants.MRU_HISTORY)) {
ILaunchConfiguration[] configs = getLaunchConfigurations(element);
for (int j = 0; j < configs.length; j++) {
history.addHistory(configs[j], false);
}
} else if (element.getNodeName().equals(IConfigurationElementConstants.FAVORITES)) {
ILaunchConfiguration[] favs = getLaunchConfigurations(element);
history.setFavorites(favs);
// add any favorites that have been added to the workspace before this plug-in
// was loaded - @see bug 231600
ILaunchConfiguration[] configurations = getLaunchManager().getLaunchConfigurations();
for (int j = 0; j < configurations.length; j++) {
history.checkFavorites(configurations[j]);
}
}
}
}
}
/**
* Restores a list of configurations.
* @param root element
* @return list of configurations under the element
*/
private ILaunchConfiguration[] getLaunchConfigurations(Element root) {
Listnull
* @since 3.3
*/
public ListILaunchConfigurationType
s that apply to the currently
* specified IResource
.
*
* @param resource the resource context
* @return an array of applicable ILaunchConfigurationType
ids, or an empty array, never null
* @since 3.3
*/
public String[] getApplicableConfigurationTypes(IResource resource) {
ListILaunchConfiguration
s that apply to the specified IResource
* @param types the array of launch configuration type identifiers
* @param resource the resource
* @return an array of applicable ILaunchConfiguration
s for the specified IResource
or an empty
* array if none, never null
* @since 3.3
*/
public ILaunchConfiguration[] getApplicableLaunchConfigurations(String[] types, IResource resource) {
ArrayListnull
if there isn't one
* @since 3.3
*/
public ILaunchConfiguration getMRUConfiguration(Listnull
if the selected resources is not a shared config
* @param receiver the object to test if it is a shared launch configuration
* @return the shared config from the selected resource or null
if the selected resources is not a shared config
* @since 3.3
*/
public ILaunchConfiguration isSharedConfig(Object receiver) {
if(receiver instanceof IFile) {
IFile file = (IFile) receiver;
String ext = file.getFileExtension();
if(ext == null) {
return null;
}
if(ext.equals("launch")) { //$NON-NLS-1$
ILaunchConfiguration config = DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(file);
if(config != null && config.exists()) {
return config;
}
}
}
else if(receiver instanceof IFileEditorInput) {
IFileEditorInput input = (IFileEditorInput) receiver;
return isSharedConfig(input.getFile());
}
else if(receiver instanceof IEditorPart) {
return isSharedConfig(((IEditorPart) receiver).getEditorInput());
}
else if (receiver instanceof IAdaptable) {
IFile file = ((IAdaptable)receiver).getAdapter(IFile.class);
if (file != null) {
return isSharedConfig(file);
}
}
return null;
}
/**
* Returns the image used to display an error in the given tab
* @param tab the tab to get the error image for
* @return the error image associated with the given tab
*/
public Image getErrorTabImage(ILaunchConfigurationTab tab) {
if (fErrorImages == null) {
fErrorImages = new ImageRegistry();
}
String key = tab.getClass().getName();
Image image = fErrorImages.get(key);
if (image == null) {
// create image
Image base = tab.getImage();
if (base == null) {
base = DebugPluginImages.getImage(IInternalDebugUIConstants.IMG_OVR_TRANSPARENT);
}
base = new Image(Display.getCurrent(), base, SWT.IMAGE_COPY);
LaunchConfigurationTabImageDescriptor desc = new LaunchConfigurationTabImageDescriptor(base, LaunchConfigurationTabImageDescriptor.ERROR);
image = desc.createImage();
fErrorImages.put(key, image);
}
return image;
}
/**
* Return the launch group with the given id, or null
* @param id the identifier of the {@link LaunchGroupExtension}
*
* @return the launch group with the given id, or null
*/
public LaunchGroupExtension getLaunchGroup(String id) {
if (fLaunchGroups == null) {
loadLaunchGroups();
}
return fLaunchGroups.get(id);
}
/**
* Return all defined launch groups
*
* @return all defined launch groups
*/
public ILaunchGroup[] getLaunchGroups() {
if (fLaunchGroups == null) {
loadLaunchGroups();
}
return fLaunchGroups.values().toArray(new ILaunchGroup[fLaunchGroups.size()]);
}
/**
* Return the launch history with the given group id, or null
* @param id the identifier of the launch history
* @return the launch history with the given group id, or null
*/
public LaunchHistory getLaunchHistory(String id) {
loadLaunchHistories();
return fLaunchHistories.get(id);
}
/**
* Returns the singleton instance of the launch manager
* @return the singleton instance of the launch manager
* @since 3.3
*/
private LaunchManager getLaunchManager() {
return (LaunchManager) DebugPlugin.getDefault().getLaunchManager();
}
/**
* Restore launch history
*/
private synchronized void loadLaunchHistories() {
if (fLaunchHistories == null) {
fRestoring = true;
ILaunchGroup[] groups = getLaunchGroups();
fLaunchHistories = new HashMapnull
if none.
*
* @param type the type
* @param mode the mode
* @return the launch group the given launch configuration belongs to, in
* the specified mode, or null
if none
*/
public ILaunchGroup getLaunchGroup(ILaunchConfigurationType type, String mode) {
if (!type.supportsMode(mode)) {
return null;
}
String category = type.getCategory();
ILaunchGroup[] groups = getLaunchGroups();
ILaunchGroup extension = null;
for (int i = 0; i < groups.length; i++) {
extension = groups[i];
if (category == null) {
if (extension.getCategory() == null && extension.getMode().equals(mode)) {
return extension;
}
} else if (category.equals(extension.getCategory())) {
if (extension.getMode().equals(mode)) {
return extension;
}
}
}
return null;
}
/**
* Returns the {@link ILaunchGroup} for the given mode set and
* {@link ILaunchConfigurationType}.
* @param type the type
* @param modeset the set of modes, which are combined to one mode string
* @return the associated {@link ILaunchGroup} or null
*
* @since 3.4.0
*/
public ILaunchGroup getLaunchGroup(ILaunchConfigurationType type, Set* @param launch the launch to prepend to its associated histories * @since 3.3 */ public void setRecentLaunch(ILaunch launch) { ILaunchGroup[] groups = DebugUITools.getLaunchGroups(); int size = groups.length; for (int i = 0; i < size; i++) { String id = groups[i].getIdentifier(); LaunchHistory history = getLaunchHistory(id); if (history != null) { history.launchAdded(launch); } } } }