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