001: package org.objectweb.celtix.bus.handlers;
002:
003: import java.io.InputStream;
004: import java.util.ArrayList;
005: import java.util.List;
006: import java.util.logging.Level;
007: import java.util.logging.Logger;
008:
009: import javax.jws.HandlerChain;
010: import javax.xml.ws.WebServiceException;
011: import javax.xml.ws.handler.Handler;
012:
013: import org.objectweb.celtix.bus.jaxws.configuration.types.HandlerChainType;
014: import org.objectweb.celtix.common.i18n.Message;
015: import org.objectweb.celtix.common.logging.LogUtils;
016: import org.objectweb.celtix.handlers.HandlerChainBuilder;
017:
018: public class AnnotationHandlerChainBuilder extends HandlerChainBuilder {
019:
020: static final Logger LOG = LogUtils
021: .getL7dLogger(AnnotationHandlerChainBuilder.class);
022:
023: public List<Handler> buildHandlerChainFor(Class<?> clz,
024: List<Handler> existingHandlers) {
025:
026: LOG.fine("building handler chain");
027: HandlerChainAnnotation hcAnn = findHandlerChainAnnotation(clz);
028: List<Handler> chain = null;
029: if (hcAnn == null) {
030: LOG.fine("no HandlerChain annotation on " + clz);
031: chain = new ArrayList<Handler>();
032: } else {
033: hcAnn.validate();
034:
035: HandlerChainDocument doc = getHandlerChainDocument(hcAnn);
036: HandlerChainType hc = doc.getChain(hcAnn.getChainName());
037:
038: if (null == hc) {
039: throw new WebServiceException(new Message(
040: "CHAIN_NOT_SPECIFIED_EXC", LOG).toString());
041: }
042:
043: chain = buildHandlerChain(hc);
044: }
045: assert chain != null;
046: if (existingHandlers != null) {
047: chain.addAll(existingHandlers);
048: }
049: return sortHandlers(chain);
050: }
051:
052: public List<Handler> buildHandlerChainFor(Class<?> clz) {
053: return buildHandlerChainFor(clz, null);
054: }
055:
056: private HandlerChainAnnotation findHandlerChainAnnotation(
057: Class<?> clz) {
058:
059: HandlerChain ann = clz.getAnnotation(HandlerChain.class);
060: Class<?> declaringClass = clz;
061:
062: if (ann == null) {
063: for (Class<?> iface : clz.getInterfaces()) {
064: if (LOG.isLoggable(Level.FINE)) {
065: LOG.fine("checking for HandlerChain annotation on "
066: + iface.getName());
067: }
068: ann = iface.getAnnotation(HandlerChain.class);
069: if (ann != null) {
070: declaringClass = iface;
071: break;
072: }
073: }
074: }
075: if (ann != null) {
076: return new HandlerChainAnnotation(ann, declaringClass);
077: } else {
078: return null;
079: }
080: }
081:
082: private static class HandlerChainAnnotation {
083: private final Class<?> declaringClass;
084: private final HandlerChain ann;
085:
086: HandlerChainAnnotation(HandlerChain hc, Class<?> clz) {
087: ann = hc;
088: declaringClass = clz;
089: }
090:
091: public Class<?> getDeclaringClass() {
092: return declaringClass;
093: }
094:
095: public String getFileName() {
096: return ann.file();
097: }
098:
099: public String getChainName() {
100: return ann.name();
101: }
102:
103: public void validate() {
104: if (null == ann.file() || "".equals(ann.file())) {
105: throw new WebServiceException(new Message(
106: "ANNOTATION_WITHOUT_URL_EXC", LOG).toString());
107: }
108: if (null == ann.name() || "".equals(ann.name())) {
109: LOG
110: .fine("no handler name specified, defaulting to first declared");
111: }
112: }
113:
114: public String toString() {
115: return "[" + declaringClass + "," + ann + "]";
116: }
117: }
118:
119: private HandlerChainDocument getHandlerChainDocument(
120: HandlerChainAnnotation hcAnn) {
121: InputStream in = hcAnn.getDeclaringClass().getResourceAsStream(
122: hcAnn.getFileName());
123:
124: if (null == in) {
125: throw new WebServiceException(new Message(
126: "HANDLER_CFG_FILE_NOT_FOUND_EXC", LOG, hcAnn
127: .getFileName()).toString());
128: }
129:
130: LOG.log(Level.INFO, "reading handler chain configuration from "
131: + hcAnn.getFileName());
132: return new HandlerChainDocument(in, true);
133: }
134: }
|