1 16 package org.apache.cocoon.components.flow; 17 18 import java.io.OutputStream ; 19 import java.util.ArrayList ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.activity.Disposable; 23 import org.apache.avalon.framework.component.Component; 24 import org.apache.avalon.framework.configuration.Configurable; 25 import org.apache.avalon.framework.configuration.Configuration; 26 import org.apache.avalon.framework.configuration.ConfigurationException; 27 import org.apache.avalon.framework.context.ContextException; 28 import org.apache.avalon.framework.context.Contextualizable; 29 import org.apache.avalon.framework.logger.AbstractLogEnabled; 30 import org.apache.avalon.framework.service.ServiceException; 31 import org.apache.avalon.framework.service.ServiceManager; 32 import org.apache.avalon.framework.service.Serviceable; 33 import org.apache.avalon.framework.thread.SingleThreaded; 34 import org.apache.cocoon.Constants; 35 import org.apache.cocoon.components.ContextHelper; 36 import org.apache.cocoon.components.flow.util.PipelineUtil; 37 import org.apache.cocoon.environment.Context; 38 import org.apache.cocoon.environment.Redirector; 39 import org.apache.excalibur.source.SourceUtil; 40 41 58 public abstract class AbstractInterpreter 59 extends AbstractLogEnabled 60 implements Component, Serviceable, Contextualizable, Interpreter, 61 SingleThreaded, Configurable, Disposable { 62 63 private String instanceID; 65 66 protected org.apache.avalon.framework.context.Context avalonContext; 67 68 71 protected ArrayList needResolve = new ArrayList (); 72 73 protected org.apache.cocoon.environment.Context context; 74 protected ServiceManager manager; 75 protected ContinuationsManager continuationsMgr; 76 77 81 protected boolean reloadScripts; 82 83 87 protected long checkTime; 88 89 public AbstractInterpreter() { 90 } 91 92 96 public void setInterpreterID(String interpreterID) { 97 this.instanceID = interpreterID; 98 } 99 100 106 public String getInterpreterID() { 107 return this.instanceID; 108 } 109 110 public void configure(Configuration config) throws ConfigurationException { 111 reloadScripts = config.getChild("reload-scripts").getValueAsBoolean(false); 112 checkTime = config.getChild("check-time").getValueAsLong(1000L); 113 } 114 115 118 public void service(ServiceManager sm) throws ServiceException { 119 this.manager = sm; 120 this.continuationsMgr = (ContinuationsManager)sm.lookup(ContinuationsManager.ROLE); 121 } 122 123 public void contextualize(org.apache.avalon.framework.context.Context context) 124 throws ContextException{ 125 this.avalonContext = context; 126 this.context = (Context)context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 127 } 128 129 132 public void dispose() { 133 if ( this.manager != null ) { 134 this.manager.release( this.continuationsMgr ); 135 this.continuationsMgr = null; 136 this.manager = null; 137 } 138 } 139 140 167 public void register(String source) { 168 synchronized (this) { 169 needResolve.add(source); 170 } 171 } 172 173 182 public void process(String uri, Object biz, OutputStream out) 183 throws Exception { 184 PipelineUtil pipeUtil = new PipelineUtil(); 186 try { 187 pipeUtil.contextualize(this.avalonContext); 188 pipeUtil.service(this.manager); 189 pipeUtil.processToStream(uri, biz, out); 190 } finally { 191 pipeUtil.dispose(); 192 } 193 } 194 195 public void forwardTo(String uri, Object bizData, 196 WebContinuation continuation, 197 Redirector redirector) 198 throws Exception { 199 if (SourceUtil.indexOfSchemeColon(uri) == -1) { 200 uri = "cocoon:/" + uri; 201 Map objectModel = ContextHelper.getObjectModel(this.avalonContext); 202 FlowHelper.setWebContinuation(objectModel, continuation); 203 FlowHelper.setContextObject(objectModel, bizData); 204 if (redirector.hasRedirected()) { 205 throw new IllegalStateException ("Pipeline has already been processed for this request"); 206 } 207 objectModel.put("cocoon:forward", "true"); 209 redirector.redirect(false, uri); 210 } else { 211 throw new Exception ("uri is not allowed to contain a scheme (cocoon:/ is always automatically used)"); 212 } 213 } 214 } 215 | Popular Tags |