1 16 package org.apache.cocoon.components.flow.apples; 17 18 import java.util.List ; 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 40 public class ApplesProcessor extends AbstractInterpreter implements Serviceable, ContinuationsDisposer { 41 42 43 private ServiceManager serviceManager; 44 45 46 public void callFunction( 47 String className, 48 List params, 49 Redirector redirector) 50 throws Exception { 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 continuationId, 78 List params, 79 Redirector redirector) 80 throws Exception { 81 82 WebContinuation wk = 83 this.continuationsMgr.lookupWebContinuation(continuationId, getInterpreterID()); 84 if (wk == null) { 85 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 processApple(params, redirector, app, wk); 98 99 } 100 101 102 private AppleController instantiateController(String className) 103 throws Exception { 104 105 108 Class clazz = Class.forName(className); 109 Object o = clazz.newInstance(); 110 return (AppleController) o; 111 } 112 113 114 115 private void processApple( 116 List params, 117 Redirector redirector, 118 AppleController app, 119 WebContinuation wk) 120 throws Exception { 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 if (app instanceof Disposable) { 133 try { 134 ((Disposable)app).dispose(); 135 } catch (Exception 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 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 this.forwardTo(uri, res.getData(), wk, redirector); 152 } 153 154 } 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 |