KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > apples > ApplesProcessor


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.apples;
17
18 import java.util.List JavaDoc;
19
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.avalon.framework.component.WrapperComponentManager;
22 import org.apache.avalon.framework.context.DefaultContext;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.avalon.framework.service.Serviceable;
26 import org.apache.cocoon.components.ContextHelper;
27 import org.apache.cocoon.components.LifecycleHelper;
28 import org.apache.cocoon.components.flow.AbstractInterpreter;
29 import org.apache.cocoon.components.flow.ContinuationsDisposer;
30 import org.apache.cocoon.components.flow.InvalidContinuationException;
31 import org.apache.cocoon.components.flow.WebContinuation;
32 import org.apache.cocoon.environment.Redirector;
33 import org.apache.cocoon.environment.Request;
34 import org.apache.cocoon.environment.Response;
35
36 /**
37  * ApplesProcessor is the core Cocoon component that provides the 'Apples'
38  * flow implementation.
39  */

40 public class ApplesProcessor extends AbstractInterpreter implements Serviceable, ContinuationsDisposer {
41
42
43     private ServiceManager serviceManager;
44
45
46     public void callFunction(
47         String JavaDoc className,
48         List JavaDoc params,
49         Redirector redirector)
50         throws Exception JavaDoc {
51
52         AppleController app = instantiateController(className);
53
54         WebContinuation wk = null;
55         if (!(app instanceof StatelessAppleController)) {
56             wk = this.continuationsMgr.createWebContinuation(app, null, 0,
57                     getInterpreterID(), this);
58             if (getLogger().isDebugEnabled())
59                 getLogger().debug("Instantiated a stateful apple, continuationid = " + wk.getId());
60         }
61
62         DefaultContext appleContext = new DefaultContext(avalonContext);
63         if (wk != null) {
64             appleContext.put("continuation-id", wk.getId());
65         }
66         
67         LifecycleHelper.setupComponent( app, getLogger(), appleContext,
68                                         this.serviceManager, new WrapperComponentManager(this.serviceManager),
69                                         null, null, true);
70         
71         processApple(params, redirector, app, wk);
72     }
73
74
75
76     public void handleContinuation(
77         String JavaDoc continuationId,
78         List JavaDoc params,
79         Redirector redirector)
80         throws Exception JavaDoc {
81
82         WebContinuation wk =
83             this.continuationsMgr.lookupWebContinuation(continuationId, getInterpreterID());
84         if (wk == null) {
85             // Throw an InvalidContinuationException to be handled inside the
86
// <map:handle-errors> sitemap element.
87
throw new InvalidContinuationException(
88                 "The continuation ID " + continuationId + " is invalid.");
89         }
90
91         AppleController app =
92             (AppleController) wk.getContinuation();
93
94         getLogger().debug("found apple from continuation: " + app);
95
96         // TODO access control checks? exception to be thrown for illegal access?
97
processApple(params, redirector, app, wk);
98
99     }
100
101
102     private AppleController instantiateController(String JavaDoc className)
103         throws Exception JavaDoc {
104
105         // TODO think about dynamic reloading of these beasts in future
106
// classloading stuf et al.
107

108         Class JavaDoc clazz = Class.forName(className);
109         Object JavaDoc o = clazz.newInstance();
110         return (AppleController) o;
111     }
112
113
114
115     private void processApple(
116         List JavaDoc params,
117         Redirector redirector,
118         AppleController app,
119         WebContinuation wk)
120         throws Exception JavaDoc {
121
122         Request cocoonRequest = ContextHelper.getRequest(this.avalonContext);
123         AppleRequest req = new DefaultAppleRequest(params, cocoonRequest);
124         Response cocoonResponse = ContextHelper.getResponse(this.avalonContext);
125         DefaultAppleResponse res = new DefaultAppleResponse(cocoonResponse);
126
127         try {
128             app.process(req, res);
129         } finally {
130             if (wk == null) {
131                 // dispose stateless apple immediatelly
132
if (app instanceof Disposable) {
133                     try {
134                         ((Disposable)app).dispose();
135                     } catch (Exception JavaDoc e) {
136                         getLogger().error("Error disposing Apple instance.", e);
137                     }
138                 }
139             }
140         }
141
142         if (res.isRedirect()) {
143             redirector.redirect(false, res.getURI());
144         } else {
145             String JavaDoc uri = res.getURI();
146             if (getLogger().isDebugEnabled()) {
147                 getLogger().debug("Apple forwards to " + uri + " with bizdata= " + res.getData() + (wk != null ? " and continuationid= " + wk.getId() : " without continuationid"));
148             }
149
150             // Note: it is ok for wk to be null
151
this.forwardTo(uri, res.getData(), wk, redirector);
152         }
153
154         //TODO allow for AppleResponse to set some boolean saying the use case
155
// is completed and the continuation can be invalidated ?
156
}
157
158
159     public void disposeContinuation(WebContinuation webContinuation) {
160         AppleController app =
161             (AppleController) webContinuation.getContinuation();
162         if (app instanceof Disposable) {
163             ((Disposable)app).dispose();
164         }
165
166     }
167
168
169     public void service(ServiceManager serviceManager) throws ServiceException {
170         super.service(serviceManager);
171         this.serviceManager = serviceManager;
172     }
173
174
175 }
176
Popular Tags