|
| 1 | +package org.jboss.ide.eclipse.as.ui.wildflyjar; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +import org.eclipse.core.resources.IContainer; |
| 6 | +import org.eclipse.core.resources.IFile; |
| 7 | +import org.eclipse.core.resources.IFolder; |
| 8 | +import org.eclipse.core.resources.IProject; |
| 9 | +import org.eclipse.core.runtime.CoreException; |
| 10 | +import org.eclipse.core.runtime.IAdaptable; |
| 11 | +import org.eclipse.core.runtime.IPath; |
| 12 | +import org.eclipse.core.runtime.Path; |
| 13 | +import org.eclipse.debug.core.DebugPlugin; |
| 14 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 15 | +import org.eclipse.debug.core.ILaunchConfigurationType; |
| 16 | +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
| 17 | +import org.eclipse.debug.core.ILaunchManager; |
| 18 | +import org.eclipse.debug.ui.DebugUITools; |
| 19 | +import org.eclipse.debug.ui.ILaunchGroup; |
| 20 | +import org.eclipse.debug.ui.ILaunchShortcut; |
| 21 | +import org.eclipse.jface.viewers.ISelection; |
| 22 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 23 | +import org.eclipse.m2e.actions.MavenLaunchConstants; |
| 24 | +import org.eclipse.m2e.internal.launch.LaunchingUtils; |
| 25 | +import org.eclipse.swt.widgets.Shell; |
| 26 | +import org.eclipse.ui.IEditorPart; |
| 27 | +import org.eclipse.ui.PlatformUI; |
| 28 | +import org.jboss.tools.as.core.server.wildflyjar.WildflyJarLaunchConfigurationDelegate; |
| 29 | + |
| 30 | +public class RunAsWildflyJar implements ILaunchShortcut { |
| 31 | + public static final String ID_EXTERNAL_TOOLS_LAUNCH_GROUP = "org.eclipse.ui.externaltools.launchGroup"; //$NON-NLS-1$ |
| 32 | + |
| 33 | + @Override |
| 34 | + public void launch(IEditorPart editor, String mode) { |
| 35 | + // Do nothing |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void launch(ISelection selection, String mode) { |
| 40 | + IContainer basedir = getBaseDir(selection); |
| 41 | + if (basedir == null) |
| 42 | + return; |
| 43 | + |
| 44 | + ILaunchConfiguration launchConfiguration = getLaunchConfiguration(basedir, mode); |
| 45 | + if (launchConfiguration == null) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + ILaunchGroup group = DebugUITools.getLaunchGroup(launchConfiguration, mode); |
| 50 | + String groupId = group != null ? group.getIdentifier() : ID_EXTERNAL_TOOLS_LAUNCH_GROUP; |
| 51 | + DebugUITools.openLaunchConfigurationDialog(getShell(), launchConfiguration, groupId, null); |
| 52 | + } |
| 53 | + |
| 54 | + private ILaunchConfiguration getLaunchConfiguration(IContainer basedir, String mode) { |
| 55 | + ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager(); |
| 56 | + ILaunchConfigurationType launchConfigurationType = launchManager |
| 57 | + .getLaunchConfigurationType(WildflyJarLaunchConfigurationDelegate.ID); |
| 58 | + |
| 59 | + try { |
| 60 | + ArrayList<ILaunchConfiguration> match = findMatchingConfigs(launchManager, launchConfigurationType, basedir.getLocation()); |
| 61 | + if( match != null && match.size() > 0 ) |
| 62 | + return match.get(0); |
| 63 | + |
| 64 | + String newName = launchManager.generateLaunchConfigurationName(basedir.getLocation().lastSegment()); |
| 65 | + ILaunchConfigurationWorkingCopy workingCopy = launchConfigurationType.newInstance(null, newName); |
| 66 | + workingCopy.setAttribute(MavenLaunchConstants.ATTR_POM_DIR, |
| 67 | + LaunchingUtils.generateProjectLocationVariableExpression(basedir.getProject())); |
| 68 | + workingCopy.setAttribute(MavenLaunchConstants.ATTR_GOALS, WildflyJarLaunchConfigurationDelegate.DEFAULT_GOAL); |
| 69 | + return workingCopy.doSave(); |
| 70 | + } catch (Exception ex) { |
| 71 | + } |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + private ArrayList<ILaunchConfiguration> findMatchingConfigs(ILaunchManager manager, ILaunchConfigurationType type, |
| 76 | + IPath basedirLocation) throws CoreException { |
| 77 | + ILaunchConfiguration[] launchConfigurations = manager.getLaunchConfigurations(type); |
| 78 | + ArrayList<ILaunchConfiguration> matchingConfigs = new ArrayList<ILaunchConfiguration>(); |
| 79 | + for (ILaunchConfiguration configuration : launchConfigurations) { |
| 80 | + try { |
| 81 | + // substitute variables (may throw exceptions) |
| 82 | + String workDir = LaunchingUtils |
| 83 | + .substituteVar(configuration.getAttribute(MavenLaunchConstants.ATTR_POM_DIR, (String) null)); |
| 84 | + if (workDir == null) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + IPath workPath = new Path(workDir); |
| 88 | + if (basedirLocation.equals(workPath)) { |
| 89 | + matchingConfigs.add(configuration); |
| 90 | + } |
| 91 | + } catch (CoreException e) { |
| 92 | + } |
| 93 | + } |
| 94 | + return matchingConfigs; |
| 95 | + } |
| 96 | + |
| 97 | + private Shell getShell() { |
| 98 | + return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); |
| 99 | + } |
| 100 | + |
| 101 | + protected IContainer getBaseDir(ISelection selection) { |
| 102 | + IContainer basedir = null; |
| 103 | + if (selection instanceof IStructuredSelection) { |
| 104 | + IStructuredSelection structuredSelection = (IStructuredSelection) selection; |
| 105 | + Object object = structuredSelection.getFirstElement(); |
| 106 | + |
| 107 | + if (object instanceof IProject || object instanceof IFolder) { |
| 108 | + basedir = (IContainer) object; |
| 109 | + } else if (object instanceof IFile) { |
| 110 | + basedir = ((IFile) object).getParent(); |
| 111 | + } else if (object instanceof IAdaptable) { |
| 112 | + IAdaptable adaptable = (IAdaptable) object; |
| 113 | + Object adapter = adaptable.getAdapter(IProject.class); |
| 114 | + if (adapter != null) { |
| 115 | + basedir = (IContainer) adapter; |
| 116 | + } else { |
| 117 | + adapter = adaptable.getAdapter(IFolder.class); |
| 118 | + if (adapter != null) { |
| 119 | + basedir = (IContainer) adapter; |
| 120 | + } else { |
| 121 | + adapter = adaptable.getAdapter(IFile.class); |
| 122 | + if (adapter != null) { |
| 123 | + basedir = ((IFile) object).getParent(); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + return basedir; |
| 130 | + } |
| 131 | +} |
0 commit comments