View Javadoc

1   /*
2    * $Id: DelegateWorkManager.java 11517 2008-03-31 21:34:19Z 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.module.jca;
12  
13  import org.mule.api.MuleException;
14  
15  import javax.resource.spi.work.ExecutionContext;
16  import javax.resource.spi.work.Work;
17  import javax.resource.spi.work.WorkException;
18  import javax.resource.spi.work.WorkListener;
19  
20  import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException;
21  
22  /**
23   * <code>DelegateWorkManager</code> is a wrapper around a WorkManager provided by a
24   * JCA container.
25   */
26  public class DelegateWorkManager implements org.mule.api.context.WorkManager
27  {
28      private final javax.resource.spi.work.WorkManager workManager;
29  
30      public DelegateWorkManager(javax.resource.spi.work.WorkManager workManager2)
31      {
32          this.workManager = workManager2;
33      }
34  
35      public void doWork(Work work) throws WorkException
36      {
37          workManager.doWork(work);
38      }
39  
40      public void doWork(Work work, long l, ExecutionContext executionContext, WorkListener workListener)
41          throws WorkException
42      {
43          workManager.doWork(work, l, executionContext, workListener);
44      }
45  
46      public long startWork(Work work) throws WorkException
47      {
48          return workManager.startWork(work);
49      }
50  
51      public long startWork(Work work, long l, ExecutionContext executionContext, WorkListener workListener)
52          throws WorkException
53      {
54          return workManager.startWork(work, l, executionContext, workListener);
55      }
56  
57      public void scheduleWork(Work work) throws WorkException
58      {
59          workManager.scheduleWork(work);
60      }
61  
62      public void scheduleWork(Work work, long l, ExecutionContext executionContext, WorkListener workListener)
63          throws WorkException
64      {
65          workManager.scheduleWork(work, l, executionContext, workListener);
66      }
67  
68      public void execute(Runnable command)
69      {
70          try
71          {
72              this.scheduleWork(new RunnableWorkAdapter(command));
73          }
74          catch (WorkException wex)
75          {
76              // unfortunately RejectedExecutionException is the closest thing we have
77              // as proper RuntimeException
78              throw new RejectedExecutionException(wex);
79          }
80      }
81  
82      public void start() throws MuleException
83      {
84          // nothing to do
85      }
86  
87      public void stop() throws MuleException
88      {
89          // nothing to do
90      }
91  
92      public void dispose()
93      {
94          // nothing to do
95      }
96  
97      protected static class RunnableWorkAdapter implements Work
98      {
99          private final Runnable command;
100 
101         public RunnableWorkAdapter(Runnable command)
102         {
103             super();
104             this.command = command;
105         }
106 
107         public void release()
108         {
109             // nothing to do
110         }
111 
112         public void run()
113         {
114             command.run();
115         }
116     }
117 
118 }