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     for (JaxbWorkflowConfiguration config : configurations) {
182       if (config.getKey().equals(key))
183         return config.getValue();
184     }
185     return null;
186   }
187 
188   /**
189    * {@inheritDoc}
190    *
191    * @see org.opencastproject.workflow.api.WorkflowInstance#removeConfiguration(java.lang.String)
192    */
193   public void removeConfiguration(String key) {
194     if (key == null || configurations == null)
195       return;
196     for (Iterator<JaxbWorkflowConfiguration> configIter = configurations.iterator(); configIter.hasNext();) {
197       JaxbWorkflowConfiguration config = configIter.next();
198       if (config.getKey().equals(key)) {
199         configIter.remove();
200         return;
201       }
202     }
203   }
204 
205   /**
206    * {@inheritDoc}
207    *
208    * @see org.opencastproject.workflow.api.WorkflowInstance#setConfiguration(java.lang.String, java.lang.String)
209    */
210   public void setConfiguration(String key, String value) {
211     if (key == null || configurations == null)
212       return;
213     for (JaxbWorkflowConfiguration config : configurations) {
214       if (config.getKey().equals(key)) {
215         config.setValue(value);
216         return;
217       }
218     }
219     // No configurations were found, so add a new one
220     configurations.add(new JaxbWorkflowConfiguration(key, value));
221   }
222 
223   /**
224    * {@inheritDoc}
225    *
226    * @see org.opencastproject.workflow.api.WorkflowOperationDefinition#getConfigurationKeys()
227    */
228   @Override
229   public Set<String> getConfigurationKeys() {
230     Set<String> set = new TreeSet<String>();
231     if (configurations != null) {
232       for (JaxbWorkflowConfiguration config : configurations) {
233         set.add(config.getKey());
234       }
235     }
236     return set;
237   }
238 
239   /**
240    *
241    * {@inheritDoc}
242    *
243    * @see org.opencastproject.workflow.api.WorkflowOperationDefinition#getRetryStrategy()
244    */
245   @Override
246   public RetryStrategy getRetryStrategy() {
247     return retryStrategy;
248   }
249 
250   /**
251    * @param retryStrategy
252    *          the retry strategy to set
253    */
254   public void setRetryStrategy(RetryStrategy retryStrategy) {
255     this.retryStrategy = retryStrategy;
256   }
257 
258   /**
259    * {@inheritDoc}
260    *
261    * @see org.opencastproject.workflow.api.WorkflowOperationDefinition#getMaxAttempts()
262    */
263   @Override
264   public int getMaxAttempts() {
265     return maxAttempts;
266   }
267 
268   /**
269    * @param maxAttempts
270    *          the maxAttempts to set
271    */
272   public void setMaxAttempts(int maxAttempts) {
273     this.maxAttempts = maxAttempts;
274   }
275 
276   /**
277    * {@inheritDoc}
278    *
279    * @see java.lang.Object#toString()
280    */
281   @Override
282   public String toString() {
283     return "operation definition:'" + id + "'";
284   }
285 
286 }