1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/policy/sourcemodel/PolicyModelGenerator.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,171 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.policy.sourcemodel; 1.30 + 1.31 +import com.sun.xml.internal.ws.policy.AssertionSet; 1.32 +import com.sun.xml.internal.ws.policy.NestedPolicy; 1.33 +import com.sun.xml.internal.ws.policy.Policy; 1.34 +import com.sun.xml.internal.ws.policy.PolicyAssertion; 1.35 +import com.sun.xml.internal.ws.policy.PolicyException; 1.36 +import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages; 1.37 +import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger; 1.38 + 1.39 +import java.util.Iterator; 1.40 + 1.41 +/** 1.42 + * Translate a policy into a PolicySourceModel. 1.43 + * 1.44 + * Code that depends on JAX-WS should use com.sun.xml.internal.ws.api.policy.ModelGenerator 1.45 + * instead of this class. 1.46 + * 1.47 + * @author Marek Potociar 1.48 + * @author Fabian Ritzmann 1.49 + */ 1.50 +public abstract class PolicyModelGenerator { 1.51 + 1.52 + private static final PolicyLogger LOGGER = PolicyLogger.getLogger(PolicyModelGenerator.class); 1.53 + 1.54 + /** 1.55 + * This protected constructor avoids direct instantiation from outside of the class 1.56 + */ 1.57 + protected PolicyModelGenerator() { 1.58 + // nothing to initialize 1.59 + } 1.60 + 1.61 + /** 1.62 + * Factory method that returns a {@link PolicyModelGenerator} instance. 1.63 + * 1.64 + * @return {@link PolicyModelGenerator} instance 1.65 + */ 1.66 + public static PolicyModelGenerator getGenerator() { 1.67 + return getNormalizedGenerator(new PolicySourceModelCreator()); 1.68 + } 1.69 + 1.70 + /** 1.71 + * Allows derived classes to create instances of the package private 1.72 + * CompactModelGenerator. 1.73 + * 1.74 + * @param creator An implementation of the PolicySourceModelCreator. 1.75 + * @return An instance of CompactModelGenerator. 1.76 + */ 1.77 + protected static PolicyModelGenerator getCompactGenerator(PolicySourceModelCreator creator) { 1.78 + return new CompactModelGenerator(creator); 1.79 + } 1.80 + 1.81 + /** 1.82 + * Allows derived classes to create instances of the package private 1.83 + * NormalizedModelGenerator. 1.84 + * 1.85 + * @param creator An implementation of the PolicySourceModelCreator. 1.86 + * @return An instance of NormalizedModelGenerator. 1.87 + */ 1.88 + protected static PolicyModelGenerator getNormalizedGenerator(PolicySourceModelCreator creator) { 1.89 + return new NormalizedModelGenerator(creator); 1.90 + } 1.91 + 1.92 + /** 1.93 + * This method translates a {@link Policy} into a 1.94 + * {@link com.sun.xml.internal.ws.policy.sourcemodel policy infoset}. The resulting 1.95 + * PolicySourceModel is disconnected from the input policy, thus any 1.96 + * additional changes in the policy will have no effect on the PolicySourceModel. 1.97 + * 1.98 + * @param policy The policy to be translated into an infoset. May be null. 1.99 + * @return translated The policy infoset. May be null if the input policy was 1.100 + * null. 1.101 + * @throws PolicyException in case Policy translation fails. 1.102 + */ 1.103 + public abstract PolicySourceModel translate(final Policy policy) throws PolicyException; 1.104 + 1.105 + /** 1.106 + * Iterates through a nested policy and returns the corresponding policy info model. 1.107 + * 1.108 + * @param parentAssertion The parent node. 1.109 + * @param policy The nested policy. 1.110 + * @return The nested policy translated to the policy info model. 1.111 + */ 1.112 + protected abstract ModelNode translate(final ModelNode parentAssertion, final NestedPolicy policy); 1.113 + 1.114 + /** 1.115 + * Add the contents of the assertion set as child node to the given model node. 1.116 + * 1.117 + * @param node The content of this assertion set are added as child nodes to this node. 1.118 + * May not be null. 1.119 + * @param assertions The assertions that are to be added to the node. May not be null. 1.120 + */ 1.121 + protected void translate(final ModelNode node, final AssertionSet assertions) { 1.122 + for (PolicyAssertion assertion : assertions) { 1.123 + final AssertionData data = AssertionData.createAssertionData(assertion.getName(), assertion.getValue(), assertion.getAttributes(), assertion.isOptional(), assertion.isIgnorable()); 1.124 + final ModelNode assertionNode = node.createChildAssertionNode(data); 1.125 + if (assertion.hasNestedPolicy()) { 1.126 + translate(assertionNode, assertion.getNestedPolicy()); 1.127 + } 1.128 + if (assertion.hasParameters()) { 1.129 + translate(assertionNode, assertion.getParametersIterator()); 1.130 + } 1.131 + } 1.132 + } 1.133 + 1.134 + /** 1.135 + * Iterates through all contained assertions and adds them to the info model. 1.136 + * 1.137 + * @param assertionParametersIterator The contained assertions. 1.138 + * @param assertionNode The node to which the assertions are added as child nodes 1.139 + */ 1.140 + protected void translate(final ModelNode assertionNode, final Iterator<PolicyAssertion> assertionParametersIterator) { 1.141 + while (assertionParametersIterator.hasNext()) { 1.142 + final PolicyAssertion assertionParameter = assertionParametersIterator.next(); 1.143 + final AssertionData data = AssertionData.createAssertionParameterData(assertionParameter.getName(), assertionParameter.getValue(), assertionParameter.getAttributes()); 1.144 + final ModelNode assertionParameterNode = assertionNode.createChildAssertionParameterNode(data); 1.145 + if (assertionParameter.hasNestedPolicy()) { 1.146 + throw LOGGER.logSevereException(new IllegalStateException(LocalizationMessages.WSP_0005_UNEXPECTED_POLICY_ELEMENT_FOUND_IN_ASSERTION_PARAM(assertionParameter))); 1.147 + } 1.148 + if (assertionParameter.hasNestedAssertions()) { 1.149 + translate(assertionParameterNode, assertionParameter.getNestedAssertionsIterator()); 1.150 + } 1.151 + } 1.152 + } 1.153 + 1.154 + 1.155 + /** 1.156 + * Allows derived classes to pass their own implementation of PolicySourceModelCreator 1.157 + * into the PolicyModelGenerator instance. 1.158 + */ 1.159 + protected static class PolicySourceModelCreator { 1.160 + 1.161 + /** 1.162 + * Create an instance of the PolicySourceModel. 1.163 + * 1.164 + * @param policy The policy that underlies the created PolicySourceModel. 1.165 + * @return An instance of the PolicySourceModel. 1.166 + */ 1.167 + protected PolicySourceModel create(final Policy policy) { 1.168 + return PolicySourceModel.createPolicySourceModel(policy.getNamespaceVersion(), 1.169 + policy.getId(), policy.getName()); 1.170 + } 1.171 + 1.172 + } 1.173 + 1.174 +}