001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.jaxws.handler;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.ws.handler.Handler;
027: import javax.xml.ws.handler.HandlerResolver;
028: import javax.xml.ws.handler.PortInfo;
029:
030: import org.apache.cxf.Bus;
031: import org.apache.cxf.common.injection.ResourceInjector;
032: import org.apache.cxf.resource.DefaultResourceManager;
033: import org.apache.cxf.resource.ResourceManager;
034: import org.apache.cxf.resource.ResourceResolver;
035:
036: public class HandlerResolverImpl implements HandlerResolver {
037: private final Map<PortInfo, List<Handler>> handlerMap = new HashMap<PortInfo, List<Handler>>();
038:
039: //private QName service;
040: private Class<?> annotationClass;
041: private Bus bus;
042:
043: public HandlerResolverImpl(Bus bus, QName serviceName,
044: Class<?> clazz) {
045: //this.service = pService;
046: this .bus = bus;
047: this .annotationClass = clazz;
048: }
049:
050: public HandlerResolverImpl() {
051: this (null, null, null);
052: }
053:
054: public List<Handler> getHandlerChain(PortInfo portInfo) {
055:
056: List<Handler> handlerChain = handlerMap.get(portInfo);
057: if (handlerChain == null) {
058: handlerChain = createHandlerChain(portInfo);
059: handlerMap.put(portInfo, handlerChain);
060: }
061: return handlerChain;
062: }
063:
064: private List<Handler> createHandlerChain(PortInfo portInfo) {
065: List<Handler> chain = null;
066:
067: if (null == chain) {
068: chain = new ArrayList<Handler>();
069: }
070: if (annotationClass != null) {
071: chain.addAll(getHandlersFromAnnotation(annotationClass));
072: }
073:
074: for (Handler h : chain) {
075: configHandler(h);
076: }
077:
078: return chain;
079: }
080:
081: /**
082: * Obtain handler chain from annotations.
083: *
084: * @param obj A endpoint implementation class or a SEI, or a generated
085: * service class.
086: */
087: private List<Handler> getHandlersFromAnnotation(Class<?> clazz) {
088: AnnotationHandlerChainBuilder builder = new AnnotationHandlerChainBuilder();
089:
090: List<Handler> chain = builder.buildHandlerChainFromClass(clazz);
091:
092: return chain;
093: }
094:
095: /**
096: * JAX-WS section 9.3.1: The runtime MUST then carry out any injections
097: * requested by the handler, typically via the javax .annotation.Resource
098: * annotation. After all the injections have been carried out, including in
099: * the case where no injections were requested, the runtime MUST invoke the
100: * method carrying a javax.annotation .PostConstruct annotation, if present.
101: */
102: private void configHandler(Handler handler) {
103: if (handler != null) {
104: ResourceManager resourceManager = bus
105: .getExtension(ResourceManager.class);
106: List<ResourceResolver> resolvers = resourceManager
107: .getResourceResolvers();
108: resourceManager = new DefaultResourceManager(resolvers);
109: // resourceManager.addResourceResolver(new WebContextEntriesResourceResolver());
110: ResourceInjector injector = new ResourceInjector(
111: resourceManager);
112: injector.inject(handler);
113: }
114:
115: }
116: }
|