1 package org.objectweb.celtix.bus.handlers; 2 3 4 5 6 import java.io.InputStream ; 7 import java.util.ArrayList ; 8 import java.util.List ; 9 import java.util.logging.Level ; 10 import java.util.logging.Logger ; 11 12 import javax.jws.HandlerChain; 13 import javax.xml.ws.WebServiceException; 14 import javax.xml.ws.handler.Handler; 15 16 import org.objectweb.celtix.bus.jaxws.configuration.types.HandlerChainType; 17 import org.objectweb.celtix.common.i18n.Message; 18 import org.objectweb.celtix.common.logging.LogUtils; 19 import org.objectweb.celtix.handlers.HandlerChainBuilder; 20 21 public class AnnotationHandlerChainBuilder extends HandlerChainBuilder { 22 23 static final Logger LOG = LogUtils.getL7dLogger(AnnotationHandlerChainBuilder.class); 24 25 public List <Handler> buildHandlerChainFor(Class <?> clz, List <Handler> existingHandlers) { 26 27 LOG.fine("building handler chain"); 28 HandlerChainAnnotation hcAnn = findHandlerChainAnnotation(clz); 29 List <Handler> chain = null; 30 if (hcAnn == null) { 31 LOG.fine("no HandlerChain annotation on " + clz); 32 chain = new ArrayList <Handler>(); 33 } else { 34 hcAnn.validate(); 35 36 HandlerChainDocument doc = getHandlerChainDocument(hcAnn); 37 HandlerChainType hc = doc.getChain(hcAnn.getChainName()); 38 39 if (null == hc) { 40 throw new WebServiceException(new Message("CHAIN_NOT_SPECIFIED_EXC", LOG).toString()); 41 } 42 43 chain = buildHandlerChain(hc); 44 } 45 assert chain != null; 46 if (existingHandlers != null) { 47 chain.addAll(existingHandlers); 48 } 49 return sortHandlers(chain); 50 } 51 52 public List <Handler> buildHandlerChainFor(Class <?> clz) { 53 return buildHandlerChainFor(clz, null); 54 } 55 56 private HandlerChainAnnotation findHandlerChainAnnotation(Class <?> clz) { 57 58 HandlerChain ann = clz.getAnnotation(HandlerChain.class); 59 Class <?> declaringClass = clz; 60 61 if (ann == null) { 62 for (Class <?> iface : clz.getInterfaces()) { 63 if (LOG.isLoggable(Level.FINE)) { 64 LOG.fine("checking for HandlerChain annotation on " + iface.getName()); 65 } 66 ann = iface.getAnnotation(HandlerChain.class); 67 if (ann != null) { 68 declaringClass = iface; 69 break; 70 } 71 } 72 } 73 if (ann != null) { 74 return new HandlerChainAnnotation(ann, declaringClass); 75 } else { 76 return null; 77 } 78 } 79 80 private static class HandlerChainAnnotation { 81 private final Class <?> declaringClass; 82 private final HandlerChain ann; 83 84 HandlerChainAnnotation(HandlerChain hc, Class <?> clz) { 85 ann = hc; 86 declaringClass = clz; 87 } 88 89 public Class <?> getDeclaringClass() { 90 return declaringClass; 91 } 92 93 public String getFileName() { 94 return ann.file(); 95 } 96 97 public String getChainName() { 98 return ann.name(); 99 } 100 101 public void validate() { 102 if (null == ann.file() || "".equals(ann.file())) { 103 throw new WebServiceException(new Message("ANNOTATION_WITHOUT_URL_EXC", LOG).toString()); 104 } 105 if (null == ann.name() || "".equals(ann.name())) { 106 LOG.fine("no handler name specified, defaulting to first declared"); 107 } 108 } 109 110 public String toString() { 111 return "[" + declaringClass + "," + ann + "]"; 112 } 113 } 114 115 private HandlerChainDocument getHandlerChainDocument(HandlerChainAnnotation hcAnn) { 116 InputStream in = hcAnn.getDeclaringClass().getResourceAsStream(hcAnn.getFileName()); 117 118 if (null == in) { 119 throw new WebServiceException(new Message("HANDLER_CFG_FILE_NOT_FOUND_EXC", LOG, 120 hcAnn.getFileName()).toString()); 121 } 122 123 LOG.log(Level.INFO, "reading handler chain configuration from " + hcAnn.getFileName()); 124 return new HandlerChainDocument(in, true); 125 } 126 } 127 | Popular Tags |