Skip to content

feat(Stereotype): added examples for Stereotype Feature #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cdi-stereotypes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target/
build/
.classpath
.project
.settings
15 changes: 15 additions & 0 deletions cdi-stereotypes/README.md
Original file line number Diff line number Diff line change
@@ -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...
54 changes: 54 additions & 0 deletions cdi-stereotypes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.craftcoder.cdi</groupId>
<artifactId>cdi-stereotype</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>cdi-stereotype</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- TESTING DEPENDENCIES -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- TESTING DEPENDENCIES -->

<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.0.0.Alpha16</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.craftcoder.cdi.audit;

public class Auditor {

public void audit() {
System.out.println("Auditing");
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
22 changes: 22 additions & 0 deletions cdi-stereotypes/src/main/java/com/craftcoder/cdi/model/Sale.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
@@ -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<Object> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.craftcoder.cdi.stereotype;

public enum Mode {
DEBUG, INFO, WARN
}
Original file line number Diff line number Diff line change
@@ -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

}
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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 {

}
Loading