KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.CascadingRuntimeException;
19 import org.apache.avalon.framework.logger.Logger;
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.cocoon.components.ContextHelper;
22 import org.apache.cocoon.components.flow.FlowHelper;
23 import org.apache.cocoon.components.flow.util.PipelineUtil;
24 import org.apache.cocoon.environment.Request;
25 import org.apache.excalibur.source.SourceUtil;
26
27 import java.io.OutputStream JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Abstract class to add basic methods for flow handling.
32  *
33  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
34  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
35  * @version CVS $Id: AbstractContinuable.java 329923 2005-10-31 22:46:16Z sylvain $
36  */

37 public abstract class AbstractContinuable implements Continuable {
38
39     private ContinuationContext getContext() {
40         if (Continuation.currentContinuation()==null)
41             throw new IllegalStateException JavaDoc("No continuation is running");
42         return (ContinuationContext) Continuation.currentContinuation().getContext();
43     }
44   
45     public Logger getLogger() {
46         return getContext().getLogger();
47     }
48
49     public void sendPageAndWait(String JavaDoc uri) {
50         sendPageAndWait(uri, new VarMap());
51     }
52
53     public void sendPageAndWait(String JavaDoc uri, Object JavaDoc bizdata) {
54
55         ContinuationContext context = getContext();
56
57                 if (context.getLogger()!=null)
58             context.getLogger().debug("send page and wait '" + uri + "'");
59
60         FlowHelper.setContextObject(ContextHelper.getObjectModel(context.getAvalonContext()), bizdata);
61
62         if (SourceUtil.indexOfSchemeColon(uri) == -1) {
63             uri = "cocoon:/" + uri;
64             if (getContext().getRedirector().hasRedirected()) {
65                 throw new IllegalStateException JavaDoc("Pipeline has already been processed for this request");
66             }
67             try {
68                 context.getRedirector().redirect(false, uri);
69             } catch (Exception JavaDoc e) {
70                 throw new CascadingRuntimeException("Cannot redirect to '"+uri+"'", e);
71             }
72         } else {
73             throw new IllegalArgumentException JavaDoc("uri is not allowed to contain a scheme (cocoon:/ is always automatically used)");
74         }
75
76         Continuation.suspend();
77     }
78
79     public void sendPage(String JavaDoc uri) {
80         sendPage(uri, new VarMap());
81     }
82
83     public void sendPage(String JavaDoc uri, Object JavaDoc bizdata) {
84
85         ContinuationContext context = getContext();
86
87                 if (context.getLogger()!=null)
88             context.getLogger().debug("send page '" + uri + "'");
89
90         FlowHelper.setContextObject(ContextHelper.getObjectModel(context.getAvalonContext()), bizdata);
91
92         if (SourceUtil.indexOfSchemeColon(uri) == -1) {
93             uri = "cocoon:/" + uri;
94             if (getContext().getRedirector().hasRedirected()) {
95                 throw new IllegalStateException JavaDoc("Pipeline has already been processed for this request");
96             }
97             try {
98                 context.getRedirector().redirect(false, uri);
99             } catch (Exception JavaDoc e) {
100                 throw new CascadingRuntimeException("Cannot redirect to '"+uri+"'", e);
101             }
102         } else {
103             throw new IllegalArgumentException JavaDoc("uri is not allowed to contain a scheme (cocoon:/ is always automatically used)");
104         }
105     }
106
107     public Request getRequest() {
108         return ContextHelper.getRequest(getContext().getAvalonContext());
109     }
110     
111     public Map JavaDoc getObjectModel() {
112         return ContextHelper.getObjectModel(getContext().getAvalonContext());
113     }
114
115     public Parameters getParameters() {
116         return getContext().getParameters();
117     }
118
119     public void processPipelineTo(String JavaDoc uri, Object JavaDoc bizdata, OutputStream JavaDoc out) {
120
121         ContinuationContext context = getContext();
122
123         PipelineUtil pipeUtil = new PipelineUtil();
124         try {
125             pipeUtil.contextualize(context.getAvalonContext());
126             pipeUtil.service(context.getServiceManager());
127             pipeUtil.processToStream(uri, bizdata, out);
128         } catch (Exception JavaDoc e) {
129             throw new CascadingRuntimeException("Cannot process pipeline to '"+uri+"'", e);
130         } finally {
131             pipeUtil.dispose();
132         }
133     }
134
135     public void redirectTo(String JavaDoc uri) {
136         try {
137             getContext().getRedirector().redirect(false, uri);
138         } catch (Exception JavaDoc e) {
139             throw new CascadingRuntimeException("Cannot redirect to '"+uri+"'", e);
140         }
141     }
142
143     public void sendStatus(int sc) {
144         getContext().getRedirector().sendStatus(sc);
145     }
146
147     /**
148      * Access components.
149      */

150     public Object JavaDoc getComponent(String JavaDoc id) {
151         try {
152             return getContext().getServiceManager().lookup(id);
153         } catch (Exception JavaDoc e) {
154             throw new CascadingRuntimeException("Cannot lookup component '"+id+"'", e);
155         }
156     }
157
158     /**
159      * Release pooled components.
160      *
161      * @param component a component
162      */

163     public void releaseComponent( Object JavaDoc component ) {
164         if (component != null) {
165             getContext().getServiceManager().release(component);
166         }
167     }
168 }
169
Popular Tags