aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.policy.sourcemodel; aoqi@0: aoqi@0: import com.sun.xml.internal.ws.policy.AssertionSet; aoqi@0: import com.sun.xml.internal.ws.policy.NestedPolicy; aoqi@0: import com.sun.xml.internal.ws.policy.Policy; aoqi@0: import com.sun.xml.internal.ws.policy.PolicyAssertion; aoqi@0: import com.sun.xml.internal.ws.policy.PolicyException; aoqi@0: import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages; aoqi@0: import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger; aoqi@0: aoqi@0: import java.util.Iterator; aoqi@0: aoqi@0: /** aoqi@0: * Translate a policy into a PolicySourceModel. aoqi@0: * aoqi@0: * Code that depends on JAX-WS should use com.sun.xml.internal.ws.api.policy.ModelGenerator aoqi@0: * instead of this class. aoqi@0: * aoqi@0: * @author Marek Potociar aoqi@0: * @author Fabian Ritzmann aoqi@0: */ aoqi@0: public abstract class PolicyModelGenerator { aoqi@0: aoqi@0: private static final PolicyLogger LOGGER = PolicyLogger.getLogger(PolicyModelGenerator.class); aoqi@0: aoqi@0: /** aoqi@0: * This protected constructor avoids direct instantiation from outside of the class aoqi@0: */ aoqi@0: protected PolicyModelGenerator() { aoqi@0: // nothing to initialize aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Factory method that returns a {@link PolicyModelGenerator} instance. aoqi@0: * aoqi@0: * @return {@link PolicyModelGenerator} instance aoqi@0: */ aoqi@0: public static PolicyModelGenerator getGenerator() { aoqi@0: return getNormalizedGenerator(new PolicySourceModelCreator()); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Allows derived classes to create instances of the package private aoqi@0: * CompactModelGenerator. aoqi@0: * aoqi@0: * @param creator An implementation of the PolicySourceModelCreator. aoqi@0: * @return An instance of CompactModelGenerator. aoqi@0: */ aoqi@0: protected static PolicyModelGenerator getCompactGenerator(PolicySourceModelCreator creator) { aoqi@0: return new CompactModelGenerator(creator); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Allows derived classes to create instances of the package private aoqi@0: * NormalizedModelGenerator. aoqi@0: * aoqi@0: * @param creator An implementation of the PolicySourceModelCreator. aoqi@0: * @return An instance of NormalizedModelGenerator. aoqi@0: */ aoqi@0: protected static PolicyModelGenerator getNormalizedGenerator(PolicySourceModelCreator creator) { aoqi@0: return new NormalizedModelGenerator(creator); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * This method translates a {@link Policy} into a aoqi@0: * {@link com.sun.xml.internal.ws.policy.sourcemodel policy infoset}. The resulting aoqi@0: * PolicySourceModel is disconnected from the input policy, thus any aoqi@0: * additional changes in the policy will have no effect on the PolicySourceModel. aoqi@0: * aoqi@0: * @param policy The policy to be translated into an infoset. May be null. aoqi@0: * @return translated The policy infoset. May be null if the input policy was aoqi@0: * null. aoqi@0: * @throws PolicyException in case Policy translation fails. aoqi@0: */ aoqi@0: public abstract PolicySourceModel translate(final Policy policy) throws PolicyException; aoqi@0: aoqi@0: /** aoqi@0: * Iterates through a nested policy and returns the corresponding policy info model. aoqi@0: * aoqi@0: * @param parentAssertion The parent node. aoqi@0: * @param policy The nested policy. aoqi@0: * @return The nested policy translated to the policy info model. aoqi@0: */ aoqi@0: protected abstract ModelNode translate(final ModelNode parentAssertion, final NestedPolicy policy); aoqi@0: aoqi@0: /** aoqi@0: * Add the contents of the assertion set as child node to the given model node. aoqi@0: * aoqi@0: * @param node The content of this assertion set are added as child nodes to this node. aoqi@0: * May not be null. aoqi@0: * @param assertions The assertions that are to be added to the node. May not be null. aoqi@0: */ aoqi@0: protected void translate(final ModelNode node, final AssertionSet assertions) { aoqi@0: for (PolicyAssertion assertion : assertions) { aoqi@0: final AssertionData data = AssertionData.createAssertionData(assertion.getName(), assertion.getValue(), assertion.getAttributes(), assertion.isOptional(), assertion.isIgnorable()); aoqi@0: final ModelNode assertionNode = node.createChildAssertionNode(data); aoqi@0: if (assertion.hasNestedPolicy()) { aoqi@0: translate(assertionNode, assertion.getNestedPolicy()); aoqi@0: } aoqi@0: if (assertion.hasParameters()) { aoqi@0: translate(assertionNode, assertion.getParametersIterator()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Iterates through all contained assertions and adds them to the info model. aoqi@0: * aoqi@0: * @param assertionParametersIterator The contained assertions. aoqi@0: * @param assertionNode The node to which the assertions are added as child nodes aoqi@0: */ aoqi@0: protected void translate(final ModelNode assertionNode, final Iterator assertionParametersIterator) { aoqi@0: while (assertionParametersIterator.hasNext()) { aoqi@0: final PolicyAssertion assertionParameter = assertionParametersIterator.next(); aoqi@0: final AssertionData data = AssertionData.createAssertionParameterData(assertionParameter.getName(), assertionParameter.getValue(), assertionParameter.getAttributes()); aoqi@0: final ModelNode assertionParameterNode = assertionNode.createChildAssertionParameterNode(data); aoqi@0: if (assertionParameter.hasNestedPolicy()) { aoqi@0: throw LOGGER.logSevereException(new IllegalStateException(LocalizationMessages.WSP_0005_UNEXPECTED_POLICY_ELEMENT_FOUND_IN_ASSERTION_PARAM(assertionParameter))); aoqi@0: } aoqi@0: if (assertionParameter.hasNestedAssertions()) { aoqi@0: translate(assertionParameterNode, assertionParameter.getNestedAssertionsIterator()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Allows derived classes to pass their own implementation of PolicySourceModelCreator aoqi@0: * into the PolicyModelGenerator instance. aoqi@0: */ aoqi@0: protected static class PolicySourceModelCreator { aoqi@0: aoqi@0: /** aoqi@0: * Create an instance of the PolicySourceModel. aoqi@0: * aoqi@0: * @param policy The policy that underlies the created PolicySourceModel. aoqi@0: * @return An instance of the PolicySourceModel. aoqi@0: */ aoqi@0: protected PolicySourceModel create(final Policy policy) { aoqi@0: return PolicySourceModel.createPolicySourceModel(policy.getNamespaceVersion(), aoqi@0: policy.getId(), policy.getName()); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: }