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 org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.geronimo.jaxws.JAXWSUtils;
023    import org.apache.geronimo.xbeans.javaee.HandlerChainType;
024    
025    import javax.xml.namespace.QName;
026    import javax.xml.ws.handler.Handler;
027    import javax.xml.ws.handler.PortInfo;
028    import java.util.Collections;
029    import java.util.Iterator;
030    import java.util.List;
031    
032    /**
033     * @version $Rev$ $Date$
034     */
035    public class GeronimoHandlerChainBuilder extends AnnotationHandlerChainBuilder {
036        private static final Log log = LogFactory.getLog(GeronimoHandlerChainBuilder.class);
037    
038        private ClassLoader classLoader = null;
039        private javax.xml.ws.handler.PortInfo portInfo;
040    
041        public GeronimoHandlerChainBuilder(ClassLoader classloader,
042                                           PortInfo portInfo) {
043            this.classLoader = classloader;
044            this.portInfo = portInfo;
045        }
046    
047        public ClassLoader getHandlerClassLoader() {
048            return this.classLoader;
049        }
050    
051        protected List<Handler> buildHandlerChain(HandlerChainType hc,
052                                                  ClassLoader classLoader) {
053            if (matchServiceName(portInfo, hc.getServiceNamePattern())
054                    && matchPortName(portInfo, hc.getPortNamePattern())
055                    && matchBinding(portInfo, hc.getProtocolBindings())) {
056                return super.buildHandlerChain(hc, classLoader);
057            } else {
058                return Collections.emptyList();
059            }
060        }
061    
062        private boolean matchServiceName(PortInfo info, String namePattern) {
063            return match((info == null ? null : info.getServiceName()), namePattern);
064        }
065    
066        private boolean matchPortName(PortInfo info, String namePattern) {
067            return match((info == null ? null : info.getPortName()), namePattern);
068        }
069    
070        private boolean matchBinding(PortInfo info, List bindings) {
071            return match((info == null ? null : info.getBindingID()), bindings);
072        }
073    
074        private boolean match(String binding, List bindings) {
075            if (binding == null) {
076                return (bindings == null || bindings.isEmpty());
077            } else {
078                if (bindings == null || bindings.isEmpty()) {
079                    return true;
080                } else {
081                    String actualBindingURI = JAXWSUtils.getBindingURI(binding);
082                    Iterator iter = bindings.iterator();
083                    while (iter.hasNext()) {
084                        String bindingToken = (String) iter.next();
085                        String bindingURI = JAXWSUtils.getBindingURI(bindingToken);
086                        if (actualBindingURI.equals(bindingURI)) {
087                            return true;
088                        }
089                    }
090                    return false;               
091                }
092            }
093        }
094        
095        public List<Handler> buildHandlerChainFromConfiguration(HandlerChainType hc) {
096            if (null == hc) {
097                return null;
098            }
099            return sortHandlers(buildHandlerChain(hc, getHandlerClassLoader()));
100        }
101    
102        /*
103         * Performs basic localName matching, namespaces are not checked!
104         */
105        private boolean match(QName name, String namePattern) {
106            if (name == null) {
107                return (namePattern == null || namePattern.equals("*"));
108            } else {
109                if (namePattern == null) {
110                    return true;
111                } else {
112                    String localNamePattern;
113    
114                    // get the local name from pattern
115                    int pos = namePattern.indexOf(':');
116                    localNamePattern = (pos == -1) ? namePattern : namePattern
117                            .substring(pos + 1);
118                    localNamePattern = localNamePattern.trim();
119    
120                    if (localNamePattern.equals("*")) {
121                        // matches anything
122                        return true;
123                    } else if (localNamePattern.endsWith("*")) {
124                        // match start
125                        localNamePattern = localNamePattern.substring(0,
126                                localNamePattern.length() - 1);
127                        return name.getLocalPart().startsWith(localNamePattern);
128                    } else {
129                        // match exact
130                        return name.getLocalPart().equals(localNamePattern);
131                    }
132                }
133            }
134        }
135    
136    }