View Javadoc

1   /*
2    * $Id: DelegateWorkManager.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.ra;
12  
13  import org.mule.umo.UMOException;
14  import org.mule.umo.manager.UMOWorkManager;
15  
16  import javax.resource.spi.work.ExecutionContext;
17  import javax.resource.spi.work.Work;
18  import javax.resource.spi.work.WorkException;
19  import javax.resource.spi.work.WorkListener;
20  import javax.resource.spi.work.WorkManager;
21  
22  import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException;
23  
24  /**
25   * <code>DelegateWorkManager</code> is a wrapper around a WorkManager provided by a
26   * JCA container.
27   */
28  public class DelegateWorkManager implements UMOWorkManager
29  {
30      private final WorkManager workManager;
31  
32      public DelegateWorkManager(WorkManager workManager)
33      {
34          this.workManager = workManager;
35      }
36  
37      public void doWork(Work work) throws WorkException
38      {
39          workManager.doWork(work);
40      }
41  
42      public void doWork(Work work, long l, ExecutionContext executionContext, WorkListener workListener)
43          throws WorkException
44      {
45          workManager.doWork(work, l, executionContext, workListener);
46      }
47  
48      public long startWork(Work work) throws WorkException
49      {
50          return workManager.startWork(work);
51      }
52  
53      public long startWork(Work work, long l, ExecutionContext executionContext, WorkListener workListener)
54          throws WorkException
55      {
56          return workManager.startWork(work, l, executionContext, workListener);
57      }
58  
59      public void scheduleWork(Work work) throws WorkException
60      {
61          workManager.scheduleWork(work);
62      }
63  
64      public void scheduleWork(Work work, long l, ExecutionContext executionContext, WorkListener workListener)
65          throws WorkException
66      {
67          workManager.scheduleWork(work, l, executionContext, workListener);
68      }
69  
70      public void execute(Runnable command)
71      {
72          try
73          {
74              this.scheduleWork(new RunnableWorkAdapter(command));
75          }
76          catch (WorkException wex)
77          {
78              // unfortunately RejectedExecutionException is the closest thing we have
79              // as proper RuntimeException
80              throw new RejectedExecutionException(wex);
81          }
82      }
83  
84      public void start() throws UMOException
85      {
86          // nothing to do
87      }
88  
89      public void stop() throws UMOException
90      {
91          // nothing to do
92      }
93  
94      public void dispose()
95      {
96          // nothing to do
97      }
98  
99      protected static class RunnableWorkAdapter implements Work
100     {
101         private final Runnable command;
102 
103         public RunnableWorkAdapter(Runnable command)
104         {
105             super();
106             this.command = command;
107         }
108 
109         public void release()
110         {
111             // nothing to do
112         }
113 
114         public void run()
115         {
116             command.run();
117         }
118     }
119 
120 }