Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.client; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.client.WSPortInfo; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.binding.BindingImpl; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.handler.HandlerChainsModel; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.util.HandlerAnnotationInfo; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.util.HandlerAnnotationProcessor; |
aoqi@0 | 35 | |
aoqi@0 | 36 | import javax.jws.HandlerChain; |
aoqi@0 | 37 | import javax.xml.ws.Service; |
aoqi@0 | 38 | import javax.xml.ws.handler.Handler; |
aoqi@0 | 39 | import javax.xml.ws.handler.HandlerResolver; |
aoqi@0 | 40 | import javax.xml.ws.handler.PortInfo; |
aoqi@0 | 41 | import javax.xml.ws.soap.SOAPBinding; |
aoqi@0 | 42 | import java.util.ArrayList; |
aoqi@0 | 43 | import java.util.HashMap; |
aoqi@0 | 44 | import java.util.List; |
aoqi@0 | 45 | import java.util.Map; |
aoqi@0 | 46 | import java.util.logging.Level; |
aoqi@0 | 47 | import java.util.logging.Logger; |
aoqi@0 | 48 | |
aoqi@0 | 49 | /** |
aoqi@0 | 50 | * Used by {@link WSServiceDelegate} to configure {@link BindingImpl} |
aoqi@0 | 51 | * with handlers. The two mechanisms encapsulated by this abstraction |
aoqi@0 | 52 | * is {@link HandlerChain} annotaion and {@link HandlerResolver} |
aoqi@0 | 53 | * interface. |
aoqi@0 | 54 | * |
aoqi@0 | 55 | * @author Kohsuke Kawaguchi |
aoqi@0 | 56 | */ |
aoqi@0 | 57 | abstract class HandlerConfigurator { |
aoqi@0 | 58 | /** |
aoqi@0 | 59 | * Configures the given {@link BindingImpl} object by adding handlers to it. |
aoqi@0 | 60 | */ |
aoqi@0 | 61 | abstract void configureHandlers(@NotNull WSPortInfo port, @NotNull BindingImpl binding); |
aoqi@0 | 62 | |
aoqi@0 | 63 | /** |
aoqi@0 | 64 | * Returns a {@link HandlerResolver}, if this object encapsulates any {@link HandlerResolver}. |
aoqi@0 | 65 | * Otherwise null. |
aoqi@0 | 66 | */ |
aoqi@0 | 67 | abstract HandlerResolver getResolver(); |
aoqi@0 | 68 | |
aoqi@0 | 69 | |
aoqi@0 | 70 | /** |
aoqi@0 | 71 | * Configures handlers by calling {@link HandlerResolver}. |
aoqi@0 | 72 | * <p> |
aoqi@0 | 73 | * When a null {@link HandlerResolver} is set by the user to |
aoqi@0 | 74 | * {@link Service#setHandlerResolver(HandlerResolver)}, we'll use this object |
aoqi@0 | 75 | * with null {@link #resolver}. |
aoqi@0 | 76 | */ |
aoqi@0 | 77 | static final class HandlerResolverImpl extends HandlerConfigurator { |
aoqi@0 | 78 | private final @Nullable HandlerResolver resolver; |
aoqi@0 | 79 | |
aoqi@0 | 80 | public HandlerResolverImpl(HandlerResolver resolver) { |
aoqi@0 | 81 | this.resolver = resolver; |
aoqi@0 | 82 | } |
aoqi@0 | 83 | |
aoqi@0 | 84 | @Override |
aoqi@0 | 85 | void configureHandlers(@NotNull WSPortInfo port, @NotNull BindingImpl binding) { |
aoqi@0 | 86 | if (resolver!=null) { |
aoqi@0 | 87 | binding.setHandlerChain(resolver.getHandlerChain(port)); |
aoqi@0 | 88 | } |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | |
aoqi@0 | 92 | @Override |
aoqi@0 | 93 | HandlerResolver getResolver() { |
aoqi@0 | 94 | return resolver; |
aoqi@0 | 95 | } |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | /** |
aoqi@0 | 99 | * Configures handlers from {@link HandlerChain} annotation. |
aoqi@0 | 100 | * |
aoqi@0 | 101 | * <p> |
aoqi@0 | 102 | * This class is a simple |
aoqi@0 | 103 | * map of PortInfo objects to handler chains. It is used by a |
aoqi@0 | 104 | * {@link WSServiceDelegate} object, and can |
aoqi@0 | 105 | * be replaced by user code with a different class implementing |
aoqi@0 | 106 | * HandlerResolver. This class is only used on the client side, and |
aoqi@0 | 107 | * it includes a lot of logging to help when there are issues since |
aoqi@0 | 108 | * it deals with port names, service names, and bindings. All three |
aoqi@0 | 109 | * must match when getting a handler chain from the map. |
aoqi@0 | 110 | * |
aoqi@0 | 111 | * <p>It is created by the {@link WSServiceDelegate} |
aoqi@0 | 112 | * class , which uses {@link HandlerAnnotationProcessor} to create |
aoqi@0 | 113 | * a handler chain and then it sets the chains on this class and they |
aoqi@0 | 114 | * are put into the map. The ServiceContext uses the map to set handler |
aoqi@0 | 115 | * chains on bindings when they are created. |
aoqi@0 | 116 | */ |
aoqi@0 | 117 | static final class AnnotationConfigurator extends HandlerConfigurator { |
aoqi@0 | 118 | private final HandlerChainsModel handlerModel; |
aoqi@0 | 119 | private final Map<WSPortInfo,HandlerAnnotationInfo> chainMap = new HashMap<WSPortInfo,HandlerAnnotationInfo>(); |
aoqi@0 | 120 | private static final Logger logger = Logger.getLogger( |
aoqi@0 | 121 | com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".handler"); |
aoqi@0 | 122 | |
aoqi@0 | 123 | AnnotationConfigurator(WSServiceDelegate delegate) { |
aoqi@0 | 124 | handlerModel = HandlerAnnotationProcessor.buildHandlerChainsModel(delegate.getServiceClass()); |
aoqi@0 | 125 | assert handlerModel!=null; // this class is suppeod to be called only when there's @HandlerCHain |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | |
aoqi@0 | 129 | void configureHandlers(WSPortInfo port, BindingImpl binding) { |
aoqi@0 | 130 | //Check in cache first |
aoqi@0 | 131 | HandlerAnnotationInfo chain = chainMap.get(port); |
aoqi@0 | 132 | |
aoqi@0 | 133 | if(chain==null) { |
aoqi@0 | 134 | logGetChain(port); |
aoqi@0 | 135 | // Put it in cache |
aoqi@0 | 136 | chain = handlerModel.getHandlersForPortInfo(port); |
aoqi@0 | 137 | chainMap.put(port,chain); |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | if (binding instanceof SOAPBinding) { |
aoqi@0 | 141 | ((SOAPBinding) binding).setRoles(chain.getRoles()); |
aoqi@0 | 142 | } |
aoqi@0 | 143 | |
aoqi@0 | 144 | logSetChain(port,chain); |
aoqi@0 | 145 | binding.setHandlerChain(chain.getHandlers()); |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | HandlerResolver getResolver() { |
aoqi@0 | 149 | return new HandlerResolver() { |
aoqi@0 | 150 | public List<Handler> getHandlerChain(PortInfo portInfo) { |
aoqi@0 | 151 | return new ArrayList<Handler>( |
aoqi@0 | 152 | handlerModel.getHandlersForPortInfo(portInfo).getHandlers()); |
aoqi@0 | 153 | } |
aoqi@0 | 154 | }; |
aoqi@0 | 155 | } |
aoqi@0 | 156 | // logged at finer level |
aoqi@0 | 157 | private void logSetChain(WSPortInfo info, HandlerAnnotationInfo chain) { |
aoqi@0 | 158 | logger.finer("Setting chain of length " + chain.getHandlers().size() + |
aoqi@0 | 159 | " for port info"); |
aoqi@0 | 160 | logPortInfo(info, Level.FINER); |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | // logged at fine level |
aoqi@0 | 164 | private void logGetChain(WSPortInfo info) { |
aoqi@0 | 165 | logger.fine("No handler chain found for port info:"); |
aoqi@0 | 166 | logPortInfo(info, Level.FINE); |
aoqi@0 | 167 | logger.fine("Existing handler chains:"); |
aoqi@0 | 168 | if (chainMap.isEmpty()) { |
aoqi@0 | 169 | logger.fine("none"); |
aoqi@0 | 170 | } else { |
aoqi@0 | 171 | for (WSPortInfo key : chainMap.keySet()) { |
aoqi@0 | 172 | logger.fine(chainMap.get(key).getHandlers().size() + |
aoqi@0 | 173 | " handlers for port info "); |
aoqi@0 | 174 | logPortInfo(key, Level.FINE); |
aoqi@0 | 175 | } |
aoqi@0 | 176 | } |
aoqi@0 | 177 | } |
aoqi@0 | 178 | |
aoqi@0 | 179 | private void logPortInfo(WSPortInfo info, Level level) { |
aoqi@0 | 180 | logger.log(level, "binding: " + info.getBindingID() + |
aoqi@0 | 181 | "\nservice: " + info.getServiceName() + |
aoqi@0 | 182 | "\nport: " + info.getPortName()); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | } |
aoqi@0 | 185 | } |