KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > CocoonRunnable


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.environment;
17
18 import org.apache.cocoon.components.CocoonComponentManager;
19
20 /**
21  * A <code>Runnable</code> wrapper or base class that inherits the execution
22  * context of the thread creating it, as it was at the time of creation.
23  * <p>
24  * It is meant to be used when processing of a request is to be split across several
25  * cooperating threads (e.g. parallel aggregation).
26  * <p>
27  * <strong>Note</strong>: a <code>CocoonRunnable</code> should not live longer than the
28  * end of the execution of the request in the creating thread, otherwise some unexpected
29  * behaviours may happen because the parent's environment has been released.
30  *
31  * @since 2.1.8
32  * @version $Id: CocoonRunnable.java 189932 2005-06-10 09:33:14Z sylvain $
33  */

34 public class CocoonRunnable extends CocoonComponentManager.AbstractCocoonRunnable {
35     Runnable JavaDoc target;
36
37     /**
38      * Creates an empty <code>CocoonRunnable</code> and copies the environment context
39      * of the calling thread, for later use when calling {@link #doRun()}. Users of this
40      * constructor will override the {@link #doRun()} method where the actual job gets done.
41      */

42     public CocoonRunnable() {
43         // Nothing special here
44
}
45
46     /**
47      * Wraps an existing <code>Runnable</code> and copies the environment context of
48      * the calling thread, for later use when the <code>Runnable</code>'s <code>run()</code>
49      * method is called.
50      *
51      * @param target the wrapped <code>Runnable</code>
52      */

53     public CocoonRunnable(Runnable JavaDoc target) {
54         this.target = target;
55     }
56
57     /**
58      * Does the actual job, in the environment of the creating thread. Calls the wrapped
59      * <code>Runnable</code> if one was given, and does nothing otherwise.
60      */

61     protected void doRun() {
62         if (target != null) {
63             target.run();
64         }
65     }
66 }
67
Popular Tags