Author: pete.muir(a)jboss.org
Date: 2010-06-19 13:36:41 -0400 (Sat, 19 Jun 2010)
New Revision: 6555
Added:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentPrinterImpl.java
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabase.java
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/TimerStartup.java
Removed:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/DatabasePopulater.java
Modified:
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
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PostTracker.java
examples/trunk/jsf/pastecode/src/main/webapp/home.xhtml
Log:
Use @Startup @Singleton :-)
Modified:
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 2010-06-19
07:21:35 UTC (rev 6554)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentLogger.java 2010-06-19
17:36:41 UTC (rev 6555)
@@ -4,9 +4,6 @@
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;
Modified:
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 2010-06-19
07:21:35 UTC (rev 6554)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentPrinter.java 2010-06-19
17:36:41 UTC (rev 6555)
@@ -1,52 +1,11 @@
package org.jboss.weld.examples.pastecode.session;
-import java.util.logging.Logger;
+import javax.ejb.Local;
-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;
-
-@Stateless
-public class CodeFragmentPrinter
+@Local
+public interface 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");
- }
- }
-}
+ public void startTimer();
+
+}
\ No newline at end of file
Copied:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentPrinterImpl.java
(from rev 6487,
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/CodeFragmentPrinterImpl.java
(rev 0)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/CodeFragmentPrinterImpl.java 2010-06-19
17:36:41 UTC (rev 6555)
@@ -0,0 +1,56 @@
+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;
+
+//TODO Needs a JBoss EJB3 fix to make this a no-interface view
+@Stateless
+public class CodeFragmentPrinterImpl implements CodeFragmentPrinter
+{
+
+ private static final int ONE_MINUTE = 60 * 1000;
+
+ @Resource
+ private TimerService timerService;
+
+ @Inject
+ private CodeFragmentLogger logger;
+
+ @Inject
+ private Logger log;
+
+ /* (non-Javadoc)
+ * @see org.jboss.weld.examples.pastecode.session.CodeFragmentPrinter#startTimer()
+ */
+ 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");
+ }
+ }
+
+}
Deleted:
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-19
07:21:35 UTC (rev 6554)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/DatabasePopulater.java 2010-06-19
17:36:41 UTC (rev 6555)
@@ -1,128 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.jboss.weld.examples.pastecode.session;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.text.SimpleDateFormat;
-import java.util.StringTokenizer;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import javax.enterprise.context.ApplicationScoped;
-import javax.inject.Inject;
-import javax.inject.Named;
-
-import org.jboss.weld.examples.pastecode.model.CodeFragment;
-import org.jboss.weld.examples.pastecode.model.Language;
-
-/**
- * This bean only populates database with preformatted data. This is due to need
- * for Hypersonic database which doesn't allow multi-line inserts. Hypersonic
- * database is embedded in JBoss AS and so there is no need to configure any
- * external database to run this example.
- *
- */
-@ApplicationScoped
-@Named("database")
-// TODO @Singleton @Startup
-public class DatabasePopulater
-{
-
- @Inject Logger log;
-
- private static final String DATA_FILE_NAME = "data.sql";
-
- @Inject
- private CodeFragmentManager codeFragmentManager;
-
- @Inject
- private CodeFragmentPrinter codeFragmentPrinter;
-
- private boolean populated;
-
- // TODO @PostConstruct
- public synchronized void populate()
- {
- if (populated)
- {
- return;
- }
-
- // Start the timer for the logger :-)
- codeFragmentPrinter.startTimer();
-
- try
- {
- String fileContent = readFileData(DATA_FILE_NAME);
- StringTokenizer st = new StringTokenizer(fileContent, "'");
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
-
- while (st.countTokens() > 1)
- {
- CodeFragment c = new CodeFragment();
- st.nextToken();
- c.setDatetime(formatter.parse(st.nextToken()));
- st.nextToken();
- c.setLanguage(Language.valueOf(st.nextToken()));
- st.nextToken();
- st.nextToken();
- st.nextToken();
- c.setUser(st.nextToken());
- st.nextToken();
- c.setText(st.nextToken());
-
- codeFragmentManager.addCodeFragment(c, false);
- }
- }
- catch (Exception e)
- {
- log.log(Level.WARNING, "Unable to read all records from " +
DATA_FILE_NAME + " file", e);
- }
-
- log.info("Successfully imported data!");
- populated = true;
- }
-
- public boolean isPopulated()
- {
- return populated;
- }
-
- private String readFileData(String fileName) throws IOException
- {
- InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName);
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
-
- String line;
- StringBuilder sb = new StringBuilder();
-
- while ((line = br.readLine()) != null)
- {
- sb.append(line).append("\n");
- }
-
- return sb.toString();
- }
-}
Copied:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabase.java
(from rev 6487,
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/PopulateDatabase.java
(rev 0)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabase.java 2010-06-19
17:36:41 UTC (rev 6555)
@@ -0,0 +1,120 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.weld.examples.pastecode.session;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.text.SimpleDateFormat;
+import java.util.StringTokenizer;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Singleton;
+import javax.ejb.Startup;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.transaction.UserTransaction;
+
+import org.jboss.weld.examples.pastecode.model.CodeFragment;
+import org.jboss.weld.examples.pastecode.model.Language;
+
+/**
+ * Populate the database with data.sql. Needed because import.sql doesn't
+ * support multi-line inserts
+ *
+ * @author pmuir
+ *
+ */
+@Startup
+@Singleton
+public class PopulateDatabase
+{
+
+ private static final String DATA_FILE_NAME = "data.sql";
+
+ @Inject
+ private Logger log;
+
+ @PersistenceContext
+ private EntityManager entityManager;
+
+ @Inject
+ private UserTransaction utx;
+
+ @PostConstruct
+ public void startup()
+ {
+
+ try
+ {
+ String fileContent = readFileData(DATA_FILE_NAME);
+ StringTokenizer st = new StringTokenizer(fileContent, "'");
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
+
+ while (st.countTokens() > 1)
+ {
+ CodeFragment c = new CodeFragment();
+ st.nextToken();
+ c.setDatetime(formatter.parse(st.nextToken()));
+ st.nextToken();
+ c.setLanguage(Language.valueOf(st.nextToken()));
+ st.nextToken();
+ st.nextToken();
+ st.nextToken();
+ c.setUser(st.nextToken());
+ st.nextToken();
+ c.setText(st.nextToken());
+
+ // Manual TX control, commit each record independently
+ utx.begin();
+ entityManager.persist(c);
+ utx.commit();
+ }
+ }
+ catch (Exception e)
+ {
+ log.log(Level.WARNING, "Unable to read all records from " +
DATA_FILE_NAME + " file", e);
+ }
+
+ log.info("Successfully imported data!");
+ }
+
+ private static String readFileData(String fileName) throws IOException
+ {
+ InputStream is =
PopulateDatabase.class.getClassLoader().getResourceAsStream(fileName);
+ BufferedReader br = new BufferedReader(new InputStreamReader(is));
+
+ String line;
+ StringBuilder sb = new StringBuilder();
+
+ while ((line = br.readLine()) != null)
+ {
+ sb.append(line).append("\n");
+ }
+
+ return sb.toString();
+ }
+}
Modified:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PostTracker.java
===================================================================
---
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PostTracker.java 2010-06-19
07:21:35 UTC (rev 6554)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PostTracker.java 2010-06-19
17:36:41 UTC (rev 6555)
@@ -5,17 +5,14 @@
import javax.ejb.Stateful;
import javax.enterprise.context.SessionScoped;
-import javax.inject.Inject;
@SessionScoped
-@Stateful // Add passivation capabilities....
+@Stateful // Adds passivation capabilities....
public class PostTracker
{
private LinkedList<Date> posts;
- @Inject DatabasePopulater databasePopulater;
-
public PostTracker()
{
this.posts = new LinkedList<Date>();
@@ -28,13 +25,15 @@
public boolean isNewPostAllowed()
{
- // if we are populating the database, skip
- if (!databasePopulater.isPopulated())
+ if (posts.size() > 2)
{
+ long diff = new Date().getTime() - posts.get(2).getTime();
+ return diff > 20 * 1000;
+ }
+ else
+ {
return true;
}
- long diff = new Date().getTime() - posts.get(2).getTime();
- return diff > 20 * 1000;
}
}
Added:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/TimerStartup.java
===================================================================
---
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/TimerStartup.java
(rev 0)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/TimerStartup.java 2010-06-19
17:36:41 UTC (rev 6555)
@@ -0,0 +1,21 @@
+package org.jboss.weld.examples.pastecode.session;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Singleton;
+import javax.ejb.Startup;
+import javax.inject.Inject;
+
+@Startup @Singleton
+public class TimerStartup
+{
+
+ @Inject
+ private CodeFragmentPrinter codeFragmentPrinter;
+
+ @PostConstruct
+ public void startup()
+ {
+ codeFragmentPrinter.startTimer();
+ }
+
+}
Property changes on:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/TimerStartup.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Modified: examples/trunk/jsf/pastecode/src/main/webapp/home.xhtml
===================================================================
--- examples/trunk/jsf/pastecode/src/main/webapp/home.xhtml 2010-06-19 07:21:35 UTC (rev
6554)
+++ examples/trunk/jsf/pastecode/src/main/webapp/home.xhtml 2010-06-19 17:36:41 UTC (rev
6555)
@@ -5,51 +5,46 @@
xmlns:f="http://java.sun.com/jsf/core"
xmlns:s="http://jboss.com/products/seam/taglib">
- <ui:composition template="template.xhtml">
- <ui:define name="viewMetadata">
- <f:metadata>
- <f:event type="preRenderView"
listener="#{database.populate}"/>
- </f:metadata>
- </ui:define>
+ <ui:composition template="template.xhtml">
- <ui:define name="mainarea">
+ <ui:define name="mainarea">
- <div class="contentHeader">Post a new PasteCode <h:messages
style="color: red" /></div>
+ <div class="contentHeader">Post a new PasteCode <h:messages
style="color: red" /></div>
- <h:inputTextarea id="text" styleClass="pastecode"
value="#{pasteWindow.codeFragment.text}"/>
+ <h:inputTextarea id="text" styleClass="pastecode"
value="#{pasteWindow.codeFragment.text}"/>
- <div class="formRow">
- <h:outputLabel for="language" value="Syntax
highlighting"/>
- <h:selectOneMenu id="language"
value="#{pasteWindow.codeFragment.language}">
- <f:selectItems value="#{languages}" var="language"
itemLabel="#{language.name}" itemValue="#{language}" />
- </h:selectOneMenu>
- </div>
+ <div class="formRow">
+ <h:outputLabel for="language" value="Syntax
highlighting"/>
+ <h:selectOneMenu id="language"
value="#{pasteWindow.codeFragment.language}">
+ <f:selectItems value="#{languages}" var="language"
itemLabel="#{language.name}" itemValue="#{language}" />
+ </h:selectOneMenu>
+ </div>
- <div class="formRow">
- <h:outputLabel for="exposure" value="Exposure"/>
- <h:selectOneMenu id="exposure"
value="#{pasteWindow.privateFragment}">
- <f:selectItem itemValue="#{true}"
itemLabel="Private"/>
- <f:selectItem itemValue="#{false}"
itemLabel="Public"/>
- </h:selectOneMenu>
- </div>
+ <div class="formRow">
+ <h:outputLabel for="exposure" value="Exposure"/>
+ <h:selectOneMenu id="exposure"
value="#{pasteWindow.privateFragment}">
+ <f:selectItem itemValue="#{true}"
itemLabel="Private"/>
+ <f:selectItem itemValue="#{false}"
itemLabel="Public"/>
+ </h:selectOneMenu>
+ </div>
- <div class="formRow">
- <h:outputLabel for="user" value="Name / title"/>
- <h:inputText id="user" maxlength="30" size="30"
value="#{pasteWindow.codeFragment.user}"/>
- </div>
+ <div class="formRow">
+ <h:outputLabel for="user" value="Name / title"/>
+ <h:inputText id="user" maxlength="30"
size="30" value="#{pasteWindow.codeFragment.user}"/>
+ </div>
- <div class="formButtons">
- <h:commandButton action="#{pasteWindow.send}" id="send"
image="img/submit.png"/>
- </div>
+ <div class="formButtons">
+ <h:commandButton action="#{pasteWindow.send}"
id="send" image="img/submit.png"/>
+ </div>
- <br style="clear:both"/>
+ <br style="clear:both"/>
- </ui:define>
+ </ui:define>
- <ui:define name="rightmenu">
- <ui:include src="rightMenuDefault.xhtml"/>
- </ui:define>
+ <ui:define name="rightmenu">
+ <ui:include src="rightMenuDefault.xhtml"/>
+ </ui:define>
- </ui:composition>
+ </ui:composition>
</html>