1 55 56 package org.jboss.axis; 57 58 import org.jboss.axis.handlers.BasicHandler; 59 import org.jboss.axis.strategies.InvocationStrategy; 60 import org.jboss.axis.strategies.WSDLGenStrategy; 61 import org.jboss.axis.utils.Messages; 62 import org.jboss.logging.Logger; 63 import org.w3c.dom.Document ; 64 import org.w3c.dom.Element ; 65 66 import javax.xml.namespace.QName ; 67 import java.util.Enumeration ; 68 import java.util.Vector ; 69 70 82 public class SimpleChain extends BasicHandler implements Chain 83 { 84 private static Logger log = Logger.getLogger(SimpleChain.class.getName()); 85 86 protected Vector handlers = new Vector (); 87 protected boolean invoked = false; 88 89 private String CAUGHTFAULT_PROPERTY = 90 "org.jboss.axis.SimpleChain.caughtFaultInResponse"; 91 92 public void init() 93 { 94 for (int i = 0; i < handlers.size(); i++) 95 ((Handler)handlers.elementAt(i)).init(); 96 } 97 98 public void cleanup() 99 { 100 for (int i = 0; i < handlers.size(); i++) 101 ((Handler)handlers.elementAt(i)).cleanup(); 102 } 103 104 private static final HandlerIterationStrategy iVisitor = 105 new InvocationStrategy(); 106 107 private static final HandlerIterationStrategy wsdlVisitor = 108 new WSDLGenStrategy(); 109 110 115 public void invoke(MessageContext msgContext) throws AxisFault 116 { 117 if (log.isDebugEnabled()) 118 { 119 log.debug("Enter: SimpleChain::invoke"); 120 } 121 122 invoked = true; 123 doVisiting(msgContext, iVisitor); 124 125 if (log.isDebugEnabled()) 126 { 127 log.debug("Exit: SimpleChain::invoke"); 128 } 129 } 130 131 135 public void generateWSDL(MessageContext msgContext) throws AxisFault 136 { 137 if (log.isDebugEnabled()) 138 { 139 log.debug("Enter: SimpleChain::generateWSDL"); 140 } 141 142 invoked = true; 143 doVisiting(msgContext, wsdlVisitor); 144 145 if (log.isDebugEnabled()) 146 { 147 log.debug("Exit: SimpleChain::generateWSDL"); 148 } 149 } 150 151 private void doVisiting(MessageContext msgContext, 152 HandlerIterationStrategy visitor) throws AxisFault 153 { 154 int i = 0; 155 try 156 { 157 Enumeration en = handlers.elements(); 158 while (en.hasMoreElements()) 159 { 160 visitor.visit((Handler)en.nextElement(), msgContext); 161 i++; 162 } 163 } 164 catch (AxisFault f) 165 { 166 if (!msgContext.isPropertyTrue(CAUGHTFAULT_PROPERTY)) 172 { 173 Message respMsg = new Message(f); 176 msgContext.setResponseMessage(respMsg); 177 msgContext.setProperty(CAUGHTFAULT_PROPERTY, Boolean.TRUE); 178 } 179 while (--i >= 0) 180 ((Handler)handlers.elementAt(i)).onFault(msgContext); 181 throw f; 182 } 183 } 184 185 191 public void onFault(MessageContext msgContext) 192 { 193 if (log.isDebugEnabled()) 194 { 195 log.debug("Enter: SimpleChain::onFault"); 196 } 197 198 for (int i = handlers.size() - 1; i >= 0; i--) 199 ((Handler)handlers.elementAt(i)).onFault(msgContext); 200 201 if (log.isDebugEnabled()) 202 { 203 log.debug("Exit: SimpleChain::onFault"); 204 } 205 } 206 207 public boolean canHandleBlock(QName qname) 208 { 209 for (int i = 0; i < handlers.size(); i++) 210 if (((Handler)handlers.elementAt(i)).canHandleBlock(qname)) 211 return (true); 212 return (false); 213 } 214 215 public void addHandler(Handler handler) 216 { 217 if (handler == null) 218 throw new InternalException(Messages.getMessage("nullHandler00", 219 "SimpleChain::addHandler")); 220 221 if (invoked) 222 throw new InternalException(Messages.getMessage("addAfterInvoke00", 223 "SimpleChain::addHandler")); 224 225 handlers.add(handler); 226 } 227 228 public boolean contains(Handler handler) 229 { 230 return (handlers.contains(handler)); 231 } 232 233 public Handler[] getHandlers() 234 { 235 if (handlers.size() == 0) 236 return null; 237 238 Handler[] ret = new Handler[handlers.size()]; 239 return ((Handler[])handlers.toArray(ret)); 240 } 241 242 public Element getDeploymentData(Document doc) 243 { 244 if (log.isDebugEnabled()) 245 { 246 log.debug(Messages.getMessage("enter00", 247 "SimpleChain::getDeploymentData")); 248 } 249 250 Element root = doc.createElementNS("", "chain"); 251 252 StringBuffer str = new StringBuffer (); 253 int i = 0; 254 while (i < handlers.size()) 255 { 256 if (i != 0) str.append(","); 257 Handler h = (Handler)handlers.elementAt(i); 258 str.append(h.getName()); 259 i++; 260 } 261 if (i > 0) 262 { 263 root.setAttribute("flow", str.toString()); 264 } 265 266 if (options != null) 267 { 268 Enumeration e = options.keys(); 269 while (e.hasMoreElements()) 270 { 271 String k = (String )e.nextElement(); 272 Object v = options.get(k); 273 Element e1 = doc.createElementNS("", "option"); 274 e1.setAttribute("name", k); 275 e1.setAttribute("value", v.toString()); 276 root.appendChild(e1); 277 } 278 } 279 280 if (log.isDebugEnabled()) 281 { 282 log.debug("Exit: SimpleChain::getDeploymentData"); 283 } 284 285 return (root); 286 } 287 } 288 | Popular Tags |