Author: dan.j.allen
Date: 2010-07-08 19:47:05 -0400 (Thu, 08 Jul 2010)
New Revision: 6590
Modified:
archetypes/trunk/javaee6-webapp-src/pom.xml
archetypes/trunk/javaee6-webapp-src/src/main/java/com/mycompany/MemberRegistration.java
archetypes/trunk/javaee6-webapp-src/src/test/java/com/mycompany/MemberRegistrationTest.java
Log:
add injectable logger
Modified: archetypes/trunk/javaee6-webapp-src/pom.xml
===================================================================
--- archetypes/trunk/javaee6-webapp-src/pom.xml 2010-07-08 22:01:16 UTC (rev 6589)
+++ archetypes/trunk/javaee6-webapp-src/pom.xml 2010-07-08 23:47:05 UTC (rev 6590)
@@ -78,6 +78,22 @@
</exclusions>
</dependency>
+ <!-- Weld Extensions provides convenient, portable CDI extensions such as an
injectable logger -->
+ <dependency>
+ <groupId>org.jboss.weld</groupId>
+ <artifactId>weld-extensions</artifactId>
+ <version>1.0.0.Alpha2</version>
+ </dependency>
+
+ <!-- Need to use the injectable logger in Weld Extensions -->
+ <!-- Both JBoss AS and GlassFish provide this API -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.5.10</version>
+ <scope>provided</scope>
+ </dependency>
+
<!-- JSR-303 (Bean Validation) Implementation -->
<!-- Provides portable constraints such as @Email -->
<!-- Hibernate Validator is the only JSR-303 implementation at the moment, so we
can assume it's provided -->
@@ -87,6 +103,7 @@
<version>4.0.0.GA</version>
<scope>provided</scope>
</dependency>
+
<!-- Needed for running tests (you may also use TestNG) -->
<dependency>
<groupId>junit</groupId>
@@ -94,6 +111,7 @@
<version>4.8.1</version>
<scope>test</scope>
</dependency>
+
<!-- Optional, but highly recommended -->
<!-- Arquillian allows you to test enterprise code such as EJBs and
Transactional JPA from JUnit/TestNG -->
<dependency>
Modified:
archetypes/trunk/javaee6-webapp-src/src/main/java/com/mycompany/MemberRegistration.java
===================================================================
---
archetypes/trunk/javaee6-webapp-src/src/main/java/com/mycompany/MemberRegistration.java 2010-07-08
22:01:16 UTC (rev 6589)
+++
archetypes/trunk/javaee6-webapp-src/src/main/java/com/mycompany/MemberRegistration.java 2010-07-08
23:47:05 UTC (rev 6590)
@@ -8,12 +8,16 @@
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.transaction.UserTransaction;
+import org.slf4j.Logger;
//@Stateful // enable when JBoss AS M4 is out
@Model
public class MemberRegistration
{
@Inject
+ private Logger log;
+
+ @Inject
@MemberRepository
private EntityManager em;
@@ -33,7 +37,7 @@
public void register() throws Exception
{
- System.out.println("Registering " + newMember.getName());
+ log.info("Registering " + newMember.getName());
utx.begin();
em.joinTransaction();
em.persist(newMember);
Modified:
archetypes/trunk/javaee6-webapp-src/src/test/java/com/mycompany/MemberRegistrationTest.java
===================================================================
---
archetypes/trunk/javaee6-webapp-src/src/test/java/com/mycompany/MemberRegistrationTest.java 2010-07-08
22:01:16 UTC (rev 6589)
+++
archetypes/trunk/javaee6-webapp-src/src/test/java/com/mycompany/MemberRegistrationTest.java 2010-07-08
23:47:05 UTC (rev 6590)
@@ -1,5 +1,7 @@
package com.mycompany;
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.InjectionPoint;
import static org.junit.Assert.*;
import javax.inject.Inject;
import org.jboss.arquillian.api.Deployment;
@@ -10,6 +12,8 @@
import org.jboss.shrinkwrap.impl.base.asset.ByteArrayAsset;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
@RunWith(Arquillian.class)
public class MemberRegistrationTest
@@ -18,12 +22,15 @@
public static Archive<?> createTestArchive() {
return ShrinkWrap.create("test.war", WebArchive.class)
.addClasses(Member.class, MemberRegistration.class, MemberRepository.class,
MemberRepositoryProducer.class)
+ .addPackage(Logger.class.getPackage())
.addWebResource("test-persistence.xml",
"classes/META-INF/persistence.xml")
.addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
}
@Inject MemberRegistration memberRegistration;
+ @Inject Logger log;
+
@Test
public void testRegister() throws Exception
{
@@ -33,6 +40,12 @@
newMember.setPhoneNumber("2125551234");
memberRegistration.register();
assertNotNull(newMember.getId());
- System.out.println(newMember.getName() + " was persisted with id " +
newMember.getId());
+ log.info(newMember.getName() + " was persisted with id " +
newMember.getId());
}
+
+ @Produces
+ public Logger produceLog(InjectionPoint injectionPoint)
+ {
+ return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass());
+ }
}