Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUwe Stieber2011-11-16 08:59:51 +0000
committerUwe Stieber2011-11-16 08:59:51 +0000
commit8706fb04b6c1417ed8d5ece83bc9fca64ddfc3ce (patch)
tree35ff2855e92a05029eb1a01455c6018c573c7175 /target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src
parent67449886c09c8ec4da4f39082373ab62ccc3d978 (diff)
downloadorg.eclipse.tcf-8706fb04b6c1417ed8d5ece83bc9fca64ddfc3ce.tar.gz
org.eclipse.tcf-8706fb04b6c1417ed8d5ece83bc9fca64ddfc3ce.tar.xz
org.eclipse.tcf-8706fb04b6c1417ed8d5ece83bc9fca64ddfc3ce.zip
Target Explorer: Refactor name space from org.eclipse.tm.te.* to org.eclipse.tcf.te.*
Diffstat (limited to 'target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src')
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractPersistenceDelegate.java20
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/PersistenceDelegateManager.java102
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/activator/CoreBundleActivator.java59
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistable.java66
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceDelegate.java65
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceService.java57
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/properties/PropertiesFilePersistenceDelegate.java274
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/services/PersistenceService.java127
8 files changed, 770 insertions, 0 deletions
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractPersistenceDelegate.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractPersistenceDelegate.java
new file mode 100644
index 000000000..dd04fb2ca
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractPersistenceDelegate.java
@@ -0,0 +1,20 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate;
+import org.eclipse.tcf.te.runtime.extensions.ExecutableExtension;
+
+/**
+ * Abstract persistence delegate implementation.
+ */
+public abstract class AbstractPersistenceDelegate extends ExecutableExtension implements IPersistenceDelegate {
+
+}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/PersistenceDelegateManager.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/PersistenceDelegateManager.java
new file mode 100644
index 000000000..802f94cb9
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/PersistenceDelegateManager.java
@@ -0,0 +1,102 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate;
+import org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager;
+import org.eclipse.tcf.te.runtime.extensions.ExecutableExtensionProxy;
+
+/**
+ * Persistence delegate manager implementation.
+ */
+public class PersistenceDelegateManager extends AbstractExtensionPointManager<IPersistenceDelegate> {
+
+ /*
+ * Thread save singleton instance creation.
+ */
+ private static class LazyInstance {
+ public static PersistenceDelegateManager instance = new PersistenceDelegateManager();
+ }
+
+ /**
+ * Constructor.
+ */
+ PersistenceDelegateManager() {
+ super();
+ }
+
+ /**
+ * Returns the singleton instance of the extension point manager.
+ */
+ public static PersistenceDelegateManager 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.delegates"; //$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 list of all contributed persistence delegates.
+ *
+ * @param unique If <code>true</code>, the method returns new instances for each
+ * contributed persistence delegate.
+ *
+ * @return The list of contributed persistence delegates, or an empty array.
+ */
+ public IPersistenceDelegate[] getDelegates(boolean unique) {
+ List<IPersistenceDelegate> contributions = new ArrayList<IPersistenceDelegate>();
+ Collection<ExecutableExtensionProxy<IPersistenceDelegate>> delegates = getExtensions().values();
+ for (ExecutableExtensionProxy<IPersistenceDelegate> delegate : delegates) {
+ IPersistenceDelegate instance = unique ? delegate.newInstance() : delegate.getInstance();
+ if (instance != null && !contributions.contains(instance)) {
+ contributions.add(instance);
+ }
+ }
+
+ return contributions.toArray(new IPersistenceDelegate[contributions.size()]);
+ }
+
+ /**
+ * Returns the persistence delegate identified by its unique id. If no persistence
+ * delegate with the specified id is registered, <code>null</code> is returned.
+ *
+ * @param id The unique id of the persistence delegate or <code>null</code>
+ * @param unique If <code>true</code>, the method returns new instances of the persistence delegate contribution.
+ *
+ * @return The persistence delegate instance or <code>null</code>.
+ */
+ public IPersistenceDelegate getDelegate(String id, boolean unique) {
+ IPersistenceDelegate contribution = null;
+ if (getExtensions().containsKey(id)) {
+ ExecutableExtensionProxy<IPersistenceDelegate> proxy = getExtensions().get(id);
+ // Get the extension instance
+ contribution = unique ? proxy.newInstance() : proxy.getInstance();
+ }
+
+ return contribution;
+ }
+}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/activator/CoreBundleActivator.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/activator/CoreBundleActivator.java
new file mode 100644
index 000000000..1a5393afe
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/activator/CoreBundleActivator.java
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.activator;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+/**
+ * The activator class controls the plug-in life cycle
+ */
+public class CoreBundleActivator implements BundleActivator {
+ // The bundle context
+ private static BundleContext context;
+
+ /**
+ * Returns the bundle context
+ *
+ * @return the bundle context
+ */
+ public static BundleContext getContext() {
+ return context;
+ }
+
+ /**
+ * Convenience method which returns the unique identifier of this plugin.
+ */
+ public static String getUniqueIdentifier() {
+ if (getContext() != null && getContext().getBundle() != null) {
+ return getContext().getBundle().getSymbolicName();
+ }
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+ */
+ @Override
+ public void start(BundleContext bundleContext) throws Exception {
+ CoreBundleActivator.context = bundleContext;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+ */
+ @Override
+ public void stop(BundleContext bundleContext) throws Exception {
+ CoreBundleActivator.context = null;
+ }
+
+}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistable.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistable.java
new file mode 100644
index 000000000..d93f862fd
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistable.java
@@ -0,0 +1,66 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.io.IOException;
+import java.net.URI;
+import java.util.Map;
+
+/**
+ * Interface to be implemented by persistable elements.
+ */
+public interface IPersistable {
+
+ /**
+ * Returns the id of the persistence storage to use for persisting data objects.
+ * <p>
+ * The method is expected to return never <code>null</code>.
+ *
+ * @return The persistence storage id.
+ */
+ public String getStorageID();
+
+ /**
+ * Returns the URI reference to pass on to the associated persistence delegate to
+ * denote the given data object.
+ * <p>
+ * The interpretation of the URI reference is up to the persistence delegate, but
+ * the method is expected to return never <code>null</code>.
+ *
+ * @param data The data object. Must not be <code>null</code>.
+ *
+ * @return The URI.
+ */
+ public URI getURI(Object data);
+
+ /**
+ * Exports the given data object to an external representation.
+ * <p>
+ * As a general guide line, it is expected that the external representation contains only base
+ * Java objects like maps, lists and Strings. Details about the valid object types can be taken
+ * from the referenced persistence delegate.
+ *
+ * @param data The data object. Must not be <code>null</code>.
+ * @return The external representation of the given data object.
+ *
+ * @throws IOException - if the operation fails.
+ */
+ public Map<String, Object> exportFrom(Object data) throws IOException;
+
+ /**
+ * Imports the given external representation into the given data object.
+ *
+ * @param data The data object. Must not be <code>null</code>.
+ * @param external The external representation. Must not be <code>null</code>.
+ *
+ * @throws IOException - if the operation fails.
+ */
+ public void importTo(Object data, Map<String, Object> external) throws IOException;
+}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceDelegate.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceDelegate.java
new file mode 100644
index 000000000..915680503
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceDelegate.java
@@ -0,0 +1,65 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.io.IOException;
+import java.net.URI;
+import java.util.Map;
+
+import org.eclipse.tcf.te.runtime.interfaces.extensions.IExecutableExtension;
+
+/**
+ * Interface to be implemented by persistence delegates.
+ */
+public interface IPersistenceDelegate extends IExecutableExtension {
+
+ /**
+ * Writes the given data to the given persistence storage.
+ * <p>
+ * The persistence storage location is defined by the specified URI reference. The exact
+ * interpretation and semantic of the URI reference is up to the persistence delegate
+ * contributor.
+ *
+ * @param uri The persistence storage location URI reference. Must not be <code>null</code>.
+ * @param data The data. Must not be <code>null</code>.
+ *
+ * @throws IOException - if the operation fails
+ */
+ public void write(URI uri, Map<String, Object> data) throws IOException;
+
+ /**
+ * Reads the data from the given persistence storage.
+ * <p>
+ * The persistence storage location is defined by the specified URI reference. The exact
+ * interpretation and semantic of the URI reference is up to the persistence delegate
+ * contributor.
+ *
+ * @param uri The persistence storage location URI reference. Must not be <code>null</code>.
+ * @return The data.
+ *
+ * @throws IOException - if the operation fails
+ */
+ public Map<String, Object> read(URI uri) throws IOException;
+
+ /**
+ * Deletes the given persistence storage.
+ * <p>
+ * The persistence storage location is defined by the specified URI reference. The exact
+ * interpretation and semantic of the URI reference is up to the persistence delegate
+ * contributor.
+ *
+ * @param uri The persistence storage location URI reference. Must not be <code>null</code>.
+ * @return <code>True</code> if the persistence storage is successfully deleted;
+ * <code>false</code> otherwise.
+ *
+ * @throws IOException - if the operation fails
+ */
+ public boolean delete(URI uri) throws IOException;
+}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceService.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceService.java
new file mode 100644
index 000000000..02d1419be
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceService.java
@@ -0,0 +1,57 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.io.IOException;
+
+import org.eclipse.tcf.te.runtime.services.interfaces.IService;
+
+/**
+ * A service for persisting elements to a persistence store.
+ */
+public interface IPersistenceService extends IService {
+
+ /**
+ * Writes the given data object via a persistence delegate to a persistence storage. The
+ * persistence delegate to use will be determined by adapting the given data object to an
+ * {@link IPersistable}.
+ *
+ * @param data The data object. Must not be <code>null</code>.
+ *
+ * @throws IOException - if the operation fails.
+ */
+ public void write(Object data) throws IOException;
+
+ /**
+ * Fills the given data object with the data read via a persistence delegate from a given
+ * persistence storage. The persistence delegate to use will be determined by adapting the given
+ * data object to an {@link IPersistable}.
+ *
+ * @param data The data object. Must not be <code>null</code>.
+ *
+ * @throws IOException - if the operation fails
+ */
+ public void read(Object data) throws IOException;
+
+ /**
+ * Deletes the persistence storage for the given data object via a persistence delegate. The
+ * persistence delegate to use will be determined by adapting the given data object to an
+ * {@link IPersistable}.
+ *
+ * @param data The data object. Must not be <code>null</code>.
+ *
+ * @return <code>True</code> if the persistence storage is successfully deleted;
+ * <code>false</code> otherwise.
+ *
+ * @throws IOException - if the operation fails
+ */
+ public boolean delete(Object data) throws IOException;
+
+}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/properties/PropertiesFilePersistenceDelegate.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/properties/PropertiesFilePersistenceDelegate.java
new file mode 100644
index 000000000..d18465c6d
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/properties/PropertiesFilePersistenceDelegate.java
@@ -0,0 +1,274 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.properties;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.tcf.te.runtime.persistence.AbstractPersistenceDelegate;
+
+/**
+ * Properties file persistence delegate implementation.
+ * <p>
+ * The persistence delegates reads and writes a simple grouped properties file format.
+ */
+public class PropertiesFilePersistenceDelegate extends AbstractPersistenceDelegate {
+
+ /* (non-Javadoc)
+ * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#write(java.net.URI, java.util.Map)
+ */
+ @Override
+ public void write(URI uri, Map<String, Object> data) throws IOException {
+ Assert.isNotNull(uri);
+ Assert.isNotNull(data);
+
+ // Only "file:" URIs are supported
+ if (!"file".equalsIgnoreCase(uri.getScheme())) { //$NON-NLS-1$
+ throw new IOException("Unsupported URI schema '" + uri.getScheme() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ // Create the file object from the given URI
+ File file = new File(uri.normalize());
+
+ // The file must be absolute
+ if (!file.isAbsolute()) {
+ throw new IOException("URI must denote an absolute file path."); //$NON-NLS-1$
+ }
+
+ // If the file extension is no set, default to "properties"
+ IPath path = new Path(file.getCanonicalPath());
+ if (path.getFileExtension() == null) {
+ file = path.addFileExtension("properties").toFile(); //$NON-NLS-1$
+ }
+
+ // Create the writer object
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); //$NON-NLS-1$
+ try {
+ // Write the first level of attributes
+ writeMap(writer, "core", data); //$NON-NLS-1$
+ } finally {
+ writer.close();
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#delete(java.net.URI)
+ */
+ @Override
+ public boolean delete(URI uri) throws IOException {
+ Assert.isNotNull(uri);
+
+ // Only "file:" URIs are supported
+ if (!"file".equalsIgnoreCase(uri.getScheme())) { //$NON-NLS-1$
+ throw new IOException("Unsupported URI schema '" + uri.getScheme() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ // Create the file object from the given URI
+ File file = new File(uri.normalize());
+
+ // The file must be absolute
+ if (!file.isAbsolute()) {
+ throw new IOException("URI must denote an absolute file path."); //$NON-NLS-1$
+ }
+
+ // If the file extension is no set, default to "properties"
+ IPath path = new Path(file.getCanonicalPath());
+ if (path.getFileExtension() == null) {
+ file = path.addFileExtension("properties").toFile(); //$NON-NLS-1$
+ }
+
+ return file.delete();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#read(java.net.URI)
+ */
+ @Override
+ public Map<String, Object> read(URI uri) throws IOException {
+ Assert.isNotNull(uri);
+
+ // Only "file:" URIs are supported
+ if (!"file".equalsIgnoreCase(uri.getScheme())) { //$NON-NLS-1$
+ throw new IOException("Unsupported URI schema '" + uri.getScheme() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ // Create the file object from the given URI
+ File file = new File(uri.normalize());
+
+ // The file must be absolute
+ if (!file.isAbsolute()) {
+ throw new IOException("URI must denote an absolute file path."); //$NON-NLS-1$
+ }
+
+ Map<String, Object> data = new HashMap<String, Object>();
+
+ // Create the reader object
+ BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); //$NON-NLS-1$
+ try {
+ read(reader, data);
+ } finally {
+ reader.close();
+ }
+
+ return !data.isEmpty() ? data : null;
+ }
+
+ /**
+ * Write the map data.
+ *
+ * @param writer The writer. Must not be <code>null</code>.
+ * @param section The section name. Must not be <code>null</code>.
+ * @param data The data. Must not be <code>null</code>.
+ */
+ protected void writeMap(BufferedWriter writer, String section, Map<String, Object> data) throws IOException {
+ Assert.isNotNull(writer);
+ Assert.isNotNull(section);
+ Assert.isNotNull(data);
+
+ // Will contain the list of map keys where the value is a map type itself.
+ List<String> childMapKeys = new ArrayList<String>();
+ // Will contain the list of map keys where the value is not an map type.
+ List<String> childKeys = new ArrayList<String>();
+
+ // Get all the map keys and filter the map type values
+ for (String key : data.keySet()) {
+ if (data.get(key) instanceof Map) childMapKeys.add(key);
+ else childKeys.add(key);
+ }
+
+ // Sort both lists
+ Collections.sort(childMapKeys);
+ Collections.sort(childKeys);
+
+ // If the child key list is not empty, write the section
+ if (!childKeys.isEmpty()) {
+ // Write a new line except it is the "core" section
+ if (!"core".equals(section)) writer.newLine(); //$NON-NLS-1$
+
+ // Write the header
+ writer.write("[" + section.trim() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
+ writer.newLine();
+
+ for (String key : childKeys) {
+ writer.write('\t');
+ writer.write(key);
+ writer.write(" = "); //$NON-NLS-1$
+
+ Object value = data.get(key);
+ if (value instanceof List) {
+ writer.write(Arrays.deepToString(((List<?>)value).toArray()));
+ } else {
+ writer.write(value.toString());
+ }
+
+ writer.newLine();
+ }
+ }
+
+ // If there are map type values, write them now
+ if (!childMapKeys.isEmpty()) {
+ for (String key : childMapKeys) {
+ // Calculate the section name
+ String newSection = "core".equals(section) ? key.trim() : section + "." + key.trim(); //$NON-NLS-1$ //$NON-NLS-2$
+ // Write it
+ writeMap(writer, newSection, (Map<String, Object>)data.get(key));
+ }
+ }
+ }
+
+ private static Pattern SECTION = Pattern.compile("\\s*\\[([^\\]]+)\\]\\s*"); //$NON-NLS-1$
+ private static Pattern PROPERTY = Pattern.compile("\\s*(.+\\s*=\\s*.+)"); //$NON-NLS-1$
+
+ /**
+ * Read the data.
+ *
+ * @param reader The reader. Must not be <code>null</code>.
+ * @param data The data. Must not be <code>null</code>.
+ */
+ protected void read(BufferedReader reader, Map<String, Object> data) throws IOException {
+ Assert.isNotNull(reader);
+ Assert.isNotNull(data);
+
+ // The sections by name for easier access.
+ // The "core" section is the incoming data object
+ Map<String, Map<String, Object>> sections = new HashMap<String, Map<String, Object>>();
+ sections.put("core", data); //$NON-NLS-1$
+
+ String currentSection = "core"; //$NON-NLS-1$
+ String line = reader.readLine();
+ while (line != null) {
+ Matcher matcher = SECTION.matcher(line);
+ if (matcher.matches()) {
+ currentSection = matcher.group(1).toLowerCase();
+ if (sections.get(currentSection) == null) {
+ sections.put(currentSection, new HashMap<String, Object>());
+ }
+ } else {
+ matcher = PROPERTY.matcher(line);
+ if (matcher.matches()) {
+ String property = matcher.group(1);
+ String[] pieces = property.split("=", 2); //$NON-NLS-1$
+ Map<String, Object> section = sections.get(currentSection);
+ section.put(pieces[0].trim(), pieces[1].trim());
+ }
+ }
+
+ line = reader.readLine();
+ }
+
+ // Recreate the sections hierarchy
+ for (String sectionName : sections.keySet()) {
+ if ("core".equals(sectionName)) continue; //$NON-NLS-1$
+ Map<String, Object> section = sections.get(sectionName);
+ if (sectionName.contains(".")) { //$NON-NLS-1$
+ // Split the section name and recreate the missing hierarchy
+ String[] pieces = sectionName.split("\\."); //$NON-NLS-1$
+ Map<String, Object> parentSection = data;
+ for (String subSectionName : pieces) {
+ if ("core".equals(subSectionName)) continue; //$NON-NLS-1$
+
+ if (sectionName.endsWith(subSectionName)) {
+ parentSection.put(subSectionName, section);
+ } else {
+ Map<String, Object> subSection = (Map<String, Object>)parentSection.get(subSectionName);
+ if (subSection == null) {
+ subSection = new HashMap<String, Object>();
+ parentSection.put(subSectionName, subSection);
+ }
+ parentSection = subSection;
+ }
+ }
+ } else {
+ // Place it into the root object, but check if it may exist
+ Map<String, Object> oldSection = (Map<String, Object>)data.get(sectionName);
+ if (oldSection != null) oldSection.putAll(section);
+ else data.put(sectionName, section);
+ }
+ }
+ }
+}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/services/PersistenceService.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/services/PersistenceService.java
new file mode 100644
index 000000000..aeb65a4c1
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/services/PersistenceService.java
@@ -0,0 +1,127 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.services;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.Map;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.Platform;
+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;
+import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceService;
+import org.eclipse.tcf.te.runtime.services.AbstractService;
+
+/**
+ * Persistence service implementation.
+ */
+public class PersistenceService extends AbstractService implements IPersistenceService {
+
+ /* (non-Javadoc)
+ * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceService#write(java.lang.Object)
+ */
+ @Override
+ public void write(Object data) throws IOException {
+ Assert.isNotNull(data);
+
+ // Determine the persistable element for the given data object
+ IPersistable persistable = data instanceof IPersistable ? (IPersistable)data : null;
+ // If the element isn't a persistable by itself, try to adapt the element
+ if (persistable == null) persistable = data instanceof IAdaptable ? (IPersistable) ((IAdaptable)data).getAdapter(IPersistable.class) : null;
+ if (persistable == null) persistable = (IPersistable) Platform.getAdapterManager().getAdapter(data, IPersistable.class);
+
+ // If the persistable could be still not determined, throw an IOException
+ if (persistable == null) throw new IOException("'data' must be adaptable to IPersistable."); //$NON-NLS-1$
+
+ // Determine the persistence delegate
+ IPersistenceDelegate delegate = persistable.getStorageID() != null ? PersistenceDelegateManager.getInstance().getDelegate(persistable.getStorageID(), false) : null;
+ // If the persistence delegate could not be determined, throw an IOException
+ if (delegate == null) throw new IOException("The persistence delegate for ID '" + persistable.getStorageID() + "' cannot be determined."); //$NON-NLS-1$ //$NON-NLS-2$
+
+ // Determine the URI
+ URI uri = persistable.getURI(data);
+ if (uri == null) throw new IOException("The URI cannot be determined."); //$NON-NLS-1$
+
+ // Get the external representation of the data object from the
+ // associated persistable.
+ Map<String, Object> externalData = persistable.exportFrom(data);
+ // If the external representation cannot be retrieved, throw an IOException
+ if (externalData == null) throw new IOException("Failed to retrieve external data representation."); //$NON-NLS-1$
+
+ // Pass on to the delegate for writing
+ delegate.write(uri, externalData);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceService#read(java.lang.Object)
+ */
+ @Override
+ public void read(Object data) throws IOException {
+ Assert.isNotNull(data);
+
+ // Determine the persistable element for the given data object
+ IPersistable persistable = data instanceof IPersistable ? (IPersistable)data : null;
+ // If the element isn't a persistable by itself, try to adapt the element
+ if (persistable == null) persistable = data instanceof IAdaptable ? (IPersistable) ((IAdaptable)data).getAdapter(IPersistable.class) : null;
+ if (persistable == null) persistable = (IPersistable) Platform.getAdapterManager().getAdapter(data, IPersistable.class);
+
+ // If the persistable could be still not determined, throw an IOException
+ if (persistable == null) throw new IOException("'data' must be adaptable to IPersistable."); //$NON-NLS-1$
+
+ // Determine the persistence delegate
+ IPersistenceDelegate delegate = persistable.getStorageID() != null ? PersistenceDelegateManager.getInstance().getDelegate(persistable.getStorageID(), false) : null;
+ // If the persistence delegate could not be determined, throw an IOException
+ if (delegate == null) throw new IOException("The persistence delegate for ID '" + persistable.getStorageID() + "' cannot be determined."); //$NON-NLS-1$ //$NON-NLS-2$
+
+ // Determine the URI
+ URI uri = persistable.getURI(data);
+ if (uri == null) throw new IOException("The URI cannot be determined."); //$NON-NLS-1$
+
+ // Pass on to the delegate for reading
+ Map<String, Object> externalData = delegate.read(uri);
+ if (externalData == null) throw new IOException("Failed to read external data representation from URI '" + uri.toString() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
+
+ // Import the external representation into the data object via
+ // the associated persistable
+ persistable.importTo(data, externalData);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceService#delete(java.lang.Object)
+ */
+ @Override
+ public boolean delete(Object data) throws IOException {
+ Assert.isNotNull(data);
+
+ // Determine the persistable element for the given data object
+ IPersistable persistable = data instanceof IPersistable ? (IPersistable)data : null;
+ // If the element isn't a persistable by itself, try to adapt the element
+ if (persistable == null) persistable = data instanceof IAdaptable ? (IPersistable) ((IAdaptable)data).getAdapter(IPersistable.class) : null;
+ if (persistable == null) persistable = (IPersistable) Platform.getAdapterManager().getAdapter(data, IPersistable.class);
+
+ // If the persistable could be still not determined, throw an IOException
+ if (persistable == null) throw new IOException("'data' must be adaptable to IPersistable."); //$NON-NLS-1$
+
+ // Determine the persistence delegate
+ IPersistenceDelegate delegate = persistable.getStorageID() != null ? PersistenceDelegateManager.getInstance().getDelegate(persistable.getStorageID(), false) : null;
+ // If the persistence delegate could not be determined, throw an IOException
+ if (delegate == null) throw new IOException("The persistence delegate for ID '" + persistable.getStorageID() + "' cannot be determined."); //$NON-NLS-1$ //$NON-NLS-2$
+
+ // Determine the URI
+ URI uri = persistable.getURI(data);
+ if (uri == null) throw new IOException("The URI cannot be determined."); //$NON-NLS-1$
+
+ // Pass on to the delegate for deleting
+ return delegate.delete(uri);
+ }
+}

Back to the top