001: package org.objectweb.celtix.handlers;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.lang.reflect.Method;
005: import java.util.ArrayList;
006: import java.util.HashMap;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.logging.Level;
010: import java.util.logging.Logger;
011:
012: import javax.xml.ws.WebServiceException;
013: import javax.xml.ws.handler.Handler;
014: import javax.xml.ws.handler.LogicalHandler;
015:
016: import org.objectweb.celtix.bus.jaxws.configuration.types.HandlerChainType;
017: import org.objectweb.celtix.bus.jaxws.configuration.types.HandlerInitParamType;
018: import org.objectweb.celtix.bus.jaxws.configuration.types.HandlerType;
019: import org.objectweb.celtix.common.i18n.Message;
020: import org.objectweb.celtix.common.logging.LogUtils;
021:
022: public class HandlerChainBuilder {
023: static final Logger LOG = LogUtils
024: .getL7dLogger(HandlerChainBuilder.class);
025:
026: public List<Handler> buildHandlerChainFromConfiguration(
027: HandlerChainType hc) {
028: if (null == hc) {
029: return null;
030: }
031: return sortHandlers(buildHandlerChain(hc));
032: }
033:
034: /**
035: * sorts the handlers into correct order. All of the logical handlers first
036: * followed by the protocol handlers
037: *
038: * @param handlers
039: * @return sorted list of handlers
040: */
041: public List<Handler> sortHandlers(List<Handler> handlers) {
042:
043: List<LogicalHandler> logicalHandlers = new ArrayList<LogicalHandler>();
044: List<Handler> protocolHandlers = new ArrayList<Handler>();
045:
046: for (Handler handler : handlers) {
047: if (handler instanceof LogicalHandler) {
048: logicalHandlers.add((LogicalHandler) handler);
049: } else {
050: protocolHandlers.add(handler);
051: }
052: }
053:
054: List<Handler> sortedHandlers = new ArrayList<Handler>();
055: sortedHandlers.addAll(logicalHandlers);
056: sortedHandlers.addAll(protocolHandlers);
057: return sortedHandlers;
058: }
059:
060: private ClassLoader getHandlerClassLoader() {
061: return getClass().getClassLoader();
062: }
063:
064: protected List<Handler> buildHandlerChain(HandlerChainType hc) {
065: List<Handler> handlerChain = new ArrayList<Handler>();
066: for (HandlerType h : hc.getHandler()) {
067: try {
068: LOG.log(Level.FINE, "loading handler", h
069: .getHandlerName());
070:
071: Class<? extends Handler> handlerClass = Class.forName(
072: h.getHandlerClass(), true,
073: getHandlerClassLoader()).asSubclass(
074: Handler.class);
075:
076: Handler handler = handlerClass.newInstance();
077: LOG.fine("adding handler to chain: " + handler);
078: configureHandler(handler, h);
079: handlerChain.add(handler);
080: } catch (ClassNotFoundException e) {
081: throw new WebServiceException(new Message(
082: "HANDLER_INSTANTIATION_EXC", LOG).toString(), e);
083: } catch (InstantiationException e) {
084: throw new WebServiceException(new Message(
085: "HANDLER_INSTANTIATION_EXC", LOG).toString(), e);
086: } catch (IllegalAccessException e) {
087: throw new WebServiceException(new Message(
088: "HANDLER_INSTANTIATION_EXC", LOG).toString(), e);
089: }
090: }
091: return handlerChain;
092: }
093:
094: private void configureHandler(Handler handler, HandlerType h) {
095:
096: Map<String, String> params = new HashMap<String, String>();
097:
098: for (HandlerInitParamType param : h.getInitParam()) {
099: params.put(param.getParamName(), param.getParamValue());
100: }
101:
102: if (params.size() > 0) {
103: try {
104: Method init = handler.getClass().getMethod("init",
105: Map.class);
106: init.invoke(handler, params);
107: } catch (InvocationTargetException ex) {
108: Throwable t = ex.getCause() != null ? ex.getCause()
109: : ex;
110: LogUtils.log(LOG, Level.WARNING,
111: "INIT_METHOD_THREW_EXCEPTION", t, handler
112: .getClass());
113: } catch (NoSuchMethodException ex) {
114: LOG.log(Level.SEVERE, "NO_INIT_METHOD_ON_HANDLER",
115: handler.getClass());
116: } catch (IllegalAccessException ex) {
117: LOG.log(Level.SEVERE, "CANNOT_ACCESS_INIT", handler
118: .getClass());
119: }
120: }
121: }
122: }
|