KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
19 import org.apache.axis2.engine.AxisConfiguration;
20 import org.apache.axis2.engine.AxisConfigurationImpl;
21 import org.apache.axis2.engine.AxisFault;
22 import org.apache.axis2.engine.Phase;
23 import org.apache.axis2.phaseresolver.util.PhaseValidator;
24
25 import javax.xml.namespace.QName JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 /**
32  * Class PhaseResolver
33  */

34 public class PhaseResolver {
35     /**
36      * Field axisConfig
37      */

38     private AxisConfiguration axisConfig;
39
40     /**
41      * Field axisService
42      */

43     private ServiceDescription axisService;
44
45
46     /**
47      * Field phaseHolder
48      */

49     private PhaseHolder phaseHolder;
50
51     public PhaseResolver() {
52     }
53
54     /**
55      * default constructor , to obuild chains for GlobalDescription
56      *
57      * @param engineConfig
58      */

59     public PhaseResolver(AxisConfiguration engineConfig) {
60         this.axisConfig = engineConfig;
61     }
62
63     /**
64      * Constructor PhaseResolver
65      *
66      * @param axisConfig
67      * @param serviceContext
68      */

69     public PhaseResolver(AxisConfiguration axisConfig,
70                          ServiceDescription serviceContext) {
71         this.axisConfig = axisConfig;
72         this.axisService = serviceContext;
73     }
74
75     /**
76      * Method buildchains
77      *
78      * @throws PhaseException
79      * @throws AxisFault
80      */

81     public void buildchains() throws PhaseException, AxisFault {
82         HashMap JavaDoc operations = axisService.getOperations();
83         Collection JavaDoc col = operations.values();
84         for (Iterator JavaDoc iterator = col.iterator(); iterator.hasNext();) {
85             OperationDescription operation = (OperationDescription) iterator.next();
86             for (int i = 1; i < 5; i++) {
87                 buildExcutionChains(i, operation);
88             }
89         }
90     }
91
92     /**
93      * this opeartion is used to build all the three cahins ,
94      * so type varible is used to difrenciate them
95      * type = 1 inflow
96      * type = 2 out flow
97      * type = 3 fault flow
98      *
99      * @param type
100      * @throws AxisFault
101      * @throws PhaseException
102      */

103     private void buildExcutionChains(int type, OperationDescription operation)
104             throws AxisFault, PhaseException {
105         int flowtype = type;
106         ArrayList JavaDoc allHandlers = new ArrayList JavaDoc();
107         ModuleDescription module;
108         Flow flow = null;
109         ///////////////////////////////////////////////////////////////////////////////////////////
110
////////////////////////// Handlers from axis2.xml from modules/////////////////////////
111
ArrayList JavaDoc modulqnames = (ArrayList JavaDoc) ((AxisConfigurationImpl) axisConfig).getEngadgedModules();
112         for (int i = 0; i < modulqnames.size(); i++) {
113             QName JavaDoc modulename = (QName JavaDoc) modulqnames.get(i);
114             module = axisConfig.getModule(modulename);
115             if (module != null) {
116                 switch (flowtype) {
117                     case PhaseMetadata.IN_FLOW:
118                         {
119                             flow = module.getInFlow();
120                             break;
121                         }
122                     case PhaseMetadata.OUT_FLOW:
123                         {
124                             flow = module.getOutFlow();
125                             break;
126                         }
127                     case PhaseMetadata.FAULT_IN_FLOW:
128                         {
129                             flow = module.getFaultInFlow();
130                             break;
131                         }
132                     case PhaseMetadata.FAULT_OUT_FLOW:
133                         {
134                             flow = module.getFaultOutFlow();
135                             break;
136                         }
137                 }
138                 axisService.addToEngagModuleList(module);
139                 operation.addToEngageModuleList(module);
140             } else {
141                 throw new PhaseException("referance to invalid module " + modulename.getLocalPart() + " by axis2.xml");
142             }
143
144             if (flow != null) {
145                 for (int j = 0; j < flow.getHandlerCount(); j++) {
146                     HandlerDescription metadata = flow.getHandler(j);
147
148                     if (!PhaseValidator.isSystemPhases(metadata.getRules().getPhaseName())) {
149                         allHandlers.add(metadata);
150                     } else {
151                         /**
152                          *This handler is trying to added to system pre defined phases , but those handlers
153                          * are already added to global chain which run irrespective of the service
154                          *
155                          */

156                         continue;
157                     }
158                 }
159             }
160
161         }
162
163         ///////////////////////////////////////////////////////////////////////////////////////////
164
////////////////////////// SERVICE HANDLERS ///////////////////////////////////////////////
165
switch (flowtype) {
166             case PhaseMetadata.IN_FLOW:
167                 {
168                     flow = axisService.getInFlow();
169                     break;
170                 }
171             case PhaseMetadata.OUT_FLOW:
172                 {
173                     flow = axisService.getOutFlow();
174                     break;
175                 }
176             case PhaseMetadata.FAULT_IN_FLOW:
177                 {
178                     flow = axisService.getFaultInFlow();
179                     break;
180                 }
181             case PhaseMetadata.FAULT_OUT_FLOW:
182                 {
183                     flow = axisService.getFaultOutFlow();
184                     break;
185                 }
186         }
187         if (flow != null) {
188             for (int j = 0; j < flow.getHandlerCount(); j++) {
189                 HandlerDescription metadata = flow.getHandler(j);
190
191                 // todo change this in properway
192
if (metadata.getRules().getPhaseName().equals("")) {
193                     throw new PhaseException("Phase dose not specified");
194                 }
195                 allHandlers.add(metadata);
196             }
197         }
198         switch (flowtype) {
199             case PhaseMetadata.IN_FLOW:
200                 {
201                     phaseHolder = new PhaseHolder(operation.getRemainingPhasesInFlow());
202                     break;
203                 }
204             case PhaseMetadata.OUT_FLOW:
205                 {
206                     phaseHolder = new PhaseHolder(operation.getPhasesOutFlow());
207                     break;
208                 }
209             case PhaseMetadata.FAULT_IN_FLOW:
210                 {
211                     phaseHolder = new PhaseHolder(operation.getPhasesInFaultFlow());
212                     break;
213                 }
214             case PhaseMetadata.FAULT_OUT_FLOW:
215                 {
216                     phaseHolder = new PhaseHolder(operation.getPhasesOutFaultFlow());
217                     break;
218                 }
219         }
220         for (int i = 0; i < allHandlers.size(); i++) {
221             HandlerDescription handlerMetaData =
222                     (HandlerDescription) allHandlers.get(i);
223             phaseHolder.addHandler(handlerMetaData);
224         }
225     }
226
227     /**
228      * Method buildTranspotsChains
229      *
230      * @throws PhaseException
231      */

232     public void buildTranspotsChains() throws PhaseException {
233         HashMap JavaDoc axisTransportIn = axisConfig.getTransportsIn();
234         HashMap JavaDoc axisTransportOut = axisConfig.getTransportsOut();
235
236         Collection JavaDoc colintrnsport = axisTransportIn.values();
237         for (Iterator JavaDoc iterator = colintrnsport.iterator();
238              iterator.hasNext();) {
239             TransportInDescription transport = (TransportInDescription) iterator.next();
240             buildINTransportChains(transport);
241         }
242
243         Collection JavaDoc colouttrnsport = axisTransportOut.values();
244         for (Iterator JavaDoc iterator = colouttrnsport.iterator();
245              iterator.hasNext();) {
246             TransportOutDescription transport = (TransportOutDescription) iterator.next();
247             buildOutTransportChains(transport);
248         }
249     }
250
251
252     private void buildINTransportChains(TransportInDescription transport)
253             throws PhaseException {
254         Flow flow = null;
255         Phase phase = null;
256         for (int type = 1; type < 4; type++) {
257             switch (type) {
258                 case PhaseMetadata.IN_FLOW:
259                     {
260                         flow = transport.getInFlow();
261                         phase = transport.getInPhase();
262                         break;
263                     }
264                 case PhaseMetadata.FAULT_IN_FLOW:
265                     {
266                         flow = transport.getFaultFlow();
267                         phase = transport.getFaultPhase();
268                         break;
269                     }
270             }
271             if (flow != null) {
272                 ArrayList JavaDoc handlers = new ArrayList JavaDoc();
273                 for (int j = 0; j < flow.getHandlerCount(); j++) {
274                     HandlerDescription metadata = flow.getHandler(j);
275                     metadata.getRules().setPhaseName(PhaseMetadata.TRANSPORT_PHASE);
276                     handlers.add(metadata);
277                 }
278                 new PhaseHolder().buildTransportHandlerChain(phase, handlers);
279             }else {
280                 continue;
281             }
282         }
283     }
284
285
286     /**
287      * Method buildTransportChains
288      *
289      * @param transport
290      * @throws PhaseException
291      */

292     private void buildOutTransportChains(TransportOutDescription transport)
293             throws PhaseException {
294         Flow flow = null;
295         Phase phase = null;
296         for (int type = 1; type < 5; type++) {
297             switch (type) {
298                 case PhaseMetadata.OUT_FLOW:
299                     {
300                         flow = transport.getOutFlow();
301                         phase = transport.getOutPhase();
302                         break;
303                     }
304                 case PhaseMetadata.FAULT_OUT_FLOW:
305                     {
306                         flow = transport.getFaultFlow();
307                         phase = transport.getFaultPhase();
308                         break;
309                     }
310             }
311             if (flow != null) {
312                 ArrayList JavaDoc handlers = new ArrayList JavaDoc();
313                 for (int j = 0; j < flow.getHandlerCount(); j++) {
314                     HandlerDescription metadata = flow.getHandler(j);
315                     metadata.getRules().setPhaseName(PhaseMetadata.TRANSPORT_PHASE);
316                     handlers.add(metadata);
317                 }
318                 new PhaseHolder().buildTransportHandlerChain(phase, handlers);
319             }else {
320                 continue;
321             }
322         }
323     }
324     
325
326     public void engageModuleGlobally(ModuleDescription module) throws AxisFault {
327         enageToGlobalChain(module);
328         HashMap JavaDoc services = axisConfig.getServices();
329         Collection JavaDoc serviceCol = services.values();
330         for (Iterator JavaDoc iterator = serviceCol.iterator(); iterator.hasNext();) {
331             ServiceDescription serviceDescription = (ServiceDescription) iterator.next();
332             serviceDescription.addModuleOperations(module);
333             engageModuleToServiceFromGlobal(serviceDescription, module);
334             serviceDescription.addToEngagModuleList(module);
335         }
336     }
337
338     /**
339      * To engage modules come form global
340      *
341      * @param service
342      * @param module
343      * @throws PhaseException
344      */

345     public void engageModuleToServiceFromGlobal(ServiceDescription service, ModuleDescription module) throws PhaseException {
346         HashMap JavaDoc opeartions = service.getOperations();
347         Collection JavaDoc opCol = opeartions.values();
348         boolean engaged = false;
349         for (Iterator JavaDoc iterator = opCol.iterator(); iterator.hasNext();) {
350             OperationDescription opDesc = (OperationDescription) iterator.next();
351             Collection JavaDoc modules = opDesc.getModules();
352             for (Iterator JavaDoc iterator1 = modules.iterator(); iterator1.hasNext();) {
353                 ModuleDescription description = (ModuleDescription) iterator1.next();
354                 if(description.getName().equals(module.getName())){
355                     engaged = true;
356                     break;
357                 }
358             }
359             if (engaged) {
360                 continue;
361             }
362             Flow flow = null;
363             for (int type = 1; type < 5; type++) {
364                 switch (type) {
365                     case PhaseMetadata.IN_FLOW:
366                         {
367                             phaseHolder = new PhaseHolder(opDesc.getRemainingPhasesInFlow());
368                             break;
369                         }
370                     case PhaseMetadata.OUT_FLOW:
371                         {
372                             phaseHolder = new PhaseHolder(opDesc.getPhasesOutFlow());
373                             break;
374                         }
375                     case PhaseMetadata.FAULT_IN_FLOW:
376                         {
377                             phaseHolder = new PhaseHolder(opDesc.getPhasesInFaultFlow());
378                             break;
379                         }
380                     case PhaseMetadata.FAULT_OUT_FLOW:
381                         {
382                             phaseHolder = new PhaseHolder(opDesc.getPhasesOutFaultFlow());
383                             break;
384                         }
385                 }
386                 ////////////////////////////////////////////////////////////////////////////////////
387
/////////////////// Modules refered by axis2.xml //////////////////////////////////
388
////////////////////////////////////////////////////////////////////////////////////
389
switch (type) {
390                     case PhaseMetadata.IN_FLOW:
391                         {
392                             flow = module.getInFlow();
393                             break;
394                         }
395                     case PhaseMetadata.OUT_FLOW:
396                         {
397                             flow = module.getOutFlow();
398                             break;
399                         }
400                     case PhaseMetadata.FAULT_IN_FLOW:
401                         {
402                             flow = module.getFaultInFlow();
403                             break;
404                         }
405                     case PhaseMetadata.FAULT_OUT_FLOW:
406                         {
407                             flow = module.getFaultOutFlow();
408                             break;
409                         }
410                 }
411                 if (flow != null) {
412                     for (int j = 0; j < flow.getHandlerCount(); j++) {
413                         HandlerDescription metadata = flow.getHandler(j);
414                         if (!PhaseValidator.isSystemPhases(metadata.getRules().getPhaseName())) {
415                             phaseHolder.addHandler(metadata);
416                         } else {
417                             continue;
418                         }
419                     }
420                 }
421             }
422             opDesc.addToEngageModuleList(module);
423         }
424     }
425
426
427     private void enageToGlobalChain(ModuleDescription module) throws PhaseException {
428         Flow flow = null;
429         for (int type = 1; type < 5; type++) {
430             switch (type) {
431                 case PhaseMetadata.IN_FLOW:
432                     {
433                         phaseHolder = new PhaseHolder(((AxisConfigurationImpl) axisConfig).getInPhasesUptoAndIncludingPostDispatch());
434                         break;
435                     }
436                 case PhaseMetadata.OUT_FLOW:
437                     {
438                         phaseHolder = new PhaseHolder(((AxisConfigurationImpl) axisConfig).getOutFlow());
439                         break;
440                     }
441                 case PhaseMetadata.FAULT_IN_FLOW:
442                     {
443                         phaseHolder = new PhaseHolder(((AxisConfigurationImpl) axisConfig).getInFaultFlow());
444                         break;
445                     }
446                 case PhaseMetadata.FAULT_OUT_FLOW:
447                     {
448                         phaseHolder = new PhaseHolder(((AxisConfigurationImpl) axisConfig).getOutFaultFlow());
449                         break;
450                     }
451             }
452             ////////////////////////////////////////////////////////////////////////////////////
453
/////////////////// Modules refered by axis2.xml //////////////////////////////////
454
////////////////////////////////////////////////////////////////////////////////////
455
switch (type) {
456                 case PhaseMetadata.IN_FLOW:
457                     {
458                         flow = module.getInFlow();
459                         break;
460                     }
461                 case PhaseMetadata.OUT_FLOW:
462                     {
463                         flow = module.getOutFlow();
464                         break;
465                     }
466                 case PhaseMetadata.FAULT_IN_FLOW:
467                     {
468                         flow = module.getFaultInFlow();
469                         break;
470                     }
471                 case PhaseMetadata.FAULT_OUT_FLOW:
472                     {
473                         flow = module.getFaultOutFlow();
474                         break;
475                     }
476             }
477             if (flow != null) {
478                 for (int j = 0; j < flow.getHandlerCount(); j++) {
479                     HandlerDescription metadata = flow.getHandler(j);
480                     if (PhaseValidator.isSystemPhases(metadata.getRules().getPhaseName())) {
481                         phaseHolder.addHandler(metadata);
482                     } else {
483                         /**
484                          * These handlers will go to operation's handler chains , since the module
485                          * try to add handlres to both sytem predefined phases and user defined phase
486                          * so global module can do that. here the global module are the module which are
487                          * reffred by axis2.xml
488                          */

489                         continue;
490                     }
491                 }
492             }
493         }
494     }
495
496
497     public void engageModuleToService(ServiceDescription service, ModuleDescription module) throws PhaseException {
498         HashMap JavaDoc opeartions = service.getOperations();
499         Collection JavaDoc opCol = opeartions.values();
500         boolean engaged = false;
501         for (Iterator JavaDoc iterator = opCol.iterator(); iterator.hasNext();) {
502             OperationDescription opDesc = (OperationDescription) iterator.next();
503             Collection JavaDoc modules = opDesc.getModules();
504             for (Iterator JavaDoc iterator1 = modules.iterator(); iterator1.hasNext();) {
505                 ModuleDescription description = (ModuleDescription) iterator1.next();
506                 if(description.getName().equals(module.getName())){
507                     engaged = true;
508                     break;
509                 }
510             }
511             if (!engaged) {
512                 engageModuleToOperation(opDesc,module);
513                 opDesc.addToEngageModuleList(module);
514             }
515         }
516         service.addModuleOperations(module);
517     }
518
519
520     public void engageModuleToOperation(OperationDescription operation, ModuleDescription module) throws PhaseException {
521         OperationDescription opDesc = operation;
522         Flow flow = null;
523         for (int type = 1; type < 5; type++) {
524             switch (type) {
525                 case PhaseMetadata.IN_FLOW:
526                     {
527                         phaseHolder = new PhaseHolder(opDesc.getRemainingPhasesInFlow());
528                         break;
529                     }
530                 case PhaseMetadata.OUT_FLOW:
531                     {
532                         phaseHolder = new PhaseHolder(opDesc.getPhasesOutFlow());
533                         break;
534                     }
535                 case PhaseMetadata.FAULT_IN_FLOW:
536                     {
537                         phaseHolder = new PhaseHolder(opDesc.getPhasesInFaultFlow());
538                         break;
539                     }
540                 case PhaseMetadata.FAULT_OUT_FLOW:
541                     {
542                         phaseHolder = new PhaseHolder(opDesc.getPhasesOutFaultFlow());
543                         break;
544                     }
545             }
546
547             switch (type) {
548                 case PhaseMetadata.IN_FLOW:
549                     {
550                         flow = module.getInFlow();
551                         break;
552                     }
553                 case PhaseMetadata.OUT_FLOW:
554                     {
555                         flow = module.getOutFlow();
556                         break;
557                     }
558                 case PhaseMetadata.FAULT_IN_FLOW:
559                     {
560                         flow = module.getFaultInFlow();
561                         break;
562                     }
563                 case PhaseMetadata.FAULT_OUT_FLOW:
564                     {
565                         flow = module.getFaultOutFlow();
566                         break;
567                     }
568             }
569             if (flow != null) {
570                 for (int j = 0; j < flow.getHandlerCount(); j++) {
571                     HandlerDescription metadata = flow.getHandler(j);
572                     if (!PhaseValidator.isSystemPhases(metadata.getRules().getPhaseName())) {
573                         phaseHolder.addHandler(metadata);
574                     } else {
575                         throw new PhaseException("Service specific module can not refer system pre defined phases : "
576                                 + metadata.getRules().getPhaseName());
577                     }
578                 }
579             }
580         }
581     }
582 }
583
Popular Tags