001: /*
002: * $Id: DelegateWorkManager.java 10808 2008-02-14 20:36:57Z acooke $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.module.jca;
012:
013: import org.mule.api.MuleException;
014: import org.mule.api.lifecycle.LifecycleTransitionResult;
015:
016: import javax.resource.spi.work.ExecutionContext;
017: import javax.resource.spi.work.Work;
018: import javax.resource.spi.work.WorkException;
019: import javax.resource.spi.work.WorkListener;
020:
021: import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException;
022:
023: /**
024: * <code>DelegateWorkManager</code> is a wrapper around a WorkManager provided by a
025: * JCA container.
026: */
027: public class DelegateWorkManager implements
028: org.mule.api.context.WorkManager {
029: private final javax.resource.spi.work.WorkManager workManager;
030:
031: public DelegateWorkManager(
032: javax.resource.spi.work.WorkManager workManager2) {
033: this .workManager = workManager2;
034: }
035:
036: public void doWork(Work work) throws WorkException {
037: workManager.doWork(work);
038: }
039:
040: public void doWork(Work work, long l,
041: ExecutionContext executionContext, WorkListener workListener)
042: throws WorkException {
043: workManager.doWork(work, l, executionContext, workListener);
044: }
045:
046: public long startWork(Work work) throws WorkException {
047: return workManager.startWork(work);
048: }
049:
050: public long startWork(Work work, long l,
051: ExecutionContext executionContext, WorkListener workListener)
052: throws WorkException {
053: return workManager.startWork(work, l, executionContext,
054: workListener);
055: }
056:
057: public void scheduleWork(Work work) throws WorkException {
058: workManager.scheduleWork(work);
059: }
060:
061: public void scheduleWork(Work work, long l,
062: ExecutionContext executionContext, WorkListener workListener)
063: throws WorkException {
064: workManager.scheduleWork(work, l, executionContext,
065: workListener);
066: }
067:
068: public void execute(Runnable command) {
069: try {
070: this .scheduleWork(new RunnableWorkAdapter(command));
071: } catch (WorkException wex) {
072: // unfortunately RejectedExecutionException is the closest thing we have
073: // as proper RuntimeException
074: throw new RejectedExecutionException(wex);
075: }
076: }
077:
078: public LifecycleTransitionResult start() throws MuleException {
079: return LifecycleTransitionResult.OK;
080: }
081:
082: public void stop() throws MuleException {
083: // nothing to do
084: }
085:
086: public void dispose() {
087: // nothing to do
088: }
089:
090: protected static class RunnableWorkAdapter implements Work {
091: private final Runnable command;
092:
093: public RunnableWorkAdapter(Runnable command) {
094: super ();
095: this .command = command;
096: }
097:
098: public void release() {
099: // nothing to do
100: }
101:
102: public void run() {
103: command.run();
104: }
105: }
106:
107: }
|