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    package org.apache.geronimo.cxf;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import javax.xml.ws.WebServiceException;
023    import javax.xml.ws.handler.Handler;
024    import javax.xml.ws.handler.HandlerResolver;
025    import javax.xml.ws.handler.PortInfo;
026    
027    import org.apache.cxf.jaxws.javaee.HandlerChainType;
028    import org.apache.cxf.jaxws.javaee.HandlerChainsType;
029    import org.apache.geronimo.jaxws.annotations.AnnotationException;
030    import org.apache.geronimo.jaxws.annotations.AnnotationProcessor;
031    
032    public class CXFHandlerResolver implements HandlerResolver {
033    
034        private HandlerChainsType handlerChains;
035    
036        private ClassLoader classLoader;
037    
038        private Class serviceClass;
039        
040        private AnnotationProcessor annotationProcessor;
041    
042        public CXFHandlerResolver(ClassLoader classLoader,
043                                  Class serviceClass,
044                                  HandlerChainsType handlerChains,
045                                  AnnotationProcessor annotationProcessor) {
046            this.classLoader = classLoader;
047            this.serviceClass = serviceClass;
048            this.handlerChains = handlerChains;
049            this.annotationProcessor = annotationProcessor;
050        }
051    
052        public List<Handler> getHandlerChain(PortInfo portInfo) {
053    
054            GeronimoHandlerChainBuilder builder = 
055                new GeronimoHandlerChainBuilder(this.classLoader, portInfo);
056    
057            List<Handler> handlers = null;
058            if (this.handlerChains == null) {
059                handlers = builder.buildHandlerChainFromClass(this.serviceClass);
060            } else {
061                handlers = new ArrayList<Handler>();
062                for (HandlerChainType handlerChain : this.handlerChains.getHandlerChain()) {
063                    handlers.addAll(builder.buildHandlerChainFromConfiguration(handlerChain));
064                }
065                handlers = builder.sortHandlers(handlers);
066            }
067    
068            if (this.annotationProcessor != null) {
069                try {
070                    for (Handler handler : handlers) {
071                        this.annotationProcessor.processAnnotations(handler);
072                        this.annotationProcessor.invokePostConstruct(handler);
073                    }
074                } catch (AnnotationException e) {
075                    throw new WebServiceException("Handler annotation failed", e);
076                }
077            }
078    
079            return handlers;
080        }
081    
082    }