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.wsdl.codegen.extension;
021:
022: import org.apache.axis2.context.ConfigurationContext;
023: import org.apache.axis2.context.ConfigurationContextFactory;
024: import org.apache.axis2.description.AxisModule;
025: import org.apache.axis2.description.AxisOperation;
026: import org.apache.axis2.description.AxisService;
027: import org.apache.axis2.description.PolicyInclude;
028: import org.apache.axis2.engine.AxisConfiguration;
029: import org.apache.axis2.modules.Module;
030: import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
031: import org.apache.neethi.Assertion;
032: import org.apache.neethi.Policy;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Element;
035:
036: import javax.xml.namespace.QName;
037: import javax.xml.parsers.DocumentBuilder;
038: import javax.xml.parsers.DocumentBuilderFactory;
039: import javax.xml.parsers.ParserConfigurationException;
040: import java.util.ArrayList;
041: import java.util.HashMap;
042: import java.util.Iterator;
043: import java.util.List;
044: import java.util.Map;
045:
046: public class PolicyEvaluator implements CodeGenExtension {
047:
048: private CodeGenConfiguration configuration;
049:
050: /**
051: * Init method to initialization
052: *
053: * @param configuration
054: * @param namespace2ExtsMap
055: */
056: private void init(CodeGenConfiguration configuration,
057: Map namespace2ExtsMap) {
058: this .configuration = configuration;
059:
060: // adding default PolicyExtensions
061: namespace2ExtsMap
062: .put(
063: "http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization",
064: new MTOMPolicyExtension(configuration));
065: namespace2ExtsMap
066: .put(
067: "http://schemas.xmlsoap.org/ws/2004/09/policy/encoding",
068: new EncodePolicyExtension());
069:
070: String repository = configuration.getRepositoryPath();
071:
072: if (repository == null) {
073: return;
074: }
075:
076: try {
077:
078: ConfigurationContext configurationCtx = ConfigurationContextFactory
079: .createConfigurationContextFromFileSystem(
080: repository, null);
081: AxisConfiguration axisConfiguration = configurationCtx
082: .getAxisConfiguration();
083:
084: for (Iterator iterator = axisConfiguration.getModules()
085: .values().iterator(); iterator.hasNext();) {
086:
087: AxisModule axisModule = (AxisModule) iterator.next();
088: String[] namespaces = axisModule
089: .getSupportedPolicyNamespaces();
090:
091: if (namespaces == null) {
092: continue;
093: }
094:
095: Module module = axisModule.getModule();
096: if (!(module instanceof ModulePolicyExtension)) {
097: continue;
098: }
099:
100: PolicyExtension ext = ((ModulePolicyExtension) module)
101: .getPolicyExtension();
102:
103: for (int i = 0; i < namespaces.length; i++) {
104: namespace2ExtsMap.put(namespaces[i], ext);
105: }
106: }
107:
108: } catch (Exception e) {
109: System.err
110: .println("cannot create repository : policy will not be supported");
111: }
112: }
113:
114: public void engage(CodeGenConfiguration configuration) {
115:
116: Map namespace2ExtMap = new HashMap();
117: //initialize
118: init(configuration, namespace2ExtMap);
119:
120: Document document = getEmptyDocument();
121: Element rootElement = document
122: .createElement("module-codegen-policy-extensions");
123:
124: AxisOperation axisOperation;
125: QName opName;
126: PolicyInclude policyInclude;
127: Policy policy;
128:
129: List axisServices = configuration.getAxisServices();
130: AxisService axisService;
131: for (Iterator servicesIter = axisServices.iterator(); servicesIter
132: .hasNext();) {
133: axisService = (AxisService) servicesIter.next();
134: for (Iterator iterator = axisService.getOperations(); iterator
135: .hasNext();) {
136: axisOperation = (AxisOperation) iterator.next();
137: opName = axisOperation.getName();
138:
139: policyInclude = axisOperation.getPolicyInclude();
140: policy = policyInclude.getEffectivePolicy();
141:
142: if (policy != null) {
143: processPolicies(document, rootElement, policy,
144: opName, namespace2ExtMap);
145: }
146: }
147: }
148:
149: // TODO: think about this how can we support this
150: configuration.putProperty("module-codegen-policy-extensions",
151: rootElement);
152: }
153:
154: /**
155: * Process policies
156: *
157: * @param document
158: * @param rootElement
159: * @param policy
160: * @param opName
161: */
162: private void processPolicies(Document document,
163: Element rootElement, Policy policy, QName opName,
164: Map ns2Exts) {
165:
166: HashMap map = new HashMap();
167:
168: for (Iterator iterator = policy.getAlternatives(); iterator
169: .hasNext();) {
170:
171: String targetNamesapce = null;
172: Assertion assertion;
173: List assertionList;
174:
175: for (Iterator assertions = ((List) iterator.next())
176: .iterator(); assertions.hasNext();) {
177:
178: assertion = (Assertion) assertions.next();
179: targetNamesapce = assertion.getName().getNamespaceURI();
180:
181: if ((assertionList = (List) map.get(targetNamesapce)) == null) {
182: assertionList = new ArrayList();
183: map.put(targetNamesapce, assertionList);
184: }
185:
186: assertionList.add(assertions);
187: }
188:
189: // here we pick the first policy alternative and ignor the rest
190: break;
191: }
192:
193: for (Iterator iterator = map.keySet().iterator(); iterator
194: .hasNext();) {
195: String targetNamespace = (String) iterator.next();
196: PolicyExtension policyExtension = (PolicyExtension) ns2Exts
197: .get(targetNamespace);
198:
199: if (policyExtension == null) {
200: System.err
201: .println("cannot find a PolicyExtension to process "
202: + targetNamespace + "type assertions");
203: continue;
204: }
205:
206: policyExtension.init(configuration);
207: policyExtension.addMethodsToStub(document, rootElement,
208: opName, (List) map.get(targetNamespace));
209: }
210: }
211:
212: private Document getEmptyDocument() {
213: try {
214: DocumentBuilder documentBuilder = DocumentBuilderFactory
215: .newInstance().newDocumentBuilder();
216:
217: return documentBuilder.newDocument();
218: } catch (ParserConfigurationException e) {
219: throw new RuntimeException(e);
220: }
221: }
222:
223: class MTOMPolicyExtension implements PolicyExtension {
224:
225: private boolean setOnce = false;
226: private CodeGenConfiguration configuration;
227:
228: public void init(CodeGenConfiguration configuration) {
229: }
230:
231: public MTOMPolicyExtension(CodeGenConfiguration configuration) {
232: this .configuration = configuration;
233: }
234:
235: public void addMethodsToStub(Document document,
236: Element element, QName operationName, List assertions) {
237:
238: // FIXME
239:
240: // if (!setOnce) {
241: // Object plainBase64PropertyMap = configuration.getProperty(Constants.PLAIN_BASE_64_PROPERTY_KEY);
242: // configuration.putProperty(Constants.BASE_64_PROPERTY_KEY, plainBase64PropertyMap);
243: //
244: // setOnce = true;
245: // }
246: //
247: // Element optimizeContent = document.createElement("optimizeContent");
248: // Element opNameElement = document.createElement("opName");
249: //
250: // opNameElement.setAttribute("ns-url", operationName.getNamespaceURI());
251: // opNameElement.setAttribute("localName", operationName.getLocalPart());
252: //
253: // optimizeContent.appendChild(opNameElement);
254: //
255: // element.appendChild(optimizeContent);
256: }
257: }
258:
259: class EncodePolicyExtension implements PolicyExtension {
260:
261: public void init(CodeGenConfiguration configuration) {
262: }
263:
264: public void addMethodsToStub(Document document,
265: Element element, QName operationName, List assertions) {
266: // TODO implement encoding
267: }
268: }
269: }
|