[weld-commits] Weld SVN: r5974 - in examples/trunk/jsf/pastecode: ejb and 4 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Tue Mar 2 09:11:51 EST 2010


Author: mgencur at redhat.com
Date: 2010-03-02 09:11:49 -0500 (Tue, 02 Mar 2010)
New Revision: 5974

Added:
   examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/model/CodeEntity.java
   examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/model/LargeCodeLog.java
   examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/Code.java
   examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/CodeBean.java
   examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/LargeCodeDecorator.java
Modified:
   examples/trunk/jsf/pastecode/ejb/pom.xml
   examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/HashComputer.java
   examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/History.java
   examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/HistoryBean.java
   examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/Paster.java
   examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabaseBean.java
   examples/trunk/jsf/pastecode/ejb/src/main/resources/META-INF/beans.xml
   examples/trunk/jsf/pastecode/readme.txt
   examples/trunk/jsf/pastecode/war/src/main/java/org/jboss/weld/examples/pastecode/DownloadServlet.java
Log:
added decorator feature and minor naming fixes

Modified: examples/trunk/jsf/pastecode/ejb/pom.xml
===================================================================
--- examples/trunk/jsf/pastecode/ejb/pom.xml	2010-03-02 13:24:32 UTC (rev 5973)
+++ examples/trunk/jsf/pastecode/ejb/pom.xml	2010-03-02 14:11:49 UTC (rev 5974)
@@ -46,6 +46,13 @@
          <scope>provided</scope>
       </dependency>
       
+      <dependency>
+	    <groupId>javax.transaction</groupId>
+	    <artifactId>jta</artifactId>
+	    <version>1.1</version>
+	    <scope>provided</scope>
+	  </dependency>
+      
    </dependencies>
    
    <build>

Added: examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/model/CodeEntity.java
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/model/CodeEntity.java	                        (rev 0)
+++ examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/model/CodeEntity.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -0,0 +1,144 @@
+/*
+ * 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.model;
+
+import java.io.Serializable;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.GenerationType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Column;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import javax.persistence.Lob;
+import java.util.Date;
+
+/**
+ * The persistent class for the code database table.
+ * 
+ */
+ at Entity
+ at Table(name = "code")
+public class CodeEntity implements Serializable, Cloneable
+{
+   private static final long serialVersionUID = 1L;
+   private int id;
+   private Date datetime;
+   private String language;
+   private String note;
+   private String text;
+   private String user;
+   private String hash;
+
+   @Id
+   @GeneratedValue(strategy = GenerationType.IDENTITY)
+   @Column(name = "id")
+   public int getId()
+   {
+      return id;
+   }
+
+   public void setId(int id)
+   {
+      this.id = id;
+   }
+
+   @Column(name = "hash")
+   public String getHash()
+   {
+      return hash;
+   }
+
+   public void setHash(String hash)
+   {
+      this.hash = hash;
+   }
+
+   public CodeEntity()
+   {
+      this.language = "";
+      this.note = "";
+      this.text = "";
+      this.user = "";
+      this.hash = null;
+      this.datetime = null;
+   }
+ 
+   @Temporal(TemporalType.TIMESTAMP)
+   @Column(name = "datetime")
+   public Date getDatetime()
+   {
+      return this.datetime;
+   }
+
+   public void setDatetime(Date datetime)
+   {
+      this.datetime = datetime;
+   }
+
+   @Column(name = "language")
+   public String getLanguage()
+   {
+      return this.language;
+   }
+
+   public void setLanguage(String language)
+   {
+      this.language = language;
+   }
+
+   @Lob()
+   @Column(name = "note")
+   public String getNote()
+   {
+      return this.note;
+   }
+
+   public void setNote(String note)
+   {
+      this.note = note;
+   }
+
+   @Lob()
+   @Column(name = "text")
+   public String getText()
+   {
+      return this.text;
+   }
+
+   public void setText(String text)
+   {
+      this.text = text;
+   }
+
+   @Column(name = "user")
+   public String getUser()
+   {
+      return this.user;
+   }
+
+   public void setUser(String user)
+   {
+      this.user = user;
+   }
+}
\ No newline at end of file

Added: examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/model/LargeCodeLog.java
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/model/LargeCodeLog.java	                        (rev 0)
+++ examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/model/LargeCodeLog.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -0,0 +1,97 @@
+/*
+ * 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.model;
+
+import java.io.Serializable;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.GenerationType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Column;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import java.util.Date;
+
+ at Entity
+ at Table(name = "largecodelog")
+public class LargeCodeLog implements Serializable
+{
+   private static final long serialVersionUID = 1L;
+   private int id;
+   private Date datetime;
+   private int codeId;   
+   private String access;
+   
+   @Id
+   @GeneratedValue(strategy = GenerationType.IDENTITY)
+   @Column(name = "id")
+   public int getId()
+   {
+      return id;
+   }
+
+   public void setId(int id)
+   {
+      this.id = id;
+   }
+
+   public LargeCodeLog(int codeId, Date dateTime, String access)
+   {
+      this.codeId = codeId;
+      this.datetime = dateTime;
+      this.access = access;
+   }
+
+   public int getCodeId()
+   {
+      return codeId;
+   }
+
+   public void setCodeId(int codeId)
+   {
+      this.codeId = codeId;
+   }
+
+   public String getAccess()
+   {
+      return access;
+   }
+
+   public void setAccess(String access)
+   {
+      this.access = access;
+   }
+
+   @Temporal(TemporalType.TIMESTAMP)
+   @Column(name = "datetime")
+   public Date getDatetime()
+   {
+      return this.datetime;
+   }
+
+   public void setDatetime(Date datetime)
+   {
+      this.datetime = datetime;
+   }
+
+}

Added: examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/Code.java
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/Code.java	                        (rev 0)
+++ examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/Code.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -0,0 +1,38 @@
+/*
+ * 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.ejb.Local;
+import java.util.List;
+import org.jboss.weld.examples.pastecode.model.CodeEntity;
+
+ at Local
+public interface Code
+{
+   public String addCode(CodeEntity code, boolean secured);
+
+   public CodeEntity getCode(String id);
+
+   public List<CodeEntity> recentCodes();
+
+   public List<CodeEntity> searchCodes(CodeEntity code, int page, QueryInfo info);
+}

Added: examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/CodeBean.java
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/CodeBean.java	                        (rev 0)
+++ examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/CodeBean.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -0,0 +1,211 @@
+/*
+ * 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.security.NoSuchAlgorithmException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import javax.annotation.Resource;
+import javax.ejb.EJBContext;
+import javax.ejb.EJBException;
+import javax.ejb.Stateless;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.persistence.*;
+import org.jboss.weld.examples.pastecode.model.CodeEntity;
+import javax.inject.*;
+import java.util.*;
+
+/**
+ * Session Bean implementation class CodeEAOBean
+ */
+ at Stateless
+ at Named("codeEAOBean")
+public class CodeBean implements Code
+{
+
+   @PersistenceContext(unitName = "pastecodeDatabase")
+   private EntityManager em;
+
+   @Resource
+   EJBContext ctx;
+
+   @Inject
+   private HashComputer hashComp;
+
+   private int MAX_CODES = 7;
+
+   private int PAGE_SIZE = 2;
+
+   public CodeBean()
+   {
+   }
+
+   public String addCode(CodeEntity code, boolean secured)
+   {
+      String result;
+      if (code.getDatetime() == null)
+      {
+         code.setDatetime(Calendar.getInstance().getTime());
+      }
+
+      if (code.getUser().trim().isEmpty())
+      {
+         code.setUser("Anonymous");
+      }
+
+      /* compute hash value and return it if secured flag has been set */
+      if (secured)
+      {
+         try
+         {
+            String hashValue = hashComp.getHashValue(code);
+            code.setHash(hashValue);
+            result = hashValue;
+            em.persist(code);
+         }
+         catch (NoSuchAlgorithmException e)
+         {
+            e.printStackTrace();
+            return null;
+         }
+      }
+      else
+      {
+         em.persist(code);
+         result = new Integer(code.getId()).toString();
+      }
+
+      // System.out.println("Result: " + result);
+
+      return result;
+   }
+
+   public CodeEntity getCode(String id)
+   {
+      boolean secured = true;
+
+      try
+      {
+         Integer.parseInt(id);
+         secured = false; /*
+                           * if it is possible to convert to number -> not
+                           * secured, otherwise -> secured
+                           */
+      }
+      catch (NumberFormatException e)
+      {
+      }
+
+      if (secured)
+      {
+         Query q = em.createQuery("SELECT c FROM CodeEntity c WHERE hash = :hash");
+         q.setParameter("hash", id);
+         return (CodeEntity) q.getSingleResult();
+      }
+      else
+      {
+         CodeEntity c = em.find(CodeEntity.class, Integer.parseInt(id));
+         /*
+          * if somebody is trying to guess Id of secured code paste he cannot
+          * pass
+          */
+         if (c.getHash() == null)
+         {
+            return c;
+         }
+         else
+         {
+            throw new EJBException("Access denied");
+         }
+      }
+   }
+
+   public List<CodeEntity> recentCodes()
+   {
+      Query q = em.createQuery("SELECT c FROM CodeEntity c WHERE hash=null ORDER BY datetime DESC ");
+      q.setMaxResults(MAX_CODES);
+      List<CodeEntity> codes = q.getResultList();
+      return codes;
+   }
+
+   /**
+    * getting codes from database needs new transaction so that we can further
+    * modify returned Codes without affecting database (when we call this
+    * function from another session bean
+    */
+   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+   public List<CodeEntity> searchCodes(CodeEntity code, int page, QueryInfo info)
+   {
+      StringBuilder sb = new StringBuilder();
+
+      String delim = "";
+      if (!code.getUser().trim().equals(""))
+      {
+         sb.append("c.user = \'" + code.getUser().trim().toLowerCase() + "\'");
+         delim = " AND";
+      }
+      if (!code.getLanguage().trim().equals(""))
+      {
+         sb.append(delim).append(" c.language = \'" + code.getLanguage().trim().toLowerCase() + "\'");
+         delim = " AND";
+      }
+      if (!code.getNote().trim().equals(""))
+      {
+         sb.append(delim).append(" c.note LIKE \'%" + code.getNote().trim().toLowerCase() + "%\'");
+         delim = " AND";
+      }
+      if (!code.getText().trim().equals(""))
+      {
+         sb.append(delim).append(" c.text LIKE \'%" + code.getText().toLowerCase() + "%\'");
+         delim = " AND";
+      }
+      if (code.getDatetime() != null)
+      {
+         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
+         Date date2 = new Date();
+         date2.setTime(code.getDatetime().getTime() + 24 * 60 * 60 * 1000); // +1
+         // day
+
+         String formattedDate1 = formatter.format(code.getDatetime());
+         String formattedDate2 = formatter.format(date2);
+
+         sb.append(delim).append(" c.datetime between \'" + formattedDate1 + "\' and \'" + formattedDate2 + "\'");
+         delim = " AND";
+      }
+
+      if (sb.toString().length() == 0)
+         sb.append("1 = \'1\'");
+
+      Query q = em.createQuery("SELECT c FROM CodeEntity c WHERE hash=null AND " + sb.toString() + " ORDER BY datetime DESC");
+      int allRecords = q.getResultList().size();
+      q.setFirstResult(page * PAGE_SIZE);
+      q.setMaxResults(PAGE_SIZE);
+      List<CodeEntity> codes = q.getResultList();
+
+      info.setPage(page);
+      info.setRecordsCount(allRecords);
+      info.setPagesCount(allRecords / PAGE_SIZE);
+
+      return codes;
+   }
+}

Modified: examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/HashComputer.java
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/HashComputer.java	2010-03-02 13:24:32 UTC (rev 5973)
+++ examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/HashComputer.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -21,7 +21,7 @@
  */
 package org.jboss.weld.examples.pastecode.session;
 
-import org.jboss.weld.examples.pastecode.model.Code;
+import org.jboss.weld.examples.pastecode.model.CodeEntity;
 import java.security.NoSuchAlgorithmException;
 import java.security.MessageDigest;
 
@@ -31,7 +31,7 @@
    {
    }
 
-   public String getHashValue(Code code) throws NoSuchAlgorithmException
+   public String getHashValue(CodeEntity code) throws NoSuchAlgorithmException
    {
       String hashValue;
       MessageDigest md = MessageDigest.getInstance("SHA-1");

Modified: examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/History.java
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/History.java	2010-03-02 13:24:32 UTC (rev 5973)
+++ examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/History.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -23,18 +23,18 @@
 
 import java.util.List;
 import javax.ejb.Local;
-import org.jboss.weld.examples.pastecode.model.Code;
+import org.jboss.weld.examples.pastecode.model.CodeEntity;
 
 @Local
 public interface History
 {
-   public List<Code> getCodes();
+   public List<CodeEntity> getCodes();
 
-   public void setCodes(List<Code> codes);
+   public void setCodes(List<CodeEntity> codes);
 
-   public Code getSearchItem();
+   public CodeEntity getSearchItem();
 
-   public void setSearchItem(Code searchItem);
+   public void setSearchItem(CodeEntity searchItem);
 
    public String search();
 

Modified: examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/HistoryBean.java
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/HistoryBean.java	2010-03-02 13:24:32 UTC (rev 5973)
+++ examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/HistoryBean.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -24,7 +24,7 @@
 import java.util.List;
 import javax.ejb.Stateful;
 import javax.annotation.PostConstruct;
-import org.jboss.weld.examples.pastecode.model.Code;
+import org.jboss.weld.examples.pastecode.model.CodeEntity;
 import javax.inject.Named;
 import javax.inject.Inject;
 import java.io.Serializable;
@@ -44,15 +44,15 @@
    private static final long serialVersionUID = 20L;
 
    transient @Inject
-   CodeEAO eao;
+   Code eao;
 
    private QueryInfo info;
 
-   private List<Code> codes;
+   private List<CodeEntity> codes;
 
    private int TRIMMED_TEXT_LEN = 120;
 
-   private Code searchItem;
+   private CodeEntity searchItem;
 
    private int page = 0;
 
@@ -63,28 +63,28 @@
    @PostConstruct
    public void initialize()
    {
-      this.searchItem = new Code();
+      this.searchItem = new CodeEntity();
       // this.info = new QueryInfo();
    }
 
-   public List<Code> getCodes()
+   public List<CodeEntity> getCodes()
    {
       return this.codes;
    }
 
-   public void setCodes(List<Code> codes)
+   public void setCodes(List<CodeEntity> codes)
    {
       this.codes = codes;
    }
 
    @Produces
    @Named("searchItem")
-   public Code getSearchItem()
+   public CodeEntity getSearchItem()
    {
       return searchItem;
    }
 
-   public void setSearchItem(Code searchItem)
+   public void setSearchItem(CodeEntity searchItem)
    {
       this.searchItem = searchItem;
    }

Added: examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/LargeCodeDecorator.java
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/LargeCodeDecorator.java	                        (rev 0)
+++ examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/LargeCodeDecorator.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -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.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 org.jboss.weld.examples.pastecode.model.CodeEntity;
+import org.jboss.weld.examples.pastecode.model.LargeCodeLog;
+import javax.transaction.UserTransaction;
+
+/**
+ * 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
+ *  
+ */
+ at Decorator
+ at TransactionManagement(TransactionManagementType.BEAN)
+public abstract class LargeCodeDecorator implements Code
+{
+   /* injecting Delagation point - mandatory */
+   @Inject @Delegate @Any Code eao; 
+
+   @PersistenceContext(unitName = "pastecodeDatabase")
+   private EntityManager em;
+   
+   @Resource UserTransaction ut;
+   
+   private long LARGE_CODE = 65536;
+   
+   public String addCode(CodeEntity code, boolean secured)
+   {
+      
+      String codeId = eao.addCode(code, secured);
+      
+      if (code.getText().length() > LARGE_CODE)
+      {
+         try
+         {
+            ut.begin();
+            em.joinTransaction();
+            em.persist(new LargeCodeLog(code.getId(), code.getDatetime(), "w")); //writing large code
+            ut.commit();
+         }
+         catch(Exception e)
+         {
+            e.printStackTrace();
+            try
+            {
+               ut.rollback();
+            }
+            catch (Exception ex)
+            {               
+            }
+         }
+      }
+      
+      return codeId;
+   }
+
+   public CodeEntity getCode(String id)
+   {
+      CodeEntity code = eao.getCode(id);
+      
+      if (code.getText().length() > LARGE_CODE)
+      {
+         try
+         {
+            ut.begin();
+            em.joinTransaction();
+            em.persist(new LargeCodeLog(code.getId(), new Date(), "r")); //reading large code
+            ut.commit();
+         }
+         catch(Exception e)
+         {
+            e.printStackTrace();
+            try
+            {
+               ut.rollback();
+            }
+            catch (Exception ex)
+            {               
+            }
+         }
+      }
+      
+      return code;
+   }
+}

Modified: examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/Paster.java
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/Paster.java	2010-03-02 13:24:32 UTC (rev 5973)
+++ examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/Paster.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -31,7 +31,7 @@
 @Model
 public class Paster
 {
-   private Code code;
+   private CodeEntity code;
 
    private String codeId;
 
@@ -41,11 +41,9 @@
 
    private boolean secured = false;
 
-   @Inject
-   DataBean data;
+   @Inject DataBean data;
 
-   transient @Inject
-   CodeEAO eao;
+   transient @Inject Code eao;
 
    public Paster()
    {
@@ -54,7 +52,7 @@
    @PostConstruct
    public void postConstruct()
    {
-      this.code = new Code();
+      this.code = new CodeEntity();
       this.theme = "shThemeDefault.css";
    }
 
@@ -67,7 +65,7 @@
    /* used for access from jsf page */
    @Produces
    @Named("code")
-   public Code getPasterCodeInstance()
+   public CodeEntity getPasterCodeInstance()
    {
       return this.code;
    }
@@ -82,7 +80,7 @@
       this.brush = data.getBrush(this.code.getLanguage());
    }
 
-   public List<Code> getCodes()
+   public List<CodeEntity> getCodes()
    {
       return eao.recentCodes();
    }

Modified: examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabaseBean.java
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabaseBean.java	2010-03-02 13:24:32 UTC (rev 5973)
+++ examples/trunk/jsf/pastecode/ejb/src/main/java/org/jboss/weld/examples/pastecode/session/PopulateDatabaseBean.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -31,7 +31,7 @@
 import java.text.SimpleDateFormat;
 import java.util.StringTokenizer;
 import javax.inject.Inject;
-import org.jboss.weld.examples.pastecode.model.Code;
+import org.jboss.weld.examples.pastecode.model.CodeEntity;
 import com.sun.org.apache.xml.internal.serialize.LineSeparator;
 
 /**
@@ -47,8 +47,8 @@
 {
 
    private @Inject
-   Instance<CodeEAO> eaoIn;
-   private CodeEAO eao;
+   Instance<Code> eaoIn;
+   private Code eao;
    private boolean secured = false;
    private static final String file = "data.sql";
    private boolean populated = false;
@@ -74,7 +74,7 @@
 
          while (st.countTokens() > 1)
          {
-            Code c = new Code();
+            CodeEntity c = new CodeEntity();
             st.nextToken();
             c.setDatetime(formatter.parse(st.nextToken()));
             st.nextToken();

Modified: examples/trunk/jsf/pastecode/ejb/src/main/resources/META-INF/beans.xml
===================================================================
--- examples/trunk/jsf/pastecode/ejb/src/main/resources/META-INF/beans.xml	2010-03-02 13:24:32 UTC (rev 5973)
+++ examples/trunk/jsf/pastecode/ejb/src/main/resources/META-INF/beans.xml	2010-03-02 14:11:49 UTC (rev 5974)
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   The contents of this file is permitted to be empty.
+   The schema definition is provided for your convenience.
+-->
+<beans xmlns="http://java.sun.com/xml/ns/javaee"
+   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+   xsi:schemaLocation="
+      http://java.sun.com/xml/ns/javaee 
+      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
+      
+      <decorators>
+      	<class>org.jboss.weld.examples.pastecode.session.LargeCodeDecorator</class>
+      </decorators>
+      
+</beans>

Modified: examples/trunk/jsf/pastecode/readme.txt
===================================================================
--- examples/trunk/jsf/pastecode/readme.txt	2010-03-02 13:24:32 UTC (rev 5973)
+++ examples/trunk/jsf/pastecode/readme.txt	2010-03-02 14:11:49 UTC (rev 5974)
@@ -11,6 +11,7 @@
 - injecting into POJO, EJB (SFSB), Servlet
 - @ApplicationScoped, @Model, @SessionScoped annotations
 - producer named methods
+- Decorators
 
 This example uses a Maven 2 build. Execute the following command to build the
 EAR. The EAR will be located in the target directory after completion of

Modified: examples/trunk/jsf/pastecode/war/src/main/java/org/jboss/weld/examples/pastecode/DownloadServlet.java
===================================================================
--- examples/trunk/jsf/pastecode/war/src/main/java/org/jboss/weld/examples/pastecode/DownloadServlet.java	2010-03-02 13:24:32 UTC (rev 5973)
+++ examples/trunk/jsf/pastecode/war/src/main/java/org/jboss/weld/examples/pastecode/DownloadServlet.java	2010-03-02 14:11:49 UTC (rev 5974)
@@ -27,8 +27,8 @@
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import org.jboss.weld.examples.pastecode.model.Code;
-import org.jboss.weld.examples.pastecode.session.CodeEAO;
+import org.jboss.weld.examples.pastecode.model.CodeEntity;
+import org.jboss.weld.examples.pastecode.session.Code;
 import javax.enterprise.inject.Instance;
 import javax.inject.Inject;
 
@@ -37,8 +37,8 @@
    private static final long serialVersionUID = 1L;
 
    @Inject
-   Instance<CodeEAO> eaoIn;
-   CodeEAO eao;
+   Instance<Code> eaoIn;
+   Code eao;
 
    public DownloadServlet()
    {
@@ -49,7 +49,7 @@
 
       this.eao = eaoIn.get();
       String id = (String) request.getParameter("id");
-      Code c = eao.getCode(id);
+      CodeEntity c = eao.getCode(id);
       String fileName = c.getUser() + "." + c.getLanguage();
       String txt = c.getText();
 



More information about the weld-commits mailing list