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.client;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.context.ConfigurationContext;
024: import org.apache.axis2.description.AxisDescription;
025: import org.apache.axis2.description.AxisModule;
026: import org.apache.axis2.description.AxisOperation;
027: import org.apache.axis2.description.AxisService;
028: import org.apache.axis2.description.PolicyInclude;
029: import org.apache.axis2.engine.AxisConfiguration;
030: import org.apache.neethi.Assertion;
031: import org.apache.neethi.Policy;
032:
033: import javax.xml.namespace.QName;
034: import java.util.ArrayList;
035: import java.util.HashMap;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: public class WSDLBasedPolicyProcessor {
040: private HashMap ns2modules = new HashMap();
041:
042: public WSDLBasedPolicyProcessor(ConfigurationContext configctx) {
043: AxisConfiguration axisConfiguration = configctx
044: .getAxisConfiguration();
045: for (Iterator iterator = axisConfiguration.getModules()
046: .values().iterator(); iterator.hasNext();) {
047: AxisModule axisModule = (AxisModule) iterator.next();
048: String[] namespaces = axisModule
049: .getSupportedPolicyNamespaces();
050:
051: if (namespaces == null) {
052: continue;
053: }
054:
055: for (int i = 0; i < namespaces.length; i++) {
056: ArrayList moduleList = null;
057: Object obj = ns2modules.get(namespaces[i]);
058: if (obj == null) {
059: moduleList = new ArrayList(5);
060: ns2modules.put(namespaces[i], moduleList);
061: } else {
062: moduleList = (ArrayList) obj;
063: }
064: moduleList.add(axisModule);
065:
066: }
067: }
068:
069: }
070:
071: public void configureServicePolices(AxisService axisService)
072: throws AxisFault {
073: Iterator operations = axisService.getOperations();
074: while (operations.hasNext()) {
075: AxisOperation axisOp = (AxisOperation) operations.next();
076: // TODO we support only operation level Policy now
077: configureOperationPolices(axisOp);
078: }
079: }
080:
081: public void configureOperationPolices(AxisOperation op)
082: throws AxisFault {
083: PolicyInclude policyInclude = op.getPolicyInclude();
084: Policy policy = policyInclude.getEffectivePolicy();
085: if (policy != null) {
086:
087: policy = (Policy) policy.normalize(policyInclude
088: .getPolicyRegistry(), false);
089:
090: for (Iterator iterator = policy.getAlternatives(); iterator
091: .hasNext();) {
092:
093: List namespaceList = new ArrayList();
094: Assertion assertion;
095:
096: /*
097: * Fist we compute the set of distinct namespaces of assertions
098: * of this particular policy alternative.
099: */
100: for (Iterator assertions = ((List) iterator.next())
101: .iterator(); assertions.hasNext();) {
102:
103: assertion = (Assertion) iterator.next();
104: QName name = assertion.getName();
105: String namespaceURI = name.getNamespaceURI();
106:
107: if (!namespaceList.contains(namespaceURI)) {
108: namespaceList.add(namespaceURI);
109: }
110: }
111:
112: /*
113: * Now we compute all the modules that are are involved in
114: * process assertions that belongs to any of the namespaces of
115: * list.
116: */
117: List modulesToEngage;
118:
119: for (Iterator namespaces = namespaceList.iterator(); iterator
120: .hasNext();) {
121: String namespace = (String) namespaces.next();
122: modulesToEngage = (List) ns2modules.get(namespace);
123:
124: if (modulesToEngage == null) {
125: /*
126: * If there isn't a single module that is not interested
127: * of assertions that belongs to a particular namespace,
128: * we simply ignore it.
129: */
130: System.err
131: .println("cannot find any modules to process "
132: + namespace + "type assertions");
133: // TODO: Log this ..
134: continue;
135:
136: } else {
137: engageModulesToAxisDescription(modulesToEngage,
138: op);
139: }
140: }
141:
142: /*
143: * We only pick the first policy alternative. Other policy
144: * alternatives are ignored.
145: */
146: break;
147: }
148: }
149: }
150:
151: /**
152: * Engages the list of Modules to the specified AxisDescription.
153: */
154: private void engageModulesToAxisDescription(List modulesToEngage,
155: AxisDescription axisDescription) throws AxisFault {
156: AxisModule axisModule;
157: String moduleName;
158:
159: for (Iterator iterator = modulesToEngage.iterator(); iterator
160: .hasNext();) {
161: axisModule = (AxisModule) iterator.next();
162: moduleName = axisModule.getName();
163:
164: if (!axisDescription.isEngaged(moduleName)) {
165: axisDescription.engageModule(axisModule);
166: (axisModule.getModule()).engageNotify(axisDescription);
167: }
168: }
169: }
170: }
|