1/*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.policy.sourcemodel;
27
28import com.sun.xml.internal.ws.policy.AssertionSet;
29import com.sun.xml.internal.ws.policy.NestedPolicy;
30import com.sun.xml.internal.ws.policy.Policy;
31import com.sun.xml.internal.ws.policy.PolicyAssertion;
32import com.sun.xml.internal.ws.policy.PolicyException;
33import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages;
34import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
35
36import java.util.Iterator;
37
38/**
39 * Translate a policy into a PolicySourceModel.
40 *
41 * Code that depends on JAX-WS should use com.sun.xml.internal.ws.api.policy.ModelGenerator
42 * instead of this class.
43 *
44 * @author Marek Potociar
45 * @author Fabian Ritzmann
46 */
47public abstract class PolicyModelGenerator {
48
49    private static final PolicyLogger LOGGER = PolicyLogger.getLogger(PolicyModelGenerator.class);
50
51    /**
52     * This protected constructor avoids direct instantiation from outside of the class
53     */
54    protected PolicyModelGenerator() {
55        // nothing to initialize
56    }
57
58    /**
59     * Factory method that returns a {@link PolicyModelGenerator} instance.
60     *
61     * @return {@link PolicyModelGenerator} instance
62     */
63    public static PolicyModelGenerator getGenerator() {
64        return getNormalizedGenerator(new PolicySourceModelCreator());
65    }
66
67    /**
68     * Allows derived classes to create instances of the package private
69     * CompactModelGenerator.
70     *
71     * @param creator An implementation of the PolicySourceModelCreator.
72     * @return An instance of CompactModelGenerator.
73     */
74    protected static PolicyModelGenerator getCompactGenerator(PolicySourceModelCreator creator) {
75        return new CompactModelGenerator(creator);
76    }
77
78    /**
79     * Allows derived classes to create instances of the package private
80     * NormalizedModelGenerator.
81     *
82     * @param creator An implementation of the PolicySourceModelCreator.
83     * @return An instance of NormalizedModelGenerator.
84     */
85    protected static PolicyModelGenerator getNormalizedGenerator(PolicySourceModelCreator creator) {
86        return new NormalizedModelGenerator(creator);
87    }
88
89    /**
90     * This method translates a {@link Policy} into a
91     * {@link com.sun.xml.internal.ws.policy.sourcemodel policy infoset}. The resulting
92     * PolicySourceModel is disconnected from the input policy, thus any
93     * additional changes in the policy will have no effect on the PolicySourceModel.
94     *
95     * @param policy The policy to be translated into an infoset. May be null.
96     * @return translated The policy infoset. May be null if the input policy was
97     * null.
98     * @throws PolicyException in case Policy translation fails.
99     */
100    public abstract PolicySourceModel translate(final Policy policy) throws PolicyException;
101
102    /**
103     * Iterates through a nested policy and returns the corresponding policy info model.
104     *
105     * @param parentAssertion The parent node.
106     * @param policy The nested policy.
107     * @return The nested policy translated to the policy info model.
108     */
109    protected abstract ModelNode translate(final ModelNode parentAssertion, final NestedPolicy policy);
110
111    /**
112     * Add the contents of the assertion set as child node to the given model node.
113     *
114     * @param node The content of this assertion set are added as child nodes to this node.
115     *     May not be null.
116     * @param assertions The assertions that are to be added to the node. May not be null.
117     */
118    protected void translate(final ModelNode node, final AssertionSet assertions) {
119        for (PolicyAssertion assertion : assertions) {
120            final AssertionData data = AssertionData.createAssertionData(assertion.getName(), assertion.getValue(), assertion.getAttributes(), assertion.isOptional(), assertion.isIgnorable());
121            final ModelNode assertionNode = node.createChildAssertionNode(data);
122            if (assertion.hasNestedPolicy()) {
123                translate(assertionNode, assertion.getNestedPolicy());
124            }
125            if (assertion.hasParameters()) {
126                translate(assertionNode, assertion.getParametersIterator());
127            }
128        }
129    }
130
131    /**
132     * Iterates through all contained assertions and adds them to the info model.
133     *
134     * @param assertionParametersIterator The contained assertions.
135     * @param assertionNode The node to which the assertions are added as child nodes
136     */
137    protected void translate(final ModelNode assertionNode, final Iterator<PolicyAssertion> assertionParametersIterator) {
138        while (assertionParametersIterator.hasNext()) {
139            final PolicyAssertion assertionParameter = assertionParametersIterator.next();
140            final AssertionData data = AssertionData.createAssertionParameterData(assertionParameter.getName(), assertionParameter.getValue(), assertionParameter.getAttributes());
141            final ModelNode assertionParameterNode = assertionNode.createChildAssertionParameterNode(data);
142            if (assertionParameter.hasNestedPolicy()) {
143                throw LOGGER.logSevereException(new IllegalStateException(LocalizationMessages.WSP_0005_UNEXPECTED_POLICY_ELEMENT_FOUND_IN_ASSERTION_PARAM(assertionParameter)));
144            }
145            if (assertionParameter.hasNestedAssertions()) {
146                translate(assertionParameterNode, assertionParameter.getNestedAssertionsIterator());
147            }
148        }
149    }
150
151
152    /**
153     * Allows derived classes to pass their own implementation of PolicySourceModelCreator
154     * into the PolicyModelGenerator instance.
155     */
156    protected static class PolicySourceModelCreator {
157
158        /**
159         * Create an instance of the PolicySourceModel.
160         *
161         * @param policy The policy that underlies the created PolicySourceModel.
162         * @return An instance of the PolicySourceModel.
163         */
164        protected PolicySourceModel create(final Policy policy) {
165            return PolicySourceModel.createPolicySourceModel(policy.getNamespaceVersion(),
166                    policy.getId(), policy.getName());
167        }
168
169    }
170
171}
172