diff options
Diffstat (limited to 'plugins/org.eclipse.net4j.util/monitor')
19 files changed, 1639 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.util/monitor/DelegatingMonitor.java b/plugins/org.eclipse.net4j.util/monitor/DelegatingMonitor.java new file mode 100644 index 0000000000..18d7e0c996 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/DelegatingMonitor.java @@ -0,0 +1,120 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +/** + * @author Eike Stepper + * @since 2.0 + */ +public abstract class DelegatingMonitor implements OMMonitor +{ + public DelegatingMonitor() + { + } + + public void checkCanceled() throws MonitorCanceledException + { + getDelegate().checkCanceled(); + } + + public boolean isCanceled() + { + return getDelegate().isCanceled(); + } + + public void setCanceled(boolean canceled) + { + getDelegate().setCanceled(canceled); + } + + public OMSubMonitor fork() + { + return getDelegate().fork(); + } + + public void fork(int workFromParent, Runnable runnable, String msg) + { + getDelegate().fork(workFromParent, runnable, msg); + } + + public void fork(int workFromParent, Runnable runnable) + { + getDelegate().fork(workFromParent, runnable); + } + + public OMSubMonitor fork(int workFromParent) + { + return getDelegate().fork(workFromParent); + } + + public void fork(Runnable runnable, String msg) + { + getDelegate().fork(runnable, msg); + } + + public void fork(Runnable runnable) + { + getDelegate().fork(runnable); + } + + public String getTask() + { + return getDelegate().getTask(); + } + + public int getTotalWork() + { + return getDelegate().getTotalWork(); + } + + public boolean hasBegun() + { + return getDelegate().hasBegun(); + } + + public void message(String msg) + { + getDelegate().message(msg); + } + + public void setTask(String task) + { + getDelegate().setTask(task); + } + + public void worked() + { + getDelegate().worked(); + } + + public void worked(int work, String msg) + { + getDelegate().worked(work, msg); + } + + public void worked(int work) + { + getDelegate().worked(work); + } + + public void worked(String msg) + { + getDelegate().worked(msg); + } + + @Override + public String toString() + { + return getDelegate().toString(); + } + + protected abstract OMMonitor getDelegate(); +} diff --git a/plugins/org.eclipse.net4j.util/monitor/IllegalMonitorNestingException.java b/plugins/org.eclipse.net4j.util/monitor/IllegalMonitorNestingException.java new file mode 100644 index 0000000000..92dbda2735 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/IllegalMonitorNestingException.java @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +/** + * @author Eike Stepper + */ +public class IllegalMonitorNestingException extends MonitorException +{ + private static final long serialVersionUID = 1L; + + public IllegalMonitorNestingException() + { + } + + public IllegalMonitorNestingException(String s) + { + super(s); + } + + public IllegalMonitorNestingException(Throwable cause) + { + super(cause); + } + + public IllegalMonitorNestingException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/MonitorAlreadyBegunException.java b/plugins/org.eclipse.net4j.util/monitor/MonitorAlreadyBegunException.java new file mode 100644 index 0000000000..19f44dea46 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/MonitorAlreadyBegunException.java @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +/** + * @author Eike Stepper + */ +public class MonitorAlreadyBegunException extends MonitorException +{ + private static final long serialVersionUID = 1L; + + public MonitorAlreadyBegunException() + { + } + + public MonitorAlreadyBegunException(String s) + { + super(s); + } + + public MonitorAlreadyBegunException(Throwable cause) + { + super(cause); + } + + public MonitorAlreadyBegunException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/MonitorCanceledException.java b/plugins/org.eclipse.net4j.util/monitor/MonitorCanceledException.java new file mode 100644 index 0000000000..92f3224f61 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/MonitorCanceledException.java @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +/** + * @author Eike Stepper + */ +public class MonitorCanceledException extends MonitorException +{ + private static final long serialVersionUID = 1L; + + public MonitorCanceledException() + { + } + + public MonitorCanceledException(String s) + { + super(s); + } + + public MonitorCanceledException(Throwable cause) + { + super(cause); + } + + public MonitorCanceledException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/MonitorException.java b/plugins/org.eclipse.net4j.util/monitor/MonitorException.java new file mode 100644 index 0000000000..c39fb00426 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/MonitorException.java @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +/** + * @author Eike Stepper + */ +public class MonitorException extends IllegalStateException +{ + private static final long serialVersionUID = 1L; + + public MonitorException() + { + } + + public MonitorException(String s) + { + super(s); + } + + public MonitorException(Throwable cause) + { + super(cause); + } + + public MonitorException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/MonitorNotBegunException.java b/plugins/org.eclipse.net4j.util/monitor/MonitorNotBegunException.java new file mode 100644 index 0000000000..e4d31e6269 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/MonitorNotBegunException.java @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +/** + * @author Eike Stepper + */ +public class MonitorNotBegunException extends MonitorException +{ + private static final long serialVersionUID = 1L; + + public MonitorNotBegunException() + { + } + + public MonitorNotBegunException(String s) + { + super(s); + } + + public MonitorNotBegunException(Throwable cause) + { + super(cause); + } + + public MonitorNotBegunException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/MonitorTest.java b/plugins/org.eclipse.net4j.util/monitor/MonitorTest.java new file mode 100644 index 0000000000..8bcab480bd --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/MonitorTest.java @@ -0,0 +1,155 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.tests; + +import org.eclipse.net4j.util.om.monitor.IllegalMonitorNestingException; +import org.eclipse.net4j.util.om.monitor.MonitorAlreadyBegunException; +import org.eclipse.net4j.util.om.monitor.MonitorUtil; +import org.eclipse.net4j.util.om.monitor.OMMonitor; +import org.eclipse.net4j.util.om.monitor.OMSubMonitor; + +/** + * @author Eike Stepper + */ +public class MonitorTest extends AbstractOMTest +{ + private static final String[] CLASSES = { "Pair", "Triple" }; + + private static final String[][] FIELDS = { { "x", "y" }, { "x", "y", "z" } }; + + public void testJoined() throws Exception + { + MonitorUtil.Legacy.startMonitoring(); + try + { + readClasses(CLASSES, FIELDS, true, true); + } + finally + { + MonitorUtil.Legacy.stopMonitoring(); + } + } + + public void testJoinedNotStarted() throws Exception + { + readClasses(CLASSES, FIELDS, true, true); + } + + public void testJoinedStopNotStarted() throws Exception + { + readClasses(CLASSES, FIELDS, true, true); + MonitorUtil.Legacy.stopMonitoring(); + } + + public void testUnjoined() throws Exception + { + MonitorUtil.Legacy.startMonitoring(); + try + { + readClasses(CLASSES, FIELDS, true, false); + fail("IllegalMonitorNestingException expected"); + } + catch (IllegalMonitorNestingException ex) + { + } + finally + { + MonitorUtil.Legacy.stopMonitoring(); + } + } + + public void testUnjoinedNotStarted() throws Exception + { + readClasses(CLASSES, FIELDS, true, false); + } + + public void testNotForked() throws Exception + { + MonitorUtil.Legacy.startMonitoring(); + try + { + readClasses(CLASSES, FIELDS, false, false); + fail("MonitorAlreadyBegunException expected"); + } + catch (MonitorAlreadyBegunException ex) + { + } + finally + { + MonitorUtil.Legacy.stopMonitoring(); + } + } + + /** + * Supports {@link MonitorUtil progress monitoring}. + */ + private static void readClasses(String[] classes, String[][] fields, boolean fork, boolean join) + { + int num = classes.length; + OMMonitor monitor = MonitorUtil.begin(2 * num, "Reading " + num + " classes"); + for (int i = 0; i < num; i++) + { + // Create class buffer + monitor.worked(1, "Created class buffer for " + classes[i]); + + // Read class + OMSubMonitor subMonitor = fork ? monitor.fork() : null; + try + { + readFields(fields[i], fork, join); + } + finally + { + if (join) + { + subMonitor.join("Read class " + classes[i]); + } + } + } + } + + /** + * Supports {@link MonitorUtil progress monitoring}. + */ + private static void readFields(String[] fields, boolean fork, boolean join) + { + int num = fields.length; + OMMonitor monitor = MonitorUtil.begin(2 * num, "Reading " + num + " fields"); + for (int i = 0; i < num; i++) + { + // Read field + monitor.worked(1, "Read field " + fields[i]); + + OMSubMonitor subMonitor = fork ? monitor.fork() : null; + try + { + readSetting(); + } + finally + { + if (join) + { + subMonitor.join(); + } + } + } + } + + /** + * Supports {@link MonitorUtil progress monitoring}. + */ + private static void readSetting() + { + OMMonitor monitor = MonitorUtil.begin(1, "Reading setting"); + // Read setting + monitor.worked("Read setting"); + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/MonitorUtil.java b/plugins/org.eclipse.net4j.util/monitor/MonitorUtil.java new file mode 100644 index 0000000000..b139567a18 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/MonitorUtil.java @@ -0,0 +1,133 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +import org.eclipse.net4j.internal.util.bundle.OM; +import org.eclipse.net4j.internal.util.om.monitor.EclipseMonitor; +import org.eclipse.net4j.internal.util.om.monitor.LegacyMonitor; +import org.eclipse.net4j.internal.util.om.monitor.MON; + +import org.eclipse.core.runtime.IProgressMonitor; + +/** + * @author Eike Stepper + */ +public final class MonitorUtil +{ + private MonitorUtil() + { + } + + public static boolean isCanceled() + { + return MON.isCanceled(); + } + + public static OMMonitor begin() + { + return MON.begin(OMMonitor.UNKNOWN, null); + } + + public static OMMonitor begin(int totalWork) + { + return MON.begin(totalWork, null); + } + + public static OMMonitor begin(String task) + { + return MON.begin(OMMonitor.UNKNOWN, task); + } + + public static OMMonitor begin(int totalWork, String task) + { + return MON.begin(totalWork, task); + } + + static void handleTrace(final OMMonitorHandler messageHandler, String msg, int level, boolean isTask) + { + if (messageHandler != null) + { + try + { + if (isTask) + { + messageHandler.handleTask(msg, level); + } + else + { + messageHandler.handleMessage(msg, level); + } + } + catch (RuntimeException ex) + { + OM.LOG.error(ex); + } + } + } + + /** + * @author Eike Stepper + */ + public static final class Eclipse + { + public static void startMonitoring(IProgressMonitor progressMonitor, final OMMonitorHandler messageHandler) + { + MON.startMonitoring(new EclipseMonitor(progressMonitor) + { + @Override + protected void trace(String msg, int level, boolean isTask) + { + super.trace(msg, level, isTask); + handleTrace(messageHandler, msg, level, isTask); + } + }); + } + + public static void startMonitoring(IProgressMonitor progressMonitor) + { + startMonitoring(progressMonitor, null); + } + + public static void stopMonitoring() + { + MON.stopMonitoring(); + } + } + + /** + * @author Eike Stepper + */ + public static final class Legacy + { + public static void startMonitoring(final OMMonitorHandler messageHandler) + { + MON.startMonitoring(new LegacyMonitor() + { + @Override + protected void trace(String msg, int level, boolean isTask) + { + super.trace(msg, level, isTask); + handleTrace(messageHandler, msg, level, isTask); + } + }); + } + + public static void startMonitoring() + { + startMonitoring(null); + } + + public static void stopMonitoring() + { + MON.stopMonitoring(); + } + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/MonitoredJob.java b/plugins/org.eclipse.net4j.util/monitor/MonitoredJob.java new file mode 100644 index 0000000000..abf85017fc --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/MonitoredJob.java @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +import org.eclipse.net4j.internal.util.bundle.OM; +import org.eclipse.net4j.internal.util.om.monitor.MON; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; + +/** + * @author Eike Stepper + */ +public abstract class MonitoredJob extends Job +{ + private String bundleID; + + public MonitoredJob(String bundleID, String name) + { + super(name); + this.bundleID = bundleID; + } + + @Override + protected void canceling() + { + MON.setCanceled(true); + super.canceling(); + } + + @Override + protected final IStatus run(IProgressMonitor monitor) + { + MonitorUtil.Eclipse.startMonitoring(monitor); + + try + { + run(); + return Status.OK_STATUS; + } + catch (Exception ex) + { + OM.LOG.error(ex); + return new Status(IStatus.ERROR, bundleID, ex.getMessage(), ex); + } + finally + { + MonitorUtil.Eclipse.stopMonitoring(); + } + } + + protected abstract void run() throws Exception; +} diff --git a/plugins/org.eclipse.net4j.util/monitor/OMMonitor.java b/plugins/org.eclipse.net4j.util/monitor/OMMonitor.java new file mode 100644 index 0000000000..3ba918ecec --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/OMMonitor.java @@ -0,0 +1,64 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +/** + * @author Eike Stepper + */ +public interface OMMonitor +{ + public static final int UNKNOWN = -1; + + public String getTask(); + + public void setTask(String task); + + public int getTotalWork(); + + public boolean hasBegun(); + + /** + * @since 2.0 + */ + public boolean isCanceled(); + + /** + * @since 2.0 + */ + public void setCanceled(boolean canceled); + + /** + * @since 2.0 + */ + public void checkCanceled() throws MonitorCanceledException; + + public void message(String msg); + + public void worked(int work, String msg); + + public void worked(int work); + + public void worked(String msg); + + public void worked(); + + public void fork(int workFromParent, Runnable runnable, String msg); + + public void fork(int workFromParent, Runnable runnable); + + public void fork(Runnable runnable, String msg); + + public void fork(Runnable runnable); + + public OMSubMonitor fork(int workFromParent); + + public OMSubMonitor fork(); +} diff --git a/plugins/org.eclipse.net4j.util/monitor/OMMonitorHandler.java b/plugins/org.eclipse.net4j.util/monitor/OMMonitorHandler.java new file mode 100644 index 0000000000..6f7be296ea --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/OMMonitorHandler.java @@ -0,0 +1,21 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +/** + * @author Eike Stepper + */ +public interface OMMonitorHandler +{ + public void handleTask(String task, int level); + + public void handleMessage(String msg, int level); +} diff --git a/plugins/org.eclipse.net4j.util/monitor/OMSubMonitor.java b/plugins/org.eclipse.net4j.util/monitor/OMSubMonitor.java new file mode 100644 index 0000000000..08c0e10d58 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/OMSubMonitor.java @@ -0,0 +1,21 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +/** + * @author Eike Stepper + */ +public interface OMSubMonitor +{ + public void join(String msg); + + public void join(); +} diff --git a/plugins/org.eclipse.net4j.util/monitor/SynchonizedSubProgressMonitor.java b/plugins/org.eclipse.net4j.util/monitor/SynchonizedSubProgressMonitor.java new file mode 100644 index 0000000000..ca3ebfa454 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/SynchonizedSubProgressMonitor.java @@ -0,0 +1,119 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.SubProgressMonitor; + +/** + * A sub progress monitor that synchronizes all methods on the parent monitor instance. + * + * @author Eike Stepper + * @since 2.0 + */ +public final class SynchonizedSubProgressMonitor extends SubProgressMonitor +{ + public SynchonizedSubProgressMonitor(IProgressMonitor monitor, int ticks) + { + super(monitor, ticks); + } + + @Override + public void beginTask(String name, int totalWork) + { + synchronized (getWrappedProgressMonitor()) + { + super.beginTask(name, totalWork); + } + } + + @Override + public void clearBlocked() + { + synchronized (getWrappedProgressMonitor()) + { + super.clearBlocked(); + } + } + + @Override + public void done() + { + synchronized (getWrappedProgressMonitor()) + { + super.done(); + } + } + + @Override + public void internalWorked(double work) + { + synchronized (getWrappedProgressMonitor()) + { + super.internalWorked(work); + } + } + + @Override + public boolean isCanceled() + { + synchronized (getWrappedProgressMonitor()) + { + return super.isCanceled(); + } + } + + @Override + public void setBlocked(IStatus reason) + { + synchronized (getWrappedProgressMonitor()) + { + super.setBlocked(reason); + } + } + + @Override + public void setCanceled(boolean b) + { + synchronized (getWrappedProgressMonitor()) + { + super.setCanceled(b); + } + } + + @Override + public void setTaskName(String name) + { + synchronized (getWrappedProgressMonitor()) + { + super.setTaskName(name); + } + } + + @Override + public void subTask(String name) + { + synchronized (getWrappedProgressMonitor()) + { + super.subTask(name); + } + } + + @Override + public void worked(int work) + { + synchronized (getWrappedProgressMonitor()) + { + super.worked(work); + } + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/TotalWorkExceededException.java b/plugins/org.eclipse.net4j.util/monitor/TotalWorkExceededException.java new file mode 100644 index 0000000000..6fbdeeb10d --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/TotalWorkExceededException.java @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.util.om.monitor; + +/** + * @author Eike Stepper + */ +public class TotalWorkExceededException extends MonitorException +{ + private static final long serialVersionUID = 1L; + + public TotalWorkExceededException() + { + } + + public TotalWorkExceededException(String s) + { + super(s); + } + + public TotalWorkExceededException(Throwable cause) + { + super(cause); + } + + public TotalWorkExceededException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/impl/EclipseMonitor.java b/plugins/org.eclipse.net4j.util/monitor/impl/EclipseMonitor.java new file mode 100644 index 0000000000..03f8cafb62 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/impl/EclipseMonitor.java @@ -0,0 +1,149 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.util.om.monitor; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.SubProgressMonitor; + +/** + * @author Eike Stepper + */ +public class EclipseMonitor extends Monitor +{ + private IProgressMonitor progressMonitor; + + public EclipseMonitor(IProgressMonitor progressMonitor) + { + super(null, 0); + if (progressMonitor == null) + { + throw new IllegalArgumentException("progressMonitor == null"); + } + + this.progressMonitor = new DelegatingProgressMonitor(progressMonitor); + } + + private EclipseMonitor(EclipseMonitor parent, int workFromParent) + { + super(parent, workFromParent); + progressMonitor = new SubProgressMonitor(new SubProgressMonitor(parent.getProgressMonitor(), workFromParent), + workFromParent); + } + + public IProgressMonitor getProgressMonitor() + { + return progressMonitor; + } + + @Override + public void setTask(String task) + { + super.setTask(task); + progressMonitor.setTaskName(task); + } + + @Override + public void worked(int work, String msg) + { + super.worked(work, msg); + progressMonitor.worked(work); + if (msg != null) + { + progressMonitor.subTask(msg); + } + } + + @Override + protected void begin(int totalWork, String task) + { + super.begin(totalWork, task); + progressMonitor.beginTask(task == null ? "" : task, totalWork); + } + + @Override + protected void done() + { + super.done(); + progressMonitor.done(); + } + + @Override + protected void message(String msg, int level) + { + super.message(msg, level); + progressMonitor.subTask(msg); + } + + @Override + protected EclipseMonitor subMonitor(int workFromParent) + { + return new EclipseMonitor(this, workFromParent); + } + + /** + * @author Eike Stepper + */ + private final class DelegatingProgressMonitor implements IProgressMonitor + { + private IProgressMonitor delegate; + + public DelegatingProgressMonitor(IProgressMonitor delegate) + { + this.delegate = delegate; + } + + public IProgressMonitor getDelegate() + { + return delegate; + } + + public void beginTask(String name, int totalWork) + { + delegate.beginTask(name, totalWork); + } + + public void done() + { + delegate.done(); + } + + public void internalWorked(double work) + { + delegate.internalWorked(work); + } + + public boolean isCanceled() + { + return delegate.isCanceled(); + } + + public void setCanceled(boolean value) + { + EclipseMonitor.this.setCanceled(value); + delegate.setCanceled(value); + } + + public void setTaskName(String name) + { + delegate.setTaskName(name); + } + + public void subTask(String name) + { + delegate.subTask(name); + } + + public void worked(int work) + { + delegate.worked(work); + } + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/impl/LegacyMonitor.java b/plugins/org.eclipse.net4j.util/monitor/impl/LegacyMonitor.java new file mode 100644 index 0000000000..cb5e8ddb18 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/impl/LegacyMonitor.java @@ -0,0 +1,33 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.util.om.monitor; + +/** + * @author Eike Stepper + */ +public class LegacyMonitor extends Monitor +{ + public LegacyMonitor() + { + super(null, 0); + } + + private LegacyMonitor(LegacyMonitor parent, int workFromParent) + { + super(parent, workFromParent); + } + + @Override + public LegacyMonitor subMonitor(int workFromParent) + { + return new LegacyMonitor(this, workFromParent); + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/impl/MON.java b/plugins/org.eclipse.net4j.util/monitor/impl/MON.java new file mode 100644 index 0000000000..430a74572e --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/impl/MON.java @@ -0,0 +1,122 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.util.om.monitor; + +import org.eclipse.net4j.internal.util.bundle.OM; +import org.eclipse.net4j.util.om.monitor.IllegalMonitorNestingException; +import org.eclipse.net4j.util.om.monitor.MonitorAlreadyBegunException; +import org.eclipse.net4j.util.om.monitor.MonitorException; +import org.eclipse.net4j.util.om.monitor.OMMonitor; + +/** + * @author Eike Stepper + */ +public final class MON +{ + public static final int UNKNOWN = -1; + + private static final ThreadLocal<Monitor> CURRENT = new ThreadLocal<Monitor>(); + + private MON() + { + } + + public static boolean isCanceled() + { + Monitor current = CURRENT.get(); + if (current == null) + { + return false; + } + + return current.isCanceled(); + } + + public static void setCanceled(boolean canceled) + { + Monitor current = CURRENT.get(); + if (current != null) + { + current.setCanceled(canceled); + } + } + + public static void startMonitoring(Monitor rootMonitor) + { + Monitor current = CURRENT.get(); + if (current != null) + { + throw new IllegalMonitorNestingException("Monitoring has already been started"); + } + + CURRENT.set(rootMonitor); + } + + public static void stopMonitoring() + { + try + { + Monitor current = CURRENT.get(); + if (current == null) + { + OM.LOG.warn("Monitoring has not been started"); + } + else + { + if (current.getParent() != null) + { + OM.LOG.warn("Illegal monitor nesting"); + } + + current.done(); + } + } + finally + { + CURRENT.set(null); + } + } + + public static OMMonitor begin(int totalWork, String task) + { + Monitor current = CURRENT.get(); + if (current == null) + { + return NullMonitor.INSTANCE; + } + + if (current.hasBegun()) + { + throw new MonitorAlreadyBegunException("Monitor has already begun"); + } + + current.begin(totalWork, task); + return current; + } + + static void checkMonitor(Monitor monitor) throws MonitorException + { + Monitor current = CURRENT.get(); + if (current != monitor) + { + throw new IllegalMonitorNestingException("Illegal monitor nesting\n" + // + "Current monitor stack:\n" + current.dump() + // + "Used monitor stack:\n" + monitor.dump()); + } + + monitor.checkCanceled(); + } + + static void setMonitor(Monitor monitor) + { + CURRENT.set(monitor); + } +} diff --git a/plugins/org.eclipse.net4j.util/monitor/impl/Monitor.java b/plugins/org.eclipse.net4j.util/monitor/impl/Monitor.java new file mode 100644 index 0000000000..b59bbd6a0b --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/impl/Monitor.java @@ -0,0 +1,291 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.util.om.monitor; + +import org.eclipse.net4j.internal.util.bundle.OM; +import org.eclipse.net4j.util.om.monitor.MonitorCanceledException; +import org.eclipse.net4j.util.om.monitor.MonitorNotBegunException; +import org.eclipse.net4j.util.om.monitor.OMMonitor; +import org.eclipse.net4j.util.om.monitor.OMSubMonitor; +import org.eclipse.net4j.util.om.monitor.TotalWorkExceededException; +import org.eclipse.net4j.util.om.trace.ContextTracer; + +/** + * @author Eike Stepper + */ +public abstract class Monitor implements OMMonitor, OMSubMonitor +{ + private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, Monitor.class); + + private static final int UNINITIALIZED = 0; + + private Monitor parent; + + private int workFromParent; + + private int totalWork = UNINITIALIZED; + + private int work; + + private String task; + + private Monitor child; + + private boolean canceled; + + public Monitor(Monitor parent, int workFromParent) + { + this.parent = parent; + this.workFromParent = workFromParent; + } + + public boolean isCanceled() + { + return canceled; + } + + public void checkCanceled() throws MonitorCanceledException + { + if (canceled) + { + throw new MonitorCanceledException(); + } + } + + public void setCanceled(boolean canceled) + { + this.canceled = canceled; + if (child != null) + { + child.setCanceled(canceled); + } + } + + public String getTask() + { + return task; + } + + public void setTask(String task) + { + this.task = task; + taskChanged(task, 0); + } + + public int getTotalWork() + { + return totalWork; + } + + public boolean hasBegun() + { + return totalWork != UNINITIALIZED; + } + + public void message(String msg) + { + if (msg != null) + { + message(msg, 0); + } + } + + public void worked(int work, String msg) throws MonitorCanceledException + { + MON.checkMonitor(this); + checkWork(work); + + this.work += work; + message(msg); + } + + public void worked(int work) throws MonitorCanceledException + { + worked(work, null); + } + + public void worked(String msg) throws MonitorCanceledException + { + worked(1, msg); + } + + public void worked() throws MonitorCanceledException + { + worked(1, null); + } + + public void fork(int workFromParent, Runnable runnable, String msg) throws MonitorCanceledException + { + MON.checkMonitor(this); + checkWork(workFromParent); + + child = subMonitor(workFromParent); + MON.setMonitor(child); + + try + { + runnable.run(); + } + finally + { + MON.checkMonitor(child); + MON.setMonitor(this); + child.done(); + child = null; + } + + work += workFromParent; + message(msg); + } + + public void fork(int workFromParent, Runnable runnable) throws MonitorCanceledException + { + fork(workFromParent, runnable, null); + } + + public void fork(Runnable runnable, String msg) throws MonitorCanceledException + { + fork(1, runnable, msg); + } + + public void fork(Runnable runnable) throws MonitorCanceledException + { + fork(1, runnable, null); + } + + public OMSubMonitor fork(int workFromParent) throws MonitorCanceledException + { + MON.checkMonitor(this); + checkWork(workFromParent); + + child = subMonitor(workFromParent); + MON.setMonitor(child); + return child; + } + + public OMSubMonitor fork() throws MonitorCanceledException + { + return fork(1); + } + + public void join(String msg) throws MonitorCanceledException + { + MON.checkMonitor(this); + MON.setMonitor(parent); + parent.setChild(null); + parent.message(msg); + done(); + } + + public void join() throws MonitorCanceledException + { + join(null); + } + + protected Monitor getChild() + { + return child; + } + + protected void setChild(Monitor child) + { + this.child = child; + } + + protected Monitor getParent() + { + return parent; + } + + protected int getWorkFromParent() + { + return workFromParent; + } + + protected String dump() + { + StringBuilder builder = new StringBuilder(); + dump(builder); + return builder.toString(); + } + + protected void begin(int totalWork, String task) throws MonitorCanceledException + { + checkCanceled(); + this.totalWork = totalWork; + if (task != null) + { + setTask(task); + } + } + + protected void done() + { + } + + protected void taskChanged(String task, int level) + { + if (parent != null) + { + parent.taskChanged(task, level + 1); + } + else + { + trace(task, level, true); + } + } + + protected void message(String msg, int level) + { + if (parent != null) + { + parent.message(msg, level + 1); + } + else + { + trace(msg, level, false); + } + } + + protected void trace(String msg, int level, boolean isTask) + { + if (TRACER.isEnabled()) + { + TRACER.trace(msg); + } + } + + protected void checkWork(int work) + { + if (!hasBegun()) + { + throw new MonitorNotBegunException("Monitor has not begun"); + } + + if (totalWork != UNKNOWN && this.work + work > totalWork) + { + throw new TotalWorkExceededException(("Work of " + work + " exceeded total work of " + totalWork)); + } + } + + protected void dump(StringBuilder builder) + { + builder.append(" "); + builder.append(task); + builder.append("\n"); + if (parent != null) + { + parent.dump(builder); + } + } + + protected abstract Monitor subMonitor(int workFromParent); +} diff --git a/plugins/org.eclipse.net4j.util/monitor/impl/NullMonitor.java b/plugins/org.eclipse.net4j.util/monitor/impl/NullMonitor.java new file mode 100644 index 0000000000..1390d8f36d --- /dev/null +++ b/plugins/org.eclipse.net4j.util/monitor/impl/NullMonitor.java @@ -0,0 +1,120 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.util.om.monitor; + +import org.eclipse.net4j.util.om.monitor.MonitorCanceledException; +import org.eclipse.net4j.util.om.monitor.OMMonitor; +import org.eclipse.net4j.util.om.monitor.OMSubMonitor; + +/** + * @author Eike Stepper + */ +public class NullMonitor implements OMMonitor, OMSubMonitor +{ + public static final NullMonitor INSTANCE = new NullMonitor(); + + private boolean canceled; + + private NullMonitor() + { + } + + public void checkCanceled() throws MonitorCanceledException + { + if (canceled) + { + throw new MonitorCanceledException(); + } + } + + public boolean isCanceled() + { + return canceled; + } + + public void setCanceled(boolean canceled) + { + this.canceled = canceled; + } + + public void join() + { + } + + public void join(String msg) + { + } + + public OMSubMonitor fork() + { + return this; + } + + public void fork(int workFromParent, Runnable runnable, String msg) + { + } + + public void fork(int workFromParent, Runnable runnable) + { + } + + public OMSubMonitor fork(int workFromParent) + { + return this; + } + + public void fork(Runnable runnable, String msg) + { + } + + public void fork(Runnable runnable) + { + } + + public String getTask() + { + return null; + } + + public int getTotalWork() + { + return 0; + } + + public boolean hasBegun() + { + return true; + } + + public void message(String msg) + { + } + + public void setTask(String task) + { + } + + public void worked() + { + } + + public void worked(int work, String msg) + { + } + + public void worked(int work) + { + } + + public void worked(String msg) + { + } +} |