Author: pete.muir(a)jboss.org
Date: 2010-06-16 23:16:43 -0400 (Wed, 16 Jun 2010)
New Revision: 6477
Added:
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/FloodingDecorator.java
Removed:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/LargeCodeDecorator.java
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabaseBean.java
Modified:
examples/trunk/jsf/pastecode/src/main/webapp/WEB-INF/beans.xml
Log:
change to FloodingDecorator
Copied:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/DatabasePopulater.java
(from rev 6470,
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabaseBean.java)
===================================================================
---
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/DatabasePopulater.java
(rev 0)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/DatabasePopulater.java 2010-06-17
03:16:43 UTC (rev 6477)
@@ -0,0 +1,123 @@
+/*
+ * 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.
+ *
+ */
+// TODO Make into an EJB Singleton which executes at startup
+@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;
+
+ private boolean populated;
+
+ // TODO @PostConstruct
+ public synchronized void populate()
+ {
+ if (populated)
+ {
+ return;
+ }
+
+ 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();
+ c.setNote(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");
+ }
+
+ 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/FloodingDecorator.java
(from rev 6470,
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/LargeCodeDecorator.java)
===================================================================
---
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/FloodingDecorator.java
(rev 0)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/FloodingDecorator.java 2010-06-17
03:16:43 UTC (rev 6477)
@@ -0,0 +1,52 @@
+/*
+ * 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 javax.decorator.Decorator;
+import javax.decorator.Delegate;
+import javax.inject.Inject;
+
+import org.jboss.weld.examples.pastecode.model.CodeFragment;
+
+@Decorator
+public abstract class FloodingDecorator implements CodeFragmentManager
+{
+ @Inject @Delegate
+ private CodeFragmentManager codeFragmentManager;
+
+ @Inject
+ private PostTracker postTracker;
+
+ public String addCodeFragment(CodeFragment code, boolean privateFragment)
+ {
+ if (postTracker.isNewPostAllowed())
+ {
+ postTracker.addPost();
+ return codeFragmentManager.addCodeFragment(code, privateFragment);
+ }
+ else
+ {
+ throw new IllegalStateException("You've posted more than 2 fragments in
the last 20s. No flooding allowed!");
+ }
+ }
+
+}
Deleted:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/LargeCodeDecorator.java
===================================================================
---
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/LargeCodeDecorator.java 2010-06-17
02:53:26 UTC (rev 6476)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/LargeCodeDecorator.java 2010-06-17
03:16:43 UTC (rev 6477)
@@ -1,122 +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.util.Date;
-
-import javax.annotation.Resource;
-import javax.decorator.Decorator;
-import javax.decorator.Delegate;
-import javax.ejb.TransactionManagement;
-import javax.ejb.TransactionManagementType;
-import javax.enterprise.inject.Any;
-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.AccessLog;
-
-/**
- * This Decorator performs logging of information about large
- * transactions. A transaction is large if the pasted code has
- * more than 65kB. When such a transaction is performed the
- * following pieces of information are being recorded:
- * - id of the transaction,
- * - time of the transaction
- * - "w" - if it was written, "r" - if it was read
- *
- */
-@Decorator
-(a)TransactionManagement(TransactionManagementType.BEAN)
-public abstract class LargeCodeDecorator implements CodeFragmentManager
-{
- /* injecting Delagation point - mandatory */
- @Inject @Delegate @Any CodeFragmentManager eao;
-
- @PersistenceContext(unitName = "pastecodeDatabase")
- private EntityManager em;
-
- @Resource UserTransaction ut;
-
- private long LARGE_CODE = 65536;
-
- public String addCodeFragment(CodeFragment code, boolean secured)
- {
-
- String codeId = eao.addCodeFragment(code, secured);
-
- if (code.getText().length() > LARGE_CODE)
- {
- try
- {
- ut.begin();
- em.joinTransaction();
- em.persist(new AccessLog(code, code.getDatetime(), "w")); //writing
large code
- ut.commit();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- try
- {
- ut.rollback();
- }
- catch (Exception ex)
- {
- }
- }
- }
-
- return codeId;
- }
-
- public CodeFragment getCodeFragment(String id)
- {
- CodeFragment code = eao.getCodeFragment(id);
-
- if (code.getText().length() > LARGE_CODE)
- {
- try
- {
- ut.begin();
- em.joinTransaction();
- em.persist(new AccessLog(code, new Date(), "r")); //reading large
code
- ut.commit();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- try
- {
- ut.rollback();
- }
- catch (Exception ex)
- {
- }
- }
- }
-
- return code;
- }
-}
Deleted:
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabaseBean.java
===================================================================
---
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabaseBean.java 2010-06-17
02:53:26 UTC (rev 6476)
+++
examples/trunk/jsf/pastecode/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabaseBean.java 2010-06-17
03:16:43 UTC (rev 6477)
@@ -1,118 +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.
- *
- */
-// TODO Make into an EJB Singleton which executes at startup
-@ApplicationScoped
-@Named("database")
-//TODO @Singleton @Startup
-public class PopulateDatabaseBean
-{
-
- @Inject Logger log;
-
- private static final String DATA_FILE_NAME = "data.sql";
-
- @Inject
- private CodeFragmentManager codeFragmentManager;
-
- private boolean populated;
-
- // TODO @PostConstruct
- public synchronized void populate()
- {
- if (populated)
- {
- return;
- }
-
- 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();
- c.setNote(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");
- }
-
- log.info("Successfully imported data!");
- populated = true;
- }
-
- 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();
- }
-}
Modified: examples/trunk/jsf/pastecode/src/main/webapp/WEB-INF/beans.xml
===================================================================
--- examples/trunk/jsf/pastecode/src/main/webapp/WEB-INF/beans.xml 2010-06-17 02:53:26 UTC
(rev 6476)
+++ examples/trunk/jsf/pastecode/src/main/webapp/WEB-INF/beans.xml 2010-06-17 03:16:43 UTC
(rev 6477)
@@ -10,7 +10,7 @@
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<decorators>
-
<class>org.jboss.weld.examples.pastecode.session.LargeCodeDecorator</class>
+
<class>org.jboss.weld.examples.pastecode.session.FloodingDecorator</class>
</decorators>
</beans>