01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.environment;
18:
19: import org.apache.cocoon.components.CocoonComponentManager;
20:
21: /**
22: * A <code>Runnable</code> wrapper or base class that inherits the execution
23: * context of the thread creating it, as it was at the time of creation.
24: * <p>
25: * It is meant to be used when processing of a request is to be split across several
26: * cooperating threads (e.g. parallel aggregation).
27: * <p>
28: * <strong>Note</strong>: a <code>CocoonRunnable</code> should not live longer than the
29: * end of the execution of the request in the creating thread, otherwise some unexpected
30: * behaviours may happen because the parent's environment has been released.
31: *
32: * @since 2.1.8
33: * @version $Id: CocoonRunnable.java 433543 2006-08-22 06:22:54Z crossley $
34: */
35: public class CocoonRunnable extends
36: CocoonComponentManager.AbstractCocoonRunnable {
37: Runnable target;
38:
39: /**
40: * Creates an empty <code>CocoonRunnable</code> and copies the environment context
41: * of the calling thread, for later use when calling {@link #doRun()}. Users of this
42: * constructor will override the {@link #doRun()} method where the actual job gets done.
43: */
44: public CocoonRunnable() {
45: // Nothing special here
46: }
47:
48: /**
49: * Wraps an existing <code>Runnable</code> and copies the environment context of
50: * the calling thread, for later use when the <code>Runnable</code>'s <code>run()</code>
51: * method is called.
52: *
53: * @param target the wrapped <code>Runnable</code>
54: */
55: public CocoonRunnable(Runnable target) {
56: this .target = target;
57: }
58:
59: /**
60: * Does the actual job, in the environment of the creating thread. Calls the wrapped
61: * <code>Runnable</code> if one was given, and does nothing otherwise.
62: */
63: protected void doRun() {
64: if (target != null) {
65: target.run();
66: }
67: }
68: }
|