diff options
author | rbrooks | 2010-12-17 19:05:11 +0000 |
---|---|---|
committer | Ryan D. Brooks | 2010-12-17 19:05:11 +0000 |
commit | 73faa3088981f055882f98a47a9894bec614451c (patch) | |
tree | 56c5f66c9cea2303d22b3fcf27c885cf99ca308d | |
parent | abbb7ab1db9334304eaf475daab3689c799b2f7a (diff) | |
download | org.eclipse.osee-73faa3088981f055882f98a47a9894bec614451c.tar.gz org.eclipse.osee-73faa3088981f055882f98a47a9894bec614451c.tar.xz org.eclipse.osee-73faa3088981f055882f98a47a9894bec614451c.zip |
bug[ats_NZYFE]: FileWatcher unnecessarily triggers updates and synchronization blocks
9 files changed, 229 insertions, 199 deletions
diff --git a/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/utility/FileChangeEvent.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/FileChangeEvent.java index b8f8422bd21..3b99fa8221a 100644 --- a/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/utility/FileChangeEvent.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/FileChangeEvent.java @@ -8,7 +8,7 @@ * Contributors: * Boeing - initial API and implementation *******************************************************************************/ -package org.eclipse.osee.framework.skynet.core.utility; +package org.eclipse.osee.framework.jdk.core.util.io; import java.io.File; diff --git a/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/utility/FileChangeType.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/FileChangeType.java index d5642c82186..f82a335a53d 100644 --- a/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/utility/FileChangeType.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/FileChangeType.java @@ -8,7 +8,7 @@ * Contributors: * Boeing - initial API and implementation *******************************************************************************/ -package org.eclipse.osee.framework.skynet.core.utility; +package org.eclipse.osee.framework.jdk.core.util.io; /** * @author Ken J. Aguilar diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/FileWatcher.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/FileWatcher.java new file mode 100644 index 00000000000..faf8a2df355 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/FileWatcher.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.util.io; + +import java.io.File; +import java.util.Map; +import java.util.Set; +import java.util.Timer; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.TimeUnit; + +/** + * @author Ken J. Aguilar + */ +public final class FileWatcher { + private final Timer timer = new Timer(); + private final Map<File, Long> filesToWatch = new ConcurrentHashMap<File, Long>(128); + private final Set<IFileWatcherListener> listeners = new CopyOnWriteArraySet<IFileWatcherListener>(); + private final long interval; + + public FileWatcher(long time, TimeUnit unit) { + interval = unit.toMillis(time); + } + + /** + * Starts the file watcher monitoring of the file system + */ + public void start() { + timer.schedule(new FileWatcherTimerTask(filesToWatch, listeners), interval, interval); + } + + public void stop() { + timer.cancel(); + listeners.clear(); + filesToWatch.clear(); + } + + /** + * adds a {@link File} to the files to be monitored. This method can be called before or after the {@link #start()} + * method is called. + */ + public void addFile(File file) { + filesToWatch.put(file, file.lastModified()); + } + + /** + * removes a {@link File} from the set of files to be monitored. This method can be called before or after the + * {@link #start()} method is called. + * + * @return returns the last know timestamp of the file before it was removed or null if it was never being monitored + * in the first place + */ + public Long removeFile(File file) { + return filesToWatch.remove(file); + } + + /** + * registers a listener who will be notified of file change events. This method can be called before or after the + * {@link #start()} method is called. + */ + public void addListener(IFileWatcherListener listener) { + listeners.add(listener); + } + + /** + * unregisters a listener from receiving file change events. This method can be called before or after the + * {@link #start()} method is called. + */ + public void removeListener(IFileWatcherListener listener) { + listeners.remove(listener); + } +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/FileWatcherTimerTask.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/FileWatcherTimerTask.java new file mode 100644 index 00000000000..33fd966a4e0 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/FileWatcherTimerTask.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2010 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.util.io; + +import java.io.File; +import java.util.LinkedList; +import java.util.Map; +import java.util.Set; +import java.util.TimerTask; + +/** + * @author Ken J. Aguilar + * @author Ryan D. Brooks + */ +final class FileWatcherTimerTask extends TimerTask { + private final Map<File, Long> filesToWatch; + private final Set<IFileWatcherListener> listeners; + + public FileWatcherTimerTask(Map<File, Long> filesToWatch, Set<IFileWatcherListener> listeners) { + super(); + this.filesToWatch = filesToWatch; + this.listeners = listeners; + } + + @Override + public void run() { + try { + LinkedList<FileChangeEvent> fileChangeEvents = new LinkedList<FileChangeEvent>(); + for (Map.Entry<File, Long> entry : filesToWatch.entrySet()) { + Long latestLastModified = entry.getKey().lastModified(); + Long storedLastModified = entry.getValue(); + if (!storedLastModified.equals(latestLastModified)) { + entry.setValue(latestLastModified); + if (storedLastModified == 0) { + // created + assert entry.getKey().exists() : "file doesn't exist"; + fileChangeEvents.add(new FileChangeEvent(entry.getKey(), FileChangeType.CREATED)); + } else if (latestLastModified == 0) { + // deleted + assert !entry.getKey().exists() : "file still exist"; + fileChangeEvents.add(new FileChangeEvent(entry.getKey(), FileChangeType.DELETED)); + } else { + // modified + assert entry.getKey().exists() : "file doesn't exist"; + fileChangeEvents.add(new FileChangeEvent(entry.getKey(), FileChangeType.MODIFIED)); + } + } + } + if (!fileChangeEvents.isEmpty()) { + // there is at least one file change event, notify listeners + for (IFileWatcherListener listener : listeners) { + listener.filesModified(fileChangeEvents); + } + } + } catch (Exception ex) { + for (IFileWatcherListener listener : listeners) { + listener.handleExcpetion(ex); + } + } + } +} diff --git a/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/utility/IFileWatcherListener.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/IFileWatcherListener.java index c969dd220d1..4247da9c18f 100644 --- a/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/utility/IFileWatcherListener.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/IFileWatcherListener.java @@ -8,7 +8,7 @@ * Contributors: * Boeing - initial API and implementation *******************************************************************************/ -package org.eclipse.osee.framework.skynet.core.utility; +package org.eclipse.osee.framework.jdk.core.util.io; import java.util.Collection; @@ -17,5 +17,7 @@ import java.util.Collection; */ public interface IFileWatcherListener { - void filesModified(Collection<FileChangeEvent> fileChangeEvents); + public void filesModified(Collection<FileChangeEvent> fileChangeEvents); + + public void handleExcpetion(Exception ex); } diff --git a/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/utility/FileWatcher.java b/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/utility/FileWatcher.java deleted file mode 100644 index 2bf1ee73ad7..00000000000 --- a/plugins/org.eclipse.osee.framework.skynet.core/src/org/eclipse/osee/framework/skynet/core/utility/FileWatcher.java +++ /dev/null @@ -1,123 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2004, 2007 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.framework.skynet.core.utility; - -import java.io.File; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.TimeUnit; -import java.util.logging.Level; -import org.eclipse.osee.framework.logging.OseeLog; -import org.eclipse.osee.framework.skynet.core.internal.Activator; - -/** - * @author Ken J. Aguilar - */ -public class FileWatcher extends TimerTask { - private final long interval; - private final Timer timer = new Timer(); - - protected final HashMap<File, Long> filesToWatch = new HashMap<File, Long>(128); - private final HashSet<IFileWatcherListener> listeners = new HashSet<IFileWatcherListener>(); - - public FileWatcher(long time, TimeUnit unit) { - interval = unit.toMillis(time); - } - - /** - * Starts the file watcher monitoring of the file system - */ - public void start() { - timer.schedule(this, interval, interval); - } - - /** - * adds a {@link File} to the files to be monitored. This method can be called before or after the {@link #start()} - * method is called. - */ - public synchronized void addFile(File file) { - filesToWatch.put(file, file.lastModified()); - } - - /** - * removes a {@link File} from the set of files to be monitored. This method can be called before or after the - * {@link #start()} method is called. - * - * @return returns the last know timestamp of the file before it was removed or null if it was never being monitored - * in the first place - */ - public synchronized Long removeFile(File file) { - return filesToWatch.remove(file); - } - - /** - * registers a listener who will be notified of file change events. This method can be called before or after the - * {@link #start()} method is called. - */ - public synchronized void addListener(IFileWatcherListener listener) { - listeners.add(listener); - } - - /** - * unregisters a listener from receiving file change events. This method can be called before or after the - * {@link #start()} method is called. - */ - public synchronized void removeListener(IFileWatcherListener listener) { - listeners.remove(listener); - } - - @Override - public synchronized void run() { - try { - LinkedList<FileChangeEvent> fileChangeEvents = new LinkedList<FileChangeEvent>(); - for (Map.Entry<File, Long> entry : filesToWatch.entrySet()) { - Long latestLastModified = entry.getKey().lastModified(); - Long storedLastModified = entry.getValue(); - if (!storedLastModified.equals(latestLastModified)) { - entry.setValue(latestLastModified); - if (storedLastModified == 0) { - // created - assert entry.getKey().exists() : "file doesn't exist"; - fileChangeEvents.add(new FileChangeEvent(entry.getKey(), FileChangeType.CREATED)); - } else if (latestLastModified == 0) { - // deleted - assert !entry.getKey().exists() : "file still exist"; - fileChangeEvents.add(new FileChangeEvent(entry.getKey(), FileChangeType.DELETED)); - } else { - // modified - assert entry.getKey().exists() : "file doesn't exist"; - fileChangeEvents.add(new FileChangeEvent(entry.getKey(), FileChangeType.MODIFIED)); - } - - } - } - if (!fileChangeEvents.isEmpty()) { - // there is at least one file change event, notify listeners - for (IFileWatcherListener listener : listeners) { - listener.filesModified(fileChangeEvents); - } - } - } catch (Exception ex) { - OseeLog.log(Activator.class, Level.SEVERE, ex); - } - } - - public void dispose() { - timer.cancel(); - listeners.clear(); - filesToWatch.clear(); - } - -} diff --git a/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/ArtifactEditFileWatcher.java b/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/ArtifactEditFileWatcher.java deleted file mode 100644 index cddb8da6a3f..00000000000 --- a/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/ArtifactEditFileWatcher.java +++ /dev/null @@ -1,69 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.framework.ui.skynet.render; - -import java.io.File; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.logging.Level; -import org.eclipse.core.runtime.jobs.IJobChangeEvent; -import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.core.runtime.jobs.JobChangeAdapter; -import org.eclipse.osee.framework.core.operation.IOperation; -import org.eclipse.osee.framework.core.operation.Operations; -import org.eclipse.osee.framework.logging.OseeLog; -import org.eclipse.osee.framework.skynet.core.utility.FileWatcher; -import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin; - -final class ArtifactEditFileWatcher extends FileWatcher { - - private final IArtifactUpdateOperationFactory opFactory; - - public ArtifactEditFileWatcher(IArtifactUpdateOperationFactory opFactory, long time, TimeUnit unit) { - super(time, unit); - this.opFactory = opFactory; - } - - @Override - public synchronized void run() { - try { - for (Map.Entry<File, Long> entry : filesToWatch.entrySet()) { - final File file = entry.getKey(); - final Long storedLastModified = entry.getValue(); - - Long latestLastModified = file.lastModified(); - boolean requiresUpdate = false; - if (!storedLastModified.equals(latestLastModified)) { - entry.setValue(latestLastModified); - if (file.exists()) { - requiresUpdate = true; - } - } - - if (requiresUpdate) { - IOperation op = opFactory.createUpdateOp(file); - Operations.executeAsJob(op, false, Job.LONG, new JobChangeAdapter() { - - @Override - public void done(IJobChangeEvent event) { - if (event.getResult().isOK()) { - OseeLog.log(SkynetGuiPlugin.class, Level.INFO, - "Updated artifact linked to: " + file.getAbsolutePath()); - } - } - }); - } - } - } catch (Exception ex) { - OseeLog.log(SkynetGuiPlugin.class, Level.SEVERE, ex); - } - } -}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/ArtifactFileMonitor.java b/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/ArtifactFileMonitor.java index aec49920ad6..d66e78ea1c7 100644 --- a/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/ArtifactFileMonitor.java +++ b/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/ArtifactFileMonitor.java @@ -19,8 +19,8 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.osee.framework.core.exception.OseeCoreException; import org.eclipse.osee.framework.core.exception.OseeExceptions; +import org.eclipse.osee.framework.jdk.core.util.io.FileWatcher; import org.eclipse.osee.framework.logging.OseeLog; -import org.eclipse.osee.framework.skynet.core.utility.FileWatcher; import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchListener; @@ -31,12 +31,13 @@ final class ArtifactFileMonitor { private final FileWatcher watcher; private boolean firstTime; - public ArtifactFileMonitor(IArtifactUpdateOperationFactory jobFactory) { + public ArtifactFileMonitor(IArtifactUpdateOperationFactory opFactory) { firstTime = true; readonlyfileAttributes = new ResourceAttributes(); readonlyfileAttributes.setReadOnly(true); - watcher = new ArtifactEditFileWatcher(jobFactory, 3, TimeUnit.SECONDS); + watcher = new FileWatcher(3, TimeUnit.SECONDS); + watcher.addListener(new ArtifactFileWatcherListener(opFactory)); watcher.start(); } diff --git a/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/ArtifactFileWatcherListener.java b/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/ArtifactFileWatcherListener.java new file mode 100644 index 00000000000..84614e9960a --- /dev/null +++ b/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/ArtifactFileWatcherListener.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2010 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.ui.skynet.render; + +import java.io.File; +import java.util.Collection; +import java.util.logging.Level; +import org.eclipse.core.runtime.jobs.IJobChangeEvent; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.core.runtime.jobs.JobChangeAdapter; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.core.operation.IOperation; +import org.eclipse.osee.framework.core.operation.Operations; +import org.eclipse.osee.framework.jdk.core.util.io.FileChangeEvent; +import org.eclipse.osee.framework.jdk.core.util.io.FileChangeType; +import org.eclipse.osee.framework.jdk.core.util.io.IFileWatcherListener; +import org.eclipse.osee.framework.logging.OseeLevel; +import org.eclipse.osee.framework.logging.OseeLog; +import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin; + +/** + * @author Ryan D. Brooks + */ +public class ArtifactFileWatcherListener implements IFileWatcherListener { + private final IArtifactUpdateOperationFactory opFactory; + + public ArtifactFileWatcherListener(IArtifactUpdateOperationFactory opFactory) { + this.opFactory = opFactory; + } + + @Override + public void filesModified(Collection<FileChangeEvent> fileChangeEvents) { + for (FileChangeEvent event : fileChangeEvents) { + if (event.getChangeType() == FileChangeType.MODIFIED) { + try { + processFileUpdate(event.getFile()); + } catch (OseeCoreException ex) { + OseeLog.log(SkynetGuiPlugin.class, OseeLevel.SEVERE_POPUP, ex); + } + } + } + } + + private void processFileUpdate(final File file) throws OseeCoreException { + IOperation op = opFactory.createUpdateOp(file); + Operations.executeAsJob(op, false, Job.LONG, new JobChangeAdapter() { + + @Override + public void done(IJobChangeEvent event) { + if (event.getResult().isOK()) { + OseeLog.log(SkynetGuiPlugin.class, Level.INFO, "Updated artifact linked to: " + file.getAbsolutePath()); + } + } + }); + } + + @Override + public void handleExcpetion(Exception ex) { + OseeLog.log(SkynetGuiPlugin.class, OseeLevel.SEVERE_POPUP, ex); + } +}
\ No newline at end of file |