|
| 1 | +/** |
| 2 | + * Copyright (c) 2020 Payara Foundation |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/legal/epl-v20.html |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + */ |
| 10 | +package org.eclipse.payara.tools.ui.micro; |
| 11 | + |
| 12 | +import static org.eclipse.core.externaltools.internal.IExternalToolConstants.ATTR_BUILD_SCOPE; |
| 13 | +import static org.eclipse.core.externaltools.internal.IExternalToolConstants.ATTR_LOCATION; |
| 14 | +import static org.eclipse.core.externaltools.internal.IExternalToolConstants.ATTR_TOOL_ARGUMENTS; |
| 15 | +import static org.eclipse.core.externaltools.internal.IExternalToolConstants.ATTR_WORKING_DIRECTORY; |
| 16 | +import static org.eclipse.debug.core.ILaunchManager.ATTR_ENVIRONMENT_VARIABLES; |
| 17 | +import static org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME; |
| 18 | + |
| 19 | +import java.io.FileNotFoundException; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.eclipse.core.resources.IProject; |
| 25 | +import org.eclipse.core.resources.IResource; |
| 26 | +import org.eclipse.core.resources.IWorkspace; |
| 27 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 28 | +import org.eclipse.core.runtime.CoreException; |
| 29 | +import org.eclipse.core.runtime.IStatus; |
| 30 | +import org.eclipse.debug.core.DebugPlugin; |
| 31 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 32 | +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
| 33 | +import org.eclipse.debug.internal.ui.SWTFactory; |
| 34 | +import org.eclipse.jdt.core.IJavaElement; |
| 35 | +import org.eclipse.jdt.core.IJavaProject; |
| 36 | +import org.eclipse.jdt.core.JavaCore; |
| 37 | +import org.eclipse.jdt.core.JavaModelException; |
| 38 | +import org.eclipse.jdt.internal.debug.ui.launcher.AbstractJavaMainTab; |
| 39 | +import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages; |
| 40 | +import org.eclipse.jdt.launching.IVMInstall; |
| 41 | +import org.eclipse.jdt.launching.JavaRuntime; |
| 42 | +import org.eclipse.m2e.core.ui.internal.MavenImages; |
| 43 | +import org.eclipse.osgi.util.NLS; |
| 44 | +import static org.eclipse.payara.tools.micro.MicroConstants.JAVA_HOME_ENV_VAR; |
| 45 | +import static org.eclipse.payara.tools.micro.MicroConstants.DEFAULT_DEBUG_PORT; |
| 46 | +import static org.eclipse.payara.tools.micro.MicroConstants.ATTR_CONTEXT_PATH; |
| 47 | +import static org.eclipse.payara.tools.micro.MicroConstants.ATTR_MICRO_VERSION; |
| 48 | +import static org.eclipse.payara.tools.micro.MicroConstants.ATTR_BUILD_ARTIFACT; |
| 49 | +import static org.eclipse.payara.tools.micro.MicroConstants.ATTR_DEBUG_PORT; |
| 50 | +import static org.eclipse.payara.tools.micro.MicroConstants.WAR_BUILD_ARTIFACT; |
| 51 | +import static org.eclipse.payara.tools.micro.MicroConstants.EXPLODED_WAR_BUILD_ARTIFACT; |
| 52 | +import static org.eclipse.payara.tools.micro.MicroConstants.UBER_JAR_BUILD_ARTIFACT; |
| 53 | +import org.eclipse.payara.tools.micro.BuildTool; |
| 54 | +import org.eclipse.payara.tools.ui.micro.wizards.Messages; |
| 55 | +import org.eclipse.swt.SWT; |
| 56 | +import org.eclipse.swt.graphics.Image; |
| 57 | +import org.eclipse.swt.layout.GridData; |
| 58 | +import org.eclipse.swt.widgets.Combo; |
| 59 | +import org.eclipse.swt.widgets.Composite; |
| 60 | +import org.eclipse.swt.widgets.Group; |
| 61 | +import org.eclipse.swt.widgets.Text; |
| 62 | + |
| 63 | +public class MicroProjectTab extends AbstractJavaMainTab { |
| 64 | + |
| 65 | + private Text contextPathText, microVersionText, debugPortText; |
| 66 | + private Combo buildArtifactCombo; |
| 67 | + |
| 68 | + @Override |
| 69 | + public void createControl(Composite parent) { |
| 70 | + Composite mainComposite = SWTFactory.createComposite(parent, 1, 1, GridData.FILL_HORIZONTAL); |
| 71 | + createProjectEditor(mainComposite); |
| 72 | + |
| 73 | + Group group = SWTFactory.createGroup(mainComposite, Messages.contextPathComponentLabel, 1, 1, GridData.FILL_HORIZONTAL); |
| 74 | + contextPathText = SWTFactory.createSingleText(group, 1); |
| 75 | + contextPathText.addModifyListener(getDefaultListener()); |
| 76 | + |
| 77 | + group = SWTFactory.createGroup(mainComposite, Messages.microVersionComponentLabel, 1, 1, GridData.FILL_HORIZONTAL); |
| 78 | + microVersionText = SWTFactory.createSingleText(group, 1); |
| 79 | + microVersionText.addModifyListener(getDefaultListener()); |
| 80 | + |
| 81 | + group = SWTFactory.createGroup(mainComposite, Messages.buildArtifactComponentLabel, 1, 1, GridData.FILL_HORIZONTAL); |
| 82 | + buildArtifactCombo = SWTFactory.createCombo( |
| 83 | + group, SWT.READ_ONLY, 1, |
| 84 | + new String[]{EMPTY_STRING, WAR_BUILD_ARTIFACT, EXPLODED_WAR_BUILD_ARTIFACT, UBER_JAR_BUILD_ARTIFACT} |
| 85 | + ); |
| 86 | + buildArtifactCombo.addModifyListener(getDefaultListener()); |
| 87 | + |
| 88 | + group = SWTFactory.createGroup(mainComposite, Messages.debugPortComponentLabel, 1, 1, GridData.FILL_HORIZONTAL); |
| 89 | + debugPortText = SWTFactory.createSingleText(group, 1); |
| 90 | + debugPortText.addModifyListener(getDefaultListener()); |
| 91 | + |
| 92 | + setControl(mainComposite); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void initializeFrom(ILaunchConfiguration config) { |
| 97 | + updateMicroSettingsFromConfig(config); |
| 98 | + super.initializeFrom(config); |
| 99 | + } |
| 100 | + |
| 101 | + private void updateMicroSettingsFromConfig(ILaunchConfiguration config) { |
| 102 | + String contextPath = EMPTY_STRING; |
| 103 | + try { |
| 104 | + contextPath = config.getAttribute(ATTR_CONTEXT_PATH, contextPath); |
| 105 | + } catch (CoreException ce) { |
| 106 | + setErrorMessage(ce.getStatus().getMessage()); |
| 107 | + } |
| 108 | + contextPathText.setText(contextPath); |
| 109 | + |
| 110 | + String microVersion = EMPTY_STRING; |
| 111 | + try { |
| 112 | + microVersion = config.getAttribute(ATTR_MICRO_VERSION, microVersion); |
| 113 | + } catch (CoreException ce) { |
| 114 | + setErrorMessage(ce.getStatus().getMessage()); |
| 115 | + } |
| 116 | + microVersionText.setText(microVersion); |
| 117 | + |
| 118 | + String buildType = EMPTY_STRING; |
| 119 | + try { |
| 120 | + buildType = config.getAttribute(ATTR_BUILD_ARTIFACT, microVersion); |
| 121 | + } catch (CoreException ce) { |
| 122 | + setErrorMessage(ce.getStatus().getMessage()); |
| 123 | + } |
| 124 | + buildArtifactCombo.setText(buildType); |
| 125 | + |
| 126 | + String debugPort = String.valueOf(DEFAULT_DEBUG_PORT); |
| 127 | + try { |
| 128 | + debugPort = config.getAttribute(ATTR_DEBUG_PORT, debugPort); |
| 129 | + } catch (CoreException ce) { |
| 130 | + setErrorMessage(ce.getStatus().getMessage()); |
| 131 | + } |
| 132 | + debugPortText.setText(debugPort); |
| 133 | + } |
| 134 | + |
| 135 | + public Image getImage() { |
| 136 | + return MavenImages.IMG_LAUNCH_MAIN; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public boolean isValid(ILaunchConfiguration config) { |
| 141 | + setErrorMessage(null); |
| 142 | + setMessage(null); |
| 143 | + String name = fProjText.getText().trim(); |
| 144 | + if (name.isEmpty()) { |
| 145 | + setErrorMessage(LauncherMessages.JavaMainTab_missing_project); |
| 146 | + return false; |
| 147 | + } |
| 148 | + IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 149 | + IStatus status = workspace.validateName(name, IResource.PROJECT); |
| 150 | + if (!status.isOK()) { |
| 151 | + setErrorMessage(NLS.bind(LauncherMessages.JavaMainTab_19, new String[]{status.getMessage()})); |
| 152 | + return false; |
| 153 | + } |
| 154 | + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name); |
| 155 | + if (!project.exists()) { |
| 156 | + setErrorMessage(NLS.bind(LauncherMessages.JavaMainTab_20, new String[]{name})); |
| 157 | + return false; |
| 158 | + } |
| 159 | + if (!project.isOpen()) { |
| 160 | + setErrorMessage(NLS.bind(LauncherMessages.JavaMainTab_21, new String[]{name})); |
| 161 | + return false; |
| 162 | + } |
| 163 | + try { |
| 164 | + BuildTool.getToolSupport(project).getExecutableHome(); |
| 165 | + } catch (FileNotFoundException e) { |
| 166 | + setErrorMessage(e.getMessage()); |
| 167 | + return false; |
| 168 | + } |
| 169 | + return true; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void setDefaults(ILaunchConfigurationWorkingCopy config) { |
| 174 | + IJavaElement javaElement = getContext(); |
| 175 | + if (javaElement != null) { |
| 176 | + initializeJavaProject(javaElement, config); |
| 177 | + } else { |
| 178 | + config.setAttribute(ATTR_PROJECT_NAME, EMPTY_STRING); |
| 179 | + } |
| 180 | + config.setAttribute(DebugPlugin.ATTR_PROCESS_FACTORY_ID, "org.eclipse.payara.tools.micro.processFactory"); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void performApply(ILaunchConfigurationWorkingCopy config) { |
| 185 | + String projectName = fProjText.getText().trim(); |
| 186 | + try { |
| 187 | + if (!projectName.isEmpty()) { |
| 188 | + IProject project = getWorkspaceRoot().getProject(projectName); |
| 189 | + if (!project.exists()) { |
| 190 | + setErrorMessage(NLS.bind(LauncherMessages.JavaMainTab_20, new String[]{projectName})); |
| 191 | + return; |
| 192 | + } |
| 193 | + BuildTool buildTool = BuildTool.getToolSupport(project); |
| 194 | + if (buildTool == null) { |
| 195 | + setErrorMessage(Messages.projectBuildNotFound); |
| 196 | + throw new IllegalStateException(Messages.projectBuildNotFound); |
| 197 | + } |
| 198 | + |
| 199 | + String debugPort = debugPortText.getText(); |
| 200 | + if (debugPort.isEmpty()) { |
| 201 | + debugPort = String.valueOf(DEFAULT_DEBUG_PORT); |
| 202 | + } |
| 203 | + config.setAttribute(ATTR_CONTEXT_PATH, contextPathText.getText()); |
| 204 | + config.setAttribute(ATTR_MICRO_VERSION, microVersionText.getText()); |
| 205 | + config.setAttribute(ATTR_BUILD_ARTIFACT, buildArtifactCombo.getText()); |
| 206 | + config.setAttribute(ATTR_DEBUG_PORT, debugPort); |
| 207 | + config.setAttribute(ATTR_PROJECT_NAME, projectName); |
| 208 | + config.setAttribute(ATTR_WORKING_DIRECTORY, project.getLocation().toOSString()); |
| 209 | + config.setAttribute(ATTR_BUILD_SCOPE, "${projects:" + project.getName() + "}"); |
| 210 | + Map<String, String> env = config.getAttribute(ATTR_ENVIRONMENT_VARIABLES, Collections.emptyMap()); |
| 211 | + if (env.isEmpty()) { |
| 212 | + config.setAttribute(ATTR_ENVIRONMENT_VARIABLES, env = new HashMap<>()); |
| 213 | + } |
| 214 | + if (!env.containsKey(JAVA_HOME_ENV_VAR)) { |
| 215 | + env.put(JAVA_HOME_ENV_VAR, getJavaHome(project)); |
| 216 | + } |
| 217 | + config.setAttribute(ATTR_LOCATION, buildTool.getExecutableHome()); |
| 218 | + config.setAttribute( |
| 219 | + ATTR_TOOL_ARGUMENTS, |
| 220 | + buildTool.getStartCommand( |
| 221 | + contextPathText.getText(), |
| 222 | + microVersionText.getText(), |
| 223 | + buildArtifactCombo.getText(), |
| 224 | + debugPortText.getText().isEmpty() ? String.valueOf(DEFAULT_DEBUG_PORT) : debugPortText.getText() |
| 225 | + ) |
| 226 | + ); |
| 227 | + |
| 228 | + } |
| 229 | + } catch (FileNotFoundException ex) { |
| 230 | + setErrorMessage(ex.getMessage()); |
| 231 | + } catch (Exception ex) { |
| 232 | + throw new IllegalStateException(ex); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + public static String getJavaHome(IProject project) throws CoreException { |
| 237 | + IJavaProject javaProject = JavaCore.create(project); |
| 238 | + IVMInstall install = JavaRuntime.getVMInstall(javaProject); |
| 239 | + return install.getInstallLocation().getAbsolutePath(); |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public String getName() { |
| 244 | + return Messages.microProjectTabTitle; |
| 245 | + } |
| 246 | + |
| 247 | +} |
0 commit comments