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