View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.jca;
8   
9   import org.mule.api.MuleException;
10  import org.mule.config.ImmutableThreadingProfile;
11  
12  import javax.resource.spi.work.ExecutionContext;
13  import javax.resource.spi.work.Work;
14  import javax.resource.spi.work.WorkException;
15  import javax.resource.spi.work.WorkListener;
16  
17  import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException;
18  
19  /**
20   * <code>DelegateWorkManager</code> is a wrapper around a WorkManager provided by a
21   * JCA container.
22   */
23  public class DelegateWorkManager implements org.mule.api.context.WorkManager
24  {
25      private final javax.resource.spi.work.WorkManager workManager;
26  
27      public DelegateWorkManager(javax.resource.spi.work.WorkManager workManager2)
28      {
29          this.workManager = workManager2;
30      }
31  
32      public void doWork(Work work) throws WorkException
33      {
34          workManager.doWork(work);
35      }
36  
37      public void doWork(Work work, long l, ExecutionContext executionContext, WorkListener workListener)
38          throws WorkException
39      {
40          workManager.doWork(work, l, executionContext, workListener);
41      }
42  
43      public long startWork(Work work) throws WorkException
44      {
45          return workManager.startWork(work);
46      }
47  
48      public long startWork(Work work, long l, ExecutionContext executionContext, WorkListener workListener)
49          throws WorkException
50      {
51          return workManager.startWork(work, l, executionContext, workListener);
52      }
53  
54      public void scheduleWork(Work work) throws WorkException
55      {
56          workManager.scheduleWork(work);
57      }
58  
59      public void scheduleWork(Work work, long l, ExecutionContext executionContext, WorkListener workListener)
60          throws WorkException
61      {
62          workManager.scheduleWork(work, l, executionContext, workListener);
63      }
64  
65      public void execute(Runnable command)
66      {
67          try
68          {
69              this.scheduleWork(new RunnableWorkAdapter(command));
70          }
71          catch (WorkException wex)
72          {
73              // unfortunately RejectedExecutionException is the closest thing we have
74              // as proper RuntimeException
75              throw new RejectedExecutionException(wex);
76          }
77      }
78  
79      public void start() throws MuleException
80      {
81          // nothing to do
82      }
83  
84      public boolean isStarted()
85      {
86          return true;
87      }
88  
89      public void stop() throws MuleException
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     public ImmutableThreadingProfile getThreadingProfile()
121     {
122         throw new UnsupportedOperationException("Container does not have a Mule ThreadingProfile");
123     }
124 
125 }