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.lang.reflect.InvocationTargetException;
021: import java.lang.reflect.Method;
022: import java.net.URL;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.ResourceBundle;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: import javax.xml.ws.WebServiceException;
032: import javax.xml.ws.handler.Handler;
033: import javax.xml.ws.handler.LogicalHandler;
034:
035: import org.apache.cxf.Bus;
036: import org.apache.cxf.common.injection.ResourceInjector;
037: import org.apache.cxf.common.logging.LogUtils;
038: import org.apache.cxf.jaxws.javaee.HandlerChainType;
039: import org.apache.cxf.jaxws.javaee.ParamValueType;
040: import org.apache.cxf.jaxws.javaee.PortComponentHandlerType;
041: import org.apache.cxf.resource.ResourceManager;
042: import org.apache.cxf.resource.ResourceResolver;
043:
044: public class HandlerChainBuilder {
045: static final Logger LOG = LogUtils
046: .getL7dLogger(HandlerChainBuilder.class);
047: private static final ResourceBundle BUNDLE = LOG
048: .getResourceBundle();
049:
050: private Bus bus;
051: private boolean handlerInitEnabled = true;
052:
053: public HandlerChainBuilder(Bus aBus) {
054: bus = aBus;
055: }
056:
057: public HandlerChainBuilder() {
058: this (null);
059: }
060:
061: public List<Handler> buildHandlerChainFromConfiguration(
062: HandlerChainType hc) {
063: if (null == hc) {
064: return null;
065: }
066: return sortHandlers(buildHandlerChain(hc,
067: getHandlerClassLoader()));
068: }
069:
070: // methods used by Geronimo to allow configuring things themselves
071: public void setHandlerInitEnabled(boolean b) {
072: handlerInitEnabled = b;
073: }
074:
075: public boolean isHandlerInitEnabled() {
076: return handlerInitEnabled;
077: }
078:
079: /**
080: * sorts the handlers into correct order. All of the logical handlers first
081: * followed by the protocol handlers
082: *
083: * @param handlers
084: * @return sorted list of handlers
085: */
086: public List<Handler> sortHandlers(List<Handler> handlers) {
087:
088: List<LogicalHandler> logicalHandlers = new ArrayList<LogicalHandler>();
089: List<Handler> protocolHandlers = new ArrayList<Handler>();
090:
091: for (Handler handler : handlers) {
092: if (handler instanceof LogicalHandler) {
093: logicalHandlers.add((LogicalHandler) handler);
094: } else {
095: protocolHandlers.add(handler);
096: }
097: }
098:
099: List<Handler> sortedHandlers = new ArrayList<Handler>();
100: sortedHandlers.addAll(logicalHandlers);
101: sortedHandlers.addAll(protocolHandlers);
102: return sortedHandlers;
103: }
104:
105: protected ClassLoader getHandlerClassLoader() {
106: return getClass().getClassLoader();
107: }
108:
109: protected List<Handler> buildHandlerChain(HandlerChainType hc,
110: ClassLoader classLoader) {
111: List<Handler> handlerChain = new ArrayList<Handler>();
112: for (PortComponentHandlerType ht : hc.getHandler()) {
113: try {
114: LOG.log(Level.FINE, "loading handler", trimString(ht
115: .getHandlerName().getValue()));
116:
117: Class<? extends Handler> handlerClass = Class.forName(
118: trimString(ht.getHandlerClass().getValue()),
119: true, classLoader).asSubclass(Handler.class);
120:
121: Handler handler = handlerClass.newInstance();
122: LOG.fine("adding handler to chain: " + handler);
123: configureHandler(handler, ht);
124: handlerChain.add(handler);
125: } catch (Exception e) {
126: throw new WebServiceException(BUNDLE
127: .getString("HANDLER_INSTANTIATION_EXC"), e);
128: }
129: }
130: return handlerChain;
131: }
132:
133: /**
134: * Resolve handler chain configuration file associated with the given class
135: *
136: * @param clz
137: * @param filename
138: * @return A URL object or null if no resource with this name is found
139: */
140: protected URL resolveHandlerChainFile(Class clz, String filename) {
141: URL handlerFile = clz.getResource(filename);
142: if (handlerFile == null) {
143: //the file location might be an absolute java.net.URL in externalForm.
144: try {
145: handlerFile = new URL(filename);
146: //test if the URL can be opened
147: handlerFile.openStream();
148: } catch (Exception e) {
149: //do nothing
150: }
151: }
152: return handlerFile;
153: }
154:
155: private void configureHandler(Handler handler,
156: PortComponentHandlerType h) {
157: if (!handlerInitEnabled) {
158: return;
159: }
160:
161: if (h.getInitParam().size() == 0) {
162: return;
163: }
164:
165: Map<String, String> params = new HashMap<String, String>();
166:
167: for (ParamValueType param : h.getInitParam()) {
168: params.put(trimString(param.getParamName() == null ? null
169: : param.getParamName().getValue()),
170: trimString(param.getParamValue() == null ? null
171: : param.getParamValue().getValue()));
172: }
173:
174: Method initMethod = getInitMethod(handler);
175: if (initMethod != null) {
176: initializeViaInitMethod(handler, params, initMethod);
177: } else {
178: initializeViaInjection(handler, params);
179: }
180: }
181:
182: private void initializeViaInjection(Handler handler,
183: final Map<String, String> params) {
184: if (bus != null) {
185: ResourceManager resMgr = bus
186: .getExtension(ResourceManager.class);
187: List<ResourceResolver> resolvers = resMgr
188: .getResourceResolvers();
189: resolvers.add(new InitParamResourceResolver(params));
190: ResourceInjector resInj = new ResourceInjector(resMgr,
191: resolvers);
192: resInj.inject(handler);
193: }
194: }
195:
196: private void initializeViaInitMethod(Handler handler,
197: Map<String, String> params, Method init) {
198: try {
199: init.invoke(handler, params);
200: } catch (InvocationTargetException ex) {
201: Throwable t = ex.getCause() != null ? ex.getCause() : ex;
202: LogUtils.log(LOG, Level.WARNING,
203: "INIT_METHOD_THREW_EXCEPTION", t, handler
204: .getClass());
205: } catch (IllegalAccessException ex) {
206: LOG.log(Level.SEVERE, "CANNOT_ACCESS_INIT", handler
207: .getClass());
208: }
209: }
210:
211: private Method getInitMethod(Handler handler) {
212: Method m = null;
213: try {
214: m = handler.getClass().getMethod("init", Map.class);
215: } catch (NoSuchMethodException ex) {
216: // emtpy
217: }
218: return m;
219: }
220:
221: private String trimString(String str) {
222: return str != null ? str.trim() : null;
223: }
224: }
|