|
| 1 | +package org.ocpsoft.prettyfaces.annotation; |
| 2 | + |
| 3 | +import java.lang.annotation.Annotation; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Iterator; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +import javax.servlet.ServletContext; |
| 12 | + |
| 13 | +import org.ocpsoft.prettyfaces.annotation.scan.ByteCodeFilter; |
| 14 | +import org.ocpsoft.prettyfaces.annotation.scan.ClassVisitor; |
| 15 | +import org.ocpsoft.prettyfaces.annotation.scan.PackageFilter; |
| 16 | +import org.ocpsoft.prettyfaces.annotation.scan.WebClassesFinder; |
| 17 | +import org.ocpsoft.prettyfaces.annotation.scan.WebLibFinder; |
| 18 | +import org.ocpsoft.prettyfaces.annotation.spi.AnnotationHandler; |
| 19 | +import org.ocpsoft.prettyfaces.annotation.spi.ClassFinder; |
| 20 | +import org.ocpsoft.prettyfaces.core.config.spi.PrettyConfigProvider; |
| 21 | + |
| 22 | +import com.ocpsoft.common.services.ServiceLoader; |
| 23 | +import com.ocpsoft.logging.Logger; |
| 24 | +import com.ocpsoft.rewrite.config.Rule; |
| 25 | + |
| 26 | +public class AnnotationConfigProvider implements PrettyConfigProvider |
| 27 | +{ |
| 28 | + |
| 29 | + private final Logger log = Logger.getLogger(AnnotationConfigProvider.class); |
| 30 | + |
| 31 | + public static final String CONFIG_SCAN_LIB_DIR = "com.ocpsoft.pretty.SCAN_LIB_DIRECTORY"; |
| 32 | + public static final String CONFIG_BASE_PACKAGES = "com.ocpsoft.pretty.BASE_PACKAGES"; |
| 33 | + |
| 34 | + @Override |
| 35 | + public Collection<Rule> getRules(ServletContext servletContext) |
| 36 | + { |
| 37 | + |
| 38 | + /* |
| 39 | + * ====================================================== |
| 40 | + * ============[ Configuration parameters ]============== |
| 41 | + * ====================================================== |
| 42 | + */ |
| 43 | + |
| 44 | + // retrieve the optional package filter configuration parameter |
| 45 | + String packageFilters = servletContext.getInitParameter(CONFIG_BASE_PACKAGES); |
| 46 | + |
| 47 | + // does the user want to scan the webapp lib directory |
| 48 | + boolean scanLibDir = false; |
| 49 | + String jarConfig = servletContext.getInitParameter(CONFIG_SCAN_LIB_DIR); |
| 50 | + if ((jarConfig != null) && jarConfig.trim().equalsIgnoreCase("true")) { |
| 51 | + scanLibDir = true; |
| 52 | + } |
| 53 | + |
| 54 | + // users can disable annotation scanning |
| 55 | + if ((packageFilters != null) && packageFilters.trim().equalsIgnoreCase("none")) { |
| 56 | + log.debug("Annotation scanning has is disabled!"); |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + /* |
| 61 | + * ====================================================== |
| 62 | + * ============[ Prepare scanning process ]============== |
| 63 | + * ====================================================== |
| 64 | + */ |
| 65 | + |
| 66 | + // the byte code filter needs to know the annotations to look for |
| 67 | + Set<Class<? extends Annotation>> annotationType = new HashSet<Class<? extends Annotation>>(); |
| 68 | + |
| 69 | + // list of annotation handlers for the ClassVisitor |
| 70 | + List<AnnotationHandler<Annotation>> annotationHandlers = new ArrayList<AnnotationHandler<Annotation>>(); |
| 71 | + |
| 72 | + // load the implementations of the AnnotationHandler SPI |
| 73 | + Iterator<AnnotationHandler<Annotation>> handlerIterator = ServiceLoader.load(AnnotationHandler.class).iterator(); |
| 74 | + while (handlerIterator.hasNext()) { |
| 75 | + AnnotationHandler<Annotation> handler = handlerIterator.next(); |
| 76 | + annotationHandlers.add(handler); |
| 77 | + annotationType.add(handler.handles()); |
| 78 | + } |
| 79 | + |
| 80 | + // this class will identify the classes that should be scanned without loading them |
| 81 | + ByteCodeFilter byteCodeFilter = new ByteCodeFilter(annotationType); |
| 82 | + |
| 83 | + // users can configure the annotation scanner to scan only specific packages |
| 84 | + PackageFilter packageFilter = new PackageFilter(packageFilters); |
| 85 | + |
| 86 | + // ClassVisitor will process all classes that ByteCodeFilter considers as worth scanning them |
| 87 | + ClassVisitor classVisitor = new ClassVisitor(annotationHandlers); |
| 88 | + |
| 89 | + // fallback to some other classloder if there is no context class loader |
| 90 | + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); |
| 91 | + if (classloader == null) { |
| 92 | + classloader = this.getClass().getClassLoader(); |
| 93 | + } |
| 94 | + |
| 95 | + /* |
| 96 | + * ====================================================== |
| 97 | + * =============[ Scanning process starts ]============== |
| 98 | + * ====================================================== |
| 99 | + */ |
| 100 | + |
| 101 | + // compile a list of class finders to run |
| 102 | + List<ClassFinder> classFinders = new ArrayList<ClassFinder>(); |
| 103 | + classFinders.add(new WebClassesFinder(servletContext, classloader, packageFilter, byteCodeFilter)); |
| 104 | + if (scanLibDir) { |
| 105 | + classFinders.add(new WebLibFinder(servletContext, classloader, packageFilter, byteCodeFilter)); |
| 106 | + } |
| 107 | + |
| 108 | + // start the scanning process |
| 109 | + for (ClassFinder finder : classFinders) { |
| 110 | + finder.findClasses(classVisitor); |
| 111 | + } |
| 112 | + |
| 113 | + // return the rules collected by the class visitor |
| 114 | + return classVisitor.getRules(); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments