1 16 package org.apache.cocoon.components.treeprocessor.sitemap; 17 18 import org.apache.avalon.framework.component.ComponentException; 19 import org.apache.avalon.framework.component.ComponentManager; 20 import org.apache.avalon.framework.component.Composable; 21 import org.apache.avalon.framework.logger.AbstractLogEnabled; 22 import org.apache.avalon.framework.logger.Logger; 23 24 import org.apache.cocoon.Constants; 25 import org.apache.cocoon.ResourceNotFoundException; 26 import org.apache.cocoon.components.notification.Notifying; 27 import org.apache.cocoon.components.notification.NotifyingBuilder; 28 import org.apache.cocoon.components.pipeline.ProcessingPipeline; 29 import org.apache.cocoon.components.treeprocessor.InvokeContext; 30 import org.apache.cocoon.components.treeprocessor.ProcessingNode; 31 import org.apache.cocoon.environment.Environment; 32 import org.apache.cocoon.environment.ObjectModelHelper; 33 34 import java.io.IOException ; 35 import java.util.Map ; 36 37 44 public class ErrorHandlerHelper extends AbstractLogEnabled 45 implements Composable { 46 47 private ComponentManager manager; 48 49 52 protected Logger handledErrorsLogger; 53 54 58 private HandleErrorsNode error404; 59 60 63 private HandleErrorsNode error500; 64 65 public void enableLogging(Logger logger) { 66 super.enableLogging(logger); 67 this.handledErrorsLogger = logger.getChildLogger("handled-errors"); 68 } 69 70 73 public void compose(ComponentManager manager) { 74 this.manager = manager; 75 } 76 77 void set404Handler(ProcessingNode node) { 78 this.error404 = (HandleErrorsNode) node; 79 } 80 81 void set500Handler(ProcessingNode node) { 82 this.error500 = (HandleErrorsNode) node; 83 } 84 85 88 public boolean isEmpty() { 89 return this.error404 == null && this.error500 == null; 90 } 91 92 public boolean isInternal() { 93 return this.error500 != null && this.error500.isInternal(); 94 } 95 96 public boolean isExternal() { 97 return this.error500 != null && this.error500.isExternal(); 98 } 99 100 103 public boolean invokeErrorHandler(Exception ex, 104 Environment env, 105 InvokeContext context) 106 throws Exception { 107 return prepareErrorHandler(ex, env, context) != null; 108 } 109 110 117 public ProcessingPipeline prepareErrorHandler(Exception ex, 118 Environment env, 119 InvokeContext context) 120 throws Exception { 121 boolean internal = !env.isExternal() && !env.isInternalRedirect(); 122 123 if (internal && !isInternal()) { 124 throw ex; 126 } else if (!internal && !isExternal()) { 127 throw ex; 129 } else if (!internal && error404 != null && ex instanceof ResourceNotFoundException) { 130 return prepareErrorHandler(error404, ex, env, context); 132 } else if (error500 != null) { 133 return prepareErrorHandler(error500, ex, env, context); 135 } 136 137 throw ex; 139 } 140 141 144 public boolean invokeErrorHandler(ProcessingNode node, 145 Exception ex, 146 Environment env, 147 InvokeContext context) 148 throws Exception { 149 return prepareErrorHandler(node, ex, env, context) != null; 150 } 151 152 160 private ProcessingPipeline prepareErrorHandler(ProcessingNode node, 161 Exception ex, 162 Environment env, 163 InvokeContext context) 164 throws Exception { 165 if (ex instanceof ResourceNotFoundException) { 166 this.handledErrorsLogger.error(ex.getMessage()); 167 } else { 168 this.handledErrorsLogger.error(ex.getMessage(), ex); 169 } 170 171 try { 172 prepare(context, env, ex); 173 174 InvokeContext errorContext = new InvokeContext(context.isBuildingPipelineOnly()); 176 errorContext.enableLogging(getLogger()); 177 errorContext.setRedirector(context.getRedirector()); 178 errorContext.compose(this.manager); 179 try { 180 if (node.invoke(env, errorContext)) { 182 return errorContext.getProcessingPipeline(); 184 } 185 } finally { 186 errorContext.dispose(); 187 } 188 } catch (Exception e) { 189 getLogger().error("An exception occured while handling errors at " + node.getLocation(), e); 190 throw e; 192 } 193 194 throw ex; 196 } 197 198 201 private void prepare(InvokeContext context, Environment env, Exception ex) 202 throws IOException , ComponentException { 203 Map objectModel = env.getObjectModel(); 204 if (objectModel.get(Constants.NOTIFYING_OBJECT) == null) { 205 207 if (!context.isBuildingPipelineOnly()) { 210 env.tryResetResponse(); 211 } 212 213 NotifyingBuilder notifyingBuilder = (NotifyingBuilder) this.manager.lookup(NotifyingBuilder.ROLE); 215 Notifying currentNotifying = null; 216 try { 217 currentNotifying = notifyingBuilder.build(this, ex); 218 } finally { 219 this.manager.release(notifyingBuilder); 220 } 221 222 objectModel.put(Constants.NOTIFYING_OBJECT, currentNotifying); 224 225 objectModel.put(ObjectModelHelper.THROWABLE_OBJECT, ex); 227 } 228 } 229 } 230 | Popular Tags |