View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.workflow.api;
23  
24  import java.util.Iterator;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  import javax.xml.bind.annotation.XmlAccessType;
29  import javax.xml.bind.annotation.XmlAccessorType;
30  import javax.xml.bind.annotation.XmlAttribute;
31  import javax.xml.bind.annotation.XmlElement;
32  import javax.xml.bind.annotation.XmlElementWrapper;
33  import javax.xml.bind.annotation.XmlRootElement;
34  import javax.xml.bind.annotation.XmlType;
35  import javax.xml.bind.annotation.adapters.XmlAdapter;
36  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
37  
38  /**
39   * See {@link WorkflowOperationDefinition}
40   */
41  @XmlType(name = "operation-definition", namespace = "http://workflow.opencastproject.org")
42  @XmlRootElement(name = "operation-definition", namespace = "http://workflow.opencastproject.org")
43  @XmlAccessorType(XmlAccessType.FIELD)
44  public class WorkflowOperationDefinitionImpl implements WorkflowOperationDefinition {
45  
46    @XmlAttribute(name = "id")
47    protected String id;
48  
49    @XmlAttribute(name = "description")
50    protected String description;
51  
52    @XmlAttribute(name = "fail-on-error")
53    protected boolean failWorkflowOnException;
54  
55    @XmlAttribute(name = "if")
56    protected String executeCondition;
57  
58    @XmlAttribute(name = "unless")
59    protected String skipCondition;
60  
61    @XmlAttribute(name = "exception-handler-workflow")
62    protected String exceptionHandlingWorkflow;
63  
64    @XmlElement(name = "configuration")
65    @XmlElementWrapper(name = "configurations")
66    protected Set<JaxbWorkflowConfiguration> configurations;
67  
68    @XmlAttribute(name = "max-attempts")
69    protected int maxAttempts;
70  
71    @XmlJavaTypeAdapter(RetryStrategy.Adapter.class)
72    @XmlAttribute(name = "retry-strategy")
73    protected RetryStrategy retryStrategy;
74  
75    /** A no-arg constructor is needed by JAXB */
76    public WorkflowOperationDefinitionImpl() {
77      super();
78      this.maxAttempts = 1;
79      this.failWorkflowOnException = true;
80      this.retryStrategy = RetryStrategy.NONE;
81    }
82  
83    /**
84     * @param id
85     *          The unique name of this operation
86     * @param description
87     *          The description of what this operation does
88     * @param exceptionHandlingWorkflow
89     *          The workflow to run when a workflow operation exception is thrown
90     * @param failWorkflowOnException
91     *          Whether an exception thrown by this operation should fail the entire {@link WorkflowInstance}
92     */
93    public WorkflowOperationDefinitionImpl(String id, String description, String exceptionHandlingWorkflow,
94            boolean failWorkflowOnException) {
95      this();
96      this.id = id;
97      this.description = description;
98      this.exceptionHandlingWorkflow = exceptionHandlingWorkflow;
99      this.failWorkflowOnException = failWorkflowOnException;
100   }
101 
102   public String getId() {
103     return id;
104   }
105 
106   public void setId(String id) {
107     this.id = id;
108   }
109 
110   public String getDescription() {
111     return description;
112   }
113 
114   public void setDescription(String description) {
115     this.description = description;
116   }
117 
118   public void setExecutionCondition(String condition) {
119     this.executeCondition = condition;
120   }
121 
122   public String getExecutionCondition() {
123     return executeCondition;
124   }
125 
126   public void setSkipCondition(String condition) {
127     this.skipCondition = condition;
128   }
129 
130   public String getSkipCondition() {
131     return skipCondition;
132   }
133 
134   /**
135    * {@inheritDoc}
136    *
137    * @see org.opencastproject.workflow.api.WorkflowOperationDefinition#getExceptionHandlingWorkflow()
138    */
139   public String getExceptionHandlingWorkflow() {
140     return exceptionHandlingWorkflow;
141   }
142 
143   public void setExceptionHandlingWorkflow(String exceptionHandlingWorkflow) {
144     this.exceptionHandlingWorkflow = exceptionHandlingWorkflow;
145   }
146 
147   /**
148    * {@inheritDoc}
149    *
150    * @see org.opencastproject.workflow.api.WorkflowOperationDefinition#isFailWorkflowOnException()
151    */
152   public boolean isFailWorkflowOnException() {
153     return failWorkflowOnException;
154   }
155 
156   public void setFailWorkflowOnException(boolean failWorkflowOnException) {
157     this.failWorkflowOnException = failWorkflowOnException;
158   }
159 
160   /**
161    * Allows JAXB handling of {@link WorkflowOperationDefinition} interfaces.
162    */
163   static class Adapter extends XmlAdapter<WorkflowOperationDefinitionImpl, WorkflowOperationDefinition> {
164     public WorkflowOperationDefinitionImpl marshal(WorkflowOperationDefinition op) throws Exception {
165       return (WorkflowOperationDefinitionImpl) op;
166     }
167 
168     public WorkflowOperationDefinition unmarshal(WorkflowOperationDefinitionImpl op) throws Exception {
169       return op;
170     }
171   }
172 
173   /**
174    * {@inheritDoc}
175    *
176    * @see org.opencastproject.workflow.api.WorkflowInstance#getConfiguration(java.lang.String)
177    */
178   public String getConfiguration(String key) {
179     if (key == null || configurations == null) {
180       return null;
181     }
182     for (JaxbWorkflowConfiguration config : configurations) {
183       if (config.getKey().equals(key)) {
184         return config.getValue();
185       }
186     }
187     return null;
188   }
189 
190   /**
191    * {@inheritDoc}
192    *
193    * @see org.opencastproject.workflow.api.WorkflowInstance#removeConfiguration(java.lang.String)
194    */
195   public void removeConfiguration(String key) {
196     if (key == null || configurations == null) {
197       return;
198     }
199     for (Iterator<JaxbWorkflowConfiguration> configIter = configurations.iterator(); configIter.hasNext();) {
200       JaxbWorkflowConfiguration config = configIter.next();
201       if (config.getKey().equals(key)) {
202         configIter.remove();
203         return;
204       }
205     }
206   }
207 
208   /**
209    * {@inheritDoc}
210    *
211    * @see org.opencastproject.workflow.api.WorkflowInstance#setConfiguration(java.lang.String, java.lang.String)
212    */
213   public void setConfiguration(String key, String value) {
214     if (key == null || configurations == null) {
215       return;
216     }
217     for (JaxbWorkflowConfiguration config : configurations) {
218       if (config.getKey().equals(key)) {
219         config.setValue(value);
220         return;
221       }
222     }
223     // No configurations were found, so add a new one
224     configurations.add(new JaxbWorkflowConfiguration(key, value));
225   }
226 
227   /**
228    * {@inheritDoc}
229    *
230    * @see org.opencastproject.workflow.api.WorkflowOperationDefinition#getConfigurationKeys()
231    */
232   @Override
233   public Set<String> getConfigurationKeys() {
234     Set<String> set = new TreeSet<String>();
235     if (configurations != null) {
236       for (JaxbWorkflowConfiguration config : configurations) {
237         set.add(config.getKey());
238       }
239     }
240     return set;
241   }
242 
243   /**
244    *
245    * {@inheritDoc}
246    *
247    * @see org.opencastproject.workflow.api.WorkflowOperationDefinition#getRetryStrategy()
248    */
249   @Override
250   public RetryStrategy getRetryStrategy() {
251     return retryStrategy;
252   }
253 
254   /**
255    * @param retryStrategy
256    *          the retry strategy to set
257    */
258   public void setRetryStrategy(RetryStrategy retryStrategy) {
259     this.retryStrategy = retryStrategy;
260   }
261 
262   /**
263    * {@inheritDoc}
264    *
265    * @see org.opencastproject.workflow.api.WorkflowOperationDefinition#getMaxAttempts()
266    */
267   @Override
268   public int getMaxAttempts() {
269     return maxAttempts;
270   }
271 
272   /**
273    * @param maxAttempts
274    *          the maxAttempts to set
275    */
276   public void setMaxAttempts(int maxAttempts) {
277     this.maxAttempts = maxAttempts;
278   }
279 
280   /**
281    * {@inheritDoc}
282    *
283    * @see java.lang.Object#toString()
284    */
285   @Override
286   public String toString() {
287     return "operation definition:'" + id + "'";
288   }
289 
290 }