src/share/jaxws_classes/com/sun/xml/internal/ws/client/HandlerConfigurator.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

     1 /*
     2  * Copyright (c) 1997, 2010, 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  */
    26 package com.sun.xml.internal.ws.client;
    28 import com.sun.istack.internal.NotNull;
    29 import com.sun.istack.internal.Nullable;
    30 import com.sun.xml.internal.ws.api.client.WSPortInfo;
    31 import com.sun.xml.internal.ws.binding.BindingImpl;
    32 import com.sun.xml.internal.ws.handler.HandlerChainsModel;
    33 import com.sun.xml.internal.ws.util.HandlerAnnotationInfo;
    34 import com.sun.xml.internal.ws.util.HandlerAnnotationProcessor;
    36 import javax.jws.HandlerChain;
    37 import javax.xml.ws.Service;
    38 import javax.xml.ws.handler.Handler;
    39 import javax.xml.ws.handler.HandlerResolver;
    40 import javax.xml.ws.handler.PortInfo;
    41 import javax.xml.ws.soap.SOAPBinding;
    42 import java.util.ArrayList;
    43 import java.util.HashMap;
    44 import java.util.List;
    45 import java.util.Map;
    46 import java.util.logging.Level;
    47 import java.util.logging.Logger;
    49 /**
    50  * Used by {@link WSServiceDelegate} to configure {@link BindingImpl}
    51  * with handlers. The two mechanisms encapsulated by this abstraction
    52  * is {@link HandlerChain} annotaion and {@link HandlerResolver}
    53  * interface.
    54  *
    55  * @author Kohsuke Kawaguchi
    56  */
    57 abstract class HandlerConfigurator {
    58     /**
    59      * Configures the given {@link BindingImpl} object by adding handlers to it.
    60      */
    61     abstract void configureHandlers(@NotNull WSPortInfo port, @NotNull BindingImpl binding);
    63     /**
    64      * Returns a {@link HandlerResolver}, if this object encapsulates any {@link HandlerResolver}.
    65      * Otherwise null.
    66      */
    67     abstract HandlerResolver getResolver();
    70     /**
    71      * Configures handlers by calling {@link HandlerResolver}.
    72      * <p>
    73      * When a null {@link HandlerResolver} is set by the user to
    74      * {@link Service#setHandlerResolver(HandlerResolver)}, we'll use this object
    75      * with null {@link #resolver}.
    76      */
    77     static final class HandlerResolverImpl extends HandlerConfigurator {
    78         private final @Nullable HandlerResolver resolver;
    80         public HandlerResolverImpl(HandlerResolver resolver) {
    81             this.resolver = resolver;
    82         }
    84         void configureHandlers(@NotNull WSPortInfo port, @NotNull BindingImpl binding) {
    85             if(resolver!=null)
    86                 binding.setHandlerChain(resolver.getHandlerChain(port));
    87         }
    90         HandlerResolver getResolver() {
    91             return resolver;
    92         }
    93     }
    95     /**
    96      * Configures handlers from {@link HandlerChain} annotation.
    97      *
    98      * <p>
    99      * This class is a simple
   100      * map of PortInfo objects to handler chains. It is used by a
   101      * {@link WSServiceDelegate} object, and can
   102      * be replaced by user code with a different class implementing
   103      * HandlerResolver. This class is only used on the client side, and
   104      * it includes a lot of logging to help when there are issues since
   105      * it deals with port names, service names, and bindings. All three
   106      * must match when getting a handler chain from the map.
   107      *
   108      * <p>It is created by the {@link WSServiceDelegate}
   109      * class , which uses {@link HandlerAnnotationProcessor} to create
   110      * a handler chain and then it sets the chains on this class and they
   111      * are put into the map. The ServiceContext uses the map to set handler
   112      * chains on bindings when they are created.
   113      */
   114     static final class AnnotationConfigurator extends HandlerConfigurator {
   115         private final HandlerChainsModel handlerModel;
   116         private final Map<WSPortInfo,HandlerAnnotationInfo> chainMap = new HashMap<WSPortInfo,HandlerAnnotationInfo>();
   117         private static final Logger logger = Logger.getLogger(
   118             com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".handler");
   120         AnnotationConfigurator(WSServiceDelegate delegate) {
   121             handlerModel = HandlerAnnotationProcessor.buildHandlerChainsModel(delegate.getServiceClass());
   122             assert handlerModel!=null; // this class is suppeod to be called only when there's @HandlerCHain
   123         }
   126         void configureHandlers(WSPortInfo port, BindingImpl binding) {
   127             //Check in cache first
   128             HandlerAnnotationInfo chain = chainMap.get(port);
   130             if(chain==null) {
   131                 logGetChain(port);
   132                 // Put it in cache
   133                 chain = handlerModel.getHandlersForPortInfo(port);
   134                 chainMap.put(port,chain);
   135             }
   137             if (binding instanceof SOAPBinding) {
   138                 ((SOAPBinding) binding).setRoles(chain.getRoles());
   139             }
   141             logSetChain(port,chain);
   142             binding.setHandlerChain(chain.getHandlers());
   143         }
   145         HandlerResolver getResolver() {
   146             return new HandlerResolver() {
   147                 public List<Handler> getHandlerChain(PortInfo portInfo) {
   148                     return new ArrayList<Handler>(
   149                         handlerModel.getHandlersForPortInfo(portInfo).getHandlers());
   150                 }
   151             };
   152         }
   153         // logged at finer level
   154         private void logSetChain(WSPortInfo info, HandlerAnnotationInfo chain) {
   155             logger.finer("Setting chain of length " + chain.getHandlers().size() +
   156                 " for port info");
   157             logPortInfo(info, Level.FINER);
   158         }
   160         // logged at fine level
   161         private void logGetChain(WSPortInfo info) {
   162             logger.fine("No handler chain found for port info:");
   163             logPortInfo(info, Level.FINE);
   164             logger.fine("Existing handler chains:");
   165             if (chainMap.isEmpty()) {
   166                 logger.fine("none");
   167             } else {
   168                 for (WSPortInfo key : chainMap.keySet()) {
   169                     logger.fine(chainMap.get(key).getHandlers().size() +
   170                         " handlers for port info ");
   171                     logPortInfo(key, Level.FINE);
   172                 }
   173             }
   174         }
   176         private void logPortInfo(WSPortInfo info, Level level) {
   177             logger.log(level, "binding: " + info.getBindingID() +
   178                 "\nservice: " + info.getServiceName() +
   179                 "\nport: " + info.getPortName());
   180         }
   181     }
   182 }

mercurial