diff --git a/cdi-stereotypes/.gitignore b/cdi-stereotypes/.gitignore new file mode 100644 index 0000000..6621476 --- /dev/null +++ b/cdi-stereotypes/.gitignore @@ -0,0 +1,5 @@ +/target/ +build/ +.classpath +.project +.settings diff --git a/cdi-stereotypes/README.md b/cdi-stereotypes/README.md new file mode 100644 index 0000000..7555883 --- /dev/null +++ b/cdi-stereotypes/README.md @@ -0,0 +1,15 @@ +# CDI Stereotypes + +Hello! + +In this tutorial, we will going to see an example of how to use CDI Stereotypes + +Enjoy! + +Let's make main annotations. + +In our projects, maked several annotations to verys goals, in your bean manager can have several annotations, each one with your objectves, but as ensure that all developers will remember of all annotations, this is only one goal of stereotype. + +This example contain a class `Checkout` with annotation `StereotypeSample` that join two annotations. + +Got to code... \ No newline at end of file diff --git a/cdi-stereotypes/pom.xml b/cdi-stereotypes/pom.xml new file mode 100644 index 0000000..bf8e548 --- /dev/null +++ b/cdi-stereotypes/pom.xml @@ -0,0 +1,54 @@ + + 4.0.0 + + com.craftcoder.cdi + cdi-stereotype + 0.0.1-SNAPSHOT + jar + + cdi-stereotype + http://maven.apache.org + + + UTF-8 + + + + + + org.hamcrest + hamcrest-all + 1.3 + + + + junit + junit + 4.12 + test + + + + + org.jboss.weld.se + weld-se-core + 3.0.0.Alpha16 + + + + + + + + org.apache.maven.plugins + maven-eclipse-plugin + + true + true + + + + + + diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/audit/Auditable.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/audit/Auditable.java new file mode 100644 index 0000000..cfd26d4 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/audit/Auditable.java @@ -0,0 +1,15 @@ +package com.craftcoder.cdi.audit; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.interceptor.InterceptorBinding; + +@InterceptorBinding +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface Auditable { + +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/audit/Auditor.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/audit/Auditor.java new file mode 100644 index 0000000..f43d0c7 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/audit/Auditor.java @@ -0,0 +1,9 @@ +package com.craftcoder.cdi.audit; + +public class Auditor { + + public void audit() { + System.out.println("Auditing"); + } + +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/audit/AuditorInterceptor.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/audit/AuditorInterceptor.java new file mode 100644 index 0000000..e386557 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/audit/AuditorInterceptor.java @@ -0,0 +1,25 @@ +package com.craftcoder.cdi.audit; + +import javax.annotation.Priority; +import javax.inject.Inject; +import javax.interceptor.AroundInvoke; +import javax.interceptor.Interceptor; +import javax.interceptor.InvocationContext; + +@Interceptor @Auditable @Priority(Interceptor.Priority.APPLICATION) +public class AuditorInterceptor { + + @Inject + private Auditor auditor; + + @AroundInvoke + public Object audit(InvocationContext ic) throws Exception { + + auditor.audit(); + + Object result = ic.proceed(); + + return result; + } + +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/model/Sale.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/model/Sale.java new file mode 100644 index 0000000..d146159 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/model/Sale.java @@ -0,0 +1,22 @@ +package com.craftcoder.cdi.model; + +import java.math.BigDecimal; + +public class Sale { + + private String product; + private BigDecimal value; + + public String getProduct() { + return product; + } + public void setProduct(String product) { + this.product = product; + } + public BigDecimal getValue() { + return value; + } + public void setValue(BigDecimal value) { + this.value = value; + } +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/AwesomeLogger.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/AwesomeLogger.java new file mode 100644 index 0000000..ae6f873 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/AwesomeLogger.java @@ -0,0 +1,22 @@ +package com.craftcoder.cdi.stereotype; + +public class AwesomeLogger { + private LogConfiguration configuration; + + public AwesomeLogger(LogConfiguration configuration) { + this.configuration = configuration; + } + + public void log(String message) { + if (configuration.isInDebugMode()) { + System.out.println("DEBUG LOG: " + message); + } + else if (configuration.isInInfoMode()) { + System.out.println("INFO LOG: " + message); + } else if (configuration.isInWarnMode()) { + System.out.println("WARN LOG: " + message); + } else { + System.out.println("DEFAULT LOG: " + message); + } + } +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/AwesomeLoggerFactory.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/AwesomeLoggerFactory.java new file mode 100644 index 0000000..2bf3485 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/AwesomeLoggerFactory.java @@ -0,0 +1,31 @@ +package com.craftcoder.cdi.stereotype; + +import javax.enterprise.inject.Produces; + +public class AwesomeLoggerFactory { + + @Produces @LoggerMode(desiredMode = Mode.DEBUG) + public AwesomeLogger createDebugLogger() { //yes, I renamed it + boolean debugMode = true; //just to be clear that the debug mode is on + LogConfiguration logInDebugMode = new LogConfiguration(false, debugMode, false); + + return new AwesomeLogger(logInDebugMode); + } + + @Produces @LoggerMode(desiredMode = Mode.INFO) + public AwesomeLogger createInfoLogger() { //new method here :) + boolean infoMode = true; + LogConfiguration logInInfoMode = new LogConfiguration(infoMode, false, false); + + return new AwesomeLogger(logInInfoMode); + } + + @Produces @LoggerMode(desiredMode = Mode.WARN) + public AwesomeLogger createWarnLogger() { //new method here :) + boolean warnMode = true; + LogConfiguration logInWarnMode = new LogConfiguration(false, false, warnMode); + + return new AwesomeLogger(logInWarnMode); + } + +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/Checkout.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/Checkout.java new file mode 100644 index 0000000..1f391a5 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/Checkout.java @@ -0,0 +1,17 @@ +package com.craftcoder.cdi.stereotype; + +import javax.inject.Inject; + +import com.craftcoder.cdi.model.Sale; + +@StereotypeSample +public class Checkout { + + @Inject @LoggerMode(desiredMode = Mode.DEBUG) + private AwesomeLogger logger; + + public void finishCheckout(Sale sale) { + + logger.log("Finishing Checkout"); + } +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/DebugMode.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/DebugMode.java new file mode 100644 index 0000000..d25b6d8 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/DebugMode.java @@ -0,0 +1,15 @@ +package com.craftcoder.cdi.stereotype; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.FIELD}) +public @interface DebugMode { + +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/InfoMode.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/InfoMode.java new file mode 100644 index 0000000..257e1d7 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/InfoMode.java @@ -0,0 +1,15 @@ +package com.craftcoder.cdi.stereotype; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.FIELD}) +public @interface InfoMode { + +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/LogConfiguration.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/LogConfiguration.java new file mode 100644 index 0000000..be333dc --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/LogConfiguration.java @@ -0,0 +1,28 @@ +package com.craftcoder.cdi.stereotype; + +public class LogConfiguration { + + private boolean infoMode; + + private boolean debugMode; + + private boolean warnMode; + + public LogConfiguration(boolean infoMode, boolean debugMode, boolean warnMode) { + this.infoMode = infoMode; + this.debugMode = debugMode; + this.warnMode = warnMode; + } + + public boolean isInInfoMode() { + return infoMode; + } + + public boolean isInDebugMode() { + return debugMode; + } + + public boolean isInWarnMode() { + return warnMode; + } +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/LoggerMode.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/LoggerMode.java new file mode 100644 index 0000000..ead7abd --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/LoggerMode.java @@ -0,0 +1,17 @@ +package com.craftcoder.cdi.stereotype; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.FIELD}) +public @interface LoggerMode { + + Mode desiredMode(); + +} \ No newline at end of file diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/MainApplication.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/MainApplication.java new file mode 100644 index 0000000..2468dcd --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/MainApplication.java @@ -0,0 +1,25 @@ +package com.craftcoder.cdi.stereotype; + +import java.math.BigDecimal; + +import javax.enterprise.inject.spi.CDI; + +import org.jboss.weld.environment.se.Weld; + +import com.craftcoder.cdi.model.Sale; + +public class MainApplication { + + public static void main(String[] args) { + try (CDI container = new Weld().initialize()) { + + Sale sale = new Sale(); + sale.setProduct("Book 1"); + sale.setValue(new BigDecimal(100)); + + Checkout checkout = container.select(Checkout.class).get(); + + checkout.finishCheckout(null); + } + } +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/Mode.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/Mode.java new file mode 100644 index 0000000..ee70114 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/Mode.java @@ -0,0 +1,5 @@ +package com.craftcoder.cdi.stereotype; + +public enum Mode { + DEBUG, INFO, WARN +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/StereotypeSample.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/StereotypeSample.java new file mode 100644 index 0000000..04ffa9a --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/StereotypeSample.java @@ -0,0 +1,22 @@ +package com.craftcoder.cdi.stereotype; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.enterprise.inject.Stereotype; + +import com.craftcoder.cdi.audit.Auditable; +import com.craftcoder.cdi.validations.Valid; + +@Stereotype +@Auditable +@Valid +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface StereotypeSample { + + //this annotation will join other annotations + +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/WarnMode.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/WarnMode.java new file mode 100644 index 0000000..4b7f545 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/stereotype/WarnMode.java @@ -0,0 +1,15 @@ +package com.craftcoder.cdi.stereotype; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.FIELD}) +public @interface WarnMode { + +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/validations/Valid.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/validations/Valid.java new file mode 100644 index 0000000..5fd7847 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/validations/Valid.java @@ -0,0 +1,18 @@ +package com.craftcoder.cdi.validations; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.interceptor.InterceptorBinding; + +@InterceptorBinding +@Documented +@Retention(RUNTIME) +@Target({ ElementType.METHOD, ElementType.TYPE}) +public @interface Valid { + +} diff --git a/cdi-stereotypes/src/main/java/com/craftcoder/cdi/validations/ValidInterceptor.java b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/validations/ValidInterceptor.java new file mode 100644 index 0000000..0415196 --- /dev/null +++ b/cdi-stereotypes/src/main/java/com/craftcoder/cdi/validations/ValidInterceptor.java @@ -0,0 +1,26 @@ +package com.craftcoder.cdi.validations; + +import javax.annotation.Priority; +import javax.interceptor.AroundInvoke; +import javax.interceptor.Interceptor; +import javax.interceptor.InvocationContext; + +import com.craftcoder.cdi.model.Sale; + +@Interceptor @Valid @Priority(Interceptor.Priority.APPLICATION) +public class ValidInterceptor { + + @AroundInvoke + public Object valid(InvocationContext ic) throws Exception { + + Sale sale = (Sale) ic.getParameters()[0]; + + if( sale != null ) { + + return ic.proceed(); + }else { + + throw new Exception("Sale Invalid"); + } + } +} diff --git a/cdi-stereotypes/src/main/resources/META-INF/beans.xml b/cdi-stereotypes/src/main/resources/META-INF/beans.xml new file mode 100755 index 0000000..5d3bced --- /dev/null +++ b/cdi-stereotypes/src/main/resources/META-INF/beans.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file