Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Valenta2004-03-03 13:21:34 -0500
committerMichael Valenta2004-03-03 13:21:34 -0500
commitbb660189c1ef356aa97a8960e78ae6714f4101c5 (patch)
treeb5432d5456812607e4e02382780a12f623d1abf6 /examples
parent6011408d6ed3c619bf48cc50cfd21c10c4c3ea5b (diff)
downloadeclipse.platform.team-bb660189c1ef356aa97a8960e78ae6714f4101c5.tar.gz
eclipse.platform.team-bb660189c1ef356aa97a8960e78ae6714f4101c5.tar.xz
eclipse.platform.team-bb660189c1ef356aa97a8960e78ae6714f4101c5.zip
Refactoring of subscribers and resource variants
Diffstat (limited to 'examples')
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemProvider.java45
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemSubscriber.java70
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemRemoteTree.java62
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemResourceVariant.java134
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemSubscriber.java115
5 files changed, 356 insertions, 70 deletions
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemProvider.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemProvider.java
index 60f64c48b..597776599 100644
--- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemProvider.java
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemProvider.java
@@ -13,12 +13,16 @@ package org.eclipse.team.examples.filesystem;
import java.io.File;
import org.eclipse.core.resources.IFileModificationValidator;
+import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
+import org.eclipse.team.core.variants.IResourceVariant;
+import org.eclipse.team.examples.filesystem.subscriber.FileSystemResourceVariant;
+import org.eclipse.team.examples.filesystem.subscriber.FileSystemSubscriber;
/**
* This example illustrates how to create a concrete implementation of a <code>RepositoryProvider</code>
@@ -71,6 +75,7 @@ public class FileSystemProvider extends RepositoryProvider {
* @see org.eclipse.team.core.RepositoryProvider#configureProject()
*/
public void configureProject() throws CoreException {
+ FileSystemSubscriber.getInstance().handleRootChanged(getProject(), true /* added */);
}
/**
@@ -82,6 +87,7 @@ public class FileSystemProvider extends RepositoryProvider {
public void deconfigure() throws CoreException {
// Clear the persistant property containing the location
getProject().setPersistentProperty(FILESYSTEM_REPO_LOC, null);
+ FileSystemSubscriber.getInstance().handleRootChanged(getProject(), false /* removed */);
}
/**
@@ -162,4 +168,43 @@ public class FileSystemProvider extends RepositoryProvider {
return new FileModificationValidator(this);
}
+ /**
+ * Return the resource variant for the local resource using the bytes to
+ * identify the variant.
+ * @param resource the resource
+ * @param bytes the bytes that identify the resource variant
+ * @return the resource variant handle
+ */
+ public IResourceVariant getResourceVariant(IResource resource, byte[] bytes) {
+ File file = getFile(resource);
+ if (file == null) return null;
+ return new FileSystemResourceVariant(file, bytes);
+ }
+
+ /**
+ * Return the resource variant for the local resource.
+ * @param resource the resource
+ * @return the resource variant
+ */
+ public IResourceVariant getResourceVariant(IResource resource) {
+ File file = getFile(resource);
+ if (file == null) return null;
+ return new FileSystemResourceVariant(file);
+ }
+
+ /**
+ * Return the <code>java.io.File</code> that the given resource maps to.
+ * Return <code>null</code> if the resource is not a child of this provider's
+ * project.
+ * @param resource the resource
+ * @return the file that the resource maps to.
+ */
+ public java.io.File getFile(IResource resource) {
+ if (resource.getProject().equals(getProject())) {
+ IPath rootdir = getRoot();
+ return new File(rootdir.append(resource.getProjectRelativePath()).toOSString());
+ }
+ return null;
+ }
+
}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemSubscriber.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemSubscriber.java
deleted file mode 100644
index 81de667bb..000000000
--- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/FileSystemSubscriber.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2003 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Common Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/cpl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.team.examples.filesystem;
-
-import org.eclipse.core.resources.IResource;
-import org.eclipse.team.core.TeamException;
-import org.eclipse.team.core.synchronize.IResourceVariantComparator;
-import org.eclipse.team.internal.core.subscribers.caches.IResourceVariantTree;
-import org.eclipse.team.internal.core.subscribers.caches.ResourceVariantTreeSubscriber;
-
-public class FileSystemSubscriber extends ResourceVariantTreeSubscriber {
-
- /* (non-Javadoc)
- * @see org.eclipse.team.internal.core.subscribers.caches.ResourceVariantTreeSubscriber#getBaseTree()
- */
- protected IResourceVariantTree getBaseTree() {
- // TODO Auto-generated method stub
- return null;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.team.internal.core.subscribers.caches.ResourceVariantTreeSubscriber#getRemoteTree()
- */
- protected IResourceVariantTree getRemoteTree() {
- // TODO Auto-generated method stub
- return null;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.team.core.subscribers.Subscriber#getName()
- */
- public String getName() {
- // TODO Auto-generated method stub
- return null;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.team.core.subscribers.Subscriber#isSupervised(org.eclipse.core.resources.IResource)
- */
- public boolean isSupervised(IResource resource) throws TeamException {
- // TODO Auto-generated method stub
- return false;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.team.core.subscribers.Subscriber#roots()
- */
- public IResource[] roots() {
- // TODO Auto-generated method stub
- return null;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.team.core.subscribers.Subscriber#getResourceComparator()
- */
- public IResourceVariantComparator getResourceComparator() {
- // TODO Auto-generated method stub
- return null;
- }
-
-
-}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemRemoteTree.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemRemoteTree.java
new file mode 100644
index 000000000..65f87d805
--- /dev/null
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemRemoteTree.java
@@ -0,0 +1,62 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.team.examples.filesystem.subscriber;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.team.core.RepositoryProvider;
+import org.eclipse.team.core.TeamException;
+import org.eclipse.team.core.variants.IResourceVariant;
+import org.eclipse.team.core.variants.ThreeWayRemoteTree;
+import org.eclipse.team.examples.filesystem.FileSystemPlugin;
+import org.eclipse.team.examples.filesystem.FileSystemProvider;
+
+/**
+ * The file sytem three-way remote resource varant tree taht provides
+ * the ability to traverse the file system for the creation of resource variants.
+ */
+public class FileSystemRemoteTree extends ThreeWayRemoteTree {
+
+ /**
+ * Create the file syetm remote resource variant tree
+ * @param subscriber the file system subscriber
+ */
+ public FileSystemRemoteTree(FileSystemSubscriber subscriber) {
+ super(subscriber);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.AbstractResourceVariantTree#fetchMembers(org.eclipse.team.core.variants.IResourceVariant, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ protected IResourceVariant[] fetchMembers(IResourceVariant variant, IProgressMonitor progress) throws TeamException {
+ if (variant.isContainer()) {
+ java.io.File[] members = ((FileSystemResourceVariant)variant).members();
+ FileSystemResourceVariant[] result = new FileSystemResourceVariant[members.length];
+ for (int i = 0; i < members.length; i++) {
+ result[i] = new FileSystemResourceVariant(members[i]);
+ }
+ return result;
+ } else {
+ return new IResourceVariant[0];
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.AbstractResourceVariantTree#fetchVariant(org.eclipse.core.resources.IResource, int, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ protected IResourceVariant fetchVariant(IResource resource, int depth, IProgressMonitor monitor) throws TeamException {
+ RepositoryProvider provider = RepositoryProvider.getProvider(resource.getProject(), FileSystemPlugin.PROVIDER_ID);
+ if (provider != null) {
+ return ((FileSystemProvider)provider).getResourceVariant(resource);
+ }
+ return null;
+ }
+}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemResourceVariant.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemResourceVariant.java
new file mode 100644
index 000000000..630b73ee5
--- /dev/null
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemResourceVariant.java
@@ -0,0 +1,134 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.team.examples.filesystem.subscriber;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Date;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.team.core.TeamException;
+import org.eclipse.team.core.variants.CachedResourceVariant;
+import org.eclipse.team.examples.filesystem.FileSystemPlugin;
+
+/**
+ * A file system resource variant. Although not strictly necessary, this
+ * class extends <code>CachedResourceVariant</code> which will
+ * cache the contents of the resource variant.
+ */
+public class FileSystemResourceVariant extends CachedResourceVariant {
+
+ private java.io.File ioFile;
+ private byte[] bytes;
+
+ /**
+ * Create a resource variant for the given file. The bytes will
+ * be calculated when they are accessed.
+ * @param file the file
+ */
+ public FileSystemResourceVariant(java.io.File file) {
+ this.ioFile = file;
+ }
+
+ /**
+ * Create a resource variant for the given file and sync bytes.
+ * @param file the file
+ * @param bytes the timestamp bytes
+ */
+ public FileSystemResourceVariant(java.io.File file, byte[] bytes) {
+ this.ioFile = file;
+ this.bytes = bytes;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.CachedResourceVariant#fetchContents(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ protected void fetchContents(IProgressMonitor monitor) throws TeamException {
+ try {
+ setContents(new BufferedInputStream(new FileInputStream(ioFile)), monitor);
+ } catch (FileNotFoundException e) {
+ throw new TeamException("Failed to fetch contents for " + getFilePath(), e);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.CachedResourceVariant#getCachePath()
+ */
+ protected String getCachePath() {
+ // append the timestamp to the file path to give each variant a unique path
+ return getFilePath() + " " + ioFile.lastModified();
+ }
+
+ private String getFilePath() {
+ try {
+ return ioFile.getCanonicalPath();
+ } catch (IOException e) {
+ // Failed for some reason. Try the absolute path.
+ FileSystemPlugin.log(new Status(IStatus.ERROR, FileSystemPlugin.ID, 0,
+ "Failed to obtain canonical path for " + ioFile.getAbsolutePath(), e));
+ return ioFile.getAbsolutePath();
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.CachedResourceVariant#getCacheId()
+ */
+ protected String getCacheId() {
+ return FileSystemPlugin.ID;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.IResourceVariant#getName()
+ */
+ public String getName() {
+ return ioFile.getName();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.IResourceVariant#isContainer()
+ */
+ public boolean isContainer() {
+ return ioFile.isDirectory();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.IResourceVariant#getContentIdentifier()
+ */
+ public String getContentIdentifier() {
+ // Use the modification timestamp as the content identifier
+ return new Date(ioFile.lastModified()).toString();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.IResourceVariant#asBytes()
+ */
+ public byte[] asBytes() {
+ if (bytes == null) {
+ // For simplicity, convert the timestamp to it's string representation.
+ // A more optimal storage format would be the 8 bytes that make up the long.
+ bytes = Long.toString(ioFile.lastModified()).getBytes();
+ }
+ return bytes;
+ }
+
+ /**
+ * Return the files contained by the file of this resource variant.
+ * @return the files contained by the file of this resource variant.
+ */
+ public File[] members() {
+ return ioFile.listFiles();
+ }
+}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemSubscriber.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemSubscriber.java
new file mode 100644
index 000000000..fc0880b46
--- /dev/null
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/filesystem/subscriber/FileSystemSubscriber.java
@@ -0,0 +1,115 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.team.examples.filesystem.subscriber;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.QualifiedName;
+import org.eclipse.team.core.RepositoryProvider;
+import org.eclipse.team.core.TeamException;
+import org.eclipse.team.core.variants.IResourceVariant;
+import org.eclipse.team.core.variants.ThreeWayRemoteTree;
+import org.eclipse.team.core.variants.ThreeWaySubscriber;
+import org.eclipse.team.core.variants.ThreeWaySynchronizer;
+import org.eclipse.team.examples.filesystem.FileSystemPlugin;
+import org.eclipse.team.examples.filesystem.FileSystemProvider;
+
+/**
+ * This is an example file system subscriber that overrides
+ * ThreeWaySubscriber. It uses a repository
+ * provider (<code>FileSystemProvider</code>) to determine and
+ * manage the roots and to create resource variants. It also makes
+ * use of a file system specific remote tree (<code>FileSystemRemoteTree</code>)
+ * for provided the remote tree access and refresh.
+ *
+ * @see ThreeWaySubscriber
+ * @see ThreeWaySynchronizer
+ * @see FileSystemProvider
+ * @see FileSystemRemoteTree
+ */
+public class FileSystemSubscriber extends ThreeWaySubscriber {
+
+ private static FileSystemSubscriber instance;
+
+ /**
+ * Return the file system subscriber singleton.
+ * @return the file system subscriber singleton.
+ */
+ public static synchronized FileSystemSubscriber getInstance() {
+ if (instance == null) {
+ instance = new FileSystemSubscriber();
+ }
+ return instance;
+ }
+
+ /**
+ * Create the file system subscriber.
+ */
+ private FileSystemSubscriber() {
+ super(new ThreeWaySynchronizer(new QualifiedName(FileSystemPlugin.ID, "workpsace-sync"))); //$NON-NLS-1$
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.ThreeWaySubscriber#getResourceVariant(org.eclipse.core.resources.IResource, byte[])
+ */
+ public IResourceVariant getResourceVariant(IResource resource, byte[] bytes) throws TeamException {
+ RepositoryProvider provider = RepositoryProvider.getProvider(resource.getProject(), FileSystemPlugin.PROVIDER_ID);
+ if (provider != null) {
+ return ((FileSystemProvider)provider).getResourceVariant(resource, bytes);
+ }
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.ThreeWaySubscriber#createRemoteTree()
+ */
+ protected ThreeWayRemoteTree createRemoteTree() {
+ return new FileSystemRemoteTree(this);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.subscribers.Subscriber#getName()
+ */
+ public String getName() {
+ return "File System Example";
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.subscribers.Subscriber#roots()
+ */
+ public IResource[] roots() {
+ List result = new ArrayList();
+ IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
+ for (int i = 0; i < projects.length; i++) {
+ IProject project = projects[i];
+ if(project.isAccessible()) {
+ RepositoryProvider provider = RepositoryProvider.getProvider(project, FileSystemPlugin.PROVIDER_ID);
+ if(provider != null) {
+ result.add(project);
+ }
+ }
+ }
+ return (IProject[]) result.toArray(new IProject[result.size()]);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.variants.ThreeWaySubscriber#handleRootChanged(org.eclipse.core.resources.IResource, boolean)
+ */
+ public void handleRootChanged(IResource resource, boolean added) {
+ // Override to allow FileSystemProvider to signal the addition and removal of roots
+ super.handleRootChanged(resource, added);
+ }
+
+}

Back to the top