[weld-commits] Weld SVN: r6487 - in examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode: session and 1 other directory.
weld-commits at lists.jboss.org
weld-commits at lists.jboss.org
Fri Jun 18 12:10:43 EDT 2010
Author: pete.muir at jboss.org
Date: 2010-06-18 12:10:41 -0400 (Fri, 18 Jun 2010)
New Revision: 6487
Added:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentLogger.java
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentPrinter.java
Removed:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/EEProducers.java
Modified:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/model/CodeFragment.java
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentManagerImpl.java
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/DatabasePopulater.java
Log:
add events and timer
Modified: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/model/CodeFragment.java
===================================================================
--- examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/model/CodeFragment.java 2010-06-18 13:27:09 UTC (rev 6486)
+++ examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/model/CodeFragment.java 2010-06-18 16:10:41 UTC (rev 6487)
@@ -184,4 +184,10 @@
{
this.user = user;
}
+
+ @Override
+ public String toString()
+ {
+ return "At " + getFriendlyDate() + " by " + getUser();
+ }
}
\ No newline at end of file
Added: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentLogger.java
===================================================================
--- examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentLogger.java (rev 0)
+++ examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentLogger.java 2010-06-18 16:10:41 UTC (rev 6487)
@@ -0,0 +1,45 @@
+package org.jboss.weld.examples.pastecode.session;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.ejb.Lock;
+import javax.ejb.LockType;
+import javax.ejb.Singleton;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Observes;
+
+import org.jboss.weld.examples.pastecode.model.CodeFragment;
+
+ at ApplicationScoped
+//@Singleton
+public class CodeFragmentLogger
+{
+
+ private final List<CodeFragment> log;
+
+ public CodeFragmentLogger()
+ {
+ this.log = new ArrayList<CodeFragment>();
+ }
+
+ //@Lock(LockType.READ)
+ public List<CodeFragment> getLog()
+ {
+ return Collections.unmodifiableList(log);
+ }
+
+ //@Lock(LockType.WRITE)
+ public void clearLog()
+ {
+ this.log.clear();
+ }
+
+ //@Lock(LockType.WRITE)
+ public void addEntry(@Observes CodeFragment codeFragment)
+ {
+ this.log.add(codeFragment);
+ }
+
+}
Property changes on: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentLogger.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Modified: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentManagerImpl.java
===================================================================
--- examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentManagerImpl.java 2010-06-18 13:27:09 UTC (rev 6486)
+++ examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentManagerImpl.java 2010-06-18 16:10:41 UTC (rev 6487)
@@ -29,6 +29,7 @@
import java.util.logging.Logger;
import javax.ejb.Stateful;
+import javax.enterprise.event.Event;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
@@ -55,6 +56,9 @@
@Inject
private Logger log;
+
+ @Inject
+ private Event<CodeFragment> event;
@PersistenceContext
private EntityManager entityManager;
@@ -83,7 +87,7 @@
String hashValue = hashComputer.getHashValue(code);
code.setHash(hashValue);
entityManager.persist(code);
- log.info("Added private pastecode: " + hashValue);
+ event.fire(code);
return hashValue;
}
catch (NoSuchAlgorithmException e)
@@ -96,9 +100,9 @@
else
{
entityManager.persist(code);
+ event.fire(code);
// Make sure we have the latest version (with the generated id!)
entityManager.refresh(code);
- log.info("Added pastecode: " + code.getId());
return new Integer(code.getId()).toString();
}
}
Added: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentPrinter.java
===================================================================
--- examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentPrinter.java (rev 0)
+++ examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentPrinter.java 2010-06-18 16:10:41 UTC (rev 6487)
@@ -0,0 +1,52 @@
+package org.jboss.weld.examples.pastecode.session;
+
+import java.util.logging.Logger;
+
+import javax.annotation.Resource;
+import javax.ejb.Stateless;
+import javax.ejb.Timeout;
+import javax.ejb.TimerService;
+import javax.inject.Inject;
+
+import org.jboss.weld.examples.pastecode.model.CodeFragment;
+
+ at Stateless
+public class CodeFragmentPrinter
+{
+
+ private static final int ONE_MINUTE = 60 * 1000;
+
+ @Resource
+ private TimerService timerService;
+
+ @Inject
+ private CodeFragmentLogger logger;
+
+ @Inject
+ private Logger log;
+
+ public void startTimer()
+ {
+ timerService.createTimer(ONE_MINUTE, ONE_MINUTE, null);
+ }
+
+ @Timeout
+ public void print()
+ {
+ if (logger.getLog().size() > 0)
+ {
+ log.info("These code fragments pasted in the last minute: ");
+ for (CodeFragment fragment : logger.getLog())
+ {
+ log.info(fragment.toString());
+ }
+ log.info("-----------------------------------------------------");
+ logger.clearLog();
+ }
+ else
+ {
+ log.info("No fragments pasted in the last minute");
+ }
+ }
+
+}
Property changes on: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentPrinter.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Modified: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/DatabasePopulater.java
===================================================================
--- examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/DatabasePopulater.java 2010-06-18 13:27:09 UTC (rev 6486)
+++ examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/DatabasePopulater.java 2010-06-18 16:10:41 UTC (rev 6487)
@@ -57,6 +57,9 @@
@Inject
private CodeFragmentManager codeFragmentManager;
+ @Inject
+ private CodeFragmentPrinter codeFragmentPrinter;
+
private boolean populated;
// TODO @PostConstruct
@@ -67,6 +70,9 @@
return;
}
+ // Start the timer for the logger :-)
+ codeFragmentPrinter.startTimer();
+
try
{
String fileContent = readFileData(DATA_FILE_NAME);
@@ -92,7 +98,7 @@
}
catch (Exception e)
{
- log.log(Level.WARNING, "Unable to read all records from " + DATA_FILE_NAME + " file");
+ log.log(Level.WARNING, "Unable to read all records from " + DATA_FILE_NAME + " file", e);
}
log.info("Successfully imported data!");
Deleted: examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/EEProducers.java
===================================================================
--- examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/EEProducers.java 2010-06-18 13:27:09 UTC (rev 6486)
+++ examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/EEProducers.java 2010-06-18 16:10:41 UTC (rev 6487)
@@ -1,15 +0,0 @@
-package org.jboss.weld.examples.pastecode.session;
-
-import javax.enterprise.inject.Produces;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-
-public class EEProducers
-{
-
- @SuppressWarnings("unused")
- @PersistenceContext(unitName = "pastecodeDatabase")
- @Produces
- private EntityManager em;
-
-}
More information about the weld-commits
mailing list