From 152cf09b068bbb6f55d7335a429cb7bc46e833bd Mon Sep 17 00:00:00 2001 From: antelder Date: Mon, 29 Dec 2008 16:09:52 +0000 Subject: Merge r720913 change to 1.x so we can start looking at a pluggable impl for the geronimo integration work and to remove the dependency from the compact distro git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@729926 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/core/work/DefaultWorkScheduler.java | 191 ++++++++++++++++++++ .../apache/tuscany/sca/core/work/Jsr237Work.java | 65 ------- .../tuscany/sca/core/work/Jsr237WorkScheduler.java | 198 --------------------- .../sca/core/work/ThreadPoolWorkManager.java | 38 ++-- .../org/apache/tuscany/sca/core/work/Work.java | 65 +++++++ .../apache/tuscany/sca/core/work/WorkEvent.java | 80 +++++++++ .../tuscany/sca/core/work/WorkEventImpl.java | 76 -------- .../org/apache/tuscany/sca/core/work/WorkItem.java | 167 +++++++++++++++++ .../apache/tuscany/sca/core/work/WorkItemImpl.java | 169 ------------------ .../apache/tuscany/sca/core/work/WorkListener.java | 32 ++++ .../org.apache.tuscany.sca.work.WorkScheduler | 2 +- .../apache/tuscany/sca/core/work/FailingWork.java | 7 +- .../sca/core/work/Jsr237WorkSchedulerTestCase.java | 4 +- .../tuscany/sca/core/work/TestWorkListener.java | 9 +- .../tuscany/sca/core/work/TimeDelayWork.java | 5 +- 15 files changed, 564 insertions(+), 544 deletions(-) create mode 100644 branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/DefaultWorkScheduler.java delete mode 100644 branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237Work.java delete mode 100644 branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237WorkScheduler.java create mode 100644 branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Work.java create mode 100644 branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEvent.java delete mode 100644 branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEventImpl.java create mode 100644 branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItem.java delete mode 100644 branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItemImpl.java create mode 100644 branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkListener.java (limited to 'branches/sca-java-1.x/modules/core/src') diff --git a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/DefaultWorkScheduler.java b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/DefaultWorkScheduler.java new file mode 100644 index 0000000000..2085a796ab --- /dev/null +++ b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/DefaultWorkScheduler.java @@ -0,0 +1,191 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.core.work; + +import java.security.AccessController; +import java.security.PrivilegedAction; + +import org.apache.tuscany.sca.work.NotificationListener; +import org.apache.tuscany.sca.work.WorkScheduler; +import org.apache.tuscany.sca.work.WorkSchedulerException; + +/** + * A work scheduler implementation based on a JSR 237 work manager. + *

+ *

+ * This needs a JSR 237 work manager implementation available for scheduling work. Instances can be configured with a + * work manager implementation that is injected in. It is the responsibility of the runtime environment to make a work + * manager implementation available. For example, if the managed environment supports work manager the runtime can use + * the appropriate lookup mechanism to inject the work manager implementation.

+ * + * @version $Rev$ $Date$ + */ +public class DefaultWorkScheduler implements WorkScheduler { + + /** + * Underlying JSR-237 work manager + */ + private ThreadPoolWorkManager jsr237WorkManager; + + /** + * Initializes the JSR 237 work manager. + * + * @param jsr237WorkManager JSR 237 work manager. + */ + public DefaultWorkScheduler() { + } + + private synchronized ThreadPoolWorkManager getWorkManager() { + if (jsr237WorkManager != null) { + return jsr237WorkManager; + } +// try { +// InitialContext ctx = new InitialContext(); +// jsr237WorkManager = (ThreadPoolWorkManager)ctx.lookup("java:comp/env/wm/TuscanyWorkManager"); +// } catch (Throwable e) { +// // ignore +// } + if (jsr237WorkManager == null) { + jsr237WorkManager = new ThreadPoolWorkManager(10); + } + return jsr237WorkManager; + } + + /** + * Schedules a unit of work for future execution. The notification listener is used to register interest in + * callbacks regarding the status of the work. + * + * @param work The unit of work that needs to be asynchronously executed. + */ + public void scheduleWork(T work) { + scheduleWork(work, null); + } + + /** + * Schedules a unit of work for future execution. The notification listener is used to register interest in + * callbacks regarding the status of the work. + * + * @param work The unit of work that needs to be asynchronously executed. + * @param listener Notification listener for callbacks. + */ + public void scheduleWork(T work, NotificationListener listener) { + + if (work == null) { + throw new IllegalArgumentException("Work cannot be null"); + } + + Work jsr237Work = new Work(work); + try { + if (listener == null) { + getWorkManager().schedule(jsr237Work); + } else { + Jsr237WorkListener jsr237WorkListener = new Jsr237WorkListener(listener, work); + getWorkManager().schedule(jsr237Work, jsr237WorkListener); + } + } catch (IllegalArgumentException ex) { + if (listener != null) { + listener.workRejected(work); + } else { + throw new WorkSchedulerException(ex); + } + } catch (Exception ex) { + throw new WorkSchedulerException(ex); + } + + } + + public void destroy() { + if (jsr237WorkManager instanceof ThreadPoolWorkManager) { + // Allow privileged access to modify threads. Requires RuntimePermission in security + // policy. + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + ((ThreadPoolWorkManager)jsr237WorkManager).destroy(); + return null; + } + }); + } + } + + /* + * WorkListener for keeping track of work status callbacks. + * + */ + private class Jsr237WorkListener implements WorkListener { + + // Notification listener + private NotificationListener listener; + + // Work + private T work; + + /* + * Initializes the notification listener. + */ + public Jsr237WorkListener(NotificationListener listener, T work) { + this.listener = listener; + this.work = work; + } + + /* + * Callback when the work is accepted. + */ + public void workAccepted(WorkEvent workEvent) { + T work = getWork(); + listener.workAccepted(work); + } + + /* + * Callback when the work is rejected. + */ + public void workRejected(WorkEvent workEvent) { + T work = getWork(); + listener.workRejected(work); + } + + /* + * Callback when the work is started. + */ + public void workStarted(WorkEvent workEvent) { + T work = getWork(); + listener.workStarted(work); + } + + /* + * Callback when the work is completed. + */ + public void workCompleted(WorkEvent workEvent) { + T work = getWork(); + Exception exception = workEvent.getException(); + if (exception != null) { + listener.workFailed(work, exception); + } else { + listener.workCompleted(work); + } + } + + /* + * Gets the underlying work from the work event. + */ + private T getWork() { + return work; + } + + } +} diff --git a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237Work.java b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237Work.java deleted file mode 100644 index adeb667277..0000000000 --- a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237Work.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.core.work; - -/** - * JCA work wrapper. - * - * @version $Rev$ $Date$ - */ -public class Jsr237Work implements commonj.work.Work { - - // Work that is being executed. - private T work; - - /* - * Initializes the work instance. - */ - public Jsr237Work(T work) { - this.work = work; - } - - /* - * Returns the completed work. - */ - public T getWork() { - return work; - } - - /* - * Release the work. - */ - public void release() { - } - - /* - * Work attributes are not daemon. - */ - public boolean isDaemon() { - return false; - } - - /* - * Runs the work. - */ - public void run() { - work.run(); - } -} \ No newline at end of file diff --git a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237WorkScheduler.java b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237WorkScheduler.java deleted file mode 100644 index ffc34e7328..0000000000 --- a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237WorkScheduler.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tuscany.sca.core.work; - -import java.security.AccessController; -import java.security.PrivilegedAction; - -import javax.naming.InitialContext; -import javax.naming.NamingException; - -import org.apache.tuscany.sca.work.NotificationListener; -import org.apache.tuscany.sca.work.WorkScheduler; -import org.apache.tuscany.sca.work.WorkSchedulerException; - -import commonj.work.WorkEvent; -import commonj.work.WorkListener; -import commonj.work.WorkManager; - -/** - * A work scheduler implementation based on a JSR 237 work manager. - *

- *

- * This needs a JSR 237 work manager implementation available for scheduling work. Instances can be configured with a - * work manager implementation that is injected in. It is the responsibility of the runtime environment to make a work - * manager implementation available. For example, if the managed environment supports work manager the runtime can use - * the appropriate lookup mechanism to inject the work manager implementation.

- * - * @version $Rev$ $Date$ - */ -public class Jsr237WorkScheduler implements WorkScheduler { - - /** - * Underlying JSR-237 work manager - */ - private WorkManager jsr237WorkManager; - - /** - * Initializes the JSR 237 work manager. - * - * @param jsr237WorkManager JSR 237 work manager. - */ - public Jsr237WorkScheduler() { - } - - private synchronized WorkManager getWorkManager() { - if (jsr237WorkManager != null) { - return jsr237WorkManager; - } - try { - InitialContext ctx = new InitialContext(); - jsr237WorkManager = (WorkManager)ctx.lookup("java:comp/env/wm/TuscanyWorkManager"); - } catch (Throwable e) { - // ignore - } - if (jsr237WorkManager == null) { - jsr237WorkManager = new ThreadPoolWorkManager(10); - } - return jsr237WorkManager; - } - - /** - * Schedules a unit of work for future execution. The notification listener is used to register interest in - * callbacks regarding the status of the work. - * - * @param work The unit of work that needs to be asynchronously executed. - */ - public void scheduleWork(T work) { - scheduleWork(work, null); - } - - /** - * Schedules a unit of work for future execution. The notification listener is used to register interest in - * callbacks regarding the status of the work. - * - * @param work The unit of work that needs to be asynchronously executed. - * @param listener Notification listener for callbacks. - */ - public void scheduleWork(T work, NotificationListener listener) { - - if (work == null) { - throw new IllegalArgumentException("Work cannot be null"); - } - - Jsr237Work jsr237Work = new Jsr237Work(work); - try { - if (listener == null) { - getWorkManager().schedule(jsr237Work); - } else { - Jsr237WorkListener jsr237WorkListener = new Jsr237WorkListener(listener, work); - getWorkManager().schedule(jsr237Work, jsr237WorkListener); - } - } catch (IllegalArgumentException ex) { - if (listener != null) { - listener.workRejected(work); - } else { - throw new WorkSchedulerException(ex); - } - } catch (Exception ex) { - throw new WorkSchedulerException(ex); - } - - } - - public void destroy() { - if (jsr237WorkManager instanceof ThreadPoolWorkManager) { - // Allow privileged access to modify threads. Requires RuntimePermission in security - // policy. - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - ((ThreadPoolWorkManager)jsr237WorkManager).destroy(); - return null; - } - }); - } - } - - /* - * WorkListener for keeping track of work status callbacks. - * - */ - private class Jsr237WorkListener implements WorkListener { - - // Notification listener - private NotificationListener listener; - - // Work - private T work; - - /* - * Initializes the notification listener. - */ - public Jsr237WorkListener(NotificationListener listener, T work) { - this.listener = listener; - this.work = work; - } - - /* - * Callback when the work is accepted. - */ - public void workAccepted(WorkEvent workEvent) { - T work = getWork(); - listener.workAccepted(work); - } - - /* - * Callback when the work is rejected. - */ - public void workRejected(WorkEvent workEvent) { - T work = getWork(); - listener.workRejected(work); - } - - /* - * Callback when the work is started. - */ - public void workStarted(WorkEvent workEvent) { - T work = getWork(); - listener.workStarted(work); - } - - /* - * Callback when the work is completed. - */ - public void workCompleted(WorkEvent workEvent) { - T work = getWork(); - Exception exception = workEvent.getException(); - if (exception != null) { - listener.workFailed(work, exception); - } else { - listener.workCompleted(work); - } - } - - /* - * Gets the underlying work from the work event. - */ - private T getWork() { - return work; - } - - } -} diff --git a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManager.java b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManager.java index fb47a65edf..dad5968f65 100644 --- a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManager.java +++ b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManager.java @@ -27,15 +27,9 @@ import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ThreadFactory; +import org.apache.tuscany.sca.work.WorkSchedulerException; import org.osoa.sca.annotations.Destroy; -import commonj.work.Work; -import commonj.work.WorkEvent; -import commonj.work.WorkException; -import commonj.work.WorkItem; -import commonj.work.WorkListener; -import commonj.work.WorkManager; - /** * A thread-pool based implementation for the JSR-237 work manager. *

@@ -46,10 +40,10 @@ import commonj.work.WorkManager; * * @version $Rev$ $Date$ */ -public class ThreadPoolWorkManager implements WorkManager { +public class ThreadPoolWorkManager { // Map of work items currently handled by the work manager - private Map workItems = new ConcurrentHashMap(); + private Map workItems = new ConcurrentHashMap(); // Thread-pool private ExecutorService executor; @@ -96,7 +90,7 @@ public class ThreadPoolWorkManager implements WorkManager { */ public WorkItem schedule(Work work, WorkListener workListener) throws IllegalArgumentException { - WorkItemImpl workItem = new WorkItemImpl(new UID().toString(), work); + WorkItem workItem = new WorkItem(new UID().toString(), work); if (workListener != null) { workItems.put(workItem, workListener); } @@ -106,7 +100,7 @@ public class ThreadPoolWorkManager implements WorkManager { } else { workItem.setStatus(WorkEvent.WORK_REJECTED); if (workListener != null) { - workListener.workRejected(new WorkEventImpl(workItem)); + workListener.workRejected(new WorkEvent(workItem)); } throw new IllegalArgumentException("Unable to schedule work"); } @@ -138,11 +132,11 @@ public class ThreadPoolWorkManager implements WorkManager { * @param workItem Work item representing the work that was accepted. * @param work Work that was accepted. */ - private void workAccepted(final WorkItemImpl workItem, final Work work) { + private void workAccepted(final WorkItem workItem, final Work work) { WorkListener listener = workItems.get(workItem); if (listener != null) { workItem.setStatus(WorkEvent.WORK_ACCEPTED); - WorkEvent event = new WorkEventImpl(workItem); + WorkEvent event = new WorkEvent(workItem); listener.workAccepted(event); } } @@ -150,11 +144,11 @@ public class ThreadPoolWorkManager implements WorkManager { /* * Method to indicate a work start. */ - private void workStarted(final WorkItemImpl workItem, final Work work) { + private void workStarted(final WorkItem workItem, final Work work) { WorkListener listener = workItems.get(workItem); if (listener != null) { workItem.setStatus(WorkEvent.WORK_STARTED); - WorkEvent event = new WorkEventImpl(workItem); + WorkEvent event = new WorkEvent(workItem); listener.workStarted(event); } } @@ -162,20 +156,20 @@ public class ThreadPoolWorkManager implements WorkManager { /* * Method to indicate a work completion. */ - private void workCompleted(final WorkItemImpl workItem, final Work work) { + private void workCompleted(final WorkItem workItem, final Work work) { workCompleted(workItem, work, null); } /* * Method to indicate a work completion. */ - private void workCompleted(final WorkItemImpl workItem, final Work work, final WorkException exception) { + private void workCompleted(final WorkItem workItem, final Work work, final WorkSchedulerException exception) { WorkListener listener = workItems.get(workItem); if (listener != null) { workItem.setStatus(WorkEvent.WORK_COMPLETED); workItem.setResult(work); workItem.setException(exception); - WorkEvent event = new WorkEventImpl(workItem); + WorkEvent event = new WorkEvent(workItem); listener.workCompleted(event); workItems.remove(workItem); } @@ -184,7 +178,7 @@ public class ThreadPoolWorkManager implements WorkManager { /* * Schedules the work using the ThreadPool. */ - private boolean scheduleWork(final Work work, final WorkItemImpl workItem) { + private boolean scheduleWork(final Work work, final WorkItem workItem) { try { executor.execute(new DecoratingWork(workItem, work)); return true; @@ -199,7 +193,7 @@ public class ThreadPoolWorkManager implements WorkManager { private final class DecoratingWork implements Runnable { // Work item for this work. - private WorkItemImpl workItem; + private WorkItem workItem; // The original work. private Work decoratedWork; @@ -207,7 +201,7 @@ public class ThreadPoolWorkManager implements WorkManager { /* * Initializes the work item and underlying work. */ - private DecoratingWork(final WorkItemImpl workItem, final Work decoratedWork) { + private DecoratingWork(final WorkItem workItem, final Work decoratedWork) { this.workItem = workItem; this.decoratedWork = decoratedWork; } @@ -221,7 +215,7 @@ public class ThreadPoolWorkManager implements WorkManager { decoratedWork.run(); workCompleted(workItem, decoratedWork); } catch (Throwable th) { - workCompleted(workItem, decoratedWork, new WorkException(th.getMessage(), th)); + workCompleted(workItem, decoratedWork, new WorkSchedulerException(th.getMessage(), th)); } } diff --git a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Work.java b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Work.java new file mode 100644 index 0000000000..c521c60f79 --- /dev/null +++ b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Work.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.core.work; + +/** + * JCA work wrapper. + * + * @version $Rev$ $Date$ + */ +public class Work { + + // Work that is being executed. + private T work; + + /* + * Initializes the work instance. + */ + public Work(T work) { + this.work = work; + } + + /* + * Returns the completed work. + */ + public T getWork() { + return work; + } + + /* + * Release the work. + */ + public void release() { + } + + /* + * Work attributes are not daemon. + */ + public boolean isDaemon() { + return false; + } + + /* + * Runs the work. + */ + public void run() { + work.run(); + } +} \ No newline at end of file diff --git a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEvent.java b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEvent.java new file mode 100644 index 0000000000..8e9a3b4c53 --- /dev/null +++ b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEvent.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.core.work; + +import org.apache.tuscany.sca.work.WorkSchedulerException; + + + +/** + * Default immutable implementation of the WorkEvent class. + * + * @version $Rev$ $Date$ + */ +class WorkEvent { + + public static final int WORK_ACCEPTED = 1; + public static final int WORK_REJECTED = 2; + public static final int WORK_STARTED = 3; + public static final int WORK_COMPLETED = 4; + + // Work item for this event + private WorkItem workItem; + + // Exception if something has gone wrong + private WorkSchedulerException exception; + + /** + * Instantiates the event. + * + * @param workItem Work item for this event. + */ + public WorkEvent(final WorkItem workItem) { + this.workItem = workItem; + this.exception = workItem.getException(); + } + + /** + * Returns the work type based on whether the work was accepted, started, + * rejected or completed. + * + * @return Work type. + */ + public int getType() { + return workItem.getStatus(); + } + + /** + * Returns the work item associated with this work type. + * + * @return Work item. + */ + public WorkItem getWorkItem() { + return workItem; + } + + /** + * Returns the exception if the work completed with an exception. + * + * @return Work exception. + */ + public WorkSchedulerException getException() { + return exception; + } +} diff --git a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEventImpl.java b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEventImpl.java deleted file mode 100644 index 67d2b66f84..0000000000 --- a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEventImpl.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tuscany.sca.core.work; - - -import commonj.work.WorkEvent; -import commonj.work.WorkException; -import commonj.work.WorkItem; - -/** - * Default immutable implementation of the WorkEvent class. - * - * @version $Rev$ $Date$ - */ -class WorkEventImpl implements WorkEvent { - - // Work item for this event - private WorkItem workItem; - - // Exception if something has gone wrong - private WorkException exception; - - /** - * Instantiates the event. - * - * @param workItem Work item for this event. - */ - public WorkEventImpl(final WorkItemImpl workItem) { - this.workItem = workItem; - this.exception = workItem.getException(); - } - - /** - * Returns the work type based on whether the work was accepted, started, - * rejected or completed. - * - * @return Work type. - */ - public int getType() { - return workItem.getStatus(); - } - - /** - * Returns the work item associated with this work type. - * - * @return Work item. - */ - public WorkItem getWorkItem() { - return workItem; - } - - /** - * Returns the exception if the work completed with an exception. - * - * @return Work exception. - */ - public WorkException getException() { - return exception; - } -} diff --git a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItem.java b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItem.java new file mode 100644 index 0000000000..8320c7364f --- /dev/null +++ b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItem.java @@ -0,0 +1,167 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.core.work; + +import org.apache.tuscany.sca.work.WorkSchedulerException; + +/** + * An identity based immutable implementation of the WorkItem + * interface. + * + * @version $Rev$ $Date$ + */ +class WorkItem { + + // Id scoped for the VM + private String id; + + // Status + private int status = -1; + + // Result + private Work result; + + // Original work + private Work originalWork; + + // Exception + private WorkSchedulerException exception; + + /** + * Instantiates an id for this item. + * + * @param id of this work event. + */ + protected WorkItem(final String id, final Work orginalWork) { + this.id = id; + this.originalWork = orginalWork; + } + + /** + * Returns the id. + * + * @return Id of this item. + */ + public String getId() { + return id; + } + + /** + * Returns the original work. + * + * @return Original work. + */ + public Work getOriginalWork() { + return originalWork; + } + + /** + * Returns the work result if the work completed. + * + * @return Work. + * @throws WorkException If the work completed with an exception. + */ + public Work getResult() { + return result; + } + + /** + * Sets the result. + * + * @param result Result. + */ + protected void setResult(final Work result) { + this.result = result; + } + + /** + * Returns the exception if work completed with an exception. + * + * @return Work exception. + */ + protected WorkSchedulerException getException() { + return exception; + } + + /** + * Sets the exception. + * + * @param exception Exception. + */ + protected void setException(final WorkSchedulerException exception) { + this.exception = exception; + } + + /** + * Returns the work type based on whether the work was accepted, started, + * rejected or completed. + * + * @return Work status. + */ + public int getStatus() { + return status; + } + + /** + * Sets the status. + * + * @param status Status. + */ + protected void setStatus(final int status) { + this.status = status; + } + + /** + * @see Object#hashCode() + */ + @Override + public int hashCode() { + return id.hashCode(); + } + + /** + * Indicates whether some other object is "equal to" this one. + * + * @param obj Object to be compared. + * @return true if this object is the same as the obj argument; false + * otherwise.. + */ + @Override + public boolean equals(final Object obj) { + return (obj != null) && (obj.getClass() == WorkItem.class) && ((WorkItem) obj).id.equals(id); + } + + /** + * Compares this object with the specified object for order. Returns a + * negative integer, zero, or a positive integer as this object is less + * than, equal to, or greater than the specified object. + * + * @param o Object to be compared. + * @return A negative integer, zero, or a positive integer as this object + * is less than, equal to, or greater than the specified object. + * @throws ClassCastException needs better documentation. + */ + public int compareTo(final Object o) { + if (o.getClass() != WorkItem.class) { + throw new ClassCastException(o.getClass().getName()); + } else { + return ((WorkItem) o).getId().compareTo(getId()); + } + } +} diff --git a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItemImpl.java b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItemImpl.java deleted file mode 100644 index 170319b6e6..0000000000 --- a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItemImpl.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tuscany.sca.core.work; - -import commonj.work.Work; -import commonj.work.WorkException; -import commonj.work.WorkItem; - -/** - * An identity based immutable implementation of the WorkItem - * interface. - * - * @version $Rev$ $Date$ - */ -class WorkItemImpl implements WorkItem { - - // Id scoped for the VM - private String id; - - // Status - private int status = -1; - - // Result - private Work result; - - // Original work - private Work originalWork; - - // Exception - private WorkException exception; - - /** - * Instantiates an id for this item. - * - * @param id of this work event. - */ - protected WorkItemImpl(final String id, final Work orginalWork) { - this.id = id; - this.originalWork = orginalWork; - } - - /** - * Returns the id. - * - * @return Id of this item. - */ - public String getId() { - return id; - } - - /** - * Returns the original work. - * - * @return Original work. - */ - public Work getOriginalWork() { - return originalWork; - } - - /** - * Returns the work result if the work completed. - * - * @return Work. - * @throws WorkException If the work completed with an exception. - */ - public Work getResult() { - return result; - } - - /** - * Sets the result. - * - * @param result Result. - */ - protected void setResult(final Work result) { - this.result = result; - } - - /** - * Returns the exception if work completed with an exception. - * - * @return Work exception. - */ - protected WorkException getException() { - return exception; - } - - /** - * Sets the exception. - * - * @param exception Exception. - */ - protected void setException(final WorkException exception) { - this.exception = exception; - } - - /** - * Returns the work type based on whether the work was accepted, started, - * rejected or completed. - * - * @return Work status. - */ - public int getStatus() { - return status; - } - - /** - * Sets the status. - * - * @param status Status. - */ - protected void setStatus(final int status) { - this.status = status; - } - - /** - * @see Object#hashCode() - */ - @Override - public int hashCode() { - return id.hashCode(); - } - - /** - * Indicates whether some other object is "equal to" this one. - * - * @param obj Object to be compared. - * @return true if this object is the same as the obj argument; false - * otherwise.. - */ - @Override - public boolean equals(final Object obj) { - return (obj != null) && (obj.getClass() == WorkItemImpl.class) && ((WorkItemImpl) obj).id.equals(id); - } - - /** - * Compares this object with the specified object for order. Returns a - * negative integer, zero, or a positive integer as this object is less - * than, equal to, or greater than the specified object. - * - * @param o Object to be compared. - * @return A negative integer, zero, or a positive integer as this object - * is less than, equal to, or greater than the specified object. - * @throws ClassCastException needs better documentation. - */ - public int compareTo(final Object o) { - if (o.getClass() != WorkItemImpl.class) { - throw new ClassCastException(o.getClass().getName()); - } else { - return ((WorkItemImpl) o).getId().compareTo(getId()); - } - } -} diff --git a/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkListener.java b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkListener.java new file mode 100644 index 0000000000..19bfaaa560 --- /dev/null +++ b/branches/sca-java-1.x/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkListener.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.core.work; + +public interface WorkListener { + + static long IMMEDIATE = 0; + static long INDEFINITE = java.lang.Long.MAX_VALUE; + + void workAccepted(WorkEvent event); + void workCompleted(WorkEvent event); + void workRejected(WorkEvent event); + void workStarted(WorkEvent event); + +} diff --git a/branches/sca-java-1.x/modules/core/src/main/resources/META-INF/services/org.apache.tuscany.sca.work.WorkScheduler b/branches/sca-java-1.x/modules/core/src/main/resources/META-INF/services/org.apache.tuscany.sca.work.WorkScheduler index 56e99be0f2..93d93491a8 100644 --- a/branches/sca-java-1.x/modules/core/src/main/resources/META-INF/services/org.apache.tuscany.sca.work.WorkScheduler +++ b/branches/sca-java-1.x/modules/core/src/main/resources/META-INF/services/org.apache.tuscany.sca.work.WorkScheduler @@ -15,4 +15,4 @@ # specific language governing permissions and limitations # under the License. -org.apache.tuscany.sca.core.work.Jsr237WorkScheduler +org.apache.tuscany.sca.core.work.DefaultWorkScheduler diff --git a/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java b/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java index 3184c3de92..c69a1908e8 100644 --- a/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java +++ b/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java @@ -18,14 +18,17 @@ */ package org.apache.tuscany.sca.core.work; -import commonj.work.Work; /** * Simple Work item that will throw an exception * * @version $Rev$ $Date$ */ -public class FailingWork implements Work { +public class FailingWork extends Work { + + public FailingWork() { + super(null); + } /** * {@inheritDoc} diff --git a/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/Jsr237WorkSchedulerTestCase.java b/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/Jsr237WorkSchedulerTestCase.java index 225f23cb93..185394d7e8 100644 --- a/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/Jsr237WorkSchedulerTestCase.java +++ b/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/Jsr237WorkSchedulerTestCase.java @@ -38,14 +38,14 @@ public class Jsr237WorkSchedulerTestCase { /** * This is the shared instance of the ThreadPoolWorkManager used by the tests */ - private static Jsr237WorkScheduler workSchedular = null; + private static DefaultWorkScheduler workSchedular = null; /** * Setup the Jsr237WorkScheduler */ @BeforeClass public static void setup() { - workSchedular = new Jsr237WorkScheduler(); + workSchedular = new DefaultWorkScheduler(); } /** diff --git a/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java b/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java index ba32a92c18..58a1b87c48 100644 --- a/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java +++ b/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java @@ -23,12 +23,9 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.tuscany.sca.work.WorkSchedulerException; import org.junit.Assert; -import commonj.work.WorkEvent; -import commonj.work.WorkException; -import commonj.work.WorkListener; - /** * A simple WorkListener that tracks invocations to it. * @@ -59,7 +56,7 @@ public class TestWorkListener implements WorkListener { /** * List of all exceptions thrown by Work items */ - private List workExceptions = Collections.synchronizedList(new ArrayList()); + private List workExceptions = Collections.synchronizedList(new ArrayList()); /** * {@inheritDoc} @@ -150,7 +147,7 @@ public class TestWorkListener implements WorkListener { * * @return A List of all exceptions that are thrown by the Work items */ - public List getWorkExceptions() { + public List getWorkExceptions() { return Collections.unmodifiableList(workExceptions); } } diff --git a/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java b/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java index 3b30d86f14..6c10057046 100644 --- a/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java +++ b/branches/sca-java-1.x/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java @@ -20,15 +20,13 @@ package org.apache.tuscany.sca.core.work; import java.util.concurrent.atomic.AtomicInteger; -import commonj.work.Work; - /** * Simple Work item that will sleep in the run() method for the specified * period of time * * @version $Rev$ $Date$ */ -public class TimeDelayWork implements Work { +public class TimeDelayWork extends Work { /** * Count of completed run() method calls @@ -46,6 +44,7 @@ public class TimeDelayWork implements Work { * @param sleepTime The amount of time to sleep (in milliseconds) in the run() method */ public TimeDelayWork(long sleepTime) { + super(null); this.sleepTime = sleepTime; } -- cgit v1.2.3