From 3f6109bedbf34e43ec86e2c6202db8ec7fc697ae Mon Sep 17 00:00:00 2001 From: Tobias Schwarz Date: Mon, 22 Oct 2012 12:34:01 +0200 Subject: [Target Explorer] variable delegates and provider für persistence delegates --- .../persistence/AbstractPathVariableDelegate.java | 41 +++++++ .../persistence/AbstractVariableDelegate.java | 126 +++++++++++++++++++++ .../persistence/GsonMapPersistenceDelegate.java | 8 +- .../te/runtime/persistence/PersistenceManager.java | 11 ++ .../persistence/interfaces/IVariableDelegate.java | 15 ++- .../persistence/interfaces/IVariableProvider.java | 25 ++++ .../persistence/internal/VariableProvider.java | 48 ++++++++ .../VariableProviderExtensionPointManager.java | 79 +++++++++++++ 8 files changed, 347 insertions(+), 6 deletions(-) create mode 100644 target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractPathVariableDelegate.java create mode 100644 target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractVariableDelegate.java create mode 100644 target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IVariableProvider.java create mode 100644 target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableProvider.java create mode 100644 target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableProviderExtensionPointManager.java (limited to 'target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf') diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractPathVariableDelegate.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractPathVariableDelegate.java new file mode 100644 index 000000000..09da17128 --- /dev/null +++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractPathVariableDelegate.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2012 Wind River Systems, Inc. and others. All rights reserved. + * This program and the accompanying materials are made available under the terms + * of the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Wind River Systems - initial API and implementation + *******************************************************************************/ + +package org.eclipse.tcf.te.runtime.persistence; + +import org.eclipse.core.runtime.Path; +import org.eclipse.tcf.te.runtime.utils.Host; + +/** + * AbstractPathVariableDelegate + */ +public abstract class AbstractPathVariableDelegate extends AbstractVariableDelegate { + + /** + * Return true, if the key represents a path value. + * @param key The key to check. + * @return true if the key represents a path value. + */ + protected abstract boolean isPathKey(String key); + + /* (non-Javadoc) + * @see org.eclipse.tcf.te.runtime.persistence.AbstractVariableDelegate#useVariable(java.lang.String, java.lang.Object, java.lang.String, java.lang.String) + */ + @Override + protected Object useVariable(String key, Object value, String variableName, String variableValue) { + if (isPathKey(key) && value instanceof String) { + String valuePath = new Path(Host.isWindowsHost() ? ((String)value).toLowerCase() : (String)value).toString(); + String variablePath = new Path(Host.isWindowsHost() ? variableValue.toLowerCase() : (String)variableValue).toString(); + return super.useVariable(key, valuePath, variableName, variablePath); + } + + return super.useVariable(key, value, variableName, variableValue); + } +} diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractVariableDelegate.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractVariableDelegate.java new file mode 100644 index 000000000..b709d41d8 --- /dev/null +++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractVariableDelegate.java @@ -0,0 +1,126 @@ +/******************************************************************************* + * Copyright (c) 2012 Wind River Systems, Inc. and others. All rights reserved. + * This program and the accompanying materials are made available under the terms + * of the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Wind River Systems - initial API and implementation + *******************************************************************************/ + +package org.eclipse.tcf.te.runtime.persistence; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableDelegate; +import org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableProvider; + +/** + * AbstractVariableDelegate + */ +public abstract class AbstractVariableDelegate implements IVariableDelegate { + + /** + * Get the list of keys this delegate is handling. + * @return The list of handled keys. + */ + protected abstract String[] getKeysToHandle(); + + /** + * Try to use a variable inside the given value. + * @param key The key of the value. + * @param value The value to inspect. + * @param variableName The variable name to use. + * @param variableValue The variable value. + * @return The new value if the variable was used, null otherwise. + */ + protected Object useVariable(String key, Object value, String variableName, String variableValue) { + if (value instanceof String && ((String)value).contains(variableValue)) { + return ((String)value).replaceAll(variableValue, "<"+variableName+">"); //$NON-NLS-1$ //$NON-NLS-2$ + } + return null; + } + + /** + * Try to restore a variable inside the given value. + * @param key The key of the value. + * @param value The value to inspect. + * @param variableName The variable name to restore. + * @param variableValue The variable value. + * @return The new value if the variable was used, null otherwise. + */ + protected Object restoreValue(String key, Object value, String variableName, String variableValue) { + if (value instanceof String && ((String)value).contains("<"+variableName+">")) { //$NON-NLS-1$ //$NON-NLS-2$ + return ((String)value).replaceAll("<"+variableName+">", variableValue); //$NON-NLS-1$ //$NON-NLS-2$ + } + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableDelegate#getVariables(java.util.Map) + */ + @Override + public Map getVariables(Map map) { + Map usedVariables = new HashMap(); + + for (String key : getKeysToHandle()) { + if (map.get(key) != null) { + for (Entry variable : getVariablesMap(null).entrySet()) { + Object newValue = useVariable(key, map.get(key), variable.getKey(), variable.getValue()); + if (newValue != null) { + map.put(key, newValue); + usedVariables.put(variable.getKey(), variable.getValue()); + } + } + } + } + + return usedVariables; + } + + /* (non-Javadoc) + * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableDelegate#putVariables(java.util.Map, java.util.Map) + */ + @Override + public Map putVariables(Map map, Map defaultVariables) { + for (String key : getKeysToHandle()) { + if (map.get(key) != null) { + for (Entry variable : getVariablesMap(defaultVariables).entrySet()) { + Object newValue = restoreValue(key, map.get(key), variable.getKey(), variable.getValue()); + if (newValue != null) { + map.put(key, newValue); + } + } + } + } + + return map; + } + + /** + * Calculate and return the map of variables to use. + * If a map of default variables is given, all variables will be added to the result + * map if the name does not exist in the list of provided {@link IVariableProvider} variables. + * @param defaultVariables Default variables or null. + * @return Map of variables to use. + */ + protected Map getVariablesMap(Map defaultVariables) { + Map variables = new HashMap(); + for (IVariableProvider provider : PersistenceManager.getInstance().getVariableProviders()) { + variables.putAll(provider.getVariables()); + } + + if (defaultVariables != null) { + for (Entry defaultVariable : defaultVariables.entrySet()) { + if (!variables.containsKey(defaultVariable.getKey())) { + variables.put(defaultVariable.getKey(), defaultVariable.getValue()); + } + } + } + + return variables; + } + +} diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/GsonMapPersistenceDelegate.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/GsonMapPersistenceDelegate.java index 71b4e4409..06c37d5b0 100644 --- a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/GsonMapPersistenceDelegate.java +++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/GsonMapPersistenceDelegate.java @@ -134,12 +134,12 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I Map data = toMap(context); if (data != null) { - Map variables = new HashMap(); + Map variables = null; IVariableDelegate[] delegates = PersistenceManager.getInstance().getVariableDelegates(this); for (IVariableDelegate delegate : delegates) { - delegate.getVariables(data, variables); + variables = delegate.getVariables(data); } - if (!variables.isEmpty()) { + if (variables != null && !variables.isEmpty()) { data.put(VARIABLES, variables); } } @@ -196,7 +196,7 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I Map variables = (Map)data.remove(VARIABLES); IVariableDelegate[] delegates = PersistenceManager.getInstance().getVariableDelegates(this); for (IVariableDelegate delegate : delegates) { - delegate.putVariables(data, variables); + data = delegate.putVariables(data, variables); } } diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/PersistenceManager.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/PersistenceManager.java index 795c9b564..b89b29d3f 100644 --- a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/PersistenceManager.java +++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/PersistenceManager.java @@ -22,9 +22,11 @@ import org.eclipse.tcf.te.runtime.extensions.ExecutableExtensionProxy; import org.eclipse.tcf.te.runtime.persistence.activator.CoreBundleActivator; import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate; import org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableDelegate; +import org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableProvider; import org.eclipse.tcf.te.runtime.persistence.internal.PersistenceDelegateBinding; import org.eclipse.tcf.te.runtime.persistence.internal.PersistenceDelegateBindingExtensionPointManager; import org.eclipse.tcf.te.runtime.persistence.internal.VariableDelegateExtensionPointManager; +import org.eclipse.tcf.te.runtime.persistence.internal.VariableProviderExtensionPointManager; /** * Persistence delegate manager implementation. @@ -155,4 +157,13 @@ public class PersistenceManager extends AbstractExtensionPointManager map, Map variables); + /** + * Extract variables from map and use them inside map values. + * @param map The map to inspect. + * @return The used variables. + */ + public Map getVariables(Map map); - public void putVariables(Map map, Map variables); + /** + * Replace all variables inside map values by current variable value or given default. + * @param map The map with variables to inspect. + * @param defaultVariables The default variables. + * @return The map without variables. + */ + public Map putVariables(Map map, Map defaultVariables); } diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IVariableProvider.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IVariableProvider.java new file mode 100644 index 000000000..a13ca029b --- /dev/null +++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IVariableProvider.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2012 Wind River Systems, Inc. and others. All rights reserved. + * This program and the accompanying materials are made available under the terms + * of the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Wind River Systems - initial API and implementation + *******************************************************************************/ + +package org.eclipse.tcf.te.runtime.persistence.interfaces; + +import java.util.Map; + +/** + * IVariableProvider + */ +public interface IVariableProvider { + + /** + * Get the list of provided variables. + * @return The provided variables. + */ + public Map getVariables(); +} diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableProvider.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableProvider.java new file mode 100644 index 000000000..bbf3b0861 --- /dev/null +++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableProvider.java @@ -0,0 +1,48 @@ +/** + * VariableProvider.java + * Created on 22.10.2012 + * + * Copyright (c) 2012 Wind River Systems, Inc. + * + * The right to copy, distribute, modify, or otherwise make use + * of this software may be licensed only pursuant to the terms + * of an applicable Wind River license agreement. + */ +package org.eclipse.tcf.te.runtime.persistence.internal; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.runtime.Path; +import org.eclipse.core.variables.IDynamicVariable; +import org.eclipse.core.variables.VariablesPlugin; +import org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableProvider; + +/** + * + */ +public class VariableProvider implements IVariableProvider { + + /* (non-Javadoc) + * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableProvider#getVariables() + */ + @Override + public Map getVariables() { + Map variables = new HashMap(); + + try { + IDynamicVariable variable = VariablesPlugin.getDefault().getStringVariableManager().getDynamicVariable("workspace_loc"); //$NON-NLS-1$ + if (variable != null) { + String value = variable.getValue(null); + if (value != null) { + variables.put("WORKSPACE_LOC", new Path(value).toString()); //$NON-NLS-1$ + } + } + } + catch (Exception e) { + } + + return variables; + } + +} diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableProviderExtensionPointManager.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableProviderExtensionPointManager.java new file mode 100644 index 000000000..452b3193d --- /dev/null +++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableProviderExtensionPointManager.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2012 Wind River Systems, Inc. and others. All rights reserved. + * This program and the accompanying materials are made available under the terms + * of the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Wind River Systems - initial API and implementation + *******************************************************************************/ +package org.eclipse.tcf.te.runtime.persistence.internal; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager; +import org.eclipse.tcf.te.runtime.extensions.ExecutableExtensionProxy; +import org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableProvider; + + +/** + * VariableProviderExtensionPointManager + */ +public class VariableProviderExtensionPointManager extends AbstractExtensionPointManager { + + /* + * Thread save singleton instance creation. + */ + private static class LazyInstance { + public static VariableProviderExtensionPointManager instance = new VariableProviderExtensionPointManager(); + } + + /** + * Constructor. + */ + VariableProviderExtensionPointManager() { + super(); + } + + /** + * Returns the singleton instance of the extension point manager. + */ + public static VariableProviderExtensionPointManager getInstance() { + return LazyInstance.instance; + } + + /* (non-Javadoc) + * @see org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager#getExtensionPointId() + */ + @Override + protected String getExtensionPointId() { + return "org.eclipse.tcf.te.runtime.persistence.variableProviders"; //$NON-NLS-1$ + } + + /* (non-Javadoc) + * @see org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager#getConfigurationElementName() + */ + @Override + protected String getConfigurationElementName() { + return "provider"; //$NON-NLS-1$ + } + + /** + * Returns all variable providers. + * + * @return The list of variable providers or an empty array. + */ + public IVariableProvider[] getProviders() { + List contributions = new ArrayList(); + Collection> delegates = getExtensions().values(); + for (ExecutableExtensionProxy delegate : delegates) { + IVariableProvider instance = delegate.getInstance(); + if (instance != null && !contributions.contains(instance)) { + contributions.add(instance); + } + } + return contributions.toArray(new IVariableProvider[contributions.size()]); + } +} -- cgit v1.2.3