Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUwe Stieber2011-11-23 15:15:02 +0000
committerUwe Stieber2011-11-23 15:15:02 +0000
commitcb68d5435bb883fdaf8a0603e8a534263d686596 (patch)
tree3047f8d9bf9f0526450b6879b66f01ab2895aacf /target_explorer/plugins/org.eclipse.tcf.te.core
parentda04cea420ccf86bc6edd0a21c0bd49d9a884528 (diff)
downloadorg.eclipse.tcf-cb68d5435bb883fdaf8a0603e8a534263d686596.tar.gz
org.eclipse.tcf-cb68d5435bb883fdaf8a0603e8a534263d686596.tar.xz
org.eclipse.tcf-cb68d5435bb883fdaf8a0603e8a534263d686596.zip
Target Explorer: Improve model node persistence mechanism
Diffstat (limited to 'target_explorer/plugins/org.eclipse.tcf.te.core')
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/adapters/ModelNodePersistableAdapter.java102
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/nls/Messages.java2
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/nls/Messages.properties2
3 files changed, 102 insertions, 4 deletions
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/adapters/ModelNodePersistableAdapter.java b/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/adapters/ModelNodePersistableAdapter.java
index 6c4bab4be..decfde327 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/adapters/ModelNodePersistableAdapter.java
+++ b/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/adapters/ModelNodePersistableAdapter.java
@@ -10,16 +10,26 @@
package org.eclipse.tcf.te.core.adapters;
import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import java.net.URI;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
+import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.te.core.activator.CoreBundleActivator;
+import org.eclipse.tcf.te.core.nls.Messages;
import org.eclipse.tcf.te.runtime.model.interfaces.IModelNode;
+import org.eclipse.tcf.te.runtime.persistence.PersistenceDelegateManager;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistable;
+import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate;
/**
* Model node persistable adapter implementation.
@@ -108,14 +118,98 @@ public class ModelNodePersistableAdapter implements IPersistable {
public Map<String, Object> exportFrom(Object data) throws IOException {
Assert.isNotNull(data);
- Map<String, Object> result = null;
+ // Create a new map instance that will hold the exported properties
+ Map<String, Object> result = new HashMap<String, Object>();
// Only model nodes are supported
- if (data instanceof IModelNode) {
- result = ((IModelNode)data).getProperties();
+ if (data instanceof IModelNode && !((IModelNode)data).isEmpty()) {
+ // Get a snapshot of all properties
+ Map<String, Object> properties = ((IModelNode)data).getProperties();
+ // And export the properties to the result map
+ exportFromMap(properties, result);
}
- return result;
+ // If the result map is empty, return null
+ return !result.isEmpty() ? result : null;
+ }
+
+ /**
+ * Exports the properties of a map from the given source into the given
+ * destination.
+ *
+ * @param src The map to export the properties from. Must not be <code>null</code>.
+ * @param dst The map to write the exported properties to. Must not be <code>null</code>.
+ *
+ * @throws IOException - if the operation fails.
+ */
+ @SuppressWarnings("unchecked")
+ protected void exportFromMap(Map<String, Object> src, Map<String, Object> dst) throws IOException {
+ Assert.isNotNull(src);
+ Assert.isNotNull(dst);
+
+ // Loop all properties and check for transient or complex properties
+ for (String key : src.keySet()) {
+ if (key.contains(".transient")) continue; //$NON-NLS-1$
+
+ // Get the property value
+ Object value = src.get(key);
+
+ // If the value is null, no need to go any further
+ if (value == null) continue;
+
+ // For String, Integer, Boolean, etc ... export them as string
+ boolean isSimpleType = value instanceof String || value instanceof Boolean || value instanceof Integer || value instanceof Long
+ || value instanceof Float || value instanceof Double;
+ if (isSimpleType) {
+ dst.put(key, value.toString());
+ continue;
+ }
+
+ // BigInteger, BigDecimal ... probably needs special handling, for now, export them as string
+ boolean isBigType = value instanceof BigInteger || value instanceof BigDecimal;
+ if (isBigType) {
+ dst.put(key, value.toString());
+ continue;
+ }
+
+ // For Lists and Arrays, do a deepToString
+ boolean isListType = value instanceof List<?> || value instanceof Object[];
+ if (isListType) {
+ dst.put(key, Arrays.deepToString(value instanceof List<?> ? ((List<?>)value).toArray() : (Object[])value));
+ continue;
+ }
+
+ // For Maps, create a new destination map and call ourself
+ boolean isMapType = value instanceof Map<?,?>;
+ if (isMapType) {
+ Map<String, Object> result = new HashMap<String, Object>();
+ exportFromMap((Map<String, Object>)value, result);
+ if (!result.isEmpty()) dst.put(key, result);
+ continue;
+ }
+
+ // For anything remaining, check if the value object type can be adapted to
+ // an IPersistable itself
+ IPersistable persistable = value instanceof IAdaptable ? (IPersistable)((IAdaptable)value).getAdapter(IPersistable.class) : null;
+ if (persistable == null) persistable = (IPersistable)Platform.getAdapterManager().getAdapter(value, IPersistable.class);
+ if (persistable != null) {
+ // Create a reference object
+ Map<String, String> reference = new HashMap<String, String>();
+ reference.put("storageID", persistable.getStorageID()); //$NON-NLS-1$
+ reference.put("uri", persistable.getURI(value).toString()); //$NON-NLS-1$
+
+ IPersistenceDelegate delegate = PersistenceDelegateManager.getInstance().getDelegate(persistable.getStorageID(), false);
+ if (delegate != null) {
+ delegate.write(persistable.getURI(value), persistable.exportFrom(value));
+ dst.put(key, reference);
+ continue;
+ }
+ }
+
+ // Falling through down here is a problem. We should never end up here,
+ // because it means we have no idea on how to persist an object
+ throw new IOException(NLS.bind(Messages.ModelNodePersistableAdapter_export_unknownType, value.getClass().getCanonicalName(), key));
+ }
}
/* (non-Javadoc)
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/nls/Messages.java b/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/nls/Messages.java
index a4ae19eb1..ab4825298 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/nls/Messages.java
+++ b/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/nls/Messages.java
@@ -34,4 +34,6 @@ public class Messages extends NLS {
public static String ConnectStrategyStepExecutor_warning_stepFailed;
public static String ConnectStrategyStepExecutor_error_stepFailed;
public static String ConnectStrategyStepExecutor_stepFailed_debugInfo;
+
+ public static String ModelNodePersistableAdapter_export_unknownType;
}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/nls/Messages.properties b/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/nls/Messages.properties
index b727f8639..292522277 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/nls/Messages.properties
+++ b/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/nls/Messages.properties
@@ -17,3 +17,5 @@ Context: {1}\n\
Connect Strategy: {2}\n\
Connect Step: {3}
ConnectStrategyStepExecutor_stepFailed_debugInfo=Debug info:\n{0}
+
+ModelNodePersistableAdapter_export_unknownType=No strategy to persist an object of type ''{0}'', key = ''{1}''.

Back to the top