1 16 package org.apache.axis2.engine; 17 18 import org.apache.axis2.context.ConfigurationContext; 19 import org.apache.axis2.context.MessageContext; 20 import org.apache.axis2.context.OperationContext; 21 import org.apache.axis2.description.OperationDescription; 22 import org.apache.axis2.description.TransportOutDescription; 23 import org.apache.axis2.om.OMAbstractFactory; 24 import org.apache.axis2.soap.*; 25 import org.apache.axis2.soap.impl.llom.SOAPProcessingException; 26 import org.apache.axis2.soap.impl.llom.soap12.SOAP12Constants; 27 import org.apache.axis2.transport.TransportSender; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import java.util.ArrayList ; 32 33 37 public class AxisEngine { 38 41 private Log log = LogFactory.getLog(getClass()); 42 private ConfigurationContext engineContext; 43 44 47 public AxisEngine(ConfigurationContext engineContext) { 48 log.info("Axis Engine Started"); 49 this.engineContext = engineContext; 50 } 51 52 64 public void send(MessageContext msgContext) throws AxisFault { 65 try { 66 verifyContextBuilt(msgContext); 67 OperationContext operationContext = msgContext.getOperationContext(); 68 69 ArrayList phases = operationContext.getAxisOperation().getPhasesOutFlow(); 70 if (msgContext.isPaused()) { 71 resumeInvocationPhases(phases, msgContext); 72 } else { 73 invokePhases(phases, msgContext); 74 } 75 76 TransportOutDescription transportOut = msgContext.getTransportOut(); 77 78 TransportSender sender = transportOut.getSender(); 79 sender.invoke(msgContext); 80 } catch (Throwable e) { 81 handleFault(msgContext, e); 82 } 83 } 84 85 97 public void receive(MessageContext msgContext) throws AxisFault { 98 boolean paused = msgContext.isPaused(); 99 try { 100 ConfigurationContext sysCtx = msgContext.getSystemContext(); 101 ArrayList phases = 102 sysCtx.getAxisConfiguration().getInPhasesUptoAndIncludingPostDispatch(); 103 104 if (paused) { 105 resumeInvocationPhases(phases, msgContext); 106 } else { 107 invokePhases(phases, msgContext); 108 } 109 verifyContextBuilt(msgContext); 110 111 OperationContext operationContext = msgContext.getOperationContext(); 112 OperationDescription operationDescription = operationContext.getAxisOperation(); 113 phases = operationDescription.getRemainingPhasesInFlow(); 114 115 if (paused) { 116 resumeInvocationPhases(phases, msgContext); 117 } else { 118 invokePhases(phases, msgContext); 119 } 120 paused = msgContext.isPaused(); 121 if (msgContext.isServerSide() && !paused) { 122 MessageReceiver reciver = operationDescription.getMessageReciever(); 124 reciver.recieve(msgContext); 125 } 126 } catch (Throwable e) { 127 handleFault(msgContext, e); 128 } 129 } 130 131 140 public void handleFault(MessageContext context, Throwable e) throws AxisFault { 141 boolean serverSide = context.isServerSide(); 142 log.error("Error Ocurred", e); 143 if (serverSide && !context.isProcessingFault()) { 144 context.setProcessingFault(true); 145 146 MessageContext faultContext = 148 new MessageContext(engineContext, 149 context.getSessionContext(), 150 context.getTransportIn(), 151 context.getTransportOut()); 152 153 if (context.getFaultTo() != null) { 154 faultContext.setFaultTo(context.getFaultTo()); 155 } else { 156 Object writer = context.getProperty(MessageContext.TRANSPORT_OUT); 157 if (writer != null) { 158 faultContext.setProperty(MessageContext.TRANSPORT_OUT, writer); 159 } else { 160 log.error("Error in fault flow", e); 162 e.printStackTrace(); 163 } 164 } 165 166 faultContext.setOperationContext(context.getOperationContext()); 167 faultContext.setProcessingFault(true); 168 faultContext.setServerSide(true); 169 SOAPEnvelope envelope = null; 170 171 try { 172 173 if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(context.getEnvelope().getNamespace().getName())) { 174 envelope = OMAbstractFactory.getSOAP12Factory().getDefaultFaultEnvelope(); 175 } else { 176 envelope = OMAbstractFactory.getSOAP11Factory().getDefaultFaultEnvelope(); 177 } 178 } catch (SOAPProcessingException e1) { 179 throw new AxisFault(e1); 180 } 181 182 SOAPBody body = envelope.getBody(); 184 185 body.getFault().setException(new AxisFault(e.getMessage(), e)); 187 extractFaultInformationFromMessageContext(context, envelope.getBody().getFault()); 188 189 faultContext.setEnvelope(envelope); 190 191 OperationContext opContext = context.getOperationContext(); 192 if (opContext != null) { 193 OperationDescription axisOperation = opContext.getAxisOperation(); 194 ArrayList phases = axisOperation.getPhasesOutFaultFlow(); 195 invokePhases(phases, context); 196 } 197 TransportSender sender = context.getTransportOut().getSender(); 199 sender.invoke(faultContext); 200 } else if (!serverSide) { 201 throw new AxisFault("", e); 203 } else { 204 log.error("Error in fault flow", e); 206 } 207 } 208 209 private void extractFaultInformationFromMessageContext(MessageContext context, SOAPFault fault) { 210 Object faultCode = context.getProperty(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME); 211 if (faultCode != null) { 212 fault.setCode((SOAPFaultCode) faultCode); 213 } 214 215 Object faultReason = context.getProperty(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME); 216 if (faultReason != null) { 217 fault.setReason((SOAPFaultReason) faultReason); 218 } 219 220 Object faultRole = context.getProperty(SOAP12Constants.SOAP_FAULT_ROLE_LOCAL_NAME); 221 if (faultRole != null) { 222 fault.getRole().setText((String ) faultRole); 223 } 224 225 Object faultNode = context.getProperty(SOAP12Constants.SOAP_FAULT_NODE_LOCAL_NAME); 226 if (faultNode != null) { 227 fault.getNode().setText((String ) faultNode); 228 } 229 230 Object faultDetail = context.getProperty(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME); 231 if (faultDetail != null) { 232 fault.setDetail((SOAPFaultDetail) faultDetail); 233 } 234 } 235 236 private void verifyContextBuilt(MessageContext msgctx) throws AxisFault { 237 if (msgctx.getSystemContext() == null) { 238 throw new AxisFault("ConfigurationContext can not be null"); 239 } 240 if (msgctx.getOperationContext() == null) { 241 throw new AxisFault("OperationContext can not be null"); 242 } 243 if (msgctx.getServiceContext() == null) { 244 throw new AxisFault("ServiceContext can not be null"); 245 } 246 } 247 248 private void invokePhases(ArrayList phases, MessageContext msgctx) throws AxisFault { 249 int count = phases.size(); 250 for (int i = 0; (i < count && !msgctx.isPaused()); i++) { 251 Phase phase = (Phase) phases.get(i); 252 phase.invoke(msgctx); 253 } 254 } 255 256 public void resumeInvocationPhases(ArrayList phases, MessageContext msgctx) throws AxisFault { 257 msgctx.setPausedFalse(); 258 int count = phases.size(); 259 boolean foudMatch = false; 260 261 for (int i = 0; i < count && !msgctx.isPaused(); i++) { 262 Phase phase = (Phase) phases.get(i); 263 if (phase.getPhaseName().equals(msgctx.getPausedPhaseName())) { 264 foudMatch = true; 265 phase.invokeStartFromHandler(msgctx.getPausedHandlerName(), msgctx); 266 } else { 267 if (foudMatch) { 268 phase.invoke(msgctx); 269 } 270 271 } 272 } 273 } 274 275 276 277 284 public Object store(ConfigurationContext context, Object obj) { 285 return context.getStorage().put(obj); 286 } 287 288 296 public Object retrieve(ConfigurationContext context, Object key) { 297 return context.getStorage().get(key); 298 } 299 300 307 public Object remove(ConfigurationContext context, Object key) { 308 return context.getStorage().remove(key); 309 } 310 311 317 public boolean clearStorage(ConfigurationContext context) { 318 return context.getStorage().clean(); 319 } 320 } 321 | Popular Tags |