KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > phaseresolver > PhaseHolder


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.phaseresolver;
17
18 import org.apache.axis2.description.HandlerDescription;
19 import org.apache.axis2.engine.Handler;
20 import org.apache.axis2.engine.Phase;
21
22 import java.util.ArrayList JavaDoc;
23
24
25 /**
26  * This class hold all the phases found in the service.xml and server.xml
27  */

28 public class PhaseHolder {
29
30
31     private ArrayList JavaDoc phaseList;
32
33     public PhaseHolder(ArrayList JavaDoc phases) {
34         this.phaseList = phases;
35     }
36
37     public PhaseHolder() {
38     }
39
40     /**
41      * Method isPhaseExist
42      *
43      * @param phaseName
44      * @return
45      */

46     private boolean isPhaseExist(String JavaDoc phaseName) {
47         for (int i = 0; i < phaseList.size(); i++) {
48             Phase phase = (Phase) phaseList.get(i);
49             if (phase.getPhaseName().equals(phaseName)) {
50                 return true;
51             }
52         }
53         return false;
54     }
55
56     /**
57      * Method addHandler
58      *
59      * @param handler
60      * @throws PhaseException
61      */

62     public void addHandler(HandlerDescription handler) throws PhaseException {
63         String JavaDoc phaseName = handler.getRules().getPhaseName();
64         if (isPhaseExist(phaseName)) {
65             getPhase(phaseName).addHandler(handler);
66         } else {
67             throw new PhaseException("Invalid Phase ," + phaseName
68                     + "for the handler "
69                     + handler.getName()
70                     + " dose not exit in axis2.xml or refering to phase in diffrent flow");
71         }
72     }
73
74     /**
75      * this method is used to get the actual phase object given in the phase array list
76      *
77      * @param phaseName
78      * @return
79      */

80     private Phase getPhase(String JavaDoc phaseName) {
81         for (int i = 0; i < phaseList.size(); i++) {
82             Phase phase = (Phase) phaseList.get(i);
83             if (phase.getPhaseName().equals(phaseName)) {
84                 return phase;
85             }
86         }
87         return null;
88     }
89
90     /**
91      * This method is to build the transport phase , here load the corresponding handlers and added them
92      * in to correct phase
93      * @param phase
94      * @param handlers
95      * @throws PhaseException
96      */

97     public void buildTransportHandlerChain(Phase phase, ArrayList JavaDoc handlers) throws PhaseException {
98         try {
99             Class JavaDoc handlerClass = null;
100             Handler handler;
101             for (int i = 0; i < handlers.size(); i++) {
102                 HandlerDescription description = (HandlerDescription) handlers.get(i);
103                 handlerClass = Class.forName(description.getClassName(), true,
104                         Thread.currentThread().getContextClassLoader());
105                 handler =
106                         (Handler) handlerClass.newInstance();
107                 handler.init(description);
108                 description.setHandler(handler);
109                 phase.addHandler(description.getHandler());
110             }
111         } catch (ClassNotFoundException JavaDoc e) {
112             throw new PhaseException(e);
113         } catch (InstantiationException JavaDoc e) {
114             throw new PhaseException(e);
115         } catch (IllegalAccessException JavaDoc e) {
116             throw new PhaseException(e);
117         }
118     }
119
120 }
121
Popular Tags