Skip to content

Commit

Permalink
Clean-up and simplify Tracing-Options processing
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Sep 24, 2023
1 parent 423b67b commit f773b1d
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.zip.ZipEntry;
Expand All @@ -36,69 +38,56 @@
import org.eclipse.pde.core.plugin.PluginRegistry;

public class TracingOptionsManager {
private Properties template;
private Map<String, String> template;

public TracingOptionsManager() {
super();
}

private Properties createTemplate() {
Properties temp = new Properties();
IPluginModelBase[] models = PluginRegistry.getAllModels();
for (IPluginModelBase model : models) {
addToTemplate(temp, model);
}
return temp;
}

private void addToTemplate(Properties temp, IPluginModelBase model) {
Properties modelOptions = getOptions(model);
if (modelOptions != null) {
temp.putAll(modelOptions);
}
}

public Hashtable<String, String> getTemplateTable(String pluginId) {
Properties tracingTemplate = getTracingTemplate();
Hashtable<String, String> defaults = new Hashtable<>();
for (Enumeration<Object> keys = tracingTemplate.keys(); keys.hasMoreElements();) {
String key = keys.nextElement().toString();
public Map<String, String> getTemplateTable(String pluginId) {
Map<String, String> tracingTemplate = getTracingTemplate();
Map<String, String> defaults = new HashMap<>();
tracingTemplate.forEach((key, value) -> {
if (belongsTo(key, pluginId)) {
defaults.put(key, tracingTemplate.get(key).toString());
defaults.put(key, value);
}
}
});
return defaults;
}

private boolean belongsTo(String option, String pluginId) {
IPath path = IPath.fromOSString(option);
String firstSegment = path.segment(0);
String firstSegment = IPath.fromOSString(option).segment(0);
return pluginId.equalsIgnoreCase(firstSegment);
}

public Properties getTracingOptions(Map<String, String> storedOptions) {
public Map<String, String> getTracingOptions(Map<String, String> storedOptions) {
// Start with the fresh template from plugins
Properties defaults = getTracingTemplateCopy();
Map<String, String> defaults = getTracingTemplateCopy();
if (storedOptions != null) {
// Load stored values, but only for existing keys
Iterator<String> iter = storedOptions.keySet().iterator();
while (iter.hasNext()) {
String key = iter.next();
storedOptions.forEach((key, value) -> {
if (defaults.containsKey(key)) {
defaults.setProperty(key, storedOptions.get(key));
defaults.put(key, value);
}
}
});
}
return defaults;
}

public Properties getTracingTemplateCopy() {
return (Properties) getTracingTemplate().clone();
public Map<String, String> getTracingTemplateCopy() {
return new HashMap<>(getTracingTemplate());
}

private synchronized Properties getTracingTemplate() {
private synchronized Map<String, String> getTracingTemplate() {
if (template == null) {
template = createTemplate();
Map<String, String> temp = new HashMap<>();
IPluginModelBase[] models = PluginRegistry.getAllModels();
Arrays.stream(models).map(TracingOptionsManager::getOptions).filter(Objects::nonNull).forEach(p -> {
@SuppressWarnings({ "rawtypes", "unchecked" })
Map<String, String> entries = (Map) p;
temp.putAll(entries); // All entries are of String/String
});
template = temp;
}
return template;
}
Expand Down Expand Up @@ -129,32 +118,31 @@ public synchronized void reset() {
template = null;
}

private void save(String fileName, Properties properties) {
try (FileOutputStream stream = new FileOutputStream(fileName);) {
private void saveOptions(Path file, Map<String, String> entries) {
Properties properties = new Properties();
properties.putAll(entries);
try (OutputStream stream = Files.newOutputStream(file);) {
properties.store(stream, "Master Tracing Options"); //$NON-NLS-1$
stream.flush();
} catch (IOException e) {
PDECore.logException(e);
}
}

public void save(String filename, Map<String, String> map, Set<String> selected) {
Properties properties = getTracingOptions(map);
for (Enumeration<Object> keys = properties.keys(); keys.hasMoreElements();) {
String key = keys.nextElement().toString();
public void save(Path file, Map<String, String> map, Set<String> selected) {
Map<String, String> properties = getTracingOptions(map);
properties.keySet().removeIf(key -> {
IPath path = IPath.fromOSString(key);
if (path.segmentCount() < 1 || !selected.contains(path.segment(0))) {
properties.remove(key);
}
}
save(filename, properties);
return path.segmentCount() < 1 || !selected.contains(path.segment(0));
});
saveOptions(file, properties);
}

public void save(String filename, Map<String, String> map) {
save(filename, getTracingOptions(map));
public void save(Path file, Map<String, String> map) {
saveOptions(file, getTracingOptions(map));
}

private Properties getOptions(IPluginModelBase model) {
private static Properties getOptions(IPluginModelBase model) {
String location = model.getInstallLocation();
if (location == null) {
return null;
Expand Down Expand Up @@ -203,19 +191,17 @@ private Properties getOptions(IPluginModelBase model) {
* support cases when: key is split in multiple lines; key use escape
* characters; comment uses ! start char
*/
private void loadComments(InputStream stream, Properties modelOptions) throws IOException {
private static void loadComments(InputStream stream, Properties modelOptions) throws IOException {
String prevComment = ""; //$NON-NLS-1$
try (BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(stream, Charset.defaultCharset()))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
new InputStreamReader(stream, StandardCharsets.ISO_8859_1))) {
for (String line; (line = bufferedReader.readLine()) != null;) {
if (line.startsWith("#") || line.trim().isEmpty()) { //$NON-NLS-1$
prevComment += "\n" + line.trim(); //$NON-NLS-1$
} else {
if (prevComment.trim().isEmpty()) {
if (prevComment.isBlank()) {
continue;
}

int eq = line.indexOf('=');
if (eq >= 0) {
String key = line.substring(0, eq).trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
Expand Down Expand Up @@ -240,44 +244,43 @@ public static Map<String, Object> getVMSpecificAttributesMap(ILaunchConfiguratio
return map;
}

public static String getTracingFileArgument(ILaunchConfiguration config, String optionsFileName) {
public static String getTracingFileArgument(ILaunchConfiguration config, Path optionsFile) {
try {
TracingOptionsManager mng = PDECore.getDefault().getTracingOptionsManager();
Map<String, String> options = config.getAttribute(IPDELauncherConstants.TRACING_OPTIONS, (Map<String, String>) null);
String selected = config.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String) null);
if (selected == null) {
mng.save(optionsFileName, options);
mng.save(optionsFile, options);
} else if (!selected.equals(IPDELauncherConstants.TRACING_NONE)) {
HashSet<String> result = new HashSet<>();
StringTokenizer tokenizer = new StringTokenizer(selected, ","); //$NON-NLS-1$
while (tokenizer.hasMoreTokens()) {
result.add(tokenizer.nextToken());
}
mng.save(optionsFileName, options, result);
Set<String> result = splitElementsByComma(selected).collect(Collectors.toSet());
mng.save(optionsFile, options, result);
}
} catch (CoreException e) {
return ""; //$NON-NLS-1$
}
return optionsFileName;
return optionsFile.toString();
}

public static String[] constructClasspath(ILaunchConfiguration configuration) throws CoreException {
double targetVersion = TargetPlatformHelper.getTargetVersion();
String jarPath = targetVersion >= 3.3 ? getEquinoxStartupPath(IPDEBuildConstants.BUNDLE_EQUINOX_LAUNCHER) : getStartupJarPath();
if (jarPath == null && targetVersion < 3.3)
if (jarPath == null && targetVersion < 3.3) {
jarPath = getEquinoxStartupPath("org.eclipse.core.launcher"); //$NON-NLS-1$

if (jarPath == null)
}
if (jarPath == null) {
return null;

ArrayList<String> entries = new ArrayList<>();
}
List<String> entries = new ArrayList<>();
entries.add(jarPath);

String bootstrap = configuration.getAttribute(IPDELauncherConstants.BOOTSTRAP_ENTRIES, ""); //$NON-NLS-1$
StringTokenizer tok = new StringTokenizer(getSubstitutedString(bootstrap), ","); //$NON-NLS-1$
while (tok.hasMoreTokens())
entries.add(tok.nextToken().trim());
return entries.toArray(new String[entries.size()]);
return splitElementsByComma(getSubstitutedString(bootstrap)).map(String::trim).toArray(String[]::new);
}

private static final Pattern COMMA = Pattern.compile(","); //$NON-NLS-1$

public static Stream<String> splitElementsByComma(String value) {
return COMMA.splitAsStream(value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
Expand Down Expand Up @@ -379,7 +378,7 @@ public String[] getProgramArguments(ILaunchConfiguration configuration) throws C
// add tracing, if turned on
if (configuration.getAttribute(IPDELauncherConstants.TRACING, false) && !IPDELauncherConstants.TRACING_NONE.equals(configuration.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String) null))) {
programArgs.add("-debug"); //$NON-NLS-1$
programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, getConfigDir(configuration).toString() + IPath.SEPARATOR + ICoreConstants.OPTIONS_FILENAME));
programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, getConfigDir(configuration).toPath().resolve(ICoreConstants.OPTIONS_FILENAME)));
}

// add the program args specified by the user
Expand Down Expand Up @@ -459,8 +458,9 @@ protected void preLaunchCheck(ILaunchConfiguration configuration, ILaunch launch
* configuration
*/
protected File getConfigDir(ILaunchConfiguration configuration) {
if (fConfigDir == null)
if (fConfigDir == null) {
fConfigDir = LaunchConfigurationHelper.getConfigurationArea(configuration);
}
return fConfigDir;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.eclipse.pde.launching;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -246,7 +247,7 @@ protected void collectExecutionArguments(ILaunchConfiguration configuration, Lis
// Create the .options file if tracing is turned on
if (configuration.getAttribute(IPDELauncherConstants.TRACING, false) && !IPDELauncherConstants.TRACING_NONE.equals(configuration.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String) null))) {
programArgs.add("-debug"); //$NON-NLS-1$
String path = getConfigurationDirectory(configuration).getPath() + IPath.SEPARATOR + ICoreConstants.OPTIONS_FILENAME;
Path path = getConfigurationDirectory(configuration).toPath().resolve(ICoreConstants.OPTIONS_FILENAME);
programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, path));
}

Expand Down
Loading

0 comments on commit f773b1d

Please sign in to comment.