001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.phaseresolver;
021:
022: import org.apache.axis2.deployment.DeploymentErrorMsgs;
023: import org.apache.axis2.description.HandlerDescription;
024: import org.apache.axis2.description.PhaseRule;
025: import org.apache.axis2.engine.Phase;
026: import org.apache.axis2.i18n.Messages;
027:
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: /**
032: * This class hold all the phases found in the services.xml and server.xml
033: */
034: public class PhaseHolder {
035: private List phaseList;
036:
037: public PhaseHolder() {
038: }
039:
040: public PhaseHolder(List phases) {
041: this .phaseList = phases;
042: }
043:
044: /**
045: * If the phase name is equal to "*" that implies , the handler should be
046: * added to each and every phase in the system for a given flow , and at that
047: * point if the phase rule contains any before or aftere then they will be
048: * ignored. Phase first and phase last are supported , but make sure you dont
049: * break any of the phase rules.
050: * <p/>
051: * If the phase name is not above then the hadler will be added to the phase
052: * specified by the phase rule , and no rules will be ignored.
053: *
054: * @param handlerDesc
055: * @throws PhaseException
056: */
057: public void addHandler(HandlerDescription handlerDesc)
058: throws PhaseException {
059: PhaseRule rule = handlerDesc.getRules();
060:
061: // Make sure this rule makes sense (throws PhaseException if not)
062: rule.validate();
063:
064: String phaseName = rule.getPhaseName();
065: if (Phase.ALL_PHASES.equals(phaseName)) {
066: handlerDesc.getRules().setBefore("");
067: handlerDesc.getRules().setAfter("");
068: for (int i = 0; i < phaseList.size(); i++) {
069: Phase phase = (Phase) phaseList.get(i);
070: phase.addHandler(handlerDesc);
071: }
072: } else {
073: if (phaseExists(phaseName)) {
074: getPhase(phaseName).addHandler(handlerDesc);
075: } else {
076: throw new PhaseException(Messages.getMessage(
077: DeploymentErrorMsgs.INVALID_PHASE, phaseName,
078: handlerDesc.getName()));
079: }
080: }
081: }
082:
083: /**
084: * this method is used to get the actual phase object given in the phase array list
085: *
086: * @param phaseName
087: */
088: private Phase getPhase(String phaseName) {
089: for (int i = 0; i < phaseList.size(); i++) {
090: Phase phase = (Phase) phaseList.get(i);
091:
092: if (phase.getPhaseName().equals(phaseName)) {
093: return phase;
094: }
095: }
096:
097: return null;
098: }
099:
100: /**
101: * Check if a named Phase exists in this holder.
102: *
103: * @param phaseName name to check
104: * @return true if a Phase matching the name was found, false otherwise
105: */
106: private boolean phaseExists(String phaseName) {
107: for (int i = 0; i < phaseList.size(); i++) {
108: Phase phase = (Phase) phaseList.get(i);
109:
110: if (phase.getPhaseName().equals(phaseName)) {
111: return true;
112: }
113: }
114:
115: return false;
116: }
117: }
|