1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20
21 package org.apache.commons.pipeline.driver;
22
23 import java.util.concurrent.BlockingQueue;
24 import java.util.concurrent.LinkedBlockingQueue;
25 import org.apache.commons.pipeline.Stage;
26 import org.apache.commons.pipeline.StageContext;
27 import org.apache.commons.pipeline.StageDriver;
28 import org.apache.commons.pipeline.StageDriverFactory;
29 import org.apache.commons.pipeline.util.BlockingQueueFactory;
30
31 /**
32 * This factory is used to create DedicatedThreadStageDriver instances configured
33 * to run specific stages.
34 *
35 */
36 public class DedicatedThreadStageDriverFactory implements StageDriverFactory {
37
38 /** Creates a new instance of DedicatedThreadStageDriverFactory */
39 public DedicatedThreadStageDriverFactory() {
40 }
41
42 /**
43 * Creates the new {@link DedicatedThreadStageDriver} based upon the configuration
44 * of this factory instance
45 * @param stage The stage to be run by the newly created driver
46 * @param context The context in which the stage will be run
47 * @return the newly created driver
48 */
49 public StageDriver createStageDriver(Stage stage, StageContext context) {
50 try {
51 return new DedicatedThreadStageDriver(stage, context, queueFactory.createQueue(), timeout, faultTolerance);
52 } catch (Exception e) {
53 throw new IllegalStateException("Instantiation of driver failed due to illegal factory state.", e);
54 }
55 }
56
57 /**
58 * Holds value of property timeout.
59 */
60 private long timeout = 500;
61
62 /**
63 * Timeout (in milliseconds) for queue polling to ensure deadlock cannot
64 * occur on thread termination. Default value is 500 ms.
65 * @return Value of property timeout.
66 */
67 public long getTimeout() {
68 return this.timeout;
69 }
70
71 /**
72 * Setter for property timeout.
73 * @param timeout New value of property timeout.
74 */
75 public void setTimeout(long timeout) {
76 this.timeout = timeout;
77 }
78
79 /**
80 * Holds value of property faultTolerance.
81 */
82 private FaultTolerance faultTolerance = FaultTolerance.NONE;
83
84 /**
85 * Getter for property faultTolerance. See {@link FaultTolerance} for valid values
86 * and enumation meanings.
87 * @return Value of property faultTolerance.
88 */
89 public FaultTolerance getFaultTolerance() {
90 return this.faultTolerance;
91 }
92
93 /**
94 * Setter for property faultTolerance.
95 *
96 * @param faultTolerance New value of property faultTolerance.
97 */
98 public void setFaultTolerance(FaultTolerance faultTolerance) {
99 this.faultTolerance = faultTolerance;
100 }
101
102 /**
103 * Convenience setter for property faultTolerance for use by Digester.
104 *
105 * @param level New value of property level ("ALL","CHECKED", or "NONE").
106 */
107 public void setFaultToleranceLevel(String level) {
108 this.faultTolerance = FaultTolerance.valueOf(level);
109 }
110
111 /**
112 * Holds value of property queueFactory.
113 */
114 private BlockingQueueFactory<?> queueFactory = new BlockingQueueFactory.LinkedBlockingQueueFactory();
115
116 /**
117 * Getter for property queueFactory.
118 * @return Value of property queueFactory.
119 */
120 public BlockingQueueFactory<?> getQueueFactory() {
121 return this.queueFactory;
122 }
123
124 /**
125 * Setter for property queueFactory.
126 * @param queueFactory New value of property queueFactory.
127 */
128 public void setQueueFactory(BlockingQueueFactory<?> queueFactory) {
129 this.queueFactory = queueFactory;
130 }
131 }