001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.geronimo.axis2;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import javax.xml.ws.WebServiceException;
024    import javax.xml.ws.handler.Handler;
025    import javax.xml.ws.handler.HandlerResolver;
026    
027    import org.apache.geronimo.jaxws.annotations.AnnotationException;
028    import org.apache.geronimo.jaxws.annotations.AnnotationProcessor;
029    import org.apache.geronimo.xbeans.javaee.HandlerChainType;
030    import org.apache.geronimo.xbeans.javaee.HandlerChainsType;
031    
032    /**
033     * @version $Rev$ $Date$
034     */
035    public class Axis2HandlerResolver implements HandlerResolver {
036    
037        private HandlerChainsType handlerChains;
038    
039        private ClassLoader classLoader;
040    
041        private Class serviceClass;
042    
043        private AnnotationProcessor annotationProcessor;
044    
045        public Axis2HandlerResolver(ClassLoader classLoader,
046                                    Class serviceClass,
047                                    HandlerChainsType handlerChains,
048                                    AnnotationProcessor annotationProcessor) {
049            this.classLoader = classLoader;
050            this.serviceClass = serviceClass;
051            this.handlerChains = handlerChains;
052            this.annotationProcessor = annotationProcessor;
053        }
054    
055        public List<Handler> getHandlerChain(javax.xml.ws.handler.PortInfo portInfo) {
056    
057            GeronimoHandlerChainBuilder builder =
058                    new GeronimoHandlerChainBuilder(this.classLoader, portInfo);
059    
060            List<Handler> handlers = null;
061            if (this.handlerChains == null) {
062                handlers = builder.buildHandlerChainFromClass(this.serviceClass);
063            } else {
064                handlers = new ArrayList<Handler>();
065                for (HandlerChainType handlerChain : this.handlerChains.getHandlerChainArray()) {
066                    handlers.addAll(builder.buildHandlerChainFromConfiguration(handlerChain));
067                }
068                handlers = builder.sortHandlers(handlers);
069            }
070    
071            if (this.annotationProcessor != null) {
072                try {
073                    for (Handler handler : handlers) {
074                        this.annotationProcessor.processAnnotations(handler);
075                        this.annotationProcessor.invokePostConstruct(handler);
076                    }
077                } catch (AnnotationException e) {
078                    throw new WebServiceException("Handler annotation failed", e);
079                }
080            }
081    
082            return handlers;
083        }
084    
085    }