[jboss-svn-commits] JBL Code SVN: r5311 - in labs/shotoku/trunk: shotoku-base/src/java/org/jboss/shotoku/service shotoku-feeds/src/etc/META-INF shotoku-feeds/src/java/org/jboss/shotoku/feeds shotoku-feeds/src/java/org/jboss/shotoku/feeds/service shotoku-svn/src/java/org/jboss/shotoku/svn shotoku-svn/src/java/org/jboss/shotoku/svn/service shotoku-tags/src/etc/META-INF shotoku-tags/src/java/org/jboss/shotoku/tags shotoku-tags/src/java/org/jboss/shotoku/tags/service

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jul 27 11:33:47 EDT 2006


Author: adamw
Date: 2006-07-27 11:33:41 -0400 (Thu, 27 Jul 2006)
New Revision: 5311

Removed:
   labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadData.java
   labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadDataEmpty.java
   labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadDataEntityManager.java
Modified:
   labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/AdministratedService.java
   labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/AdministratedServiceImpl.java
   labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/ShotokuService.java
   labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/ShotokuServiceImpl.java
   labs/shotoku/trunk/shotoku-feeds/src/etc/META-INF/persistence.xml
   labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsService.java
   labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java
   labs/shotoku/trunk/shotoku-svn/src/java/org/jboss/shotoku/svn/SvnService.java
   labs/shotoku/trunk/shotoku-svn/src/java/org/jboss/shotoku/svn/service/SvnServiceImpl.java
   labs/shotoku/trunk/shotoku-tags/src/etc/META-INF/persistence.xml
   labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/TagService.java
   labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java
Log:
EntityManager fix

Modified: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/AdministratedService.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/AdministratedService.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/AdministratedService.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -7,7 +7,7 @@
  * ShotokuAdmin web application and are periodically updated.
  * @author Adam Warski (adamw at aster.pl)
  */
-public interface AdministratedService<T> {
+public interface AdministratedService {
     public long getLastUpdate();
     public Date getLastUpdateDate();
     public void setLastUpdate(long lastUpdate);
@@ -19,7 +19,7 @@
     public String getServiceName();
     public String getServiceDescription();
 
-    public void update(final UpdateThreadData<T> data);
+    public void update();
 
     public AdministratedService getServiceInstance();
 

Modified: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/AdministratedServiceImpl.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/AdministratedServiceImpl.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/AdministratedServiceImpl.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -9,8 +9,8 @@
 /**
  * @author Adam Warski (adamw at aster.pl)
  */
-public abstract class AdministratedServiceImpl<T>
-        implements AdministratedService<T> {
+public abstract class AdministratedServiceImpl
+        implements AdministratedService {
     private static final Logger log =
             Logger.getLogger(AdministratedService.class);
 
@@ -86,18 +86,12 @@
     }
 
     public void startUpdateThread() {
-        startUpdateThread(new UpdateThreadDataEmpty<T>());
-    }
-
-    public void startUpdateThread(final UpdateThreadData<T> data) {
         new Thread() {
             {
                 setDaemon(true);
             }
 
             public void run() {
-                data.create();
-
                 while (getServiceRunnable()) {
                     try {
                         sleep(getTimerInterval());
@@ -106,7 +100,7 @@
                     }
 
                     try {
-                        update(data);
+                        update();
                     } catch (Throwable t) {
                         // Making sure that an exception won't stop the thread.
                         log.error("Update method threw an exception.", t);
@@ -115,8 +109,6 @@
                     setLastUpdate(Calendar.getInstance().getTimeInMillis());
                 }
 
-                data.destroy();
-
                 log.info(getServiceName() + " deaemon thread terminated.");
             }
         }.start();

Modified: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/ShotokuService.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/ShotokuService.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/ShotokuService.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -29,7 +29,7 @@
  * 
  * @author Adam Warski (adamw at aster.pl)
  */
-public interface ShotokuService extends AdministratedService<Object> {
+public interface ShotokuService extends AdministratedService {
     /*
      * CACHE
      */

Modified: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/ShotokuServiceImpl.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/ShotokuServiceImpl.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/ShotokuServiceImpl.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -44,7 +44,7 @@
 @Service(objectName=Constants.SHOTOKU_SERVICE_NAME)
 @Local(ShotokuServiceLocal.class)
 @Management(ShotokuService.class)
-public class ShotokuServiceImpl extends AdministratedServiceImpl<Object>
+public class ShotokuServiceImpl extends AdministratedServiceImpl
         implements ShotokuService, ShotokuServiceLocal {
     Logger log = Logger.getLogger(AdministratedService.class);
 
@@ -129,7 +129,7 @@
      * Update function.
      */
 
-    public void update(UpdateThreadData<Object> data) {
+    public void update() {
         for (ShotokuCacheItem sci : cacheItems) {
             try {
                 sci.update();

Deleted: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadData.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadData.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadData.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -1,10 +0,0 @@
-package org.jboss.shotoku.service;
-
-/**
- * @author Adam Warski (adamw at aster.pl)
- */
-public interface UpdateThreadData<T> {
-    public void create();
-    public void destroy();
-    public T get();
-}

Deleted: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadDataEmpty.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadDataEmpty.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadDataEmpty.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -1,18 +0,0 @@
-package org.jboss.shotoku.service;
-
-/**
- * @author Adam Warski (adamw at aster.pl)
- */
-public class UpdateThreadDataEmpty<T> implements UpdateThreadData<T> {
-    public void create() {
-
-    }
-
-    public void destroy() {
-
-    }
-
-    public T get() {
-        return null;
-    }
-}

Deleted: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadDataEntityManager.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadDataEntityManager.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/service/UpdateThreadDataEntityManager.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -1,29 +0,0 @@
-package org.jboss.shotoku.service;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-
-/**
- * @author Adam Warski (adamw at aster.pl)
- */
-public class UpdateThreadDataEntityManager
-        implements UpdateThreadData<EntityManager> {
-    private EntityManager updaterThreadEm;
-    private EntityManagerFactory emf;
-
-    public UpdateThreadDataEntityManager(EntityManagerFactory emf) {
-        this.emf = emf;
-    }
-
-    public void create() {
-        updaterThreadEm = emf.createEntityManager();
-    }
-
-    public void destroy() {
-        updaterThreadEm.close();
-    }
-
-    public EntityManager get() {
-        return updaterThreadEm;
-    }
-}

Modified: labs/shotoku/trunk/shotoku-feeds/src/etc/META-INF/persistence.xml
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/etc/META-INF/persistence.xml	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-feeds/src/etc/META-INF/persistence.xml	2006-07-27 15:33:41 UTC (rev 5311)
@@ -7,15 +7,4 @@
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
         </properties>
     </persistence-unit>
-
-    <persistence-unit name="feeds-noauto">
-        <jta-data-source>java:/LabsDS</jta-data-source>
-        <properties>
-            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
-        </properties>
-
-        <properties>
-            <property name="jboss.entity.manager.factory.jndi.name" value="java:/FeedsEMF"/>
-        </properties>
-    </persistence-unit>
 </persistence>

Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsService.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsService.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsService.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -6,12 +6,10 @@
 import org.jboss.shotoku.feeds.comments.CommentsNotAvialableException;
 import org.jboss.shotoku.feeds.comments.UnauthorizedToCommentException;
 
-import javax.persistence.EntityManager;
-
 /**
  * @author Adam Warski (adamw at aster.pl)
  */
-public interface FeedsService extends AdministratedService<EntityManager> {
+public interface FeedsService extends AdministratedService {
     public Feed getFeed(String id, String name, String type);
 
     public void addComment(String feedId, String feedName, String feedElement,

Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -16,14 +16,12 @@
 import org.jboss.shotoku.feeds.tools.FeedsTools;
 import org.jboss.shotoku.service.AdministratedServiceImpl;
 import org.jboss.shotoku.service.AdministratedService;
-import org.jboss.shotoku.service.UpdateThreadData;
-import org.jboss.shotoku.service.UpdateThreadDataEntityManager;
 import org.jboss.shotoku.ContentManager;
 import org.jboss.shotoku.Node;
 import org.jboss.shotoku.Directory;
-import org.jboss.shotoku.exceptions.ResourceDoesNotExist;
 import org.jboss.shotoku.exceptions.ResourceAlreadyExists;
 import org.jboss.shotoku.exceptions.NameFormatException;
+import org.jboss.shotoku.exceptions.ResourceDoesNotExist;
 import org.jboss.shotoku.user.UserNotFoundException;
 import org.jboss.shotoku.user.tools.UserConstants;
 import org.jboss.shotoku.tags.tools.UserTools;
@@ -32,10 +30,11 @@
 import org.apache.log4j.Logger;
 
 import javax.ejb.Local;
-import javax.persistence.PersistenceContext;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnit;
 import javax.naming.InitialContext;
+import javax.transaction.UserTransaction;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.List;
 import java.util.ArrayList;
@@ -47,7 +46,7 @@
 @Local(FeedsServiceLocal.class)
 @Management(FeedsService.class)
 @Depends({UserConstants.USER_SERVICE_NAME,Constants.SHOTOKU_SERVICE_NAME})
-public class FeedsServiceImpl extends AdministratedServiceImpl<EntityManager>
+public class FeedsServiceImpl extends AdministratedServiceImpl
         implements FeedsService, FeedsServiceLocal {
     private static final Logger log = Logger.getLogger(AdministratedService.class);
 
@@ -63,8 +62,8 @@
     public static final Object synchronizer = new Object();
 
     @SuppressWarnings({"UNUSED_SYMBOL"})
-    @PersistenceContext(unitName="feeds")
-    private EntityManager manager;
+    @PersistenceUnit
+    private EntityManagerFactory emf;
 
     private FeedsDescriptor getFeedsDescriptor(String id) {
         if (!fds.contains(id)) {
@@ -124,8 +123,13 @@
         }
 
         // Saving the comment for later addition to Shotoku.
-        manager.persist(new CommentEntity(feedId, feedName, feedType,
-                feedElement, username, title, content));
+        EntityManager em = emf.createEntityManager();
+        try {
+            em.persist(new CommentEntity(feedId, feedName, feedType,
+                    feedElement, username, title, content));
+        } finally {
+            em.close();
+        }
     }
 
     /*
@@ -157,98 +161,128 @@
         return createCommentNode(commentDir, 0);
     }
 
-    public void update(UpdateThreadData<EntityManager> data) {
-        //System.out.println("UPDATE OF FEEDS ");
-        // Adding any (possible) comments.
-        //noinspection unchecked
-        List<CommentEntity> result = data.get().createQuery(
-                "FROM CommentEntity").getResultList();
+    private void removeCommentEntity(EntityManager em, CommentEntity ce)
+            throws Throwable {
+        UserTransaction tran = null;
+        try {
+            InitialContext jndiCntx = new InitialContext();
+            tran = (UserTransaction) jndiCntx.lookup("UserTransaction");
 
-        List<CommentEntity> failedComments = new ArrayList<CommentEntity>();
-        for (CommentEntity ce : result) {
-            try {
-                CommentableFeed feed = getFeedsDescriptor(ce.getFeedId()).
-                        getCommentableFeed(ce.getFeedName(), ce.getFeedType());
+            tran.begin();
+            em.joinTransaction();
+            em.remove(ce);
+            tran.commit();
+        } catch (Throwable t) {
+            if (tran != null) {
+                tran.rollback();
+            }
 
-                // Checking if the feed exists.
-                if (feed == null || !feed.getCommentsEnabled()) {
-                    failedComments.add(ce);
-                    continue;
-                }
+            throw t;
+        }
+    }
 
-                Node commentedElement = feed.getNodeForFeedElement(
-                        ce.getFeedElement());
+    public void update() {
+        // Adding any (possible) comments.
+        EntityManager updateThreadEm = emf.createEntityManager();
 
-                // Checking if the commented element exists.
-                if (commentedElement == null) {
-                    failedComments.add(ce);
-                    continue;
-                }
+        try {
+            //noinspection unchecked
+            List<CommentEntity> result = updateThreadEm.createQuery(
+                    "FROM CommentEntity").getResultList();
 
-                // Adding the comment.
-                Directory root = commentedElement.getParent();
-                String commentDirName = commentedElement.getName() +
-                        "_comments";
+            List<CommentEntity> failedComments = new ArrayList<CommentEntity>();
+            for (CommentEntity ce : result) {
+                try {
+                    CommentableFeed feed = getFeedsDescriptor(ce.getFeedId()).
+                            getCommentableFeed(ce.getFeedName(), ce.getFeedType());
 
-                // Getting/ creating the comment directory.
-                Directory commentDir;
-                try {
-                    commentDir = root.getDirectory(commentDirName);
-                } catch (ResourceDoesNotExist e) {
-                    try {
-                        commentDir = root.newDirectory(commentDirName);
-                    } catch (ResourceAlreadyExists e1) {
-                        log.warn("A node which does exist and doesn't.", e1);
+                    // Checking if the feed exists.
+                    if (feed == null || !feed.getCommentsEnabled()) {
                         failedComments.add(ce);
                         continue;
                     }
+
+                    Node commentedElement = feed.getNodeForFeedElement(
+                            ce.getFeedElement());
+
+                    // Checking if the commented element exists.
+                    if (commentedElement == null) {
+                        failedComments.add(ce);
+                        continue;
+                    }
+
+                    // Adding the comment.
+                    Directory root = commentedElement.getParent();
+                    String commentDirName = commentedElement.getName() +
+                            "_comments";
+
+                    // Getting/ creating the comment directory.
+                    Directory commentDir;
+                    try {
+                        commentDir = root.getDirectory(commentDirName);
+                    } catch (ResourceDoesNotExist e) {
+                        try {
+                            commentDir = root.newDirectory(commentDirName);
+                        } catch (ResourceAlreadyExists e1) {
+                            log.warn("A node which does exist and doesn't.", e1);
+                            failedComments.add(ce);
+                            continue;
+                        }
+                    }
+
+                    // Creating the comment node.
+                    Node commentNode = createCommentNode(commentDir);
+                    commentNode.setContent(ce.getContent());
+                    commentNode.setProperty("title", ce.getTitle());
+                    commentNode.setProperty("author", ce.getUsername());
+                    if (feed.getCommentsModerated()) {
+                        commentNode.setProperty("accepted", "0");
+                    }
+
+                    // Saving.
+                    commentNode.getContentManager().save("Adding a comment",
+                            commentNode, commentDir);
+                } catch (Throwable t) {
+                    log.warn("Exception when trying to add a comment.", t);
+                    failedComments.add(ce);
                 }
 
-                // Creating the comment node.
-                Node commentNode = createCommentNode(commentDir);
-                commentNode.setContent(ce.getContent());
-                commentNode.setProperty("title", ce.getTitle());
-                commentNode.setProperty("author", ce.getUsername());
-                if (feed.getCommentsModerated()) {
-                    commentNode.setProperty("accepted", "0");
+                try {
+                    removeCommentEntity(updateThreadEm, ce);
+                } catch (Throwable t) {
+                    log.warn("Exception when trying to remove a comment entity.", t);
                 }
-
-                // Saving.
-                commentNode.getContentManager().save("Adding a comment",
-                        commentNode, commentDir);
-            } catch (Throwable t) {
-                log.warn("Exception when trying to add a comment.", t);
-                failedComments.add(ce);
             }
 
-            data.get().remove(ce);
-        }
+            // Logging information about comments, which addition failed.
+            for (CommentEntity ce : failedComments) {
+                log.warn("Failed to add comment: " +
+                        ce.getFeedId() + " :: " + ce.getFeedName() + " :: " +
+                        ce.getFeedType() + " :: " + ce.getFeedElement() + " :: " +
+                        ce.getTitle() + " :: " + ce.getContent() + " :: " +
+                        ce.getUsername());
 
-        // Logging information about comments, which addition failed.
-        for (CommentEntity ce : failedComments) {
-            log.warn("Failed to add comment: " +
-                    ce.getFeedId() + " :: " + ce.getFeedName() + " :: " +
-                    ce.getFeedType() + " :: " + ce.getFeedElement() + " :: " +
-                    ce.getTitle() + " :: " + ce.getContent() + " :: " +
-                    ce.getUsername());
+                try {
+                    removeCommentEntity(updateThreadEm, ce);
+                } catch (Throwable t) {
+                    log.warn("Exception when trying to remove a comment entity.", t);
+                }
+            }
 
-            data.get().remove(ce);
+            // Updating descriptors.
+            for (String id : fds.keySet()) {
+                FeedsDescriptor fd = fds.get(id);
+                fds.put(id, new FeedsDescriptor(id, conf));
+                fd.release();
+            }
+        } finally {
+            updateThreadEm.close();
         }
-
-        // Updating descriptors.
-        for (String id : fds.keySet()) {
-            FeedsDescriptor fd = fds.get(id);
-            fds.put(id, new FeedsDescriptor(id, conf));
-            fd.release();
-        }
     }
 
     /*
      * Service management.
      */
-
-    private EntityManagerFactory emf;
-
     public void create() throws Exception {
         super.create();
 
@@ -256,22 +290,17 @@
                 FeedsConstants.PROPERTY_INTERVAL, 10000));
         fds = new ConcurrentHashMap<String, FeedsDescriptor>();
 
-        InitialContext ctx = new InitialContext();
-        emf = (EntityManagerFactory) ctx.lookup("java:/FeedsEMF");
-
         super.afterCreate();
     }
 
     public void start() throws Exception {
         super.start();
-        startUpdateThread(new UpdateThreadDataEntityManager(emf));
+        startUpdateThread();
         super.afterStart();
     }
 
     public void destroy() {
-        super.destroy();
-        emf.close();
-        afterDestroy();
+
     }
 
     /*

Modified: labs/shotoku/trunk/shotoku-svn/src/java/org/jboss/shotoku/svn/SvnService.java
===================================================================
--- labs/shotoku/trunk/shotoku-svn/src/java/org/jboss/shotoku/svn/SvnService.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-svn/src/java/org/jboss/shotoku/svn/SvnService.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -31,7 +31,7 @@
  * @author Adam Warski (adamw at aster.pl)
  * @author Damon Sicore (damon at sicore.com)
  */
-public interface SvnService extends AdministratedService<Object> {
+public interface SvnService extends AdministratedService {
     /**
      * Registers a repository in the service. Has an effect only if the
      * repository wasn't earlier registered.

Modified: labs/shotoku/trunk/shotoku-svn/src/java/org/jboss/shotoku/svn/service/SvnServiceImpl.java
===================================================================
--- labs/shotoku/trunk/shotoku-svn/src/java/org/jboss/shotoku/svn/service/SvnServiceImpl.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-svn/src/java/org/jboss/shotoku/svn/service/SvnServiceImpl.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -39,7 +39,6 @@
 import org.jboss.shotoku.tools.Constants;
 import org.jboss.shotoku.service.AdministratedServiceImpl;
 import org.jboss.shotoku.service.AdministratedService;
-import org.jboss.shotoku.service.UpdateThreadData;
 import org.jboss.shotoku.ContentManager;
 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
 import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
@@ -52,7 +51,7 @@
 @Local(SvnServiceLocal.class)
 @Management(SvnService.class)
 @Depends(Constants.SHOTOKU_SERVICE_NAME)
-public class SvnServiceImpl extends AdministratedServiceImpl<Object>
+public class SvnServiceImpl extends AdministratedServiceImpl
            implements SvnService, SvnServiceLocal {
     private static final Logger log = Logger.getLogger(AdministratedService.class);
 
@@ -107,7 +106,7 @@
         return SvnTools.getService();
     }
 
-    public void update(UpdateThreadData<Object> data) {
+    public void update() {
         log.debug("Starting SVN update.");
         for (SvnRepository repo : repositories.values()) {
             try {

Modified: labs/shotoku/trunk/shotoku-tags/src/etc/META-INF/persistence.xml
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/etc/META-INF/persistence.xml	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-tags/src/etc/META-INF/persistence.xml	2006-07-27 15:33:41 UTC (rev 5311)
@@ -8,7 +8,7 @@
         </properties>
     </persistence-unit>
 
-    <persistence-unit name="tags-noauto">
+    <!--<persistence-unit name="tags-noauto">
         <jta-data-source>java:/LabsDS</jta-data-source>
         <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
@@ -17,5 +17,5 @@
         <properties>
             <property name="jboss.entity.manager.factory.jndi.name" value="java:/TagsEMF"/>
         </properties>
-    </persistence-unit>
+    </persistence-unit>-->
 </persistence>

Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/TagService.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/TagService.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/TagService.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -6,13 +6,12 @@
 import org.jboss.shotoku.tags.exceptions.TagGetException;
 import org.jboss.shotoku.tags.tools.FeedType;
 
-import javax.persistence.EntityManager;
 import java.util.List;
 
 /**
  * @author Adam Warski (adamw at aster.pl)
  */
-public interface TagService extends AdministratedService<EntityManager>  {
+public interface TagService extends AdministratedService  {
     public void addTag(Tag t) throws TagAddException;
     public void deleteTag(Tag t) throws TagDeleteException;
 

Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java	2006-07-27 15:05:50 UTC (rev 5310)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java	2006-07-27 15:33:41 UTC (rev 5311)
@@ -28,6 +28,8 @@
 import javax.ejb.Local;
 import javax.persistence.*;
 import javax.naming.InitialContext;
+import javax.transaction.UserTransaction;
+import javax.transaction.SystemException;
 
 import org.apache.log4j.Logger;
 import org.jboss.annotation.ejb.Management;
@@ -36,8 +38,6 @@
 import org.jboss.shotoku.tools.Constants;
 import org.jboss.shotoku.service.AdministratedServiceImpl;
 import org.jboss.shotoku.service.AdministratedService;
-import org.jboss.shotoku.service.UpdateThreadData;
-import org.jboss.shotoku.service.UpdateThreadDataEntityManager;
 import org.jboss.shotoku.tags.*;
 import org.jboss.shotoku.tags.tools.TagTools;
 import org.jboss.shotoku.tags.tools.FeedType;
@@ -59,13 +59,15 @@
 @Local(TagServiceLocal.class)
 @Management(TagService.class)
 @Depends(Constants.SHOTOKU_SERVICE_NAME)
-public class TagServiceImpl extends AdministratedServiceImpl<EntityManager>
+public class TagServiceImpl extends AdministratedServiceImpl
         implements TagService, TagServiceLocal {
     private static final Logger log = Logger.getLogger(AdministratedService.class);
 
     private boolean syncOn;
     private ContentManager cm;
 
+    @SuppressWarnings({"UNUSED_SYMBOL"})
+    @PersistenceUnit
     private EntityManagerFactory emf;
 
     /*
@@ -89,73 +91,93 @@
                             org.jboss.shotoku.tags.tools.Constants.PROPERTY_CMDIR, ""));
         }
 
-        InitialContext ctx = new InitialContext();
-        emf = (EntityManagerFactory) ctx.lookup("java:/TagsEMF");
-
         super.afterCreate();
     }
 
     public void start() throws Exception {
         super.start();
-        startUpdateThread(new UpdateThreadDataEntityManager(emf));
+        startUpdateThread();
         log.info("Tag service started with update interval: "
                 + getTimerInterval());
     }
 
     public void destroy() {
-        super.destroy();
-        emf.close();
-        afterDestroy();
+
     }
 
     /*
       * Timer-handling functions.
       */
 
-    public void update(UpdateThreadData<EntityManager> data) {
+    public void update() {
         if (!syncOn) {
             // Synchronization is turned off.
             return;
         }
 
-        // Performing synchronization (tags -> shotoku).
-        // noinspection unchecked
-        List<TagEntity> result = data.get()
-                .createQuery("FROM TagEntity WHERE synced = 0 OR synced IS NULL")
-                .getResultList();
+        EntityManager em = emf.createEntityManager();
 
-        if (result.size() == 0) {
-            return;
-        }
+        try {
+            // Performing synchronization (tags -> shotoku).
+            // noinspection unchecked
+            List<TagEntity> result = em.createQuery("FROM TagEntity " +
+                    "WHERE synced = 0 OR synced IS NULL")
+                    .getResultList();
 
-        Set<Resource> toSave = new HashSet<Resource>();
-        for (TagEntity te : result) {
-            if (org.jboss.shotoku.tags.tools.Constants.SHOTOKU_TAG.equals
-                    (te.getType())) {
-                try {
-                    Node n = cm.getNode(te.getResourceId());
-                    n.setProperty(te.getShotokuPropReprName(),
-                            te.getShotokuPropReprValue());
-                    toSave.add(n);
-                } catch (ResourceDoesNotExist e) {
-                    log.warn("Unable to synchronize tag with Shotoku resource, " +
-                            "the tagged resource does not exist.", e);
+            if (result.size() == 0) {
+                return;
+            }
+
+            Set<Resource> toSave = new HashSet<Resource>();
+            for (TagEntity te : result) {
+                if (org.jboss.shotoku.tags.tools.Constants.SHOTOKU_TAG.equals
+                        (te.getType())) {
+                    try {
+                        Node n = cm.getNode(te.getResourceId());
+                        n.setProperty(te.getShotokuPropReprName(),
+                                te.getShotokuPropReprValue());
+                        toSave.add(n);
+                    } catch (ResourceDoesNotExist e) {
+                        log.warn("Unable to synchronize tag with Shotoku resource, " +
+                                "the tagged resource does not exist.", e);
+                    }
                 }
             }
-        }
 
-        try {
-            cm.save("", toSave);
-        } catch (SaveException e) {
-            log.warn(e);
-            return;
-        }
+            try {
+                cm.save("", toSave);
+            } catch (SaveException e) {
+                log.warn(e);
+                return;
+            }
 
-        data.get().getTransaction().begin();
-        for (TagEntity te : result) {
-            te.setSynced(true);
+            UserTransaction tran = null;
+            try {
+                InitialContext jndiCntx = new InitialContext();
+                tran = (UserTransaction) jndiCntx.lookup("UserTransaction");
+
+                tran.begin();
+                em.joinTransaction();
+
+                for (TagEntity te : result) {
+                    te.setSynced(true);
+                }
+
+                tran.commit();
+            } catch (Throwable t) {
+                if (tran != null) {
+                    try {
+                        tran.rollback();
+                    } catch (SystemException e) {
+                        // Oh well ...
+                    }
+                }
+
+                log.warn("Unable to synchronize tags.", t);
+            }
+        } finally {
+            em.close();
         }
-        data.get().getTransaction().commit();
     }
 
     public String getServiceId() {
@@ -175,13 +197,9 @@
     }
 
     /*
-      * TagService implementation.
-      */
+     * TagService implementation.
+     */
 
-    @SuppressWarnings({"UNUSED_SYMBOL"})
-    @PersistenceContext(unitName="tags")
-    private EntityManager manager;
-
     /**
      * MIN_SIMILARITY - minimal similarity for checking relatedTags needed
      */
@@ -201,18 +219,24 @@
     }
 
     public void addTag(Tag t) throws TagAddException {
+        EntityManager manager = emf.createEntityManager();
         try {
             manager.persist(getTagEntity(t).normalizeName());
         } catch (Throwable e) {
             throw new TagAddException(e);
+        } finally {
+            manager.close();
         }
     }
 
     public void deleteTag(Tag t) throws TagDeleteException {
+        EntityManager manager = emf.createEntityManager();
         try {
             manager.remove(getTagEntity(t));
         } catch (Throwable e) {
             throw new TagDeleteException(e);
+        } finally {
+            manager.close();
         }
     }
 
@@ -225,6 +249,7 @@
     }
 
     public List<Tag> getTags(String resourceId) throws TagGetException {
+        EntityManager manager = emf.createEntityManager();
         try {
             // noinspection unchecked
             List<TagEntity> result = manager
@@ -240,10 +265,13 @@
             return ret;
         } catch (Throwable e) {
             throw new TagGetException(e);
+        } finally {
+            manager.close();
         }
     }
 
     public Tag getTag(String tagName) throws TagGetException {
+        EntityManager manager = emf.createEntityManager();
         try {
             // noinspection unchecked
             TagEntity result = (TagEntity) manager.createQuery(
@@ -257,10 +285,13 @@
             return result.getTag();
         } catch (Throwable e) {
             throw new TagGetException(e);
+        } finally {
+            manager.close();
         }
     }
 
     public List<Tag> getTags(List<String> tagNames) throws TagGetException {
+        EntityManager manager = emf.createEntityManager();
         try {
             StringBuffer querySb = new StringBuffer("FROM TagEntity WHERE ");
             int i = 0;
@@ -293,10 +324,13 @@
             return ret;
         } catch (Throwable e) {
             throw new TagGetException(e);
+        } finally {
+            manager.close();
         }
     }
 
     public List<Tag> getUniqueTags(List<String> tagNames) throws TagGetException {
+        EntityManager manager = emf.createEntityManager();
         try {
             StringBuffer querySb = new StringBuffer(
                     "SELECT name, resourceId, min(dateCreated) " +
@@ -336,6 +370,8 @@
             return ret;
         } catch (Throwable e) {
             throw new TagGetException(e);
+        } finally {
+            manager.close();
         }
     }
 
@@ -345,34 +381,39 @@
 
         Map<String, List<Tag>> otherResources = new HashMap<String, List<Tag>>();
 
-        for (Tag relatedTag : relateTo) {
-            List<TagEntity> result = manager.createQuery(
-                    "from TagEntity where name = :name").setParameter("name",
-                    relatedTag.getName()).getResultList();
+        EntityManager manager = emf.createEntityManager();
+        try {
+            for (Tag relatedTag : relateTo) {
+                List<TagEntity> result = manager.createQuery(
+                        "from TagEntity where name = :name").setParameter("name",
+                        relatedTag.getName()).getResultList();
 
-            for (TagEntity otherTag : result) {
-                if (!otherResources.containsKey(otherTag.getResourceId())) {
-                    otherResources.put(otherTag.getResourceId(),
-                            getTags(otherTag.getResourceId()));
+                for (TagEntity otherTag : result) {
+                    if (!otherResources.containsKey(otherTag.getResourceId())) {
+                        otherResources.put(otherTag.getResourceId(),
+                                getTags(otherTag.getResourceId()));
+                    }
                 }
             }
-        }
 
-        for (List<Tag> tagList : otherResources.values()) {
-            if (checkSimilarity(relateTo, tagList) >= MIN_SIMILARITY) {
-                ret.addAll(tagList);
+            for (List<Tag> tagList : otherResources.values()) {
+                if (checkSimilarity(relateTo, tagList) >= MIN_SIMILARITY) {
+                    ret.addAll(tagList);
+                }
             }
-        }
 
-        // don't return "relateTo" members
-        List<Tag> endRet = new ArrayList<Tag>(ret);
-        for (Tag tag : ret) {
-            if (tagListContainsTag(tag, relateTo)) {
-                endRet.remove(tag);
+            // don't return "relateTo" members
+            List<Tag> endRet = new ArrayList<Tag>(ret);
+            for (Tag tag : ret) {
+                if (tagListContainsTag(tag, relateTo)) {
+                    endRet.remove(tag);
+                }
             }
+
+            return endRet;
+        } finally {
+            manager.close();
         }
-
-        return endRet;
     }
 
     private boolean tagListContainsTag(Tag tag, List<Tag> listToCheck) {
@@ -398,6 +439,7 @@
     }
 
     public List<Tag> getTagsByAuthor(String author) throws TagGetException {
+        EntityManager manager = emf.createEntityManager();
         try {
             // noinspection unchecked
             List<TagEntity> result = manager
@@ -413,11 +455,14 @@
             return ret;
         } catch (Throwable e) {
             throw new TagGetException(e);
+        } finally {
+            manager.close();
         }
     }
 
     public List<Tag> getUniqueTagsByAuthor(String author)
             throws TagGetException {
+        EntityManager manager = emf.createEntityManager();
         try {
             // noinspection unchecked
             List<Object[]> result = manager
@@ -439,11 +484,14 @@
             return ret;
         } catch (Throwable e) {
             throw new TagGetException(e);
+        } finally {
+            manager.close();
         }
     }
 
     public List<Tag> getTags(String name, String author)
             throws TagGetException {
+        EntityManager manager = emf.createEntityManager();
         try {
             // noinspection unchecked
             List<TagEntity> result = (List<TagEntity>) manager.createQuery(
@@ -463,11 +511,14 @@
             return ret;
         } catch (Throwable e) {
             throw new TagGetException(e);
+        } finally {
+            manager.close();
         }
     }
 
     public List<Tag> getUniqueTags(String resourceId)
             throws TagGetException {
+        EntityManager manager = emf.createEntityManager();
         try {
             // noinspection unchecked
             List<Object[]> result = manager
@@ -489,6 +540,8 @@
             return ret;
         } catch (Throwable e) {
             throw new TagGetException(e);
+        } finally {
+            manager.close();
         }
     }
 
@@ -521,22 +574,32 @@
     }
 
     public List<String> getAllTagsNames() throws TagGetException {
-        //noinspection unchecked
-        return (List<String>) manager.createQuery("SELECT DISTINCT name FROM TagEntity")
-                .getResultList();
+        EntityManager manager = emf.createEntityManager();
+        try {
+            //noinspection unchecked
+            return (List<String>) manager.createQuery("SELECT DISTINCT name FROM TagEntity")
+                    .getResultList();
+        } finally {
+            manager.close();
+        }
     }
 
     public List<Tag> getAllTags() throws TagGetException {
-        List<Tag> result = new ArrayList<Tag>();
-        //noinspection unchecked
-        List<TagEntity> tagEnitites = manager.createQuery("from TagEntity")
-                .getResultList();
+        EntityManager manager = emf.createEntityManager();
+        try {
+            List<Tag> result = new ArrayList<Tag>();
+            //noinspection unchecked
+            List<TagEntity> tagEnitites = manager.createQuery("from TagEntity")
+                    .getResultList();
 
-        for (TagEntity tagEnt : tagEnitites) {
-            result.add(tagEnt.getTag());
+            for (TagEntity tagEnt : tagEnitites) {
+                result.add(tagEnt.getTag());
+            }
+
+            return result;
+        } finally {
+            manager.close();
         }
-
-        return result;
     }
 
     public List<Tag> searchTags(String tag, String author, String keyword) {
@@ -562,73 +625,88 @@
     }
 
     public int getFeedHits(FeedType feedType, String data) {
-        HitsEntity ce = manager.find(HitsEntity.class,
-                new HitsIdEntity(data, feedType.toString()));
+        EntityManager manager = emf.createEntityManager();
+        try {
+            HitsEntity ce = manager.find(HitsEntity.class,
+                    new HitsIdEntity(data, feedType.toString()));
 
-        return ce == null ? 0 : ce.getCount();
+            return ce == null ? 0 : ce.getCount();
+        } finally {
+            manager.close();
+        }
     }
 
     public int getFeedVisits(FeedType feedType, String data) {
-        VisitsEntity ve = manager.find(VisitsEntity.class,
-                new VisitsIdEntity(data, feedType.toString()));
+        EntityManager manager = emf.createEntityManager();
+        try {
+            VisitsEntity ve = manager.find(VisitsEntity.class,
+                    new VisitsIdEntity(data, feedType.toString()));
 
-        return ve == null ? 0 : ve.getCount();
+            return ve == null ? 0 : ve.getCount();
+        } finally {
+            manager.close();
+        }
     }
 
     public void increaseFeedCounters(FeedType feedType, String data,
                                      String ipAddress) {
-        // 1. Increasing the hit counter.
-        HitsEntity ce = manager.find(HitsEntity.class,
-                new HitsIdEntity(data, feedType.toString()));
+        EntityManager manager = emf.createEntityManager();
+        try {
+            // 1. Increasing the hit counter.
+            HitsEntity ce = manager.find(HitsEntity.class,
+                    new HitsIdEntity(data, feedType.toString()));
 
-        if (ce == null) {
-            manager.persist(new HitsEntity(
-                    new HitsIdEntity(data, feedType.toString()), 1));
-        } else {
-            ce.setCount(ce.getCount()+1);
-            manager.flush();
-        }
+            if (ce == null) {
+                manager.persist(new HitsEntity(
+                        new HitsIdEntity(data, feedType.toString()), 1));
+            } else {
+                ce.setCount(ce.getCount()+1);
+                manager.flush();
+            }
 
-        // 2. Increasing the visits counter.
-        VisitsIpsEntity dbVie = manager.find(VisitsIpsEntity.class,
-                new VisitsIpsIdEntity(data, feedType.toString(), ipAddress));
+            // 2. Increasing the visits counter.
+            VisitsIpsEntity dbVie = manager.find(VisitsIpsEntity.class,
+                    new VisitsIpsIdEntity(data, feedType.toString(), ipAddress));
 
-        if (dbVie == null) {
-            // Storing the new ip address.
-            try {
-                manager.persist(new VisitsIpsEntity(data,
-                        feedType.toString(), ipAddress));
-            } catch (Exception e) {
-                // Somebody must have already added it.
-                return;
-            }
+            if (dbVie == null) {
+                // Storing the new ip address.
+                try {
+                    manager.persist(new VisitsIpsEntity(data,
+                            feedType.toString(), ipAddress));
+                } catch (Exception e) {
+                    // Somebody must have already added it.
+                    return;
+                }
 
-            // Increasing the counter.
-            VisitsEntity ve = manager.find(VisitsEntity.class,
-                    new VisitsIdEntity(data, feedType.toString()));
+                // Increasing the counter.
+                VisitsEntity ve = manager.find(VisitsEntity.class,
+                        new VisitsIdEntity(data, feedType.toString()));
 
-            if (ve == null) {
-                manager.persist(new VisitsEntity(
-                        new VisitsIdEntity(data, feedType.toString()), 1));
-            } else {
-                ve.setCount(ve.getCount()+1);
-                manager.flush();
+                if (ve == null) {
+                    manager.persist(new VisitsEntity(
+                            new VisitsIdEntity(data, feedType.toString()), 1));
+                } else {
+                    ve.setCount(ve.getCount()+1);
+                    manager.flush();
+                }
             }
+        } finally {
+            manager.close();
         }
     }
 
-	public Integer getAllSubscribers(String author) throws TagGetException {
-		Integer visits = 0;
-		
-		List<Tag> tagsByAuthor = getTagsByAuthor(author);
-		
-		for (Tag tag : tagsByAuthor) {
-			visits += getFeedVisits(FeedType.AUTHOR_TAG, tag.getName()+"+"+tag.getAuthor());
-		}
-		
-		visits += getFeedVisits(FeedType.AUTHOR_ALL, author);
-		visits += getFeedVisits(FeedType.AUTHOR_UNIQUE, author);
-		
-		return visits;
-	}
+    public Integer getAllSubscribers(String author) throws TagGetException {
+        Integer visits = 0;
+
+        List<Tag> tagsByAuthor = getTagsByAuthor(author);
+
+        for (Tag tag : tagsByAuthor) {
+            visits += getFeedVisits(FeedType.AUTHOR_TAG, tag.getName()+"+"+tag.getAuthor());
+        }
+
+        visits += getFeedVisits(FeedType.AUTHOR_ALL, author);
+        visits += getFeedVisits(FeedType.AUTHOR_UNIQUE, author);
+
+        return visits;
+    }
 }




More information about the jboss-svn-commits mailing list