KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > java > Continuation


1 /*
2  * Copyright 1999-2004 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.components.flow.java;
17
18 import java.util.HashMap JavaDoc;
19
20 /**
21  * Continations object to store the current execution. The contiunation
22  * object can only used once.
23  *
24  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
25  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
26  * @version CVS $Id: Continuation.java 30932 2004-07-29 17:35:38Z vgritsenko $
27  */

28 public class Continuation {
29     private ContinuationStack stack;
30     private Object JavaDoc context;
31
32     private static HashMap JavaDoc continuationsmap = new HashMap JavaDoc();
33
34     public boolean restoring = false;
35     public boolean capturing = false;
36
37     /**
38      * Create new continuation.
39      */

40     public Continuation(Object JavaDoc context) {
41         stack = new ContinuationStack();
42         this.context = context;
43     }
44
45     /**
46      * Create a new continuation, which continue a previous continuation.
47      */

48     public Continuation(Continuation parent, Object JavaDoc context) {
49         if (parent == null)
50             throw new NullPointerException JavaDoc("Parent continuation is null");
51
52         stack = new ContinuationStack(parent.stack);
53         this.context = context;
54         restoring = true;
55     }
56
57     /**
58      * Return the stack, which is used to store the frame information.
59      */

60     public ContinuationStack getStack() {
61         return stack;
62     }
63
64     /**
65      * Return context object, which is associated to this continuation.
66      */

67     public Object JavaDoc getContext() {
68         return context;
69     }
70
71     /**
72      * Stop the running continuation.
73      */

74     public static void suspend() {
75
76         Continuation continuation = Continuation.currentContinuation();
77
78         if (continuation == null)
79             throw new IllegalStateException JavaDoc("No continuation is running");
80
81         if (continuation.restoring) {
82             continuation.capturing = false;
83         } else {
84             continuation.capturing = true;
85         }
86         continuation.restoring = false;
87     }
88
89     /**
90      * True, if the continuation restores the previous stack trace to the
91      * last invocation of suspend().
92      */

93     public boolean isRestoring() {
94         return restoring;
95     }
96
97     /**
98      * True, is the continuation freeze the strack trace, and stops the continuation.
99      */

100     public boolean isCapturing() {
101         return capturing;
102     }
103
104     /**
105      * Bind the continuation to running thread.
106      */

107     public void registerThread() {
108         synchronized (continuationsmap) {
109             continuationsmap.put(Thread.currentThread(), this);
110         }
111     }
112
113     /**
114      * Unbind the continuation to running thread.
115      */

116     public void deregisterThread() {
117         synchronized (continuationsmap) {
118             continuationsmap.remove(Thread.currentThread());
119         }
120     }
121
122     /**
123      * Return the continuation, which is associated to the
124      * current thread.
125      */

126     public static Continuation currentContinuation() {
127         synchronized (continuationsmap) {
128             Thread JavaDoc t = Thread.currentThread();
129             return (Continuation) continuationsmap.get(t);
130         }
131     }
132 }
133
Popular Tags