1. Micha Kops
  2. hascode-tutorials

Commits

Micha Kops  committed 6e96867

Java EE 6 Interceptor Tutorial Sources added

  • Participants
  • Parent commits 7789106
  • Branches default

Comments (0)

Files changed (20)

File jee6-interceptor-tutorial/.classpath

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java"/>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

File jee6-interceptor-tutorial/.project

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>jee6-interceptor-tutorial</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
+	</natures>
+</projectDescription>

File jee6-interceptor-tutorial/.settings/org.eclipse.core.resources.prefs

View file
+#Mon Aug 15 21:17:46 CEST 2011
+eclipse.preferences.version=1
+encoding//src/main/java=UTF-8
+encoding//src/test/java=UTF-8

File jee6-interceptor-tutorial/.settings/org.eclipse.jdt.core.prefs

View file
+#Mon Aug 15 21:17:46 CEST 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.source=1.6

File jee6-interceptor-tutorial/.settings/org.eclipse.m2e.core.prefs

View file
+#Mon Aug 15 21:17:46 CEST 2011
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1

File jee6-interceptor-tutorial/pom.xml

View file
+<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.hascode.tutorial.jee</groupId>
+    <artifactId>jee6-interceptor-tutorial</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>war</packaging>
+
+    <name>jee6-interceptor-tutorial</name>
+
+    <properties>
+        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>javax</groupId>
+            <artifactId>javaee-web-api</artifactId>
+            <version>6.0</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.8.2</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>2.3.2</version>
+                <configuration>
+                    <source>1.6</source>
+                    <target>1.6</target>
+                    <compilerArguments>
+                        <endorseddirs>${endorsed.dir}</endorseddirs>
+                    </compilerArguments>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-war-plugin</artifactId>
+                <version>2.1.1</version>
+                <configuration>
+                    <failOnMissingWebXml>false</failOnMissingWebXml>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <version>2.1</version>
+                <executions>
+                    <execution>
+                        <phase>validate</phase>
+                        <goals>
+                            <goal>copy</goal>
+                        </goals>
+                        <configuration>
+                            <outputDirectory>${endorsed.dir}</outputDirectory>
+                            <silent>true</silent>
+                            <artifactItems>
+                                <artifactItem>
+                                    <groupId>javax</groupId>
+                                    <artifactId>javaee-endorsed-api</artifactId>
+                                    <version>6.0</version>
+                                    <type>jar</type>
+                                </artifactItem>
+                            </artifactItems>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+        <pluginManagement>
+        	<plugins>
+        		<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
+        		<plugin>
+        			<groupId>org.eclipse.m2e</groupId>
+        			<artifactId>lifecycle-mapping</artifactId>
+        			<version>1.0.0</version>
+        			<configuration>
+        				<lifecycleMappingMetadata>
+        					<pluginExecutions>
+        						<pluginExecution>
+        							<pluginExecutionFilter>
+        								<groupId>
+        									org.apache.maven.plugins
+        								</groupId>
+        								<artifactId>
+        									maven-dependency-plugin
+        								</artifactId>
+        								<versionRange>
+        									[2.1,)
+        								</versionRange>
+        								<goals>
+        									<goal>copy</goal>
+        								</goals>
+        							</pluginExecutionFilter>
+        							<action>
+        								<ignore></ignore>
+        							</action>
+        						</pluginExecution>
+        					</pluginExecutions>
+        				</lifecycleMappingMetadata>
+        			</configuration>
+        		</plugin>
+        	</plugins>
+        </pluginManagement>
+    </build>
+
+</project>

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/Book.java

View file
+package com.hascode.tutorial.jee;
+
+public class Book {
+	private String title;
+
+	public String getTitle() {
+		return title;
+	}
+
+	public void setTitle(String title) {
+		this.title = title;
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/BookEJB.java

View file
+package com.hascode.tutorial.jee;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.ejb.Stateless;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+@Stateless
+public class BookEJB {
+	private final Logger logger = Logger.getLogger("com.hascode.tutorial.jee");
+
+	public void createBook(final Book book) {
+		logger.log(Level.INFO, "creating a new book with title {0} in BookEJB",
+				book.getTitle());
+		// persist
+	}
+
+	public Set<Book> findBookByTitle(final String title) {
+		logger.log(Level.INFO,
+				"searching book with given title {0} in BookEJB", title);
+		// find
+		return new HashSet<Book>();
+	}
+
+	@AroundInvoke
+	private Object logMethodExecution(final InvocationContext ic)
+			throws Exception {
+		logger.log(Level.INFO,
+				"interceptor invoked on target {0} and method {1}",
+				new Object[] { ic.getTarget().toString(),
+						ic.getMethod().getName() });
+		try {
+			return ic.proceed();
+		} finally {
+		}
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/BookServlet.java

View file
+package com.hascode.tutorial.jee;
+
+import java.io.IOException;
+import java.util.logging.Logger;
+
+import javax.ejb.EJB;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@WebServlet(name="bookServlet", urlPatterns="/printBookInformation")
+public class BookServlet extends HttpServlet {
+	
+	@EJB
+	private BookEJB bookEJB;
+
+	@Override
+	protected void doGet(HttpServletRequest req, HttpServletResponse res)
+			throws ServletException, IOException {
+		Book book = new Book();
+		book.setTitle("The joy of interceptors v.1");
+		bookEJB.createBook(book);
+		bookEJB.findBookByTitle("test");
+		res.getWriter().append("Watch your logs..");
+		
+	}
+
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/Interceptor1.java

View file
+package com.hascode.tutorial.jee;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+public class Interceptor1 {
+	private final Logger logger = Logger.getLogger("com.hascode.tutorial.jee");
+
+	@AroundInvoke
+	private Object logMethodExecution(final InvocationContext ic)
+			throws Exception {
+		logger.log(Level.INFO,
+				"interceptor #1 invoked on target {0} and method {1}",
+				new Object[] { ic.getTarget().toString(),
+						ic.getMethod().getName() });
+		try {
+			return ic.proceed();
+		} finally {
+		}
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/Interceptor2.java

View file
+package com.hascode.tutorial.jee;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+public class Interceptor2 {
+	private final Logger logger = Logger.getLogger("com.hascode.tutorial.jee");
+
+	@AroundInvoke
+	private Object logMethodExecution(final InvocationContext ic)
+			throws Exception {
+		logger.log(Level.INFO,
+				"interceptor #2 invoked on target {0} and method {1}",
+				new Object[] { ic.getTarget().toString(),
+						ic.getMethod().getName() });
+		try {
+			return ic.proceed();
+		} finally {
+		}
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/Interceptor3.java

View file
+package com.hascode.tutorial.jee;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+public class Interceptor3 {
+	private final Logger logger = Logger.getLogger("com.hascode.tutorial.jee");
+
+	@AroundInvoke
+	private Object logMethodExecution(final InvocationContext ic)
+			throws Exception {
+		logger.log(Level.INFO,
+				"interceptor #3 invoked on target {0} and method {1}",
+				new Object[] { ic.getTarget().toString(),
+						ic.getMethod().getName() });
+		try {
+			return ic.proceed();
+		} finally {
+		}
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/Interceptor4.java

View file
+package com.hascode.tutorial.jee;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+public class Interceptor4 {
+	private final Logger logger = Logger.getLogger("com.hascode.tutorial.jee");
+
+	@AroundInvoke
+	private Object logMethodExecution(final InvocationContext ic)
+			throws Exception {
+		logger.log(Level.INFO,
+				"interceptor #4 invoked on target {0} and method {1}",
+				new Object[] { ic.getTarget().toString(),
+						ic.getMethod().getName() });
+		try {
+			return ic.proceed();
+		} finally {
+		}
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/Product.java

View file
+package com.hascode.tutorial.jee;
+
+public class Product {
+	private String name;
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(final String name) {
+		this.name = name;
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/ProductEJB.java

View file
+package com.hascode.tutorial.jee;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.ejb.Stateless;
+import javax.interceptor.ExcludeClassInterceptors;
+import javax.interceptor.Interceptors;
+
+@Stateless
+@Interceptors({ Interceptor1.class, Interceptor3.class })
+public class ProductEJB {
+	private final Logger logger = Logger.getLogger("com.hascode.tutorial.jee");
+
+	@Interceptors({ Interceptor2.class, Interceptor4.class })
+	public void createProduct(final Product product) {
+		logger.log(Level.INFO,
+				"creating a new product with name {0} in ProductEJB",
+				product.getName());
+		// persist
+	}
+
+	@ExcludeClassInterceptors
+	@Interceptors(Interceptor2.class)
+	public Set<Product> findProductByName(final String name) {
+		logger.log(Level.INFO,
+				"searching product with given name {0} in ProductEJB", name);
+		// find
+		return new HashSet<Product>();
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/ProductServlet.java

View file
+package com.hascode.tutorial.jee;
+
+import java.io.IOException;
+
+import javax.ejb.EJB;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@WebServlet(name = "productServlet", urlPatterns = "/printProductInformation")
+public class ProductServlet extends HttpServlet {
+
+	@EJB
+	private ProductEJB productEJB;
+
+	@Override
+	protected void doGet(final HttpServletRequest req,
+			final HttpServletResponse res) throws ServletException, IOException {
+		Product product = new Product();
+		product.setName("Extra strong coffee");
+		productEJB.createProduct(product);
+		productEJB.findProductByName("sugar");
+		res.getWriter().append("Watch your logs..");
+	}
+
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/ProtocolInterceptor.java

View file
+package com.hascode.tutorial.jee;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+public class ProtocolInterceptor {
+	private final Logger logger = Logger.getLogger("com.hascode.tutorial.jee");
+
+	@AroundInvoke
+	private Object logMethodExecution(final InvocationContext ic)
+			throws Exception {
+		logger.log(Level.INFO,
+				"protocol interceptor invoked on target {0} and method {1}",
+				new Object[] { ic.getTarget().toString(),
+						ic.getMethod().getName() });
+		try {
+			return ic.proceed();
+		} finally {
+		}
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/User.java

View file
+package com.hascode.tutorial.jee;
+
+public class User {
+	private String name;
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/UserEJB.java

View file
+package com.hascode.tutorial.jee;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.ejb.Stateless;
+import javax.interceptor.Interceptors;
+
+@Stateless
+public class UserEJB {
+	private final Logger logger = Logger.getLogger("com.hascode.tutorial.jee");
+
+	@Interceptors(ProtocolInterceptor.class)
+	public void createUser(final User user) {
+		logger.log(Level.INFO, "creating a new user with name {0} in UserEJB",
+				user.getName());
+		// persist
+	}
+
+	public Set<User> findBookByTitle(final String name) {
+		logger.log(Level.INFO, "searching user with given name {0} in UserEJB",
+				name);
+		// find
+		return new HashSet<User>();
+	}
+}

File jee6-interceptor-tutorial/src/main/java/com/hascode/tutorial/jee/UserServlet.java

View file
+package com.hascode.tutorial.jee;
+
+import java.io.IOException;
+
+import javax.ejb.EJB;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@WebServlet(name = "userServlet", urlPatterns = "/printUserInformation")
+public class UserServlet extends HttpServlet {
+
+	@EJB
+	private UserEJB userEJB;
+
+	@Override
+	protected void doGet(final HttpServletRequest req,
+			final HttpServletResponse res) throws ServletException, IOException {
+		User user = new User();
+		user.setName("Alfred E. Neumann");
+		userEJB.createUser(user);
+		userEJB.findBookByTitle("Charles Bukowski");
+		res.getWriter().append("Watch your logs..");
+	}
+
+}