Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Schwarz2012-10-19 00:28:42 -0400
committerTobias Schwarz2012-10-19 00:28:42 -0400
commit7b1c89c7ff1eb8ab019a8d9955b031782dc4ea6b (patch)
treee6446ac5062d467c8986d961a2ad46c172019060 /target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence
parent64273084c411ea7b319721f81dc28d108936375e (diff)
downloadorg.eclipse.tcf-7b1c89c7ff1eb8ab019a8d9955b031782dc4ea6b.tar.gz
org.eclipse.tcf-7b1c89c7ff1eb8ab019a8d9955b031782dc4ea6b.tar.xz
org.eclipse.tcf-7b1c89c7ff1eb8ab019a8d9955b031782dc4ea6b.zip
[Target Explorer] introduce IVariableDelegate for persistence delegates
Diffstat (limited to 'target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence')
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/GsonMapPersistenceDelegate.java49
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/PersistenceManager.java12
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IVariableDelegate.java23
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableDelegateExtensionPointManager.java85
4 files changed, 164 insertions, 5 deletions
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 854c33209..7772e5f1b 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
@@ -29,6 +29,7 @@ import org.eclipse.core.runtime.Path;
import org.eclipse.tcf.te.runtime.extensions.ExecutableExtension;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate;
+import org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableDelegate;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
import com.google.gson.Gson;
@@ -41,6 +42,8 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I
private final String defaultFileExtension;
+ protected static final String VARIABLES = "__VariablesMap__"; //$NON-NLS-1$
+
/**
* Constructor.
*/
@@ -76,7 +79,7 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I
* @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#write(java.lang.Object, java.lang.Object, java.lang.String)
*/
@Override
- public Object write(Object context, Object container, String key) throws IOException {
+ public final Object write(Object context, Object container, String key) throws IOException {
Assert.isNotNull(context);
Assert.isNotNull(container);
@@ -106,7 +109,8 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I
try {
writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); //$NON-NLS-1$
Gson gson = new GsonBuilder().setPrettyPrinting().create();
- gson.toJson(toMap(context), Map.class, writer);
+
+ gson.toJson(internalToMap(context), Map.class, writer);
} finally {
if (writer != null) {
writer.close();
@@ -115,17 +119,44 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I
}
else if (container instanceof String || String.class.equals(container)) {
Gson gson = new GsonBuilder().create();
- container = gson.toJson(toMap(context));
+
+ container = gson.toJson(internalToMap(context));
}
return container;
}
+ /*
+ * Convert the context to a Map, extract and use variables and add them to the map as key VARIABLE.
+ */
+ private Map<String,Object> internalToMap(Object context) {
+ try {
+ Map<String,Object> data = toMap(context);
+
+ if (data != null) {
+ Map<String,String> variables = new HashMap<String, String>();
+ IVariableDelegate[] delegates = PersistenceManager.getInstance().getVariableDelegates(this);
+ for (IVariableDelegate delegate : delegates) {
+ delegate.getVariables(data, variables);
+ }
+ if (!variables.isEmpty()) {
+ data.put(VARIABLES, variables);
+ }
+ }
+ return data;
+ }
+ catch (Exception e) {
+
+ }
+
+ return null;
+ }
+
/* (non-Javadoc)
* @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#read(java.lang.Object, java.lang.Object, java.lang.String)
*/
@Override
- public Object read(Object context, Object container, String key) throws IOException {
+ public final Object read(Object context, Object container, String key) throws IOException {
Assert.isNotNull(container);
Gson gson = new GsonBuilder().create();
@@ -161,6 +192,14 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I
data = gson.fromJson((String)container, Map.class);
}
+ if (data != null && data.containsKey(VARIABLES)) {
+ Map<String,String> variables = (Map<String,String>)data.remove(VARIABLES);
+ IVariableDelegate[] delegates = PersistenceManager.getInstance().getVariableDelegates(this);
+ for (IVariableDelegate delegate : delegates) {
+ delegate.putVariables(data, variables);
+ }
+ }
+
return data != null ? fromMap(data, context) : context;
}
@@ -241,7 +280,7 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I
* @throws IOException
*/
protected Object fromMap(Map<String,Object> map, Object context) throws IOException {
- if (context == null || (context instanceof Class && ((Class<?>)context).isInstance(map))) {
+ if (context == null || context.equals(map.getClass())) {
return map;
}
else if (context instanceof Map) {
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 c8e03c25f..795c9b564 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
@@ -21,8 +21,10 @@ import org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager;
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.internal.PersistenceDelegateBinding;
import org.eclipse.tcf.te.runtime.persistence.internal.PersistenceDelegateBindingExtensionPointManager;
+import org.eclipse.tcf.te.runtime.persistence.internal.VariableDelegateExtensionPointManager;
/**
* Persistence delegate manager implementation.
@@ -143,4 +145,14 @@ public class PersistenceManager extends AbstractExtensionPointManager<IPersisten
return delegates.get(0);
}
+
+ /**
+ * Returns the bound variable delegates for the given persistence delegate.
+ *
+ * @param persistenceDelegate The persistence delegate.
+ * @return The list of bound variable delegates or an empty array.
+ */
+ public IVariableDelegate[] getVariableDelegates(IPersistenceDelegate persistenceDelegate) {
+ return VariableDelegateExtensionPointManager.getInstance().getDelegates(persistenceDelegate);
+ }
}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IVariableDelegate.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IVariableDelegate.java
new file mode 100644
index 000000000..5a95009c9
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IVariableDelegate.java
@@ -0,0 +1,23 @@
+/*******************************************************************************
+ * 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;
+
+/**
+ * IVariableDelegate
+ */
+public interface IVariableDelegate {
+
+ public void getVariables(Map<String,Object> map, Map<String,String> variables);
+
+ public void putVariables(Map<String,Object> map, Map<String,String> variables);
+}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableDelegateExtensionPointManager.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableDelegateExtensionPointManager.java
new file mode 100644
index 000000000..ed00938fd
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/internal/VariableDelegateExtensionPointManager.java
@@ -0,0 +1,85 @@
+/*******************************************************************************
+ * 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.IPersistenceDelegate;
+import org.eclipse.tcf.te.runtime.persistence.interfaces.IVariableDelegate;
+
+
+/**
+ * VariableDelegateExtensionPointManager
+ */
+public class VariableDelegateExtensionPointManager extends AbstractExtensionPointManager<IVariableDelegate> {
+
+ /*
+ * Thread save singleton instance creation.
+ */
+ private static class LazyInstance {
+ public static VariableDelegateExtensionPointManager instance = new VariableDelegateExtensionPointManager();
+ }
+
+ /**
+ * Constructor.
+ */
+ VariableDelegateExtensionPointManager() {
+ super();
+ }
+
+ /**
+ * Returns the singleton instance of the extension point manager.
+ */
+ public static VariableDelegateExtensionPointManager 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.variableDelegates"; //$NON-NLS-1$
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager#getConfigurationElementName()
+ */
+ @Override
+ protected String getConfigurationElementName() {
+ return "delegate"; //$NON-NLS-1$
+ }
+
+ /**
+ * Returns the bound variable delegates for the given persistence delegate.
+ *
+ * @param persistenceDelegate The persistence delegate.
+ * @return The list of bound variable delegates or an empty array.
+ */
+ public IVariableDelegate[] getDelegates(IPersistenceDelegate persistenceDelegate) {
+ List<IVariableDelegate> contributions = new ArrayList<IVariableDelegate>();
+ Collection<ExecutableExtensionProxy<IVariableDelegate>> delegates = getExtensions().values();
+ for (ExecutableExtensionProxy<IVariableDelegate> delegate : delegates) {
+ String id = delegate.getConfigurationElement().getAttribute("delegateId"); //$NON-NLS-1$
+ if (id == null || id.equals(persistenceDelegate.getId())) {
+ IVariableDelegate instance = delegate.getInstance();
+ if (instance != null && !contributions.contains(instance)) {
+ contributions.add(instance);
+ }
+ }
+ }
+
+ return contributions.toArray(new IVariableDelegate[contributions.size()]);
+ }
+}

Back to the top