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: */package org.apache.geronimo.axis2;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.geronimo.jaxws.JAXWSUtils;
021: import org.apache.geronimo.xbeans.javaee.HandlerChainType;
022:
023: import javax.xml.namespace.QName;
024: import javax.xml.ws.handler.Handler;
025: import javax.xml.ws.handler.PortInfo;
026: import java.util.Collections;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: /**
031: * @version $Rev$ $Date$
032: */
033: public class GeronimoHandlerChainBuilder extends
034: AnnotationHandlerChainBuilder {
035: private static final Log log = LogFactory
036: .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()),
064: namePattern);
065: }
066:
067: private boolean matchPortName(PortInfo info, String namePattern) {
068: return match((info == null ? null : info.getPortName()),
069: namePattern);
070: }
071:
072: private boolean matchBinding(PortInfo info, List bindings) {
073: return match((info == null ? null : info.getBindingID()),
074: bindings);
075: }
076:
077: private boolean match(String binding, List bindings) {
078: if (binding == null) {
079: return (bindings == null || bindings.isEmpty());
080: } else {
081: if (bindings == null || bindings.isEmpty()) {
082: return true;
083: } else {
084: String actualBindingURI = JAXWSUtils
085: .getBindingURI(binding);
086: Iterator iter = bindings.iterator();
087: while (iter.hasNext()) {
088: String bindingToken = (String) iter.next();
089: String bindingURI = JAXWSUtils
090: .getBindingURI(bindingToken);
091: if (actualBindingURI.equals(bindingURI)) {
092: return true;
093: }
094: }
095: return false;
096: }
097: }
098: }
099:
100: public List<Handler> buildHandlerChainFromConfiguration(
101: HandlerChainType hc) {
102: if (null == hc) {
103: return null;
104: }
105: return sortHandlers(buildHandlerChain(hc,
106: getHandlerClassLoader()));
107: }
108:
109: /*
110: * Performs basic localName matching, namespaces are not checked!
111: */
112: private boolean match(QName name, String namePattern) {
113: if (name == null) {
114: return (namePattern == null || namePattern.equals("*"));
115: } else {
116: if (namePattern == null) {
117: return true;
118: } else {
119: String localNamePattern;
120:
121: // get the local name from pattern
122: int pos = namePattern.indexOf(':');
123: localNamePattern = (pos == -1) ? namePattern
124: : namePattern.substring(pos + 1);
125: localNamePattern = localNamePattern.trim();
126:
127: if (localNamePattern.equals("*")) {
128: // matches anything
129: return true;
130: } else if (localNamePattern.endsWith("*")) {
131: // match start
132: localNamePattern = localNamePattern.substring(0,
133: localNamePattern.length() - 1);
134: return name.getLocalPart().startsWith(
135: localNamePattern);
136: } else {
137: // match exact
138: return name.getLocalPart().equals(localNamePattern);
139: }
140: }
141: }
142: }
143:
144: }
|