1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/client/HandlerConfigurator.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,182 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.client; 1.30 + 1.31 +import com.sun.istack.internal.NotNull; 1.32 +import com.sun.istack.internal.Nullable; 1.33 +import com.sun.xml.internal.ws.api.client.WSPortInfo; 1.34 +import com.sun.xml.internal.ws.binding.BindingImpl; 1.35 +import com.sun.xml.internal.ws.handler.HandlerChainsModel; 1.36 +import com.sun.xml.internal.ws.util.HandlerAnnotationInfo; 1.37 +import com.sun.xml.internal.ws.util.HandlerAnnotationProcessor; 1.38 + 1.39 +import javax.jws.HandlerChain; 1.40 +import javax.xml.ws.Service; 1.41 +import javax.xml.ws.handler.Handler; 1.42 +import javax.xml.ws.handler.HandlerResolver; 1.43 +import javax.xml.ws.handler.PortInfo; 1.44 +import javax.xml.ws.soap.SOAPBinding; 1.45 +import java.util.ArrayList; 1.46 +import java.util.HashMap; 1.47 +import java.util.List; 1.48 +import java.util.Map; 1.49 +import java.util.logging.Level; 1.50 +import java.util.logging.Logger; 1.51 + 1.52 +/** 1.53 + * Used by {@link WSServiceDelegate} to configure {@link BindingImpl} 1.54 + * with handlers. The two mechanisms encapsulated by this abstraction 1.55 + * is {@link HandlerChain} annotaion and {@link HandlerResolver} 1.56 + * interface. 1.57 + * 1.58 + * @author Kohsuke Kawaguchi 1.59 + */ 1.60 +abstract class HandlerConfigurator { 1.61 + /** 1.62 + * Configures the given {@link BindingImpl} object by adding handlers to it. 1.63 + */ 1.64 + abstract void configureHandlers(@NotNull WSPortInfo port, @NotNull BindingImpl binding); 1.65 + 1.66 + /** 1.67 + * Returns a {@link HandlerResolver}, if this object encapsulates any {@link HandlerResolver}. 1.68 + * Otherwise null. 1.69 + */ 1.70 + abstract HandlerResolver getResolver(); 1.71 + 1.72 + 1.73 + /** 1.74 + * Configures handlers by calling {@link HandlerResolver}. 1.75 + * <p> 1.76 + * When a null {@link HandlerResolver} is set by the user to 1.77 + * {@link Service#setHandlerResolver(HandlerResolver)}, we'll use this object 1.78 + * with null {@link #resolver}. 1.79 + */ 1.80 + static final class HandlerResolverImpl extends HandlerConfigurator { 1.81 + private final @Nullable HandlerResolver resolver; 1.82 + 1.83 + public HandlerResolverImpl(HandlerResolver resolver) { 1.84 + this.resolver = resolver; 1.85 + } 1.86 + 1.87 + void configureHandlers(@NotNull WSPortInfo port, @NotNull BindingImpl binding) { 1.88 + if(resolver!=null) 1.89 + binding.setHandlerChain(resolver.getHandlerChain(port)); 1.90 + } 1.91 + 1.92 + 1.93 + HandlerResolver getResolver() { 1.94 + return resolver; 1.95 + } 1.96 + } 1.97 + 1.98 + /** 1.99 + * Configures handlers from {@link HandlerChain} annotation. 1.100 + * 1.101 + * <p> 1.102 + * This class is a simple 1.103 + * map of PortInfo objects to handler chains. It is used by a 1.104 + * {@link WSServiceDelegate} object, and can 1.105 + * be replaced by user code with a different class implementing 1.106 + * HandlerResolver. This class is only used on the client side, and 1.107 + * it includes a lot of logging to help when there are issues since 1.108 + * it deals with port names, service names, and bindings. All three 1.109 + * must match when getting a handler chain from the map. 1.110 + * 1.111 + * <p>It is created by the {@link WSServiceDelegate} 1.112 + * class , which uses {@link HandlerAnnotationProcessor} to create 1.113 + * a handler chain and then it sets the chains on this class and they 1.114 + * are put into the map. The ServiceContext uses the map to set handler 1.115 + * chains on bindings when they are created. 1.116 + */ 1.117 + static final class AnnotationConfigurator extends HandlerConfigurator { 1.118 + private final HandlerChainsModel handlerModel; 1.119 + private final Map<WSPortInfo,HandlerAnnotationInfo> chainMap = new HashMap<WSPortInfo,HandlerAnnotationInfo>(); 1.120 + private static final Logger logger = Logger.getLogger( 1.121 + com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".handler"); 1.122 + 1.123 + AnnotationConfigurator(WSServiceDelegate delegate) { 1.124 + handlerModel = HandlerAnnotationProcessor.buildHandlerChainsModel(delegate.getServiceClass()); 1.125 + assert handlerModel!=null; // this class is suppeod to be called only when there's @HandlerCHain 1.126 + } 1.127 + 1.128 + 1.129 + void configureHandlers(WSPortInfo port, BindingImpl binding) { 1.130 + //Check in cache first 1.131 + HandlerAnnotationInfo chain = chainMap.get(port); 1.132 + 1.133 + if(chain==null) { 1.134 + logGetChain(port); 1.135 + // Put it in cache 1.136 + chain = handlerModel.getHandlersForPortInfo(port); 1.137 + chainMap.put(port,chain); 1.138 + } 1.139 + 1.140 + if (binding instanceof SOAPBinding) { 1.141 + ((SOAPBinding) binding).setRoles(chain.getRoles()); 1.142 + } 1.143 + 1.144 + logSetChain(port,chain); 1.145 + binding.setHandlerChain(chain.getHandlers()); 1.146 + } 1.147 + 1.148 + HandlerResolver getResolver() { 1.149 + return new HandlerResolver() { 1.150 + public List<Handler> getHandlerChain(PortInfo portInfo) { 1.151 + return new ArrayList<Handler>( 1.152 + handlerModel.getHandlersForPortInfo(portInfo).getHandlers()); 1.153 + } 1.154 + }; 1.155 + } 1.156 + // logged at finer level 1.157 + private void logSetChain(WSPortInfo info, HandlerAnnotationInfo chain) { 1.158 + logger.finer("Setting chain of length " + chain.getHandlers().size() + 1.159 + " for port info"); 1.160 + logPortInfo(info, Level.FINER); 1.161 + } 1.162 + 1.163 + // logged at fine level 1.164 + private void logGetChain(WSPortInfo info) { 1.165 + logger.fine("No handler chain found for port info:"); 1.166 + logPortInfo(info, Level.FINE); 1.167 + logger.fine("Existing handler chains:"); 1.168 + if (chainMap.isEmpty()) { 1.169 + logger.fine("none"); 1.170 + } else { 1.171 + for (WSPortInfo key : chainMap.keySet()) { 1.172 + logger.fine(chainMap.get(key).getHandlers().size() + 1.173 + " handlers for port info "); 1.174 + logPortInfo(key, Level.FINE); 1.175 + } 1.176 + } 1.177 + } 1.178 + 1.179 + private void logPortInfo(WSPortInfo info, Level level) { 1.180 + logger.log(level, "binding: " + info.getBindingID() + 1.181 + "\nservice: " + info.getServiceName() + 1.182 + "\nport: " + info.getPortName()); 1.183 + } 1.184 + } 1.185 +}