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 
26 package com.sun.xml.internal.ws.policy.jaxws;
27 
28 import com.sun.xml.internal.ws.addressing.policy.AddressingFeatureConfigurator;
29 import com.sun.xml.internal.ws.api.model.wsdl.WSDLModel;
30 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
31 import com.sun.xml.internal.ws.api.model.wsdl.WSDLService;
32 import com.sun.xml.internal.ws.encoding.policy.FastInfosetFeatureConfigurator;
33 import com.sun.xml.internal.ws.encoding.policy.MtomFeatureConfigurator;
34 import com.sun.xml.internal.ws.encoding.policy.SelectOptimalEncodingFeatureConfigurator;
35 import com.sun.xml.internal.ws.policy.PolicyException;
36 import com.sun.xml.internal.ws.policy.PolicyMap;
37 import com.sun.xml.internal.ws.policy.PolicyMapKey;
38 import com.sun.xml.internal.ws.policy.jaxws.spi.PolicyFeatureConfigurator;
39 import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
40 import com.sun.xml.internal.ws.util.ServiceFinder;
41 
42 import javax.xml.namespace.QName;
43 import javax.xml.ws.WebServiceFeature;
44 import javax.xml.ws.WebServiceException;
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.Iterator;
48 import java.util.LinkedList;
49 
50 /**
51  * @author Rama Pulavarthi
52  * @author Fabian Ritzmann
53  */
54 public class PolicyUtil {
55 
56     private static final PolicyLogger LOGGER = PolicyLogger.getLogger(PolicyUtil.class);
57     private static final Collection<PolicyFeatureConfigurator> CONFIGURATORS =
58             new LinkedList<PolicyFeatureConfigurator>();
59 
60     static {
61         // Add feature configurators that are already built into JAX-WS
CONFIGURATORS.add(new AddressingFeatureConfigurator())62         CONFIGURATORS.add(new AddressingFeatureConfigurator());
CONFIGURATORS.add(new MtomFeatureConfigurator())63         CONFIGURATORS.add(new MtomFeatureConfigurator());
CONFIGURATORS.add(new FastInfosetFeatureConfigurator())64         CONFIGURATORS.add(new FastInfosetFeatureConfigurator());
CONFIGURATORS.add(new SelectOptimalEncodingFeatureConfigurator())65         CONFIGURATORS.add(new SelectOptimalEncodingFeatureConfigurator());
66 
67         // Dynamically discover remaining feature configurators
addServiceProviders(CONFIGURATORS, PolicyFeatureConfigurator.class)68         addServiceProviders(CONFIGURATORS, PolicyFeatureConfigurator.class);
69     }
70 
71     /**
72      * Adds the dynamically discovered implementations for the given service class
73      * to the given collection.
74      *
75      * @param <T> The type of the service class.
76      * @param providers The discovered implementations are added to this collection.
77      * @param service The service interface.
78      */
addServiceProviders(Collection<T> providers, Class<T> service)79     public static <T> void addServiceProviders(Collection<T> providers, Class<T> service) {
80         final Iterator<T> foundProviders = ServiceFinder.find(service).iterator();
81         while (foundProviders.hasNext()) {
82             providers.add(foundProviders.next());
83         }
84     }
85 
86     /**
87      * Iterates through the ports in the WSDL model, for each policy in the policy
88      * map that is attached at endpoint scope computes a list of corresponding
89      * WebServiceFeatures and sets them on the port.
90      *
91      * @param model The WSDL model
92      * @param policyMap The policy map
93      * @throws PolicyException If the list of WebServiceFeatures could not be computed
94      */
configureModel(final WSDLModel model, PolicyMap policyMap)95     public static void configureModel(final WSDLModel model, PolicyMap policyMap) throws PolicyException {
96         LOGGER.entering(model, policyMap);
97         for (WSDLService service : model.getServices().values()) {
98             for (WSDLPort port : service.getPorts()) {
99                 final Collection<WebServiceFeature> features = getPortScopedFeatures(policyMap, service.getName(), port.getName());
100                 for (WebServiceFeature feature : features) {
101                     port.addFeature(feature);
102                     port.getBinding().addFeature(feature);
103                 }
104             }
105         }
106         LOGGER.exiting();
107     }
108 
109     /**
110      * Returns the list of features that correspond to the policies in the policy
111      * map for a give port
112      *
113      * @param policyMap The service policies
114      * @param serviceName The service name
115      * @param portName The service port name
116      * @return List of features for the given port corresponding to the policies in the map
117      */
getPortScopedFeatures(PolicyMap policyMap, QName serviceName, QName portName)118     public static Collection<WebServiceFeature> getPortScopedFeatures(PolicyMap policyMap, QName serviceName, QName portName) {
119         LOGGER.entering(policyMap, serviceName, portName);
120         Collection<WebServiceFeature> features = new ArrayList<WebServiceFeature>();
121         try {
122             final PolicyMapKey key = PolicyMap.createWsdlEndpointScopeKey(serviceName, portName);
123             for (PolicyFeatureConfigurator configurator : CONFIGURATORS) {
124                 Collection<WebServiceFeature> additionalFeatures = configurator.getFeatures(key, policyMap);
125                 if (additionalFeatures != null) {
126                     features.addAll(additionalFeatures);
127                 }
128             }
129         } catch (PolicyException e) {
130             throw new WebServiceException(e);
131         }
132         LOGGER.exiting(features);
133         return features;
134     }
135 
136 }
137