Skip to content

Commit 55d545e

Browse files
committed
First prototype
1 parent 67ec7ec commit 55d545e

26 files changed

Lines changed: 1892 additions & 0 deletions

‎annotations/pom.xml‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- Copyright 2010 Lincoln Baxter, III Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
4+
file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
6+
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
7+
governing permissions and limitations under the License. -->
8+
9+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<parent>
14+
<groupId>org.ocpsoft.prettyfaces</groupId>
15+
<artifactId>prettyfaces-parent</artifactId>
16+
<version>4.0.0-SNAPSHOT</version>
17+
<relativePath>../pom.xml</relativePath>
18+
</parent>
19+
20+
<artifactId>prettyfaces-annotations</artifactId>
21+
<packaging>jar</packaging>
22+
23+
<name>PrettyFaces Annotations</name>
24+
<description>Annotation Support</description>
25+
<url>http://ocpsoft.org/prettyfaces/</url>
26+
27+
<dependencies>
28+
29+
<dependency>
30+
<groupId>org.ocpsoft.prettyfaces</groupId>
31+
<artifactId>prettyfaces-core</artifactId>
32+
<version>${project.version}</version>
33+
</dependency>
34+
35+
</dependencies>
36+
37+
</project>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.ocpsoft.prettyfaces.annotation.api;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Inherited
11+
@Documented
12+
@Target(ElementType.TYPE)
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface ForwardTo {
15+
16+
String value();
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.ocpsoft.prettyfaces.annotation.api;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Inherited
11+
@Documented
12+
@Target(ElementType.TYPE)
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface JSFValidator {
15+
16+
String validatorId();
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.ocpsoft.prettyfaces.annotation.api;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Inherited
11+
@Documented
12+
@Target(ElementType.FIELD)
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface NamedParameter {
15+
16+
String value() default "";
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.ocpsoft.prettyfaces.annotation.api;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Inherited
11+
@Documented
12+
@Target(ElementType.TYPE)
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface URLPattern {
15+
16+
String value();
17+
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.ocpsoft.prettyfaces.annotation.handlers;
2+
3+
import java.lang.reflect.AnnotatedElement;
4+
5+
import org.ocpsoft.prettyfaces.annotation.api.ForwardTo;
6+
import org.ocpsoft.prettyfaces.annotation.scan.MappingBuilder;
7+
import org.ocpsoft.prettyfaces.annotation.spi.AnnotationHandler;
8+
9+
import com.ocpsoft.rewrite.servlet.config.Forward;
10+
11+
public class ForwardToHandler implements AnnotationHandler<ForwardTo> {
12+
13+
@Override
14+
public Class<ForwardTo> handles() {
15+
return ForwardTo.class;
16+
}
17+
18+
@Override
19+
public void process(ForwardTo annotation, AnnotatedElement element, MappingBuilder builder) {
20+
builder.setOperation(Forward.to(annotation.value()));
21+
}
22+
23+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.ocpsoft.prettyfaces.annotation.handlers;
2+
3+
import java.lang.reflect.AnnotatedElement;
4+
import java.lang.reflect.Field;
5+
6+
import javax.faces.context.FacesContext;
7+
import javax.faces.validator.Validator;
8+
import javax.faces.validator.ValidatorException;
9+
10+
import org.ocpsoft.prettyfaces.annotation.api.JSFValidator;
11+
import org.ocpsoft.prettyfaces.annotation.scan.MappingBuilder;
12+
import org.ocpsoft.prettyfaces.annotation.spi.AnnotationHandler;
13+
14+
import com.ocpsoft.rewrite.context.EvaluationContext;
15+
import com.ocpsoft.rewrite.event.Rewrite;
16+
import com.ocpsoft.rewrite.param.Constraint;
17+
18+
public class JSFValidatorHandler implements AnnotationHandler<JSFValidator>
19+
{
20+
21+
@Override
22+
public Class<JSFValidator> handles()
23+
{
24+
return JSFValidator.class;
25+
}
26+
27+
@Override
28+
public void process(JSFValidator annotation, AnnotatedElement element, MappingBuilder builder)
29+
{
30+
31+
// works only for fields and not for methods
32+
if (element instanceof Field) {
33+
34+
Field field = (Field) element;
35+
36+
// create the custom constraint
37+
String validatorId = annotation.validatorId();
38+
JSFValidatorConstraint constraint = new JSFValidatorConstraint(validatorId);
39+
40+
// register the constraint
41+
builder.addParameterConstraint(field.getName(), constraint);
42+
43+
}
44+
45+
}
46+
47+
/**
48+
*
49+
* Implementation of {@link Constraint} which validates using a JSF validator
50+
*
51+
* @author Christian Kaltepoth
52+
*
53+
*/
54+
public static class JSFValidatorConstraint implements Constraint<String>
55+
{
56+
57+
private final String validatorId;
58+
59+
public JSFValidatorConstraint(String validatorId)
60+
{
61+
this.validatorId = validatorId;
62+
}
63+
64+
@Override
65+
public boolean isSatisfiedBy(Rewrite event, EvaluationContext context, String value)
66+
{
67+
68+
// obtain the JSF validator
69+
FacesContext facesContext = FacesContext.getCurrentInstance();
70+
Validator validator = facesContext.getApplication().createValidator(validatorId);
71+
72+
// perform validation
73+
try {
74+
75+
validator.validate(facesContext, null, value);
76+
return true;
77+
}
78+
catch (ValidatorException e) {
79+
return false;
80+
}
81+
}
82+
83+
}
84+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.ocpsoft.prettyfaces.annotation.handlers;
2+
3+
import java.lang.reflect.AnnotatedElement;
4+
import java.lang.reflect.Field;
5+
6+
import org.ocpsoft.prettyfaces.annotation.api.NamedParameter;
7+
import org.ocpsoft.prettyfaces.annotation.scan.MappingBuilder;
8+
import org.ocpsoft.prettyfaces.annotation.spi.AnnotationHandler;
9+
10+
public class NamedParameterHandler implements AnnotationHandler<NamedParameter>
11+
{
12+
13+
@Override
14+
public Class<NamedParameter> handles()
15+
{
16+
return NamedParameter.class;
17+
}
18+
19+
@Override
20+
public void process(NamedParameter annotation, AnnotatedElement element, MappingBuilder builder) {
21+
22+
if(element instanceof Field) {
23+
Field field = (Field) element;
24+
25+
// default name is the name of the field
26+
String paramName = field.getName();
27+
28+
// but the name specified in the annotation is preferred
29+
if(!annotation.value().isEmpty()) {
30+
paramName = annotation.value();
31+
}
32+
33+
builder.addParameterInjection(field, paramName);
34+
35+
}
36+
37+
}
38+
}

0 commit comments

Comments
 (0)