Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcbateman2010-04-27 18:09:16 -0400
committercbateman2010-04-27 18:09:16 -0400
commit7d403bd41db3a7be3893e444ef9a977562028c92 (patch)
treef8ce5e633916109f8ce8f9f4c754af70c34707f6
parent81ee32e5e0bc83a7c1b3d2feb394d9996371e015 (diff)
downloadwebtools.jsf-7d403bd41db3a7be3893e444ef9a977562028c92.tar.gz
webtools.jsf-7d403bd41db3a7be3893e444ef9a977562028c92.tar.xz
webtools.jsf-7d403bd41db3a7be3893e444ef9a977562028c92.zip
Refactor out the resource tracker concept.
-rw-r--r--jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/managedobject/AbstractManagedObject.java5
-rw-r--r--jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/LifecycleListener.java55
-rw-r--r--jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceManager.java63
-rw-r--r--jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceSingletonObjectManager.java17
-rw-r--r--jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceTracker.java122
5 files changed, 230 insertions, 32 deletions
diff --git a/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/managedobject/AbstractManagedObject.java b/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/managedobject/AbstractManagedObject.java
index a8ac64359..9138f0e35 100644
--- a/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/managedobject/AbstractManagedObject.java
+++ b/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/managedobject/AbstractManagedObject.java
@@ -35,7 +35,10 @@ public abstract class AbstractManagedObject implements IManagedObject
* org.eclipse.jst.jsf.common.internal.managedobject.IManagedObject#dispose
* ()
*/
- public abstract void dispose();
+ public void dispose()
+ {
+ _isDisposed.set(true);
+ }
public boolean isDisposed()
{
diff --git a/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/LifecycleListener.java b/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/LifecycleListener.java
index 370969458..1667b239d 100644
--- a/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/LifecycleListener.java
+++ b/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/LifecycleListener.java
@@ -253,7 +253,7 @@ public class LifecycleListener extends ImmutableLifecycleListener implements
// must use iterator to ensure copy on write behaviour
for (final IResource res : _resources)
{
- if (res != null && (proj == res || proj == res.getProject()))
+ if (proj == res || proj == res.getProject())
{
fireLifecycleEvent(new ResourceLifecycleEvent(res,
EventType.RESOURCE_INACCESSIBLE,
@@ -273,7 +273,7 @@ public class LifecycleListener extends ImmutableLifecycleListener implements
// if the resource being tracked is the resource being
// deleted,
// then fire a resource delete event
- if (res != null && proj == res)
+ if (proj == res)
{
fireLifecycleEvent(new ResourceLifecycleEvent(res,
EventType.RESOURCE_INACCESSIBLE,
@@ -282,7 +282,7 @@ public class LifecycleListener extends ImmutableLifecycleListener implements
// if the resource being tracked is a resource in the
// project being
// deleted, then fire a project deleted event
- else if (res != null && proj == res.getProject())
+ else if (proj == res.getProject())
{
fireLifecycleEvent(new ResourceLifecycleEvent(res,
EventType.RESOURCE_INACCESSIBLE,
@@ -296,30 +296,28 @@ public class LifecycleListener extends ImmutableLifecycleListener implements
{
for (final IResource res : _resources)
{
- if (res != null) {
- IResourceDelta delta = event.getDelta();
-
- // long seqId2 = _seqId++;
- // if (ENABLE_TEST_TRACKING && _testTracker != null)
- // {
- // _testTracker.fireEvent(Event.START_TRACKING, seqId2,
- // "testFindMember");
- // }
- // only care about post change events to resources
- // that we are tracking
- delta = delta.findMember(res.getFullPath());
-
- if (delta != null)
- {
- visit(delta);
- }
-
- // if (ENABLE_TEST_TRACKING && _testTracker != null)
- // {
- // _testTracker.fireEvent(Event.STOP_TRACKING, seqId2,
- // "testFindMember");
- // }
- }
+ IResourceDelta delta = event.getDelta();
+
+ // long seqId2 = _seqId++;
+ // if (ENABLE_TEST_TRACKING && _testTracker != null)
+ // {
+ // _testTracker.fireEvent(Event.START_TRACKING, seqId2,
+ // "testFindMember");
+ // }
+ // only care about post change events to resources
+ // that we are tracking
+ delta = delta.findMember(res.getFullPath());
+
+ if (delta != null)
+ {
+ visit(delta);
+ }
+
+ // if (ENABLE_TEST_TRACKING && _testTracker != null)
+ // {
+ // _testTracker.fireEvent(Event.STOP_TRACKING, seqId2,
+ // "testFindMember");
+ // }
}
}
break;
@@ -379,7 +377,8 @@ public class LifecycleListener extends ImmutableLifecycleListener implements
{
for (final IResourceDelta childDelta : delta.getAffectedChildren())
{
- if (childDelta.getResource().getType() == IResource.FILE
+ if ((childDelta.getResource().getType() == IResource.FILE
+ || childDelta.getResource().getType() == IResource.FOLDER)
&& childDelta.getKind() == IResourceDelta.ADDED)
{
fireLifecycleEvent(new ResourceLifecycleEvent(childDelta.getResource(),
diff --git a/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceManager.java b/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceManager.java
new file mode 100644
index 000000000..2ecedd469
--- /dev/null
+++ b/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceManager.java
@@ -0,0 +1,63 @@
+package org.eclipse.jst.jsf.common.internal.resource;
+
+import java.util.List;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspace;
+
+/**
+ * A resource singleton object manager that manages singletons that are aware
+ * of the the resources they are singletons for.
+ *
+ * @author cbateman
+ * @param <RESTYPE>
+ *
+ */
+public abstract class ResourceManager<RESTYPE extends IResource> extends
+ ResourceSingletonObjectManager<ResourceTracker<RESTYPE>, IResource>
+{
+ /**
+ * @param workspace
+ */
+ public ResourceManager(IWorkspace workspace)
+ {
+ super(workspace);
+ }
+
+ public void dispose()
+ {
+ super.dispose();
+ }
+
+ /**
+ * @param listener
+ */
+ public void addListener(final IResourceLifecycleListener listener)
+ {
+ super.addLifecycleEventListener(listener);
+ }
+
+ /**
+ * @param listener
+ */
+ public void removeListener(final IResourceLifecycleListener listener)
+ {
+ super.removeLifecycleEventListener(listener);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jst.jsf.common.internal.resource.ResourceSingletonObjectManager#createNewInstance(org.eclipse.core.resources.IResource)
+ */
+ protected abstract ResourceTracker<RESTYPE> createNewInstance(final IResource resource);
+
+ /**
+ * Initialize any state about the current resources we are managing.
+ */
+ public abstract void initResources();
+
+ /**
+ * @return the current list of resources being managed.
+ */
+ public abstract List<RESTYPE> getResources();
+
+} \ No newline at end of file
diff --git a/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceSingletonObjectManager.java b/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceSingletonObjectManager.java
index 0f52167ad..f129dd482 100644
--- a/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceSingletonObjectManager.java
+++ b/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceSingletonObjectManager.java
@@ -254,9 +254,14 @@ public abstract class ResourceSingletonObjectManager<MANAGEDOBJECT extends IMana
return _lifecycleListener;
}
- /*package*/ static class ManagedResourceObject<MANAGEDOBJECT extends IManagedObject>
+ /**
+ * @author cbateman
+ *
+ * @param <MANAGEDOBJECT>
+ */
+ protected final static class ManagedResourceObject<MANAGEDOBJECT extends IManagedObject>
{
- private final MANAGEDOBJECT _managedObject;
+ private final MANAGEDOBJECT _managedObject;
private final MyLifecycleEventListener _eventListener;
private ManagedResourceObject(final MANAGEDOBJECT managedObject,
@@ -266,11 +271,17 @@ public abstract class ResourceSingletonObjectManager<MANAGEDOBJECT extends IMana
_eventListener = eventListener;
}
+ /**
+ * @return the managed object
+ */
public MANAGEDOBJECT getManagedObject()
{
return _managedObject;
}
+ /**
+ * @return the event listener
+ */
public MyLifecycleEventListener getEventListener()
{
return _eventListener;
@@ -298,7 +309,7 @@ public abstract class ResourceSingletonObjectManager<MANAGEDOBJECT extends IMana
final EventResult result = EventResult.getDefaultEventResult();
// not interested
- if ((_resource != null && !_resource.equals(event.getAffectedResource()) || _resource == null)) //if resource == null, then it is the workspace, and can bail
+ if (!_resource.equals(event.getAffectedResource()))
{
return EventResult.getDefaultEventResult();
}
diff --git a/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceTracker.java b/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceTracker.java
new file mode 100644
index 000000000..cc9e78af9
--- /dev/null
+++ b/jsf/plugins/org.eclipse.jst.jsf.common/src/org/eclipse/jst/jsf/common/internal/resource/ResourceTracker.java
@@ -0,0 +1,122 @@
+package org.eclipse.jst.jsf.common.internal.resource;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.jst.jsf.common.internal.managedobject.AbstractManagedObject;
+import org.eclipse.jst.jsf.common.internal.resource.ResourceLifecycleEvent.EventType;
+import org.eclipse.jst.jsf.common.internal.resource.ResourceLifecycleEvent.ReasonType;
+
+/**
+ * A managed object that tracks changes to a resource.
+ *
+ * @author cbateman
+ * @param <RESTYPE>
+ *
+ */
+public abstract class ResourceTracker<RESTYPE extends IResource> extends AbstractManagedObject implements
+ IResourceLifecycleListener
+{
+ private final RESTYPE _resource;
+ private final AtomicLong _lastModifiedStamp = new AtomicLong();
+
+ /**
+ * @param resource
+ */
+ public ResourceTracker(final RESTYPE resource)
+ {
+ super();
+ _resource = resource;
+ _lastModifiedStamp.set(resource.getModificationStamp());
+ }
+
+ /**
+ * @return the resource that is being tracked
+ */
+ public final RESTYPE getResource()
+ {
+ return _resource;
+ }
+
+ /**
+ * @return the last modificatino stamp stored for the resource.
+ */
+ public final long getLastModifiedStamp()
+ {
+ return _lastModifiedStamp.get();
+ }
+
+ public EventResult acceptEvent(final ResourceLifecycleEvent event)
+ {
+ if (!isInteresting(event))
+ {
+ return EventResult.getDefaultEventResult();
+ }
+
+ final EventType eventType = event.getEventType();
+
+ final ReasonType reasonType = event.getReasonType();
+ switch (eventType)
+ {
+ case RESOURCE_ADDED:
+ // added resources kick an add event.
+ fireResourceAdded(event.getAffectedResource(), reasonType);
+ break;
+ case RESOURCE_CHANGED:
+ // changed resources kick a change event
+ fireResourceChanged(reasonType);
+ break;
+ case RESOURCE_INACCESSIBLE:
+ // removed resources kick a remove event
+ fireResourceInAccessible(reasonType);
+ break;
+ }
+
+ return EventResult.getDefaultEventResult();
+ }
+
+ /**
+ * @param event
+ * @return true if this event is interesting
+ */
+ protected boolean isInteresting(final ResourceLifecycleEvent event)
+ {
+ return _resource.equals(event.getAffectedResource());
+ }
+
+
+ /**
+ * @param reasonType
+ */
+ protected abstract void fireResourceInAccessible(ReasonType reasonType);
+
+ /**
+ * @param reasonType
+ */
+ protected abstract void fireResourceChanged(ReasonType reasonType);
+
+ /**
+ * @param affectedResource
+ * @param reasonType
+ */
+ protected abstract void fireResourceAdded(IResource affectedResource, ReasonType reasonType);
+
+ @Override
+ public void dispose()
+ {
+ super.dispose();
+ }
+
+ @Override
+ public void checkpoint()
+ {
+ // nothing currently persisted
+ }
+
+ @Override
+ public void destroy()
+ {
+ // nothing currently persisted
+ }
+
+}

Back to the top