KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > engine > Phase


1 /*
2 * Copyright 2004,2005 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */

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 JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 /**
28  * <p>This is Phase, a orderd collection of Handlers.
29  * seems this is Handler Chain with order.</p>
30  * Should this exttends Hanlders?
31  */

32 public class Phase {
33
34     /**
35      * Field phaseName
36      */

37     private String JavaDoc phaseName;
38
39     /**
40      * Field handlers
41      */

42     private ArrayList JavaDoc handlers;
43
44     /**
45      * Field log
46      */

47     private Log log = LogFactory.getLog(getClass());
48
49     /**
50      * to keet info about phase first handler
51      */

52     private Handler phaseFirst = null;
53
54     /**
55      * Field phasefirstset
56      */

57     private boolean phasefirstset;
58
59     /**
60      * to keet info about phase last handler
61      */

62     private Handler phaseLast = null;
63
64     /**
65      * Field phaselastset
66      */

67     private boolean phaselastset;
68
69     /**
70      * Field BOTH_BEFORE_AFTER
71      */

72     private static final int BOTH_BEFORE_AFTER = 0;
73
74     /**
75      * Field BEFORE
76      */

77     private static final int BEFORE = 1;
78
79     /**
80      * Field AFTER
81      */

82     private static final int AFTER = 2;
83
84     /**
85      * Field ANYWHERE
86      */

87     private static final int ANYWHERE = 3;
88
89     /**
90      * this is want if the phaseFirst and phaseLst same hanlder
91      * that is for this phase there is only one phase
92      */

93     private boolean isonehanlder;
94
95     /**
96      * Constructor Phase
97      *
98      * @param phaseName
99      */

100     public Phase(String JavaDoc phaseName) {
101         handlers = new ArrayList JavaDoc();
102         this.phaseName = phaseName;
103     }
104
105     /**
106      * Method addHandler
107      *
108      * @param handler
109      * @param index
110      */

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     /**
118      * add to next empty handler
119      *
120      * @param handler
121      */

122     public void addHandler(Handler handler) {
123         log.info("Handler " + handler.getName() + " Added to the Phase " + phaseName);
124         handlers.add(handler);
125     }
126
127     /**
128      * If need to see how this works look at the stack!
129      *
130      * @param msgctx
131      * @throws AxisFault
132      */

133     public void invoke(MessageContext msgctx) throws AxisFault {
134         msgctx.setPausedPhaseName(this.getPhaseName());
135         //If phase first Hnadler is there then it should run first
136
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         //Invoking the rest of handler except phaseFirst and phaseLast
149
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                     //This line should be after the invoke as if the invocation failed this handlers is takn care of and
163
//no need to revoke agien
164
indexOfHandlerToExecute++;
165                 }
166             }
167         }
168         //If phase last handler is there will invoke that here
169
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     /**
184      * @return Returns the name.
185      */

186     public String JavaDoc getPhaseName() {
187         return phaseName;
188     }
189
190     public int getHandlerCount() {
191         return handlers.size();
192     }
193
194     //////////////////////////////////////////////////////////////// FROM PhaseMetaData //////////
195

196     /**
197      * Method getBeforeAfter
198      *
199      * @param handler
200      * @return
201      * @throws org.apache.axis2.phaseresolver.PhaseException
202      *
203      */

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     /**
227      * Method setPhaseFirst
228      *
229      * @param phaseFirst
230      * @throws PhaseException
231      */

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     /**
250      * Method setPhaseLast
251      *
252      * @param phaseLast
253      * @throws PhaseException
254      */

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     /**
273      * Method addHandler
274      *
275      * @param handler
276      * @throws PhaseException
277      */

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     /**
312      * This method is to check whether user try to add a handler whose before property is
313      * phaseFitsr handler , this cannot allowed , so this will throws an exception
314      * otherewise it will retun
315      *
316      * @throws PhaseException
317      */

318     private void validatebefore(Handler handler) throws PhaseException {
319         if (phaseFirst != null) {
320             String JavaDoc 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     /**
332      * This method is to check user try to add or plase a hander after the phaseLast
333      * that operation dose not allowd so then this throw a exception
334      *
335      * @throws PhaseException
336      */

337     private void validateafter(Handler handler) throws PhaseException {
338         if (phaseLast != null) {
339             String JavaDoc 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     /**
351      * Method insertBefore
352      *
353      * @param handler
354      */

355     private void insertBefore(Handler handler) {
356         String JavaDoc 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         //added as last handler
376
handlers.add(handler);
377     }
378
379     /**
380      * Method insertAfter
381      *
382      * @param handler
383      */

384     private void insertAfter(Handler handler) {
385         String JavaDoc 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     /**
412      * This method assume that both the before and after cant be a same hander
413      * that dose not check inside this , it should check befor calling this method
414      *
415      * @param handler
416      * @throws PhaseException
417      */

418     private void insertBeforeandAfter(Handler handler) throws PhaseException {
419         int before = -1;
420         int after = -1;
421
422         /**
423          * if hander.after = PhaseFirts and hnder.before = phaselast then
424          * just add the entery to vector
425          */

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                 // no point of continue since both the before and after index has found
474
if (after > before) {
475                     //TODO fix me Deepal , (have to check this)
476
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     /**
522      * To get the all the handlers in the phase
523      * @return
524      */

525     public ArrayList JavaDoc getHandlers() {
526         ArrayList JavaDoc phaseHandlers = new ArrayList JavaDoc();
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 JavaDoc name, MessageContext msgctx) throws AxisFault {
540         msgctx.setPausedPhaseName(this.getPhaseName());
541         boolean foudMatch = false;
542         ArrayList JavaDoc 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 JavaDoc toString() {
556         return this.getPhaseName();
557     }
558
559 }
560
Popular Tags