1 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 ; 28 import java.util.Map ; 29 30 37 public abstract class AbstractContinuable implements Continuable { 38 39 private ContinuationContext getContext() { 40 if (Continuation.currentContinuation()==null) 41 throw new IllegalStateException ("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 uri) { 50 sendPageAndWait(uri, new VarMap()); 51 } 52 53 public void sendPageAndWait(String uri, Object 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 ("Pipeline has already been processed for this request"); 66 } 67 try { 68 context.getRedirector().redirect(false, uri); 69 } catch (Exception e) { 70 throw new CascadingRuntimeException("Cannot redirect to '"+uri+"'", e); 71 } 72 } else { 73 throw new IllegalArgumentException ("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 uri) { 80 sendPage(uri, new VarMap()); 81 } 82 83 public void sendPage(String uri, Object 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 ("Pipeline has already been processed for this request"); 96 } 97 try { 98 context.getRedirector().redirect(false, uri); 99 } catch (Exception e) { 100 throw new CascadingRuntimeException("Cannot redirect to '"+uri+"'", e); 101 } 102 } else { 103 throw new IllegalArgumentException ("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 getObjectModel() { 112 return ContextHelper.getObjectModel(getContext().getAvalonContext()); 113 } 114 115 public Parameters getParameters() { 116 return getContext().getParameters(); 117 } 118 119 public void processPipelineTo(String uri, Object bizdata, OutputStream 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 e) { 129 throw new CascadingRuntimeException("Cannot process pipeline to '"+uri+"'", e); 130 } finally { 131 pipeUtil.dispose(); 132 } 133 } 134 135 public void redirectTo(String uri) { 136 try { 137 getContext().getRedirector().redirect(false, uri); 138 } catch (Exception 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 150 public Object getComponent(String id) { 151 try { 152 return getContext().getServiceManager().lookup(id); 153 } catch (Exception e) { 154 throw new CascadingRuntimeException("Cannot lookup component '"+id+"'", e); 155 } 156 } 157 158 163 public void releaseComponent( Object component ) { 164 if (component != null) { 165 getContext().getServiceManager().release(component); 166 } 167 } 168 } 169 | Popular Tags |