Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Schwarz2012-07-09 13:38:37 +0000
committerUwe Stieber2012-07-09 13:38:37 +0000
commit8b409058a81a99360980bffcd2ba00aaa87240f6 (patch)
treeeb579dba6f8f2e675d38778094cd24ebf4987987 /target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence
parentb597943e493c505151baec6c896d9e41c2ee6bc0 (diff)
downloadorg.eclipse.tcf-8b409058a81a99360980bffcd2ba00aaa87240f6.tar.gz
org.eclipse.tcf-8b409058a81a99360980bffcd2ba00aaa87240f6.tar.xz
org.eclipse.tcf-8b409058a81a99360980bffcd2ba00aaa87240f6.zip
Target Explorer: ADD History Management
Diffstat (limited to 'target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence')
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/META-INF/MANIFEST.MF1
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/plugin.xml25
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/GsonMapPersistenceDelegate.java (renamed from target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractGsonMapPersistenceDelegate.java)24
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/PersistenceManager.java4
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/activator/CoreBundleActivator.java17
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/history/HistoryManager.java187
6 files changed, 248 insertions, 10 deletions
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/META-INF/MANIFEST.MF b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/META-INF/MANIFEST.MF
index 1e29e2f0c..a88a8d288 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/META-INF/MANIFEST.MF
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/META-INF/MANIFEST.MF
@@ -15,6 +15,7 @@ Bundle-ActivationPolicy: lazy
Bundle-Localization: plugin
Export-Package: org.eclipse.tcf.te.runtime.persistence,
org.eclipse.tcf.te.runtime.persistence.activator;x-internal:=true,
+ org.eclipse.tcf.te.runtime.persistence.history,
org.eclipse.tcf.te.runtime.persistence.interfaces,
org.eclipse.tcf.te.runtime.persistence.internal;x-internal:=true,
org.eclipse.tcf.te.runtime.persistence.services
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/plugin.xml b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/plugin.xml
index 6e9d117ab..7b705bcde 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/plugin.xml
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/plugin.xml
@@ -15,4 +15,29 @@
</service>
</extension>
+<!-- Persistence delegate contributions -->
+ <extension point="org.eclipse.tcf.te.runtime.persistence.delegates">
+ <delegate
+ id="org.eclipse.tcf.te.runtime.persistence.gson.map"
+ class="org.eclipse.tcf.te.runtime.persistence.GsonMapPersistenceDelegate">
+ </delegate>
+ </extension>
+
+<!-- Persistence delegate bindings contributions -->
+ <extension
+ point="org.eclipse.tcf.te.runtime.persistence.bindings">
+ <binding
+ delegateId="org.eclipse.tcf.te.runtime.persistence.gson.map"
+ id="org.eclipse.tcf.te.runtime.persistence.binding.gson.map">
+ <enablement>
+ <with variable="container">
+ <instanceof value="java.net.URI"/>
+ </with>
+ <with variable="context">
+ <instanceof value="java.util.Map"/>
+ </with>
+ </enablement>
+ </binding>
+ </extension>
+
</plugin>
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractGsonMapPersistenceDelegate.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/GsonMapPersistenceDelegate.java
index 85665f9cb..09e72bdf1 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/AbstractGsonMapPersistenceDelegate.java
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/GsonMapPersistenceDelegate.java
@@ -35,28 +35,36 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
- * AbstractGsonMapPersistenceDelegate
+ * GsonMapPersistenceDelegate
*/
-public abstract class AbstractGsonMapPersistenceDelegate extends ExecutableExtension implements IPersistenceDelegate {
+public class GsonMapPersistenceDelegate extends ExecutableExtension implements IPersistenceDelegate {
private final String defaultFileExtension;
/**
* Constructor.
*/
- public AbstractGsonMapPersistenceDelegate() {
+ public GsonMapPersistenceDelegate() {
this("json"); //$NON-NLS-1$
}
/**
* Constructor.
*/
- public AbstractGsonMapPersistenceDelegate(String defaultFileExtension) {
+ public GsonMapPersistenceDelegate(String defaultFileExtension) {
super();
Assert.isNotNull(defaultFileExtension);
this.defaultFileExtension = defaultFileExtension;
}
+ /* (non-Javadoc)
+ * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#getPersistedClass(java.lang.Object)
+ */
+ @Override
+ public Class<?> getPersistedClass(Object context) {
+ return Map.class;
+ }
+
/**
* Return the default file extension if container is an URI.
*/
@@ -100,7 +108,9 @@ public abstract class AbstractGsonMapPersistenceDelegate extends ExecutableExten
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(toMap(context), Map.class, writer);
} finally {
- if (writer != null) writer.close();
+ if (writer != null) {
+ writer.close();
+ }
}
}
else if (container instanceof String || String.class.equals(container)) {
@@ -142,7 +152,9 @@ public abstract class AbstractGsonMapPersistenceDelegate extends ExecutableExten
reader = new InputStreamReader(new FileInputStream(file), "UTF-8"); //$NON-NLS-1$
data = gson.fromJson(reader, Map.class);
} finally {
- if (reader != null) reader.close();
+ if (reader != null) {
+ reader.close();
+ }
}
}
else if (container instanceof String) {
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 2534966a3..4b60113e7 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
@@ -135,8 +135,8 @@ public class PersistenceManager extends AbstractExtensionPointManager<IPersisten
}
if (delegates.size() > 1) {
- IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(), "Found multiple persistence delgates for " + context + //$NON-NLS-1$
- (container != null ? " to store in " + container : "")); //$NON-NLS-1$ //$NON-NLS-2$
+ IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(), "Found multiple persistence delgates for " + context.getClass().getName() + //$NON-NLS-1$
+ " (" + context + ")" + (container != null ? " to store in " + container : "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(status);
}
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
index f1fcf20b5..49ceaa24c 100644
--- 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
@@ -9,15 +9,17 @@
*******************************************************************************/
package org.eclipse.tcf.te.runtime.persistence.activator;
-import org.osgi.framework.BundleActivator;
+import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.BundleContext;
/**
* The activator class controls the plug-in life cycle
*/
-public class CoreBundleActivator implements BundleActivator {
+public class CoreBundleActivator extends Plugin {
// The bundle context
private static BundleContext context;
+ // The shared instance of this plug-in.
+ private static CoreBundleActivator plugin;
/**
* Returns the bundle context
@@ -29,6 +31,15 @@ public class CoreBundleActivator implements BundleActivator {
}
/**
+ * Returns the shared instance
+ *
+ * @return the shared instance
+ */
+ public static CoreBundleActivator getDefault() {
+ return plugin;
+ }
+
+ /**
* Convenience method which returns the unique identifier of this plugin.
*/
public static String getUniqueIdentifier() {
@@ -45,6 +56,7 @@ public class CoreBundleActivator implements BundleActivator {
@Override
public void start(BundleContext bundleContext) throws Exception {
CoreBundleActivator.context = bundleContext;
+ plugin = this;
}
/*
@@ -54,6 +66,7 @@ public class CoreBundleActivator implements BundleActivator {
@Override
public void stop(BundleContext bundleContext) throws Exception {
CoreBundleActivator.context = null;
+ plugin = null;
}
}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/history/HistoryManager.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/history/HistoryManager.java
new file mode 100644
index 000000000..334d44ff1
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/history/HistoryManager.java
@@ -0,0 +1,187 @@
+/*******************************************************************************
+ * 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.history;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.tcf.te.runtime.events.ChangeEvent;
+import org.eclipse.tcf.te.runtime.events.EventManager;
+import org.eclipse.tcf.te.runtime.persistence.activator.CoreBundleActivator;
+import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
+import org.eclipse.tcf.te.runtime.services.ServiceManager;
+
+/**
+ * History manager implementation.
+ */
+public class HistoryManager {
+ // the maximum length of the history per id
+ private final static int HISTORY_LENGTH = 5;
+
+ // The map maintaining the history
+ private Map<String, List<String>> history = new HashMap<String, List<String>>();
+
+ /*
+ * Thread save singleton instance creation.
+ */
+ private static class LazyInstance {
+ public static HistoryManager instance = new HistoryManager();
+ }
+
+ /**
+ * Returns the singleton instance of the history point manager.
+ */
+ public static HistoryManager getInstance() {
+ return LazyInstance.instance;
+ }
+
+ /**
+ * Constructor.
+ */
+ HistoryManager() {
+ super();
+ initialize();
+ }
+
+ /**
+ * Initialize the history manager.
+ */
+ private void initialize() {
+ history.clear();
+ try {
+ // Get the persistence service
+ IURIPersistenceService uRIPersistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
+ if (uRIPersistenceService == null) {
+ throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
+ }
+ // Save the history to the persistence storage
+ history = (Map<String,List<String>>)uRIPersistenceService.read(history, getURI());
+ } catch (IOException e) {
+ }
+ }
+
+ // Get the URI for history persistence
+ private URI getURI() {
+ IPath pluginPath = CoreBundleActivator.getDefault().getStateLocation();
+ pluginPath = pluginPath.append(".history"); //$NON-NLS-1$
+
+ return pluginPath.toFile().toURI();
+ }
+
+ /**
+ * Write the history to disk.
+ */
+ public void flush() {
+ try {
+ // Get the persistence service
+ IURIPersistenceService uRIPersistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
+ if (uRIPersistenceService == null) {
+ throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
+ }
+ // Save the history to the persistence storage
+ uRIPersistenceService.write(history, getURI());
+ } catch (IOException e) {
+ }
+ }
+
+ /**
+ * Get the history for a given history id.
+ * @param historyId The history id.
+ * @return The list of ids within the history ids list or an empty list.
+ */
+ public String[] getHistory(String historyId) {
+ Assert.isNotNull(historyId);
+
+ List<String> ids = history.get(historyId);
+ if (ids == null) {
+ ids = new ArrayList<String>();
+ }
+
+ return ids.toArray(new String[ids.size()]);
+ }
+
+ /**
+ * Get the fist entry of a history ids list.
+ * @param historyId The history id.
+ * @return The first entry of the history ids list or null if no history is available for that id.
+ */
+ public String getFirst(String historyId) {
+ String[] history = getHistory(historyId);
+ return history.length > 0 ? history[0] : null;
+ }
+
+ /**
+ * Add a new history entry to the top of the history ids list.
+ * If the list size exceeds the HISTORY_LENGTH, the last element of the list will be removed.
+ * @param historyId The history id.
+ * @param id The id to be added to the top of history ids list.
+ * @return <code>true</code> if the id
+ */
+ public boolean add(String historyId, String id) {
+ Assert.isNotNull(historyId);
+ Assert.isNotNull(id);
+
+ List<String> ids = history.get(historyId);
+ if (ids == null) {
+ ids = new ArrayList<String>();
+ history.put(historyId, ids);
+ }
+ if (ids.contains(id)) {
+ ids.remove(id);
+ }
+
+ ids.add(0, id);
+
+ while (ids.size() > HISTORY_LENGTH) {
+ ids.remove(HISTORY_LENGTH);
+ }
+
+ flush();
+
+ EventManager.getInstance().fireEvent(new ChangeEvent(this, ChangeEvent.ID_ADDED, historyId, historyId));
+
+ return true;
+ }
+
+ /**
+ * Remove a id from the history ids list.
+ * @param historyId The history id.
+ * @param id The id to be removed from the history ids list.
+ * @return <code>true</code> if the id was removed from the history ids list.
+ */
+ public boolean remove(String historyId, String id) {
+ Assert.isNotNull(historyId);
+ Assert.isNotNull(id);
+
+ boolean removed = false;
+
+ List<String> ids = history.get(historyId);
+ if (ids != null) {
+ removed |= ids.remove(id);
+ if (ids.isEmpty()) {
+ history.remove(historyId);
+ }
+ }
+
+ if (removed) {
+ flush();
+ EventManager.getInstance().fireEvent(new ChangeEvent(this, ChangeEvent.ID_REMOVED, historyId, historyId));
+ }
+
+ return removed;
+ }
+
+}

Back to the top