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.interceptor;
019:
020: import java.lang.annotation.Annotation;
021: import java.util.ArrayList;
022: import java.util.List;
023: import java.util.ResourceBundle;
024:
025: import javax.jws.WebService;
026:
027: import org.apache.cxf.common.classloader.ClassLoaderUtils;
028: import org.apache.cxf.common.i18n.BundleUtils;
029: import org.apache.cxf.common.i18n.Message;
030: import org.apache.cxf.common.util.StringUtils;
031: import org.apache.cxf.feature.AbstractFeature;
032: import org.apache.cxf.feature.Features;
033:
034: public class AnnotationInterceptors {
035:
036: private static final ResourceBundle BUNDLE = BundleUtils
037: .getBundle(AnnotationInterceptors.class);
038:
039: private Class<?> clazz;
040:
041: public AnnotationInterceptors(Class<?> clz) {
042: clazz = clz;
043: }
044:
045: public List<Interceptor> getInFaultInterceptors() {
046: return getAnnotationObject(InFaultInterceptors.class,
047: Interceptor.class);
048: }
049:
050: private <T> List<T> getAnnotationObject(
051: Class<? extends Annotation> annotationClazz, Class<T> type) {
052: Annotation annotation = clazz.getAnnotation(annotationClazz);
053: if (annotation == null) {
054: WebService ws = clazz.getAnnotation(WebService.class);
055: if (ws != null
056: && !StringUtils.isEmpty(ws.endpointInterface())) {
057: String seiClassName = ws.endpointInterface().trim();
058: Class<?> seiClass = null;
059: try {
060: seiClass = ClassLoaderUtils.loadClass(seiClassName,
061: this .getClass());
062: } catch (ClassNotFoundException e) {
063: throw new Fault(
064: new Message("COULD_NOT_FIND_SEICLASS",
065: BUNDLE, seiClass), e);
066: }
067: annotation = seiClass.getAnnotation(annotationClazz);
068: if (annotation != null) {
069: return initializeAnnotationObjects(
070: getAnnotationObjectNames(annotation), type);
071: }
072: }
073: } else {
074: return initializeAnnotationObjects(
075: getAnnotationObjectNames(annotation), type);
076: }
077: return null;
078: }
079:
080: private String[] getAnnotationObjectNames(Annotation ann) {
081: if (ann instanceof InFaultInterceptors) {
082: return ((InFaultInterceptors) ann).interceptors();
083: } else if (ann instanceof InInterceptors) {
084: return ((InInterceptors) ann).interceptors();
085: } else if (ann instanceof OutFaultInterceptors) {
086: return ((OutFaultInterceptors) ann).interceptors();
087: } else if (ann instanceof OutInterceptors) {
088: return ((OutInterceptors) ann).interceptors();
089: } else if (ann instanceof Features) {
090: return ((Features) ann).features();
091: }
092:
093: throw new UnsupportedOperationException(
094: "Doesn't support the annotation: " + ann);
095: }
096:
097: private <T> List<T> initializeAnnotationObjects(
098: String[] annotationObjects, Class<T> type) {
099: List<T> theAnnotationObjects = new ArrayList<T>();
100: if (annotationObjects != null && annotationObjects.length > 0) {
101: for (String annObjectName : annotationObjects) {
102: Object object = null;
103: try {
104: object = ClassLoaderUtils.loadClass(annObjectName,
105: this .getClass()).newInstance();
106: theAnnotationObjects.add(type.cast(object));
107: } catch (ClassNotFoundException e) {
108: throw new Fault(new Message(
109: "COULD_NOT_CREATE_ANNOTATION_OBJECT",
110: BUNDLE, annObjectName), e);
111: } catch (InstantiationException ie) {
112: throw new Fault(new Message(
113: "COULD_NOT_CREATE_ANNOTATION_OBJECT",
114: BUNDLE, annObjectName), ie);
115: } catch (IllegalAccessException iae) {
116: throw new Fault(new Message(
117: "COULD_NOT_CREATE_ANNOTATION_OBJECT",
118: BUNDLE, annObjectName), iae);
119: } catch (ClassCastException ex) {
120: throw new Fault(new Message(
121: "COULD_NOT_CREATE_ANNOTATION_OBJECT",
122: BUNDLE, annObjectName), ex);
123: }
124: }
125: }
126: return theAnnotationObjects;
127: }
128:
129: public List<Interceptor> getInInterceptors() {
130: return getAnnotationObject(InInterceptors.class,
131: Interceptor.class);
132: }
133:
134: public List<Interceptor> getOutFaultInterceptors() {
135: return getAnnotationObject(OutFaultInterceptors.class,
136: Interceptor.class);
137: }
138:
139: public List<Interceptor> getOutInterceptors() {
140: return getAnnotationObject(OutInterceptors.class,
141: Interceptor.class);
142: }
143:
144: public List<AbstractFeature> getFeatures() {
145: return getAnnotationObject(Features.class,
146: AbstractFeature.class);
147: }
148:
149: }
|