Basic examples on cdi and beans validation
This commit is contained in:
13
general/LoggingProducer.java
Normal file
13
general/LoggingProducer.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package general;
|
||||||
|
|
||||||
|
import javax.enterprise.inject.Produces;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
||||||
|
public class LoggingProducer {
|
||||||
|
|
||||||
|
@Produces
|
||||||
|
public Logger produceLogger() {
|
||||||
|
return Logger.getLogger("xglogger");
|
||||||
|
}
|
||||||
|
}
|
||||||
56
servlet/Servlettest1.java
Normal file
56
servlet/Servlettest1.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package servlet;
|
||||||
|
|
||||||
|
import test1_cdi.*;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 25/04/2015.
|
||||||
|
*/
|
||||||
|
@WebServlet(name = "Servlettest1")
|
||||||
|
public class Servlettest1 extends HttpServlet {
|
||||||
|
|
||||||
|
@Inject @ClassI
|
||||||
|
test1_cdi.Class c;
|
||||||
|
|
||||||
|
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
response.setContentType("text/html;charset=UTF-8");
|
||||||
|
try (PrintWriter out = response.getWriter()) {
|
||||||
|
/* TODO output your page here. */
|
||||||
|
|
||||||
|
out.println("<!DOCTYPE html>");
|
||||||
|
out.println("<html>");
|
||||||
|
out.println("<head>");
|
||||||
|
out.println("<title>Servlet xg</title>");
|
||||||
|
out.println("</head>");
|
||||||
|
out.println("<body>");
|
||||||
|
out.println("<h1>Servlet xg at " + request.getContextPath() + "</h1>");
|
||||||
|
|
||||||
|
out.println(c.printStudent());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
out.println("</body>");
|
||||||
|
out.println("</html>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
processRequest(request, response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
processRequest(request, response);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
157
servlet/Servlettest2.java
Normal file
157
servlet/Servlettest2.java
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
package servlet;
|
||||||
|
|
||||||
|
import test2_constraints_validation.*;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import javax.validation.ValidatorFactory;
|
||||||
|
import javax.validation.executable.ExecutableValidator;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 25/04/2015.
|
||||||
|
*/
|
||||||
|
@WebServlet(name = "Servlettest2")
|
||||||
|
public class Servlettest2 extends HttpServlet {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ValidatorFactory validatorFactory;
|
||||||
|
@Inject
|
||||||
|
Validator validator;
|
||||||
|
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
xg_substring_class xg_substring_class;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
substring_class substring_class;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
general_class_wrapper general_class_wrapper;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
general_class general_class;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
class_with_builtin_constraints class_with_builtin_constraints;
|
||||||
|
|
||||||
|
|
||||||
|
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
response.setContentType("text/html;charset=UTF-8");
|
||||||
|
try (PrintWriter out = response.getWriter()) {
|
||||||
|
/* TODO output your page here. */
|
||||||
|
|
||||||
|
out.println("<!DOCTYPE html>");
|
||||||
|
out.println("<html>");
|
||||||
|
out.println("<head>");
|
||||||
|
out.println("<title>Servlet xg</title>");
|
||||||
|
out.println("</head>");
|
||||||
|
out.println("<body>");
|
||||||
|
out.println("<h1>Servlet xg at " + request.getContextPath() + "</h1>");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
///////////////// entire class validation
|
||||||
|
|
||||||
|
Set<ConstraintViolation<xg_substring_class>> violations = validator.validate(xg_substring_class);
|
||||||
|
if (violations.size() > 0) out.println("error"); else out.println("tutto ok");
|
||||||
|
|
||||||
|
out.println("<br>");
|
||||||
|
|
||||||
|
Set<ConstraintViolation<substring_class>> violations2 = validator.validate(substring_class);
|
||||||
|
if (violations2.size() > 0) out.println("error"); else out.println("tutto ok");
|
||||||
|
|
||||||
|
out.println("<br>");
|
||||||
|
|
||||||
|
Set<ConstraintViolation<general_class_wrapper>> violations3 = validator.validate(general_class_wrapper);
|
||||||
|
if (violations3.size() > 0) {
|
||||||
|
out.println("error");
|
||||||
|
Iterator<ConstraintViolation<general_class_wrapper>> it = violations3.iterator();
|
||||||
|
while (it.hasNext()){
|
||||||
|
ConstraintViolation<general_class_wrapper> t = it.next();
|
||||||
|
out.println(t.getInvalidValue() + " " + t.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
} else{
|
||||||
|
out.println("tutto ok");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
out.println("<br>");
|
||||||
|
|
||||||
|
Set<ConstraintViolation<general_class>> violations4 = validator.validate(general_class);
|
||||||
|
if (violations4.size() > 0) out.println("error"); else out.println("tutto ok");
|
||||||
|
|
||||||
|
out.println("<br>");
|
||||||
|
|
||||||
|
//////////////// single method paramenter validation
|
||||||
|
|
||||||
|
Method method = null;
|
||||||
|
try {
|
||||||
|
method = class_with_builtin_constraints.class.getMethod("calculatePrice", Float.class);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
ExecutableValidator methodValidator = validator. forExecutables();
|
||||||
|
Set<ConstraintViolation<class_with_builtin_constraints>> violations5 = methodValidator.validateParameters(class_with_builtin_constraints, method, new Object[]{new Float(2)});
|
||||||
|
if (violations5.size() > 0) out.println("error"); else out.println("tutto ok");
|
||||||
|
|
||||||
|
out.println("<br>");
|
||||||
|
//////////////// single method return value parameter validation
|
||||||
|
|
||||||
|
method = null;
|
||||||
|
try {
|
||||||
|
method = class_with_builtin_constraints.class.getMethod("calculatePrice", Float.class);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
methodValidator = validator. forExecutables();
|
||||||
|
Set<ConstraintViolation<class_with_builtin_constraints>> violations6 = methodValidator.validateReturnValue(class_with_builtin_constraints, method, new Float(6));
|
||||||
|
if (violations6.size() > 0) out.println("error"); else out.println("tutto ok");
|
||||||
|
|
||||||
|
|
||||||
|
out.println("<br>");
|
||||||
|
//////////////// check single property
|
||||||
|
Set<ConstraintViolation<class_with_builtin_constraints>> violations7 = validator.validateValue(class_with_builtin_constraints.class, "title", "aabb");
|
||||||
|
if (violations7.size() > 0) out.println("error"); else out.println("tutto ok");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
out.println("</body>");
|
||||||
|
out.println("</html>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
processRequest(request, response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
processRequest(request, response);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
54
test1_cdi/Class.java
Normal file
54
test1_cdi/Class.java
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.enterprise.event.Observes;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import javax.enterprise.event.Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@ClassI
|
||||||
|
@GeneralInterceptorA
|
||||||
|
public class Class implements ClassInterface {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Logger l;
|
||||||
|
|
||||||
|
@Inject @StudentI(implementation = student_implementations.two)
|
||||||
|
Student s;
|
||||||
|
|
||||||
|
@Inject @NumberOfStudents
|
||||||
|
int number_of_students;
|
||||||
|
|
||||||
|
@Inject @Event1
|
||||||
|
Event<Object> event_shooter1;
|
||||||
|
|
||||||
|
@Inject @Event2
|
||||||
|
Event<Object> event_shooter2;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void do_post_construct(){
|
||||||
|
l.info("XGLOG: Post Construct done");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String printStudent (){
|
||||||
|
event_shooter1.fire(new Object());
|
||||||
|
event_shooter2.fire(new Object());
|
||||||
|
return (s.getinfo() +" " + number_of_students);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void observe1 (@Observes @Event1 Object b){
|
||||||
|
l.info("XGLOG: Event1 captured!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void observe2 (@Observes @Event2 Object b){
|
||||||
|
l.info("XGLOG: Event2 captured!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
25
test1_cdi/ClassDecorator.java
Normal file
25
test1_cdi/ClassDecorator.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.decorator.Decorator;
|
||||||
|
import javax.decorator.Delegate;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 27/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Decorator
|
||||||
|
public class ClassDecorator implements ClassInterface {
|
||||||
|
|
||||||
|
@Inject @Delegate @ClassI
|
||||||
|
private ClassInterface c;
|
||||||
|
|
||||||
|
|
||||||
|
public String printStudent() {
|
||||||
|
return c.printStudent() + " decorated ";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
20
test1_cdi/ClassI.java
Normal file
20
test1_cdi/ClassI.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
import javax.inject.Qualifier;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.FIELD;
|
||||||
|
import static java.lang.annotation.ElementType.METHOD;
|
||||||
|
import static java.lang.annotation.ElementType.TYPE;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Target({FIELD, TYPE, METHOD})
|
||||||
|
public @interface ClassI {
|
||||||
|
}
|
||||||
12
test1_cdi/ClassInterface.java
Normal file
12
test1_cdi/ClassInterface.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 27/04/2015.
|
||||||
|
*/
|
||||||
|
public interface ClassInterface {
|
||||||
|
|
||||||
|
|
||||||
|
String printStudent ();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
47
test1_cdi/ClassProducer.java
Normal file
47
test1_cdi/ClassProducer.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
import javax.enterprise.inject.*;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
public class ClassProducer {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Logger l;
|
||||||
|
|
||||||
|
@Inject @StudentI(implementation = student_implementations.one)
|
||||||
|
Student s;
|
||||||
|
|
||||||
|
@Produces @NumberOfStudents
|
||||||
|
private int number_of_students(){
|
||||||
|
|
||||||
|
return 10;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Produces
|
||||||
|
private int number_of_students_2(){
|
||||||
|
|
||||||
|
return 20;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void clean (@Disposes int s) {
|
||||||
|
l.info("Do something to clean the producer");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clean2(@Disposes @NumberOfStudents int s) {
|
||||||
|
l.info("Do something to clean the producer");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
19
test1_cdi/Event1.java
Normal file
19
test1_cdi/Event1.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.inject.Qualifier;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Target({FIELD, TYPE, METHOD, PARAMETER})
|
||||||
|
public @interface Event1 {}
|
||||||
|
|
||||||
|
|
||||||
19
test1_cdi/Event2.java
Normal file
19
test1_cdi/Event2.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.inject.Qualifier;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Target({FIELD, TYPE, METHOD, PARAMETER})
|
||||||
|
public @interface Event2 {}
|
||||||
|
|
||||||
|
|
||||||
58
test1_cdi/GeneralInterceptor.java
Normal file
58
test1_cdi/GeneralInterceptor.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 27/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Priority;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.interceptor.*;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@Interceptor
|
||||||
|
@GeneralInterceptorA
|
||||||
|
@Priority(200)
|
||||||
|
|
||||||
|
public class GeneralInterceptor {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private Logger logger;
|
||||||
|
|
||||||
|
|
||||||
|
@AroundConstruct
|
||||||
|
private void logConstruct(InvocationContext ic) throws Exception {
|
||||||
|
logger.info("XGLOG: Begin Constructor Interceptor");
|
||||||
|
try {
|
||||||
|
ic.proceed();
|
||||||
|
} finally {
|
||||||
|
logger.info("XGLOG: End Constructor Interceptor");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void logPostConstruct (InvocationContext ic) throws Exception {
|
||||||
|
logger.info("XGLOG: Begin PostConstructor Interceptor");
|
||||||
|
try {
|
||||||
|
ic.proceed();
|
||||||
|
} finally {
|
||||||
|
logger.info("XGLOG: End PostConstructor Interceptor");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@AroundInvoke
|
||||||
|
public Object logMethod(InvocationContext ic) throws Exception {
|
||||||
|
logger.info("XGLOG: Begin " + ic.getTarget().toString() + " "+ ic.getMethod().getName()+ " Interceptor");
|
||||||
|
try {
|
||||||
|
return ic.proceed();
|
||||||
|
} finally {
|
||||||
|
logger.info("XGLOG: End " + ic.getTarget().toString() + " "+ ic.getMethod().getName() + " Interceptor");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
19
test1_cdi/GeneralInterceptorA.java
Normal file
19
test1_cdi/GeneralInterceptorA.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
import javax.interceptor.InterceptorBinding;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 27/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@InterceptorBinding
|
||||||
|
@Target({METHOD, TYPE, CONSTRUCTOR})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface GeneralInterceptorA {
|
||||||
|
}
|
||||||
20
test1_cdi/NumberOfStudents.java
Normal file
20
test1_cdi/NumberOfStudents.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.inject.Qualifier;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Target({FIELD, TYPE, METHOD,PARAMETER})
|
||||||
|
public @interface NumberOfStudents {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
10
test1_cdi/Student.java
Normal file
10
test1_cdi/Student.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
public interface Student {
|
||||||
|
|
||||||
|
String getinfo ();
|
||||||
|
|
||||||
|
}
|
||||||
15
test1_cdi/StudentAlternative.java
Normal file
15
test1_cdi/StudentAlternative.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
import javax.enterprise.inject.Alternative;
|
||||||
|
|
||||||
|
@Alternative @StudentI(implementation = student_implementations.one)
|
||||||
|
public class StudentAlternative implements Student {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getinfo() {
|
||||||
|
return "i'm an alternative";
|
||||||
|
}
|
||||||
|
}
|
||||||
15
test1_cdi/StudentAlternative2.java
Normal file
15
test1_cdi/StudentAlternative2.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
import javax.enterprise.inject.Alternative;
|
||||||
|
|
||||||
|
@Alternative @StudentI(implementation = student_implementations.two)
|
||||||
|
public class StudentAlternative2 implements Student {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getinfo() {
|
||||||
|
return "i'm an alternative 2";
|
||||||
|
}
|
||||||
|
}
|
||||||
21
test1_cdi/StudentI.java
Normal file
21
test1_cdi/StudentI.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.inject.Qualifier;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Target({FIELD, TYPE, METHOD})
|
||||||
|
public @interface StudentI {
|
||||||
|
student_implementations implementation();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
14
test1_cdi/StudentImplemented.java
Normal file
14
test1_cdi/StudentImplemented.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@StudentI(implementation = student_implementations.one)
|
||||||
|
public class StudentImplemented implements Student {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getinfo() {
|
||||||
|
return "null null";
|
||||||
|
}
|
||||||
|
}
|
||||||
14
test1_cdi/StudentImplemented2.java
Normal file
14
test1_cdi/StudentImplemented2.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 26/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@StudentI(implementation = student_implementations.two)
|
||||||
|
public class StudentImplemented2 implements Student {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getinfo() {
|
||||||
|
return "null null 2";
|
||||||
|
}
|
||||||
|
}
|
||||||
3
test1_cdi/student_implementations.java
Normal file
3
test1_cdi/student_implementations.java
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package test1_cdi;
|
||||||
|
|
||||||
|
public enum student_implementations {one,two}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
import javax.validation.constraints.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
public class class_with_builtin_constraints {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@NotNull @Size(min = 4, max = 50)
|
||||||
|
private String title;
|
||||||
|
@NotNull
|
||||||
|
private Float price;
|
||||||
|
|
||||||
|
|
||||||
|
@Pattern(regexp = "[A-Z][a-z]{1,}")
|
||||||
|
private String musicCompany;
|
||||||
|
|
||||||
|
@Max(value = 5)
|
||||||
|
private Integer numberOfCDs;
|
||||||
|
|
||||||
|
@NotNull @DecimalMin("5.8")
|
||||||
|
public Float calculatePrice(@DecimalMin("1.4") Float rate) {
|
||||||
|
return price * rate;
|
||||||
|
}
|
||||||
|
@DecimalMin("9.99")
|
||||||
|
public Float calculateVAT() {
|
||||||
|
return price * 0.196f;
|
||||||
|
}
|
||||||
|
|
||||||
|
@AssertTrue
|
||||||
|
public Boolean CheckStringLength (@NotNull String a) {
|
||||||
|
return a.length() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
42
test2_constraints_validation/general_class.java
Normal file
42
test2_constraints_validation/general_class.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.validation.constraints.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@general_class_constraint
|
||||||
|
public class general_class {
|
||||||
|
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
private Double price;
|
||||||
|
|
||||||
|
@PostConstruct private void ps (){
|
||||||
|
title = "AAA";
|
||||||
|
price = 121.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean CheckStringLength ( String a) {
|
||||||
|
return a.length() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(Double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
}
|
||||||
24
test2_constraints_validation/general_class_constraint.java
Normal file
24
test2_constraints_validation/general_class_constraint.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.Payload;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Constraint(validatedBy = {general_class_validator.class})
|
||||||
|
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER,TYPE})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface general_class_constraint {
|
||||||
|
|
||||||
|
String message() default "This is a sample of constraint for a class";
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
|
||||||
|
}
|
||||||
23
test2_constraints_validation/general_class_validator.java
Normal file
23
test2_constraints_validation/general_class_validator.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
public class general_class_validator implements ConstraintValidator<general_class_constraint,general_class> {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(general_class_constraint general_class_constraint) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(general_class general_class, ConstraintValidatorContext constraintValidatorContext) {
|
||||||
|
if (general_class.getPrice() > 0 && general_class.getTitle().length() > 0)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
19
test2_constraints_validation/general_class_wrapper.java
Normal file
19
test2_constraints_validation/general_class_wrapper.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
public class general_class_wrapper {
|
||||||
|
|
||||||
|
@Inject @general_class_constraint
|
||||||
|
private general_class g;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
private void ps (){
|
||||||
|
g.setPrice(10.0);
|
||||||
|
g.setTitle("Artur");
|
||||||
|
}
|
||||||
|
}
|
||||||
15
test2_constraints_validation/substring_class.java
Normal file
15
test2_constraints_validation/substring_class.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
public class substring_class {
|
||||||
|
|
||||||
|
|
||||||
|
@substring_constraint (xg_attribute_1 = "xgiovio" )
|
||||||
|
String a = "arxgiovio";
|
||||||
|
|
||||||
|
@substring_constraint (xg_attribute_1 = "mario" )
|
||||||
|
String b = "xgarturomario";
|
||||||
|
|
||||||
|
}
|
||||||
25
test2_constraints_validation/substring_constraint.java
Normal file
25
test2_constraints_validation/substring_constraint.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.Payload;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Constraint(validatedBy = {substring_validator.class})
|
||||||
|
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface substring_constraint {
|
||||||
|
|
||||||
|
String message() default "This string must contain the xg_attribute_1 attribute ";
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
|
||||||
|
String xg_attribute_1() default "";
|
||||||
|
}
|
||||||
23
test2_constraints_validation/substring_validator.java
Normal file
23
test2_constraints_validation/substring_validator.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
public class substring_validator implements ConstraintValidator<substring_constraint,String> {
|
||||||
|
|
||||||
|
String xg_attribute_1;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(substring_constraint substring_constraint) {
|
||||||
|
this.xg_attribute_1 = substring_constraint.xg_attribute_1();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
|
||||||
|
return s.contains(this.xg_attribute_1);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
test2_constraints_validation/xg_substring_class.java
Normal file
15
test2_constraints_validation/xg_substring_class.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
public class xg_substring_class {
|
||||||
|
|
||||||
|
|
||||||
|
@xg_substring_constraint
|
||||||
|
String a = "arxgiovio";
|
||||||
|
|
||||||
|
@xg_substring_constraint
|
||||||
|
String b = "xgarturo";
|
||||||
|
|
||||||
|
}
|
||||||
22
test2_constraints_validation/xg_substring_constraint.java
Normal file
22
test2_constraints_validation/xg_substring_constraint.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
import javax.validation.*;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Constraint(validatedBy = {xg_substring_validator.class})
|
||||||
|
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface xg_substring_constraint {
|
||||||
|
|
||||||
|
String message() default "This string doesn't contain xg";
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
|
||||||
|
}
|
||||||
20
test2_constraints_validation/xg_substring_validator.java
Normal file
20
test2_constraints_validation/xg_substring_validator.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package test2_constraints_validation;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 29/04/2015.
|
||||||
|
*/
|
||||||
|
public class xg_substring_validator implements ConstraintValidator<xg_substring_constraint,String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(xg_substring_constraint xg_substring_constraint) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
|
||||||
|
return s.contains("xg");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user