1 /*******************************************************************************
2  * Copyright (c) 2005, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     EclipseSource Corporation - ongoing enhancements
14  *******************************************************************************/
15 package org.eclipse.pde.internal.ui.wizards.product;
16 
17 import java.util.*;
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.core.plugin.TargetPlatform;
26 import org.eclipse.pde.internal.core.ifeature.IEnvironment;
27 import org.eclipse.pde.internal.core.iproduct.*;
28 import org.eclipse.pde.internal.core.iproduct.IProduct;
29 import org.eclipse.pde.internal.launching.launcher.BundleLauncherHelper;
30 import org.eclipse.pde.internal.ui.PDEPlugin;
31 import org.eclipse.pde.launching.IPDELauncherConstants;
32 
33 /**
34  * This operation generates a product configuration filling in fields based on information
35  * stored a launch configuration. Product, application, JRE, and config information is
36  * collected from the launch config.
37  */
38 public class ProductFromConfigOperation extends BaseProductCreationOperation {
39 
40 	private ILaunchConfiguration fLaunchConfiguration;
41 
ProductFromConfigOperation(IFile file, ILaunchConfiguration config)42 	public ProductFromConfigOperation(IFile file, ILaunchConfiguration config) {
43 		super(file);
44 		fLaunchConfiguration = config;
45 	}
46 
47 	@Override
initializeProduct(IProduct product)48 	protected void initializeProduct(IProduct product) {
49 		if (fLaunchConfiguration == null)
50 			return;
51 		try {
52 			IProductModelFactory factory = product.getModel().getFactory();
53 			boolean useProduct = fLaunchConfiguration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false);
54 			if (useProduct) {
55 				String id = fLaunchConfiguration.getAttribute(IPDELauncherConstants.PRODUCT, (String) null);
56 				if (id != null) {
57 					initializeProductInfo(factory, product, id);
58 				}
59 			} else {
60 				String appName = fLaunchConfiguration.getAttribute(IPDELauncherConstants.APPLICATION, TargetPlatform.getDefaultApplication());
61 				product.setApplication(appName);
62 			}
63 
64 			// Set JRE info from information from the launch configuration
65 			String jreString = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String) null);
66 			if (jreString != null) {
67 				IPath jreContainerPath = new Path(jreString);
68 				IJREInfo jreInfo = product.getJREInfo();
69 				if (jreInfo == null) {
70 					jreInfo = product.getModel().getFactory().createJVMInfo();
71 				}
72 				jreInfo.setJREContainerPath(TargetPlatform.getOS(), jreContainerPath);
73 				product.setJREInfo(jreInfo);
74 			}
75 
76 			// fetch the plug-ins models
77 			Set<String> set = new HashSet<>();
78 			Map<IPluginModelBase, String> map = BundleLauncherHelper.getWorkspaceBundleMap(fLaunchConfiguration, set);
79 			map.putAll(BundleLauncherHelper.getTargetBundleMap(fLaunchConfiguration, set));
80 
81 			addPlugins(factory, product, map);
82 
83 			if (fLaunchConfiguration.getAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, true)) {
84 				super.initializeProduct(product);
85 			} else {
86 				String path = fLaunchConfiguration.getAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, "/"); //$NON-NLS-1$
87 				IContainer container = PDEPlugin.getWorkspace().getRoot().getContainerForLocation(new Path(path));
88 				if (container != null) {
89 					IConfigurationFileInfo info = factory.createConfigFileInfo();
90 					info.setUse(null, "custom"); //$NON-NLS-1$
91 					info.setPath(null, container.getFullPath().toString());
92 					product.setConfigurationFileInfo(info);
93 				} else {
94 					super.initializeProduct(product);
95 				}
96 			}
97 
98 			// set vm and program arguments from the launch configuration
99 			String vmargs = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, (String) null);
100 			String programArgs = fLaunchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, (String) null);
101 			if (vmargs != null || programArgs != null) {
102 				IArgumentsInfo arguments = product.getLauncherArguments();
103 				if (arguments == null)
104 					arguments = factory.createLauncherArguments();
105 				if (vmargs != null)
106 					arguments.setVMArguments(vmargs, IArgumentsInfo.L_ARGS_ALL);
107 				if (programArgs != null) {
108 					String[] parsedArgs = DebugPlugin.splitArguments(programArgs);
109 					List<String> unwantedArgs = Arrays.asList(new String[] {'-' + IEnvironment.P_ARCH, '-' + IEnvironment.P_NL, '-' + IEnvironment.P_OS, '-' + IEnvironment.P_WS});
110 					StringBuilder filteredArgs = new StringBuilder();
111 					for (int i = 0; i < parsedArgs.length; i++) {
112 						if (unwantedArgs.contains(parsedArgs[i].toLowerCase())) {
113 							if (!parsedArgs[i + 1].startsWith("-")) { //$NON-NLS-1$
114 								i++; // skip its value too
115 								continue;
116 							}
117 						}
118 						filteredArgs.append(parsedArgs[i] + ' ');
119 					}
120 					programArgs = filteredArgs.toString().trim();
121 					if (programArgs.length() > 0)
122 						arguments.setProgramArguments(programArgs, IArgumentsInfo.L_ARGS_ALL);
123 				}
124 				product.setLauncherArguments(arguments);
125 			}
126 
127 		} catch (CoreException e) {
128 			PDEPlugin.logException(e);
129 		}
130 	}
131 }
132