|
From: <pat...@us...> - 2010-08-03 19:15:32
|
Revision: 1108
http://cishell.svn.sourceforge.net/cishell/?rev=1108&view=rev
Author: pataphil
Date: 2010-08-03 19:15:24 +0000 (Tue, 03 Aug 2010)
Log Message:
-----------
* Cleaned up and refactored a bit of code.
* Introduced the notion of the Algorithm Invocation Service, though it's commented out.
* Reviewed by Micah.
Modified Paths:
--------------
trunk/core/org.cishell.framework/src/org/cishell/app/service/scheduler/SchedulerService.java
trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmCreationCanceledException.java
trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmCreationFailedException.java
trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmFactory.java
trunk/core/org.cishell.framework/src/org/cishell/framework/data/BasicData.java
trunk/core/org.cishell.framework/src/org/cishell/framework/data/Data.java
trunk/core/org.cishell.reference/.settings/org.eclipse.jdt.core.prefs
trunk/core/org.cishell.reference/META-INF/MANIFEST.MF
trunk/core/org.cishell.reference/src/org/cishell/reference/app/service/scheduler/SchedulerServiceImpl.java
trunk/core/org.cishell.reference.services/src/org/cishell/reference/services/Activator.java
Added Paths:
-----------
trunk/core/org.cishell.framework/src/org/cishell/service/algorithminvocation/
trunk/core/org.cishell.framework/src/org/cishell/service/algorithminvocation/AlgorithmInvocationService.java
trunk/core/org.cishell.reference/src/org/cishell/reference/app/service/algorithminvocation/
trunk/core/org.cishell.reference/src/org/cishell/reference/app/service/algorithminvocation/AlgorithmInvocationServiceImpl.java
Modified: trunk/core/org.cishell.framework/src/org/cishell/app/service/scheduler/SchedulerService.java
===================================================================
--- trunk/core/org.cishell.framework/src/org/cishell/app/service/scheduler/SchedulerService.java 2010-08-03 18:35:25 UTC (rev 1107)
+++ trunk/core/org.cishell.framework/src/org/cishell/app/service/scheduler/SchedulerService.java 2010-08-03 19:15:24 UTC (rev 1108)
@@ -28,7 +28,6 @@
* service as it is not guaranteed to be available like the standard CIShell
* services are.
*
- * @author Bruce Herr (bh...@bh...)
*/
public interface SchedulerService {
/**
@@ -37,10 +36,9 @@
* enough resources to fulfill the request.
*
* @param algorithm The algorithm to be run
- * @param ref A reference to the Algorithm's associated service, may
- * be <code>null</code>
+ * @param reference A reference to the Algorithm's associated service, may be <code>null</code>
*/
- public void runNow(Algorithm algorithm, ServiceReference ref);
+ public void runNow(Algorithm algorithm, ServiceReference reference);
/**
* Schedules an Algorithm to be run when convenient. This schedules an
@@ -48,10 +46,9 @@
* Algorithms will be scheduled in this way.
*
* @param algorithm The Algorithm to be scheduled
- * @param ref A reference to the Algorithm's associated service, may
- * be <code>null</code>
+ * @param reference A reference to the Algorithm's associated service, may be <code>null</code>
*/
- public void schedule(Algorithm algorithm, ServiceReference ref);
+ public void schedule(Algorithm algorithm, ServiceReference reference);
/**
* Schedules an Algorithm to be run at a specific time. The Algorithm will
@@ -60,11 +57,10 @@
* resources to fulfill the request.
*
* @param algorithm The Algorithm to be scheduled
- * @param ref A reference to the Algorithm's associated service, may
- * be <code>null</code>
+ * @param reference A reference to the Algorithm's associated service, may be <code>null</code>
* @param time What time this Algorithm should be run
*/
- public void schedule(Algorithm algorithm, ServiceReference ref, Calendar time);
+ public void schedule(Algorithm algorithm, ServiceReference reference, Calendar time);
/**
* Reschedules an already scheduled Algorithm to be run at a different time.
Modified: trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmCreationCanceledException.java
===================================================================
--- trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmCreationCanceledException.java 2010-08-03 18:35:25 UTC (rev 1107)
+++ trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmCreationCanceledException.java 2010-08-03 19:15:24 UTC (rev 1108)
@@ -1,6 +1,5 @@
package org.cishell.framework.algorithm;
-// TODO: Make this a regular Exception (not RuntimeException).
public class AlgorithmCreationCanceledException extends RuntimeException {
private static final long serialVersionUID = 9017277008277139930L;
Modified: trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmCreationFailedException.java
===================================================================
--- trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmCreationFailedException.java 2010-08-03 18:35:25 UTC (rev 1107)
+++ trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmCreationFailedException.java 2010-08-03 19:15:24 UTC (rev 1108)
@@ -1,6 +1,5 @@
package org.cishell.framework.algorithm;
-// TODO: Make this a regular Exception (not RuntimeException).
public class AlgorithmCreationFailedException extends RuntimeException {
private static final long serialVersionUID = 9017277008277139930L;
Modified: trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmFactory.java
===================================================================
--- trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmFactory.java 2010-08-03 18:35:25 UTC (rev 1107)
+++ trunk/core/org.cishell.framework/src/org/cishell/framework/algorithm/AlgorithmFactory.java 2010-08-03 19:15:24 UTC (rev 1108)
@@ -32,7 +32,6 @@
* CIShell Specification 1.0</a> for documentation on the full requirements for
* algorithm creation.
*
- * @author Bruce Herr (bh...@bh...)
*/
public interface AlgorithmFactory {
@@ -47,7 +46,7 @@
* @param parameters A set of key-value pairs that were created based on
* the associated input specification published to the
* {@link MetaTypeService}
- * @param context The context by which the Algorithm can gain access to
+ * @param ciShellContext The context by which the Algorithm can gain access to
* standard CIShell services
* @return An <code>Algorithm</code> primed for execution
*/
@@ -55,5 +54,5 @@
* the signature, and update the entire code base to conform to it.
*/
public Algorithm createAlgorithm(
- Data[] data, Dictionary<String, Object> parameters, CIShellContext context);
+ Data[] data, Dictionary<String, Object> parameters, CIShellContext ciShellContext);
}
Modified: trunk/core/org.cishell.framework/src/org/cishell/framework/data/BasicData.java
===================================================================
--- trunk/core/org.cishell.framework/src/org/cishell/framework/data/BasicData.java 2010-08-03 18:35:25 UTC (rev 1107)
+++ trunk/core/org.cishell.framework/src/org/cishell/framework/data/BasicData.java 2010-08-03 19:15:24 UTC (rev 1108)
@@ -24,7 +24,7 @@
* @author Bruce Herr (bh...@bh...)
*/
public class BasicData implements Data {
- private Dictionary properties;
+ private Dictionary<String, Object> properties;
private Object data;
private String format;
@@ -35,7 +35,7 @@
* @param data The data being wrapped
*/
public BasicData(Object data, String format) {
- this(new Hashtable(), data, format);
+ this(new Hashtable<String, Object>(), data, format);
}
/**
@@ -44,7 +44,7 @@
* @param properties The metadata about the data
* @param data The data being wrapped
*/
- public BasicData(Dictionary properties, Object data, String format) {
+ public BasicData(Dictionary<String, Object> properties, Object data, String format) {
this.properties = properties;
this.data = data;
this.format = format;
@@ -60,7 +60,7 @@
/**
* @see org.cishell.framework.data.Data#getMetadata()
*/
- public Dictionary getMetadata() {
+ public Dictionary<String, Object> getMetadata() {
return properties;
}
Modified: trunk/core/org.cishell.framework/src/org/cishell/framework/data/Data.java
===================================================================
--- trunk/core/org.cishell.framework/src/org/cishell/framework/data/Data.java 2010-08-03 18:35:25 UTC (rev 1107)
+++ trunk/core/org.cishell.framework/src/org/cishell/framework/data/Data.java 2010-08-03 19:15:24 UTC (rev 1108)
@@ -20,8 +20,7 @@
* A class that contains data, its format, and its metadata. This class is used
* to pass data between algorithms and is what algorithms optionally create when
* executed.
- *
- * @author Bruce Herr (bh...@bh...)
+ *
*/
public interface Data {
/**
@@ -30,7 +29,7 @@
*
* @return The data's metadata
*/
- public Dictionary getMetadata();
+ public Dictionary<String, Object> getMetadata();
/**
* Returns the data stored in this Data object
Added: trunk/core/org.cishell.framework/src/org/cishell/service/algorithminvocation/AlgorithmInvocationService.java
===================================================================
--- trunk/core/org.cishell.framework/src/org/cishell/service/algorithminvocation/AlgorithmInvocationService.java (rev 0)
+++ trunk/core/org.cishell.framework/src/org/cishell/service/algorithminvocation/AlgorithmInvocationService.java 2010-08-03 19:15:24 UTC (rev 1108)
@@ -0,0 +1,126 @@
+
+/* TODO: When we have time, we should talk about the design of the Algorithm Invocation Service (if
+ * we even use that name in the end).
+ * It's somewhat apparent that there is a use for this service, but exactly how it fits into
+ * CIShell and all of the tools remains to be fully clarified.
+ * This is all commented out for now because the design/use need discussion.
+ */
+
+//package org.cishell.service.algorithminvocation;
+//
+//import java.util.Dictionary;
+//
+//import org.cishell.framework.CIShellContext;
+//import org.cishell.framework.algorithm.Algorithm;
+//import org.cishell.framework.algorithm.AlgorithmCanceledException;
+//import org.cishell.framework.algorithm.AlgorithmCreationCanceledException;
+//import org.cishell.framework.algorithm.AlgorithmCreationFailedException;
+//import org.cishell.framework.algorithm.AlgorithmExecutionException;
+//import org.cishell.framework.algorithm.AlgorithmFactory;
+//import org.cishell.framework.data.Data;
+//
+///**
+// * Provides the caller with various ways of creating algorithms, executing them, and
+// * gathering/mutating parameters.
+// * When creating an algorithm (from a factory), if the factory implements ParameterMutator,
+// * mutateParameters() will be called on it.
+// * All methods can optionally operate on a new thread, which is determined by shouldUseNewThread.
+// */
+//public interface AlgorithmInvocationService {
+// /**
+// * Uses factory to create an algorithm, presenting the user with a GUI for parameters.
+// */
+// public Algorithm createAlgorithm(
+// final AlgorithmFactory factory,
+// final Data[] data,
+// final CIShellContext ciShellContext,
+// boolean shouldUseNewThread)
+// throws AlgorithmCreationCanceledException, AlgorithmCreationFailedException;
+//
+// /**
+// * Uses factory to create an algorithm, using parameters (instead of presenting the user with a
+// * GUI for them).
+// */
+// public Algorithm createAlgorithm(
+// final AlgorithmFactory factory,
+// final Data[] data,
+// final Dictionary<String, Object> parameters,
+// final CIShellContext ciShellContext,
+// boolean shouldUseNewThread)
+// throws AlgorithmCreationCanceledException, AlgorithmCreationFailedException;
+//
+// /**
+// * Invokes algorithm, returning the Data[] result of algorithm.execute().
+// * If logExceptionThrown is true, any exception thrown will be logged to the
+// * default LogService.
+// * If displayRuntimeException is true, the stack trace of any exception thrown will be
+// * displayed in an error message box.
+// */
+// public Data[] invokeAlgorithm(
+// final Algorithm algorithm,
+// final boolean logExceptionThrown,
+// final boolean displayRuntimeException,
+// boolean shouldUseNewThread)
+// throws AlgorithmCanceledException, AlgorithmExecutionException;
+//
+// /**
+// * Invokes algorithm, assuming sensible defaults for inline algorithm execution (that is,
+// * not explicitly invoked from a menu/etc.), and return the Data[] result of
+// * algorithm.execute().
+// * Most likely wraps invokeAlgorithm().
+// */
+// public Data[] simpleInvokeAlgorithm(final Algorithm algorithm, Thread thread)
+// throws AlgorithmCanceledException, AlgorithmExecutionException;
+//
+// /**
+// * Given factory, presents the user with a GUI for parameters to use for creating and executing
+// * an algorithm.
+// * Most likely wraps createAlgorithm() and invokeAlgorithm().
+// */
+// public Data[] createAndInvokeAlgorithm(
+// final AlgorithmFactory factory,
+// final Data[] data,
+// final CIShellContext ciShellContext,
+// final boolean logExceptionThrown,
+// final boolean displayRuntimeException,
+// boolean shouldUseNewThread) throws
+// AlgorithmCreationCanceledException,
+// AlgorithmCreationFailedException,
+// AlgorithmCanceledException,
+// AlgorithmExecutionException;
+//
+// /**
+// * Given factory, uses parameters to create and execute an algorithm.
+// * Most likely wraps createAlgorithm() and invokeAlgorithm().
+// */
+// public Data[] createAndInvokeAlgorithm(
+// final AlgorithmFactory factory,
+// final Data[] data,
+// final Dictionary<String, Object> parameters,
+// final CIShellContext ciShellContext,
+// final boolean logExceptionThrown,
+// final boolean displayRuntimeException,
+// boolean shouldUseNewThread) throws
+// AlgorithmCreationCanceledException,
+// AlgorithmCreationFailedException,
+// AlgorithmCanceledException,
+// AlgorithmExecutionException;
+//
+// /**
+// * Given factory, uses parameters to create and execute an algorithm.
+// * Sensible defaults for inline algorithm execution (that is, not explicitly invoked from a
+// * menu/etc.) are used.
+// * Returns the Data[] result of algorithm.execute().
+// * Most likely wraps createAlgorithm() and simpleInvokeAlgorithm().
+// */
+// public Data[] simpleCreateAndInvokeAlgorithm(
+// final AlgorithmFactory factory,
+// final Data[] data,
+// final Dictionary<String, Object> parameters,
+// CIShellContext ciShellContext,
+// boolean shouldUseNewThread) throws
+// AlgorithmCreationCanceledException,
+// AlgorithmCreationFailedException,
+// AlgorithmCanceledException,
+// AlgorithmExecutionException;
+//}
\ No newline at end of file
Modified: trunk/core/org.cishell.reference/.settings/org.eclipse.jdt.core.prefs
===================================================================
--- trunk/core/org.cishell.reference/.settings/org.eclipse.jdt.core.prefs 2010-08-03 18:35:25 UTC (rev 1107)
+++ trunk/core/org.cishell.reference/.settings/org.eclipse.jdt.core.prefs 2010-08-03 19:15:24 UTC (rev 1108)
@@ -1,12 +1,12 @@
-#Mon Jul 27 15:45:33 EDT 2009
+#Wed Jul 21 20:42:37 EDT 2010
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
-org.eclipse.jdt.core.compiler.compliance=1.4
+org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
-org.eclipse.jdt.core.compiler.problem.assertIdentifier=warning
-org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning
-org.eclipse.jdt.core.compiler.source=1.3
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.5
Modified: trunk/core/org.cishell.reference/META-INF/MANIFEST.MF
===================================================================
--- trunk/core/org.cishell.reference/META-INF/MANIFEST.MF 2010-08-03 18:35:25 UTC (rev 1107)
+++ trunk/core/org.cishell.reference/META-INF/MANIFEST.MF 2010-08-03 19:15:24 UTC (rev 1108)
@@ -1,4 +1,3 @@
-Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: CIShell Reference Service Implementations
Bundle-SymbolicName: org.cishell.reference
@@ -14,9 +13,11 @@
org.osgi.service.log,
org.osgi.service.metatype;version="1.1.0",
org.osgi.service.prefs
-Export-Package: org.cishell.reference.app.service.datamanager,
+Export-Package: org.cishell.reference.app.service.algorithminvocation,
+ org.cishell.reference.app.service.datamanager,
org.cishell.reference.app.service.scheduler,
org.cishell.reference.service.conversion,
org.cishell.reference.service.metatype
Eclipse-LazyStart: true
Require-Bundle: edu.uci.ics.jung
+Bundle-RequiredExecutionEnvironment: J2SE-1.5
Added: trunk/core/org.cishell.reference/src/org/cishell/reference/app/service/algorithminvocation/AlgorithmInvocationServiceImpl.java
===================================================================
--- trunk/core/org.cishell.reference/src/org/cishell/reference/app/service/algorithminvocation/AlgorithmInvocationServiceImpl.java (rev 0)
+++ trunk/core/org.cishell.reference/src/org/cishell/reference/app/service/algorithminvocation/AlgorithmInvocationServiceImpl.java 2010-08-03 19:15:24 UTC (rev 1108)
@@ -0,0 +1,85 @@
+//package org.cishell.reference.app.service.algorithminvocation;
+//
+//import java.util.Dictionary;
+//import java.util.Hashtable;
+//
+//import org.cishell.framework.CIShellContext;
+//import org.cishell.framework.algorithm.Algorithm;
+//import org.cishell.framework.algorithm.AlgorithmCreationCanceledException;
+//import org.cishell.framework.algorithm.AlgorithmCreationFailedException;
+//import org.cishell.framework.algorithm.AlgorithmFactory;
+//import org.cishell.framework.data.Data;
+//import org.cishell.service.algorithminvocation.AlgorithmInvocationService;
+//import org.osgi.service.log.LogService;
+//
+//public class AlgorithmInvocationServiceImpl implements AlgorithmInvocationService {
+// private LogService logger;
+//
+// public AlgorithmInvocationServiceImpl(LogService logger) {
+// this.logger = logger;
+// }
+//
+// @SuppressWarnings("unchecked")
+// public Algorithm createAlgorithm(
+// final AlgorithmFactory factory,
+// final Data[] data,
+// final CIShellContext ciShellContext,
+// boolean shouldUseNewThread)
+// throws AlgorithmCreationCanceledException, AlgorithmCreationFailedException {
+// /* TODO: Refactor org.cishell.utilities into several plugins so there are no
+// * circular dependencies!
+// */
+//
+// final AlgorithmCreationCanceledException[] canceledException =
+// new AlgorithmCreationCanceledException[1];
+// final AlgorithmCreationFailedException[] failedException =
+// new AlgorithmCreationFailedException[1];
+// final Algorithm[] algorithm = new Algorithm[1];
+//
+// Runnable operator = new Runnable() {
+// public void run() {
+// /* TODO: Refactor algorithm creation code out of
+// * org.cishell.reference.gui.menumanager, and call it here.
+// */
+//
+// try {
+// // TODO: readFromMetadataFile
+// Dictionary<String, Object> parameters = new Hashtable<String, Object>();
+// // TODO: mutateParameters
+// Dictionary<String, Object> mutatedParameters = parameters;
+// // TODO: Invoke GUI builder service, getting user-entered parameters.
+// Dictionary<String, Object> userEnteredParameters = mutatedParameters;
+//
+// algorithm[0] =
+// factory.createAlgorithm(data, userEnteredParameters, ciShellContext);
+// } catch (AlgorithmCreationCanceledException e) {
+// canceledException[0] = e;
+// } catch (AlgorithmCreationFailedException e) {
+// failedException[0] = e;
+// }
+// }
+// };
+//
+// if (shouldUseNewThread) {
+// new Thread(operator).start();
+// } else {
+// operator.run();
+// }
+//
+// return algorithm[0];
+// }
+//
+// public Algorithm createAlgorithm(
+// final AlgorithmFactory factory,
+// final Data[] data,
+// final Dictionary<String, Object> parameters,
+// final CIShellContext ciShellContext,
+// boolean shouldUseNewThread)
+// throws AlgorithmCreationCanceledException, AlgorithmCreationFailedException {
+// final AlgorithmCreationCanceledException[] canceledException =
+// new AlgorithmCreationCanceledException[1];
+// final AlgorithmCreationFailedException[] failedException =
+// new AlgorithmCreationFailedException[1];
+//
+// }
+//}
\ No newline at end of file
Modified: trunk/core/org.cishell.reference/src/org/cishell/reference/app/service/scheduler/SchedulerServiceImpl.java
===================================================================
--- trunk/core/org.cishell.reference/src/org/cishell/reference/app/service/scheduler/SchedulerServiceImpl.java 2010-08-03 18:35:25 UTC (rev 1107)
+++ trunk/core/org.cishell.reference/src/org/cishell/reference/app/service/scheduler/SchedulerServiceImpl.java 2010-08-03 19:15:24 UTC (rev 1108)
@@ -30,7 +30,7 @@
import org.cishell.app.service.scheduler.SchedulerService;
import org.cishell.framework.algorithm.Algorithm;
import org.cishell.framework.data.Data;
-import org.cishell.reference.app.service.scheduler.AlgorithmTask.STATE;
+import org.cishell.reference.app.service.scheduler.AlgorithmTask.AlgorithmState;
import org.osgi.framework.ServiceReference;
/**
@@ -74,156 +74,150 @@
* algorithm, consider using Quartz http://www.opensymphony.com/quartz/ </li>
* </ul>
*
- * @author Shashikant Penumarthy
- * @author Bruce Herr (bh...@bh...)
*/
-// May 8, 2006 7:05:32 PM Shashikant Penumarthy: Initial Implementation
-// July 19, 2006 10:30:00 AM Bruce Herr: Ported to new CIShell
public class SchedulerServiceImpl implements SchedulerService {
/**
* This timer runs the algorithm scheduling task.
*/
- private Timer _schedulerTimer;
+ private Timer schedulerTimer;
/**
* The task which schedules algorithms to run on the _algRunningTimer.
*/
- private AlgSchedulerTask _algSchedulerTask;
+ private AlgorithmSchedulerTask algorithmSchedulerTask;
/**
* Convenience object for informing all the schedulers.
*/
- private SchedulerListenerInformer _schedulerListenerInformer;
+ private SchedulerListenerInformer schedulerListenerInformer;
+
+ private boolean isShutDown = true;
public SchedulerServiceImpl() {
- _initialize();
+ initialize();
}
- public SchedulerServiceImpl(int maxSimultaneousAlgsLimit) {
+ public SchedulerServiceImpl(int maxSimultaneousAlgorithm) {
this();
- _algSchedulerTask.setMaxSimultaneousAlgs(maxSimultaneousAlgsLimit);
- _isShutDown = false;
+ this.algorithmSchedulerTask.setMaxSimultaneousAlgorithms(maxSimultaneousAlgorithm);
+ this.isShutDown = false;
}
- public synchronized final void setMaxSimultaneousAlgs(int max) {
- _algSchedulerTask.setMaxSimultaneousAlgs(max);
+ public synchronized final void setMaxSimultaneousAlgorithms(int max) {
+ this.algorithmSchedulerTask.setMaxSimultaneousAlgorithms(max);
}
- private final void _initialize() {
- _schedulerTimer = new Timer(true);
- _schedulerListenerInformer = new SchedulerListenerInformer();
- _algSchedulerTask = new AlgSchedulerTask(_schedulerListenerInformer);
- _schedulerTimer.schedule(_algSchedulerTask, 0L, 500L);
+ private final void initialize() {
+ this.schedulerTimer = new Timer(true);
+ this.schedulerListenerInformer = new SchedulerListenerInformer();
+ this.algorithmSchedulerTask = new AlgorithmSchedulerTask(this.schedulerListenerInformer);
+ this.schedulerTimer.schedule(this.algorithmSchedulerTask, 0L, 500L);
}
public synchronized final void shutDown() {
- _algSchedulerTask.cancel();
- _schedulerTimer.cancel();
- _isShutDown = true;
+ this.algorithmSchedulerTask.cancel();
+ this.schedulerTimer.cancel();
+ this.isShutDown = true;
}
public final boolean isEmpty() {
- return _algSchedulerTask.isEmpty();
+ return this.algorithmSchedulerTask.isEmpty();
}
public final boolean isRunning() {
- return _algSchedulerTask.isRunning();
+ return this.algorithmSchedulerTask.isRunning();
}
public final int numRunning() {
- return _algSchedulerTask.numRunning();
+ return this.algorithmSchedulerTask.numRunning();
}
- private boolean _isShutDown = true;
-
public final boolean isShutDown() {
- return _isShutDown;
+ return this.isShutDown;
}
public boolean reschedule(Algorithm algorithm, Calendar newTime) {
- // Shaky method. Ideally this is done at a higher level. But still, here
- // goes...
- ServiceReference ref = _algSchedulerTask.getServiceReference(algorithm);
- boolean status = false;
+ // Shaky method. Ideally this is done at a higher level. But still, here goes...
+ ServiceReference reference = this.algorithmSchedulerTask.getServiceReference(algorithm);
+ boolean canReschedule = false;
+
try {
- STATE algState = _algSchedulerTask.getAlgorithmState(algorithm);
+ AlgorithmState algorithmState =
+ this.algorithmSchedulerTask.getAlgorithmState(algorithm);
- // Cannot reschedule running algs
- if (algState.equals(STATE.RUNNING)) {
- status = false;
+ // Cannot reschedule running algorithms.
+ if (algorithmState.equals(AlgorithmState.RUNNING)) {
+ canReschedule = false;
+ } else if (algorithmState.equals(AlgorithmState.STOPPED)) {
+ this.algorithmSchedulerTask.purgeFinished();
+ this.algorithmSchedulerTask.schedule(algorithm, reference, newTime);
+ canReschedule = true;
+ } else if (algorithmState.equals(AlgorithmState.NEW)) {
+ this.algorithmSchedulerTask.cancel(algorithm);
+ this.algorithmSchedulerTask.schedule(algorithm, reference, newTime);
+ } else {
+ throw new IllegalStateException("Encountered an invalid state: " + algorithmState);
}
- else if (algState.equals(STATE.STOPPED)) {
- _algSchedulerTask.purgeFinished();
- _algSchedulerTask.schedule(algorithm, ref, newTime);
- status = true;
- }
- else if (algState.equals(STATE.NEW)) {
- _algSchedulerTask.cancel(algorithm);
- _algSchedulerTask.schedule(algorithm, ref, newTime);
- }
- else {
- throw new IllegalStateException(
- "Encountered an invalid state: " + algState);
- }
- } catch (NoSuchElementException nsee) {
- _algSchedulerTask.schedule(algorithm, ref, newTime);
- status = true;
+ } catch (NoSuchElementException e) {
+ this.algorithmSchedulerTask.schedule(algorithm, reference, newTime);
+ canReschedule = true;
}
- return status;
+ return canReschedule;
}
- public void runNow(Algorithm algorithm, ServiceReference ref) {
+ public void runNow(Algorithm algorithm, ServiceReference reference) {
// There is currently no difference between this one and
- // schedule(Algorithm, ref).
- schedule(algorithm, ref);
+ // schedule(Algorithm, reference).
+ schedule(algorithm, reference);
}
- public void schedule(Algorithm algorithm, ServiceReference ref) {
- schedule(algorithm, ref, Calendar.getInstance());
+ public void schedule(Algorithm algorithm, ServiceReference reference) {
+ schedule(algorithm, reference, Calendar.getInstance());
}
- public void schedule(Algorithm algorithm, ServiceReference ref, Calendar time) {
- _algSchedulerTask.schedule(algorithm, ref, time);
+ public void schedule(Algorithm algorithm, ServiceReference reference, Calendar time) {
+ this.algorithmSchedulerTask.schedule(algorithm, reference, time);
}
public boolean unschedule(Algorithm algorithm) {
- return _algSchedulerTask.cancel(algorithm);
+ return this.algorithmSchedulerTask.cancel(algorithm);
}
public void addSchedulerListener(SchedulerListener listener) {
- _schedulerListenerInformer.addSchedulerListener(listener);
+ this.schedulerListenerInformer.addSchedulerListener(listener);
}
public void removeSchedulerListener(SchedulerListener listener) {
- _schedulerListenerInformer.removeSchedulerListener(listener);
+ this.schedulerListenerInformer.removeSchedulerListener(listener);
}
public synchronized void clearSchedule() {
- _algSchedulerTask.cancel();
- _schedulerTimer.cancel();
+ this.algorithmSchedulerTask.cancel();
+ this.schedulerTimer.cancel();
- _schedulerTimer = new Timer(true);
- _algSchedulerTask = new AlgSchedulerTask(_schedulerListenerInformer);
- _schedulerTimer.schedule(_algSchedulerTask, 0L, 500L);
+ this.schedulerTimer = new Timer(true);
+ this.algorithmSchedulerTask = new AlgorithmSchedulerTask(this.schedulerListenerInformer);
+ // TODO: Make constants for these magic numbers.
+ this.schedulerTimer.schedule(this.algorithmSchedulerTask, 0L, 500L);
- _schedulerListenerInformer.schedulerCleared();
+ this.schedulerListenerInformer.schedulerCleared();
}
public Algorithm[] getScheduledAlgorithms() {
- return _algSchedulerTask.getScheduledAlgorithms();
+ return this.algorithmSchedulerTask.getScheduledAlgorithms();
}
public Calendar getScheduledTime(Algorithm algorithm) {
- return _algSchedulerTask.getScheduledTime(algorithm);
+ return this.algorithmSchedulerTask.getScheduledTime(algorithm);
}
public ServiceReference getServiceReference(Algorithm algorithm) {
- return _algSchedulerTask.getServiceReference(algorithm);
+ return this.algorithmSchedulerTask.getServiceReference(algorithm);
}
public void setRunning(boolean isRunning) {
- _algSchedulerTask.setRunning(isRunning);
- _schedulerListenerInformer.schedulerRunStateChanged(isRunning);
+ this.algorithmSchedulerTask.setRunning(isRunning);
+ this.schedulerListenerInformer.schedulerRunStateChanged(isRunning);
}
}
@@ -238,133 +232,124 @@
* @author Team IVC
*/
class SchedulerListenerInformer implements SchedulerListener {
+ private List<SchedulerListener> schedulerListeners;
- private List _schedulerListeners;
-
public SchedulerListenerInformer() {
- _schedulerListeners = new ArrayList();
+ this.schedulerListeners = new ArrayList<SchedulerListener>();
}
public void addSchedulerListener(SchedulerListener listener) {
- _schedulerListeners.add(listener);
+ this.schedulerListeners.add(listener);
}
public void removeSchedulerListener(SchedulerListener listener) {
- _schedulerListeners.remove(listener);
+ this.schedulerListeners.remove(listener);
}
public void algorithmScheduled(Algorithm algorithm, Calendar time) {
- for (Iterator iter = _schedulerListeners.iterator() ; iter.hasNext() ; ) {
- SchedulerListener sl = (SchedulerListener) iter.next() ;
- sl.algorithmScheduled(algorithm, time);
+ for (SchedulerListener schedulerListener : this.schedulerListeners) {
+ schedulerListener.algorithmScheduled(algorithm, time);
}
}
public synchronized void algorithmStarted(Algorithm algorithm) {
- for (Iterator iter = _schedulerListeners.iterator() ; iter.hasNext() ; ) {
- SchedulerListener sl = (SchedulerListener) iter.next() ;
- sl.algorithmStarted(algorithm);
+ for (SchedulerListener schedulerListener : this.schedulerListeners) {
+ schedulerListener.algorithmStarted(algorithm);
}
}
public void algorithmError(Algorithm algorithm, Throwable error) {
- for (Iterator iter = _schedulerListeners.iterator() ; iter.hasNext() ; ) {
- SchedulerListener sl = (SchedulerListener) iter.next() ;
- sl.algorithmError(algorithm, error);
+ for (SchedulerListener schedulerListener : this.schedulerListeners) {
+ schedulerListener.algorithmError(algorithm, error);
}
}
public void algorithmFinished(Algorithm algorithm, Data[] createdDM) {
- for (Iterator iter = _schedulerListeners.iterator() ; iter.hasNext() ; ) {
- SchedulerListener sl = (SchedulerListener) iter.next() ;
- sl.algorithmFinished(algorithm, createdDM);
+ for (SchedulerListener schedulerListener : this.schedulerListeners) {
+ schedulerListener.algorithmFinished(algorithm, createdDM);
}
}
public void algorithmRescheduled(Algorithm algorithm, Calendar time) {
- for (Iterator iter = _schedulerListeners.iterator() ; iter.hasNext() ; ) {
- SchedulerListener sl = (SchedulerListener) iter.next() ;
- sl.algorithmRescheduled(algorithm, time);
+ for (SchedulerListener schedulerListener : this.schedulerListeners) {
+ schedulerListener.algorithmRescheduled(algorithm, time);
}
}
public void algorithmUnscheduled(Algorithm algorithm) {
- for (Iterator iter = _schedulerListeners.iterator() ; iter.hasNext() ; ) {
- SchedulerListener sl = (SchedulerListener) iter.next() ;
- sl.algorithmUnscheduled(algorithm);
+ for (SchedulerListener schedulerListener : this.schedulerListeners) {
+ schedulerListener.algorithmUnscheduled(algorithm);
}
}
public void schedulerCleared() {
- for (Iterator iter = _schedulerListeners.iterator() ; iter.hasNext() ; ) {
- SchedulerListener sl = (SchedulerListener) iter.next() ;
- sl.schedulerCleared();
+ for (SchedulerListener schedulerListener : this.schedulerListeners) {
+ schedulerListener.schedulerCleared();
}
}
public void schedulerRunStateChanged(boolean isRunning) {
- for (Iterator iter = _schedulerListeners.iterator() ; iter.hasNext() ; ) {
- SchedulerListener sl = (SchedulerListener) iter.next() ;
- sl.schedulerRunStateChanged(isRunning);
+ for (SchedulerListener schedulerListener : this.schedulerListeners) {
+ schedulerListener.schedulerRunStateChanged(isRunning);
}
}
}
-class AlgSchedulerTask extends TimerTask implements SchedulerListener {
+class AlgorithmSchedulerTask extends TimerTask implements SchedulerListener {
+ public static final int AS_MANY_SIMULTANEOUS_ALGORITHMS_AS_NEEDED = -1;
+ private Map<Algorithm, AlgorithmTask> tasksByAlgorithms;
+ private Map<Algorithm, ServiceReference> serviceReferencesByAlgorithms;
+ private volatile boolean isRunning = true;
+ private volatile int runningTaskCount = 0;
+ private SchedulerListener schedulerListener;
+ private int maxSimultaneousAlgorithms = AS_MANY_SIMULTANEOUS_ALGORITHMS_AS_NEEDED;
- private Map _algMap;
- private Map _algServiceMap;
- private volatile boolean _running = true;
-
- // Default allow as many as needed
- private int _maxSimultaneousAlgs = -1;
-
/**
* Maximum number of algorithms allowed to run simultaneously. This value
* can be changed at runtime without any problems. Negative values are
* interpreted to mean 'no limit'.
*
* @param max
- * The maximum number of algorithms that can be simultaneously
- * run.
+ * The maximum number of algorithms that can be simultaneously run.
*/
- public synchronized final void setMaxSimultaneousAlgs(final int max) {
- if (max < -1)
- this._maxSimultaneousAlgs = -1;
- else
- this._maxSimultaneousAlgs = max;
+ public synchronized final void setMaxSimultaneousAlgorithms(final int max) {
+ if (max < -1) {
+ this.maxSimultaneousAlgorithms = AS_MANY_SIMULTANEOUS_ALGORITHMS_AS_NEEDED;
+ } else {
+ this.maxSimultaneousAlgorithms = max;
+ }
}
public synchronized Algorithm[] getScheduledAlgorithms() {
- return (Algorithm[]) _algMap.keySet().toArray(new Algorithm[0]);
+ return this.tasksByAlgorithms.keySet().toArray(new Algorithm[0]);
}
public synchronized final boolean isEmpty() {
- return _algMap.size() == 0;
+ return this.tasksByAlgorithms.size() == 0;
}
public synchronized final int numRunning() {
- return _numRunning;
+ return this.runningTaskCount;
}
- private SchedulerListener _schedulerListener;
-
- public AlgSchedulerTask(SchedulerListener listener) {
- _algMap = Collections.synchronizedMap(new HashMap());
- _algServiceMap = new HashMap();
- setSchedulerListener(listener);
+ public AlgorithmSchedulerTask(SchedulerListener listener) {
+ this.tasksByAlgorithms =
+ Collections.synchronizedMap(new HashMap<Algorithm, AlgorithmTask>());
+ this.serviceReferencesByAlgorithms = new HashMap<Algorithm, ServiceReference>();
+ this.setSchedulerListener(listener);
}
public synchronized final void setSchedulerListener(SchedulerListener listener) {
- _schedulerListener = listener;
+ this.schedulerListener = listener;
}
public final ServiceReference getServiceReference(Algorithm algorithm) {
- return (ServiceReference) _algServiceMap.get(algorithm);
+ return this.serviceReferencesByAlgorithms.get(algorithm);
}
public synchronized final Calendar getScheduledTime(Algorithm algorithm) {
- AlgorithmTask task = (AlgorithmTask)_algMap.get(algorithm);
+ AlgorithmTask task = this.tasksByAlgorithms.get(algorithm);
+
if (task != null) {
return task.getScheduledTime();
} else {
@@ -372,10 +357,13 @@
}
}
- public synchronized final boolean cancel(Algorithm alg) {
- AlgorithmTask task = (AlgorithmTask) this._algMap.get(alg);
- if (task == null)
+ public synchronized final boolean cancel(Algorithm algorithm) {
+ AlgorithmTask task = this.tasksByAlgorithms.get(algorithm);
+
+ if (task == null) {
return false;
+ }
+
// The algorithm will run till the end and
// then stop so there's no real way to cancel running algorithms.
// Clients should always check the state of an algorithm before trying
@@ -384,23 +372,23 @@
}
public synchronized final void schedule(Algorithm alg, ServiceReference ref, Calendar time) {
- AlgorithmTask task = (AlgorithmTask) this._algMap.get(alg);
+ AlgorithmTask task = this.tasksByAlgorithms.get(alg);
// If alg already exists, do some checks...
if (task != null) {
- STATE state = task.getState();
+ AlgorithmState state = task.getState();
// If its still running, we can't schedule it again.
- if (state.equals(STATE.RUNNING)) {
+ if (state.equals(AlgorithmState.RUNNING)) {
throw new RuntimeException(
"Cannot schedule running algorithm. Check state of algorithm first.");
}
// If its new or waiting to run, we refuse to schedule it to force
// user to explicitly
// cancel and reschedule.
- else if (state.equals(STATE.NEW)) {
+ else if (state.equals(AlgorithmState.NEW)) {
throw new RuntimeException(
"Algorithm is already scheduled to run. Cancel existing schedule first.");
}
- else if (state.equals(STATE.STOPPED)) {
+ else if (state.equals(AlgorithmState.STOPPED)) {
// If it was stopped but not cleaned up yet, clean it up
purgeFinished();
}
@@ -414,21 +402,21 @@
}
public synchronized final int getMaxSimultaneousAlgs() {
- return this._maxSimultaneousAlgs;
+ return this.maxSimultaneousAlgorithms;
}
public synchronized final void registerAlgorithmTask(Algorithm algorithm, AlgorithmTask algorithmTask) {
- this._algServiceMap.put(algorithm, algorithmTask.getServiceReference());
- this._algMap.put(algorithm, algorithmTask);
+ this.serviceReferencesByAlgorithms.put(algorithm, algorithmTask.getServiceReference());
+ this.tasksByAlgorithms.put(algorithm, algorithmTask);
}
/**
- * @param alg
+ * @param algorithm
* The algorithm whose state we want to query.
* @return State of the specified algorithm.
*/
- public synchronized final STATE getAlgorithmState(Algorithm alg) {
- AlgorithmTask task = (AlgorithmTask) this._algMap.get(alg);
+ public synchronized final AlgorithmState getAlgorithmState(Algorithm algorithm) {
+ AlgorithmTask task = this.tasksByAlgorithms.get(algorithm);
if (task == null)
throw new NoSuchElementException("Algorithm doesn't exist.");
return task.getState();
@@ -439,92 +427,95 @@
*/
public synchronized final void purgeFinished() {
synchronized (this) {
- Iterator iter = this._algMap
- .entrySet().iterator();
- while (iter.hasNext()) {
- Map.Entry entry = (Map.Entry) iter.next();
- AlgorithmTask task = (AlgorithmTask) entry.getValue();
- if (task.getState() == STATE.STOPPED) {
- iter.remove();
- _algServiceMap.remove(entry.getKey());
+ Iterator<Map.Entry<Algorithm, AlgorithmTask>> entries =
+ this.tasksByAlgorithms.entrySet().iterator();
+
+ while (entries.hasNext()) {
+ Map.Entry<Algorithm, AlgorithmTask> entry = entries.next();
+ AlgorithmTask task = entry.getValue();
+
+ if (task.getState() == AlgorithmState.STOPPED) {
+ entries.remove();
+ this.serviceReferencesByAlgorithms.remove(entry.getKey());
}
}
}
}
- private synchronized final boolean _limitReached() {
- return (_maxSimultaneousAlgs != -1)
- && (_numRunning >= _maxSimultaneousAlgs);
+ private synchronized final boolean limitReached() {
+ return
+ (this.maxSimultaneousAlgorithms != AS_MANY_SIMULTANEOUS_ALGORITHMS_AS_NEEDED) &&
+ (this.runningTaskCount >= this.maxSimultaneousAlgorithms);
}
public void setRunning(boolean isRunning) {
- _running = isRunning;
+ this.isRunning = isRunning;
}
public boolean isRunning() {
- return _running;
+ return this.isRunning;
}
public void run() {
- if (_running) {
+ if (this.isRunning) {
synchronized (this) {
// If we are running the max allowable, wait until next turn.
Date now = Calendar.getInstance().getTime();
// Iterate through algorithms.
- Collection tasks = this._algMap.values();
- for (Iterator iter = tasks.iterator() ; iter.hasNext() ;) {
- AlgorithmTask task = (AlgorithmTask) iter.next() ;
- if (_limitReached())
+ Collection<AlgorithmTask> tasks = this.tasksByAlgorithms.values();
+
+ for (AlgorithmTask task : tasks) {
+ if (limitReached()) {
return;
- if ((task.getState() == STATE.NEW)
+ }
+
+ if ((task.getState() == AlgorithmState.NEW)
&& now.compareTo(task.getScheduledTime().getTime()) >= 0) {
- // Run immediately
+ // Run immediately.
task.start();
}
}
}
}
}
-
- private volatile int _numRunning = 0;
public synchronized void algorithmScheduled(Algorithm algorithm, Calendar time) {
- _schedulerListener.algorithmScheduled(algorithm, time);
+ this.schedulerListener.algorithmScheduled(algorithm, time);
}
public synchronized void algorithmStarted(Algorithm algorithm) {
- _numRunning++;
- _schedulerListener.algorithmStarted(algorithm);
+ this.runningTaskCount++;
+ this.schedulerListener.algorithmStarted(algorithm);
}
public synchronized void algorithmError(Algorithm algorithm, Throwable error) {
- _numRunning--;
- _schedulerListener.algorithmError(algorithm, error);
+ this.runningTaskCount--;
+ this.schedulerListener.algorithmError(algorithm, error);
purgeFinished();
}
public synchronized void algorithmFinished(Algorithm algorithm, Data[] createdDM) {
- _numRunning--;
- _schedulerListener.algorithmFinished(algorithm, createdDM);
+ this.runningTaskCount--;
+ this.schedulerListener.algorithmFinished(algorithm, createdDM);
purgeFinished();
}
public synchronized void algorithmRescheduled(Algorithm algorithm, Calendar time) {
- _schedulerListener.algorithmRescheduled(algorithm, time);
+ this.schedulerListener.algorithmRescheduled(algorithm, time);
}
public synchronized void algorithmUnscheduled(Algorithm algorithm) {
- _schedulerListener.algorithmUnscheduled(algorithm);
+ this.schedulerListener.algorithmUnscheduled(algorithm);
}
public synchronized void schedulerCleared() {
- _schedulerListener.schedulerCleared();
+ this.schedulerListener.schedulerCleared();
}
public synchronized void schedulerRunStateChanged(boolean isRunning) {
- _schedulerListener.schedulerRunStateChanged(isRunning);
+ this.schedulerListener.schedulerRunStateChanged(isRunning);
}
}
@@ -542,147 +533,196 @@
// May 8, 2006 7:19:00 PM Shashikant Penumarthy: Initial implementation.
//July 19, 2006 10:45:00 AM Bruce Herr: Ported to new CIShell
class AlgorithmTask implements Runnable {
-
/**
* The states in which algorithm tasks can exist.
*
* @author Team IVC
*/
- static final class STATE {
- private String _name ;
- public STATE(String name) {
- this._name = name ;
- }
- public final boolean equals(Object object) {
- if (! (object instanceof STATE))
- return false ;
- STATE state = (STATE) object ;
- return state._name.compareTo(_name) == 0;
- }
- /** New algorithms are in this state. */
- public static final STATE NEW = new STATE("NEW") ;
- /** Running algorithms are in this state. */
- public static final STATE RUNNING = new STATE("RUNNING") ;
- /** Algorithms either cancelled or finished are in this state. */
- public static final STATE STOPPED = new STATE("STOPPED") ;
- /** Algorithm had an error while executing */
- public static final STATE ERROR = new STATE("ERROR");
- }
+ private volatile boolean isCanceled = false;
+ private final Algorithm algorithm;
- private volatile boolean _noRun = false;
+ /* NOTE: TimerTask keeps its own schedule variable which can be retrieved using
+ * scheduledExecutionTime() method. We don't use that here.
+ */
+ private final Calendar scheduledTime;
+ private final ServiceReference serviceReference;
+ private volatile AlgorithmState state;
+ // Execution status of the algorithm (i.e.) return value.
+ private Data[] result;
+
+ // The exception thrown, if an algorithm had one while executing.
+ private Exception exceptionThrown;
+
+ // Deliberately allow only one listener. Its not the algorithms job to do all the informing.
+ private SchedulerListener schedulerListener;
+
public synchronized final boolean cancel() {
- if (_noRun)
+ if (this.isCanceled) {
return true;
- if (_state.equals(STATE.RUNNING))
+ }
+
+ if (this.state.equals(AlgorithmState.RUNNING)) {
return false;
- _state = STATE.STOPPED;
- _noRun = true;
- return _noRun;
+ }
+
+ this.state = AlgorithmState.STOPPED;
+ this.isCanceled = true;
+
+ return this.isCanceled;
}
public synchronized final void start() {
- if (_noRun)
+ if (this.isCanceled) {
return;
- _setState(STATE.RUNNING);
+ }
+
+ setState(AlgorithmState.RUNNING);
new Thread(this).start();
}
- private final Algorithm _alg;
+ public AlgorithmTask(
+ Algorithm algorithm,
+ ServiceReference serviceReference,
+ Calendar scheduledTime,
+ AlgorithmSchedulerTask algorithmSchedulerTask) {
+ this.algorithm = algorithm;
+ this.serviceReference = serviceReference;
+ this.scheduledTime = scheduledTime;
+ this.schedulerListener = algorithmSchedulerTask;
- // NOTE: TimerTask keeps its own schedule variable which can be retrieved
- // using scheduledExecutionTime() method. We don't use that here.
- private final Calendar _scheduledTime;
-
- private final ServiceReference _ref;
-
- private volatile STATE _state;
-
- /**
- * Execution status of the algorithm (i.e.) return value.
- */
- private Data[] _result;
-
- /**
- * The error, if an algorithm had one while executing
- */
- private Exception _error;
-
- /**
- * Deliberately allow only one listener. Its not the algorithms job to do
- * all the informing.
- */
- private SchedulerListener _schedulerListener;
-
- public AlgorithmTask(Algorithm alg, ServiceReference ref, Calendar scheduledTime,
- //SchedulerListener listener) {
- AlgSchedulerTask algSchedulerTask) {
- _alg = alg;
- _ref = ref;
- _scheduledTime = scheduledTime;
- _schedulerListener = algSchedulerTask;
- algSchedulerTask.registerAlgorithmTask(alg, this);
- _init();
+ algorithmSchedulerTask.registerAlgorithmTask(algorithm, this);
+ init();
}
public synchronized final Calendar getScheduledTime() {
- // Do a defensive copy cuz we don't want clients changing
- // the time using this reference!
+ /* Do a defensive copy because we don't want clients changing the time using
+ * this reference!
+ */
Calendar calendar = Calendar.getInstance();
- calendar.setTime(this._scheduledTime.getTime());
+ calendar.setTime(this.scheduledTime.getTime());
+
return calendar;
}
public synchronized final ServiceReference getServiceReference() {
- return _ref;
+ return this.serviceReference;
}
- private final void _init() {
- _result = null;
- _setState(STATE.NEW);
+ private final void init() {
+ this.result = null;
+ setState(AlgorithmState.NEW);
}
public synchronized final Data[] getResult() {
- return _result;
+ return this.result;
}
- private synchronized final void _setState(STATE state) {
- this._state = state;
- // Inform listeners
- if (_schedulerListener != null) {
- if (this._state.equals(STATE.NEW)) {
- _schedulerListener.algorithmScheduled(_alg, _scheduledTime);
- }
- else if (this._state.equals(STATE.RUNNING)) {
- _schedulerListener.algorithmStarted(_alg);
- }
- else if (this._state.equals(STATE.STOPPED)) {
- _noRun = true;
- _schedulerListener.algorithmFinished(_alg, getResult());
- }
- else if (this._state.equals(STATE.ERROR)) {
- _noRun = true;
- _schedulerListener.algorithmError(_alg, _error);
- }
- else {
- throw new IllegalStateException(
- "Encountered illegal algorithm state: " + _state);
- }
+ private synchronized final void setState(AlgorithmState state) {
+ this.state = state;
+ // Inform listeners.
+ if (this.schedulerListener != null) {
+ this.state.performAction(
+ algorithm,
+ this.schedulerListener,
+ this.scheduledTime,
+ getResult(),
+ this.exceptionThrown);
+ this.isCanceled = this.state.isCanceledNow();
}
}
- public synchronized final STATE getState() {
- return this._state;
+ public synchronized final AlgorithmState getState() {
+ return this.state;
}
public void run() {
try {
- _result = _alg.execute();
+ this.result = this.algorithm.execute();
} catch (Exception e) {
- _error = e;
- _setState(STATE.ERROR);
+ this.exceptionThrown = e;
+ setState(AlgorithmState.ERROR);
} finally {
- _setState(STATE.STOPPED);
+ setState(AlgorithmState.STOPPED);
}
}
+
+ static class AlgorithmState {
+ /** New algorithms are in this state. */
+ public static final AlgorithmState NEW = new AlgorithmState("NEW", false) {
+ public void performAction(
+ Algorithm algorithm,
+ SchedulerListener schedulerListener,
+ Calendar scheduledTime,
+ Data[] result,
+ Exception exceptionThrown) {
+ schedulerListener.algorithmScheduled(algorithm, scheduledTime);
+ }
+ };
+
+ /** Running algorithms are in this state. */
+ public static final AlgorithmState RUNNING = new AlgorithmState("RUNNING", false) {
+ public void performAction(
+ Algorithm algorithm,
+ SchedulerListener schedulerListener,
+ Calendar scheduledTime,
+ Data[] result,
+ Exception exceptionThrown) {
+ schedulerListener.algorithmStarted(algorithm);
+ }
+ };
+ /** Algorithms either cancelled or finished are in this state. */
+ public static final AlgorithmState STOPPED = new AlgorithmState("STOPPED", true) {
+ public void performAction(
+ Algorithm algorithm,
+ SchedulerListener schedulerListener,
+ Calendar scheduledTime,
+ Data[] result,
+ Exception exceptionThrown) {
+ schedulerListener.algorithmFinished(algorithm, result);
+ }
+ };
+ /** Algorithm had an exceptionThrown while executing */
+ public static final AlgorithmState ERROR = new AlgorithmState("ERROR", true) {
+ public void performAction(
+ Algorithm algorithm,
+ SchedulerListener schedulerListener,
+ Calendar scheduledTime,
+ Data[] result,
+ Exception exceptionThrown) {
+ schedulerListener.algorithmError(algorithm, exceptionThrown);
+ }
+ };
+
+ private String name;
+ private boolean isCanceled;
+
+ public AlgorithmState(String name, boolean isCanceled) {
+ this.name = name;
+ this.isCanceled = isCanceled;
+ }
+
+ public final boolean equals(Object object) {
+ if (!(object instanceof AlgorithmState)) {
+ return false;
+ }
+
+ AlgorithmState state = (AlgorithmState) object;
+
+ return state.name.compareTo(name) == 0;
+ }
+
+ public void performAction(
+ Algorithm algorithm,
+ SchedulerListener schedulerListener,
+ Calendar scheduledTime,
+ Data[] result,
+ Exception exceptionThrown) {
+ throw new IllegalStateException("Encountered illegal algorithm state: " + this);
+ }
+
+ public boolean isCanceledNow() {
+ return this.isCanceled;
+ }
+ }
}
Modified: trunk/core/org.cishell.reference.services/src/org/cishell/reference/services/Activator.java
===================================================================
--- trunk/core/org.cishell.reference.services/src/org/cishell/reference/services/Activator.java 2010-08-03 18:35:25 UTC (rev 1107)
+++ trunk/core/org.cishell.reference.services/src/org/cishell/reference/services/Activator.java 2010-08-03 19:15:24 UTC (rev 1108)
@@ -15,36 +15,46 @@
import org.osgi.framework.ServiceRegistration;
public class Activator implements BundleActivator {
- private ServiceRegistration conversionReg;
- private ServiceRegistration schedulerReg;
- private ServiceRegistration dataManagerReg;
+ private ServiceRegistration conversionRegistration;
+ private ServiceRegistration schedulerRegistration;
+ private ServiceRegistration dataManagerRegistration;
+// private ServiceRegistration algorithmInvokerRegistration;
/**
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
- public void start(BundleContext bContext) throws Exception {
- CIShellContext ciContext = new LocalCIShellContext(bContext);
+ public void start(BundleContext bundleContext) throws Exception {
+ CIShellContext ciShellContext = new LocalCIShellContext(bundleContext);
- DataConversionService conversionService =
- new DataConversionServiceImpl(bContext, ciContext);
- conversionReg = bContext.registerService(
- DataConversionService.class.getName(), conversionService, new Hashtable());
+ DataConversionService conversionService =
+ new DataConversionServiceImpl(bundleContext, ciShellContext);
+ this.conversionRegistration = bundleContext.registerService(
+ DataConversionService.class.getName(),
+ conversionService,
+ new Hashtable<String, Object>());
SchedulerService scheduler = new SchedulerServiceImpl();
- schedulerReg = bContext.registerService(
- SchedulerService.class.getName(), scheduler, new Hashtable());
+ this.schedulerRegistration = bundleContext.registerService(
+ SchedulerService.class.getName(), scheduler, new Hashtable<String, Object>());
DataManagerService dataManager = new DataManagerServiceImpl();
- dataManagerReg = bContext.registerService(
- DataManagerService.class.getName(), dataManager, new Hashtable());
+ this.dataManagerRegistration = bundleContext.registerService(
+ DataManagerService.class.getName(), dataManager, new Hashtable<String, Object>());
+
+// AlgorithmInvocationService algorithmInvoker = new AlgorithmInvocationServiceImpl();
+// this.algorithmInvokerRegistration = bundleContext.registerService(
+// AlgorithmInvocationService.class.getName(),
+// algorithmInvoker,
+// new Hashtable<String, Object>());
}
/**
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
- public vo...
[truncated message content] |