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.tools.util;
019:
020: import java.io.File;
021: import java.lang.annotation.Annotation;
022: import java.lang.reflect.Method;
023: import java.net.URL;
024: import java.net.URLClassLoader;
025: import java.security.AccessController;
026: import java.security.PrivilegedAction;
027: import java.util.logging.Logger;
028:
029: import javax.jws.WebParam;
030: import javax.jws.WebResult;
031:
032: import org.apache.cxf.common.i18n.Message;
033: import org.apache.cxf.common.logging.LogUtils;
034: import org.apache.cxf.tools.common.ToolException;
035:
036: public final class AnnotationUtil {
037: private static final Logger LOG = LogUtils
038: .getL7dLogger(AnnotationUtil.class);
039:
040: private AnnotationUtil() {
041:
042: }
043:
044: public static <T extends Annotation> T getPrivClassAnnotation(
045: final Class<?> clazz, final Class<T> anoClass) {
046: return AccessController.doPrivileged(new PrivilegedAction<T>() {
047: public T run() {
048: return clazz.getAnnotation(anoClass);
049: }
050: });
051: }
052:
053: public static <T extends Annotation> T getPrivMethodAnnotation(
054: final Method method, final Class<T> anoClass) {
055: return AccessController.doPrivileged(new PrivilegedAction<T>() {
056: public T run() {
057: return method.getAnnotation(anoClass);
058: }
059: });
060: }
061:
062: public static Annotation[][] getPrivParameterAnnotations(
063: final Method method) {
064: return (Annotation[][]) AccessController
065: .doPrivileged(new PrivilegedAction<Annotation[][]>() {
066: public Annotation[][] run() {
067: return method.getParameterAnnotations();
068: }
069: });
070: }
071:
072: public static synchronized URLClassLoader getClassLoader(
073: ClassLoader parent) {
074: URL[] urls = URIParserUtil.pathToURLs(getClassPath());
075: return new URLClassLoader(urls, parent);
076: }
077:
078: public static synchronized Class loadClass(String className,
079: ClassLoader parent) {
080: Class clazz = null;
081: URL[] urls = URIParserUtil.pathToURLs(getClassPath());
082: URLClassLoader classLoader = new URLClassLoader(urls, parent);
083: try {
084: clazz = classLoader.loadClass(className);
085: } catch (Exception e) {
086: Message msg = new Message("FAIL_TO_LOAD_CLASS", LOG,
087: className);
088: throw new ToolException(msg, e);
089: }
090: return clazz;
091: }
092:
093: private static String getClassPath() {
094: ClassLoader loader = AnnotationUtil.class.getClassLoader();
095: StringBuffer classpath = new StringBuffer(System
096: .getProperty("java.class.path"));
097: if (loader instanceof URLClassLoader) {
098: URLClassLoader urlloader = (URLClassLoader) loader;
099: for (URL url : urlloader.getURLs()) {
100: classpath.append(File.pathSeparatorChar);
101: classpath.append(url.getFile());
102: }
103: }
104: return classpath.toString();
105: }
106:
107: public static String capitalize(String name) {
108: if (name == null || name.length() == 0) {
109: return name;
110: }
111: char chars[] = name.toCharArray();
112: chars[0] = Character.toUpperCase(chars[0]);
113: return new String(chars);
114: }
115:
116: public static WebParam getWebParam(Method method, String paraName) {
117:
118: Annotation[][] anno = getPrivParameterAnnotations(method);
119: int count = method.getParameterTypes().length;
120: for (int i = 0; i < count; i++) {
121: for (Annotation ann : anno[i]) {
122: if (ann.annotationType() == WebParam.class) {
123: WebParam webParam = (WebParam) ann;
124: if (paraName.equals(webParam.name())) {
125: return webParam;
126: }
127: }
128: }
129: }
130: return null;
131:
132: }
133:
134: public static WebResult getWebResult(Method method) {
135:
136: Annotation ann = method.getAnnotation(WebResult.class);
137: if (ann == null) {
138: return null;
139: } else {
140: return (WebResult) ann;
141: }
142: }
143:
144: }
|