1 16 package org.apache.axis2.engine; 17 18 import org.apache.axis2.context.MessageContext; 19 import org.apache.axis2.description.HandlerDescription; 20 import org.apache.axis2.phaseresolver.PhaseException; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.xml.namespace.QName ; 25 import java.util.ArrayList ; 26 27 32 public class Phase { 33 34 37 private String phaseName; 38 39 42 private ArrayList handlers; 43 44 47 private Log log = LogFactory.getLog(getClass()); 48 49 52 private Handler phaseFirst = null; 53 54 57 private boolean phasefirstset; 58 59 62 private Handler phaseLast = null; 63 64 67 private boolean phaselastset; 68 69 72 private static final int BOTH_BEFORE_AFTER = 0; 73 74 77 private static final int BEFORE = 1; 78 79 82 private static final int AFTER = 2; 83 84 87 private static final int ANYWHERE = 3; 88 89 93 private boolean isonehanlder; 94 95 100 public Phase(String phaseName) { 101 handlers = new ArrayList (); 102 this.phaseName = phaseName; 103 } 104 105 111 public void addHandler(Handler handler, int index) { 112 log.info( 113 "Handler " + handler.getName() + "Added to place " + 1 + " At the Phase " + phaseName); 114 handlers.add(index, handler); 115 } 116 117 122 public void addHandler(Handler handler) { 123 log.info("Handler " + handler.getName() + " Added to the Phase " + phaseName); 124 handlers.add(handler); 125 } 126 127 133 public void invoke(MessageContext msgctx) throws AxisFault { 134 msgctx.setPausedPhaseName(this.getPhaseName()); 135 if (phaseFirst != null) { 137 if (msgctx.isPaused()) { 138 return; 139 } else { 140 log.info( 141 "Invoke the Phase first handler " 142 + phaseFirst.getName() 143 + "with in the Phase " 144 + phaseName); 145 phaseFirst.invoke(msgctx); 146 } 147 } 148 int indexOfHandlerToExecute = 0; 150 while (indexOfHandlerToExecute < handlers.size()) { 151 if (msgctx.isPaused()) { 152 break; 153 } else { 154 Handler handler = (Handler) handlers.get(indexOfHandlerToExecute); 155 if (handler != null) { 156 log.info( 157 "Invoke the Handler " 158 + handler.getName() 159 + "with in the Phase " 160 + phaseName); 161 handler.invoke(msgctx); 162 indexOfHandlerToExecute++; 165 } 166 } 167 } 168 if (phaseLast != null) { 170 if (msgctx.isPaused()) { 171 return; 172 } else { 173 log.info( 174 "Invoke the Phase first handler " 175 + phaseLast.getName() 176 + "with in the Phase " 177 + phaseName); 178 phaseLast.invoke(msgctx); 179 } 180 } 181 } 182 183 186 public String getPhaseName() { 187 return phaseName; 188 } 189 190 public int getHandlerCount() { 191 return handlers.size(); 192 } 193 194 196 204 private int getBeforeAfter(Handler handler) throws PhaseException { 205 if ((!handler.getHandlerDesc().getRules().getBefore().equals("")) 206 && (!handler.getHandlerDesc().getRules().getAfter().equals(""))) { 207 if (handler 208 .getHandlerDesc() 209 .getRules() 210 .getBefore() 211 .equals(handler.getHandlerDesc().getRules().getAfter())) { 212 throw new PhaseException( 213 "Both before and after cannot be the same for this handler" 214 + handler.getName()); 215 } 216 return BOTH_BEFORE_AFTER; 217 } else if (!handler.getHandlerDesc().getRules().getBefore().equals("")) { 218 return BEFORE; 219 } else if (!handler.getHandlerDesc().getRules().getAfter().equals("")) { 220 return AFTER; 221 } else { 222 return ANYWHERE; 223 } 224 } 225 226 232 public void setPhaseFirst(Handler phaseFirst) throws PhaseException { 233 if (phasefirstset) { 234 throw new PhaseException( 235 "PhaseFirst alredy has been set, cannot have two phaseFirst Handler for same phase " 236 + this.getPhaseName()); 237 } else { 238 if (getBeforeAfter(phaseFirst) != ANYWHERE) { 239 throw new PhaseException( 240 "Handler with PhaseFirst can not have any before or after proprty error in " 241 + phaseFirst.getName()); 242 } else { 243 this.phaseFirst = phaseFirst; 244 } 245 phasefirstset = true; 246 } 247 } 248 249 255 public void setPhaseLast(Handler phaseLast) throws PhaseException { 256 if (phaselastset) { 257 throw new PhaseException( 258 "PhaseLast already has been set, cannot have two PhaseLast Handler for same phase " 259 + this.getPhaseName()); 260 } else { 261 if (getBeforeAfter(phaseLast) != ANYWHERE) { 262 throw new PhaseException( 263 "Handler with PhaseLast property can not have any before or after property error in " 264 + phaseLast.getName()); 265 } else { 266 this.phaseLast = phaseLast; 267 } 268 phaselastset = true; 269 } 270 } 271 272 278 public void addHandler(HandlerDescription handler) throws PhaseException { 279 if (isonehanlder) { 280 throw new PhaseException( 281 this.getPhaseName() 282 + "can only have one handler, since there is a " 283 + "handler with both phaseFirst and PhaseLast true "); 284 } else { 285 if (handler.getRules().isPhaseFirst() && handler.getRules().isPhaseLast()) { 286 if (handlers.size() > 0) { 287 throw new PhaseException( 288 this.getPhaseName() 289 + " can not have more than one handler " 290 + handler.getName() 291 + " is invalid or incorrect phase rules"); 292 } else { 293 handlers.add(handler.getHandler()); 294 isonehanlder = true; 295 return; 296 } 297 } else if (handler.getRules().isPhaseFirst()) { 298 setPhaseFirst(handler.getHandler()); 299 return; 300 } else if (handler.getRules().isPhaseLast()) { 301 setPhaseLast(handler.getHandler()); 302 return; 303 } else { 304 insertHandler(handler); 305 return; 306 } 307 308 } 309 } 310 311 318 private void validatebefore(Handler handler) throws PhaseException { 319 if (phaseFirst != null) { 320 String phasFirstname = phaseFirst.getHandlerDesc().getName().getLocalPart(); 321 if (handler.getHandlerDesc().getRules().getBefore().equals(phasFirstname)) { 322 throw new PhaseException( 323 "Trying to insert a Handler " 324 + handler.getName() 325 + " before phaseFirst " 326 + phasFirstname); 327 } 328 } 329 } 330 331 337 private void validateafter(Handler handler) throws PhaseException { 338 if (phaseLast != null) { 339 String phaseLastName = phaseLast.getHandlerDesc().getName().getLocalPart(); 340 if (handler.getName().equals(phaseLastName)) { 341 throw new PhaseException( 342 "Try to insert a Handler " 343 + handler.getName() 344 + " after phaseLast " 345 + phaseLastName); 346 } 347 } 348 } 349 350 355 private void insertBefore(Handler handler) { 356 String beforename = handler.getHandlerDesc().getRules().getBefore(); 357 if (phaseLast != null) { 358 if (phaseLast.getHandlerDesc().getName().getLocalPart().equals(beforename)) { 359 handlers.add(handler); 360 return; 361 } 362 } 363 for (int i = 0; i < handlers.size(); i++) { 364 Handler temphandler = (Handler) handlers.get(i); 365 if (temphandler.getHandlerDesc().getName().getLocalPart().equals(beforename)) { 366 if (i == 0) { 367 handlers.add(0, handler); 368 return; 369 } else { 370 handlers.add(i - 1, handler); 371 return; 372 } 373 } 374 } 375 handlers.add(handler); 377 } 378 379 384 private void insertAfter(Handler handler) { 385 String afterName = handler.getHandlerDesc().getRules().getAfter(); 386 if (phaseFirst != null) { 387 if (phaseFirst.getHandlerDesc().getName().getLocalPart().equals(afterName)) { 388 handlers.add(0, handler); 389 return; 390 } 391 } 392 int count = handlers.size(); 393 for (int i = 0; i < count; i++) { 394 Handler temphandler = (Handler) handlers.get(i); 395 if (temphandler.getHandlerDesc().getName().getLocalPart().equals(afterName)) { 396 if (i == count - 1) { 397 handlers.add(handler); 398 return; 399 } else { 400 handlers.add(i + 1, handler); 401 return; 402 } 403 } 404 } 405 if (handlers.size() > 0) { 406 handlers.add(0, handler); 407 } else 408 handlers.add(handler); 409 } 410 411 418 private void insertBeforeandAfter(Handler handler) throws PhaseException { 419 int before = -1; 420 int after = -1; 421 422 426 if ((phaseFirst != null) && (phaseLast != null)) { 427 if ((phaseFirst 428 .getHandlerDesc() 429 .getName() 430 .getLocalPart() 431 .equals(handler.getHandlerDesc().getRules().getAfter())) 432 && (phaseLast 433 .getHandlerDesc() 434 .getName() 435 .getLocalPart() 436 .equals(handler.getHandlerDesc().getRules().getBefore()))) { 437 handlers.add(handler); 438 return; 439 } 440 } 441 442 if (phaseFirst != null 443 && (phaseFirst 444 .getHandlerDesc() 445 .getName() 446 .getLocalPart() 447 .equals(handler.getHandlerDesc().getRules().getAfter()))) { 448 after = 0; 449 } 450 if (phaseLast != null 451 && (phaseLast 452 .getHandlerDesc() 453 .getName() 454 .getLocalPart() 455 .equals(handler.getHandlerDesc().getRules().getBefore()))) { 456 before = handlers.size(); 457 } 458 459 for (int i = 0; i < handlers.size(); i++) { 460 Handler temphandler = (Handler) handlers.get(i); 461 if (handler 462 .getHandlerDesc() 463 .getRules() 464 .getAfter() 465 .equals(temphandler.getHandlerDesc().getName().getLocalPart())) { 466 after = i; 467 } else if ( 468 handler.getHandlerDesc().getRules().getBefore().equals( 469 temphandler.getHandlerDesc().getName().getLocalPart())) { 470 before = i; 471 } 472 if ((after >= 0) && (before >= 0)) { 473 if (after > before) { 475 throw new PhaseException( 477 "incorrect handler order for " + handler.getHandlerDesc().getName()); 478 } else { 479 if (after + 1 <= handlers.size()) { 480 handlers.add(after + 1, handler); 481 return; 482 } else { 483 handlers.add(after, handler); 484 return; 485 } 486 } 487 } 488 } 489 handlers.add(handler); 490 } 491 492 private void insertHandler(HandlerDescription handler) throws PhaseException { 493 Handler han = handler.getHandler(); 494 int type = getBeforeAfter(han); 495 validateafter(han); 496 validatebefore(han); 497 switch (type) { 498 case BOTH_BEFORE_AFTER : 499 { 500 insertBeforeandAfter(han); 501 break; 502 } 503 case BEFORE : 504 { 505 insertBefore(han); 506 break; 507 } 508 case AFTER : 509 { 510 insertAfter(han); 511 break; 512 } 513 case ANYWHERE : 514 { 515 handlers.add(han); 516 break; 517 } 518 } 519 } 520 521 525 public ArrayList getHandlers() { 526 ArrayList phaseHandlers = new ArrayList (); 527 if (phaseFirst != null) { 528 phaseHandlers.add(phaseFirst); 529 } 530 for (int i = 0; i < handlers.size(); i++) { 531 Handler handler = (Handler) handlers.get(i); 532 phaseHandlers.add(handler); 533 } 534 if (phaseLast != null) { 535 phaseHandlers.add(phaseLast); 536 } 537 return phaseHandlers; 538 } 539 public void invokeStartFromHandler(QName name, MessageContext msgctx) throws AxisFault { 540 msgctx.setPausedPhaseName(this.getPhaseName()); 541 boolean foudMatch = false; 542 ArrayList phaseHandlers = getHandlers(); 543 for (int i = 0; i < phaseHandlers.size(); i++) { 544 Handler handler = (Handler) handlers.get(i); 545 if(handler != null && handler.getName().equals(name)){ 546 foudMatch = true; 547 } 548 549 if(handler != null && foudMatch){ 550 handler.invoke(msgctx); 551 } 552 } 553 } 554 555 public String toString() { 556 return this.getPhaseName(); 557 } 558 559 } 560 | Popular Tags |