[jboss-svn-commits] JBL Code SVN: r11200 - in labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3: counters and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Apr 21 19:05:56 EDT 2007
Author: unibrew
Date: 2007-04-21 19:05:56 -0400 (Sat, 21 Apr 2007)
New Revision: 11200
Added:
labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/
labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersEntity.java
labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersMDB.java
labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersService.java
labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersServiceBean.java
Log:
[JBLAB] I repaired counters service.
Added: labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersEntity.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersEntity.java (rev 0)
+++ labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersEntity.java 2007-04-21 23:05:56 UTC (rev 11200)
@@ -0,0 +1,129 @@
+
+ /*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * 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.forge.ejb3.counters;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import org.jboss.forge.common.projects.Sorting;
+
+/**
+ *
+ * @author Ryszard Kozmik
+ *
+ * EJB3 Entity which is used to persist in database all download counters for projects.
+ */
+ at Entity
+ at Table(name = "jblab_counters")
+public class CountersEntity
+{
+
+ private String path;
+ private long counterValue;
+ private String order;
+ private String projectId;
+ private boolean visible;
+
+
+ /**
+ * Default constructor for CountersEntity sets its attributes to default values.
+ */
+ public CountersEntity ()
+ {
+ this.path = "";
+ this.order = Sorting.RANDOM.toString();
+ this.projectId="";
+ this.visible = true;
+ }
+
+ /**
+ * Main constructor for CountersEntity which takes all possible values to set all its attributes.
+ */
+ public CountersEntity (String path, long initValue, String projectId, boolean visible, String order)
+ {
+ this.path = path;
+ this.counterValue=initValue;
+ this.projectId = projectId;
+ this.visible = visible;
+ this.order = order;
+ }
+
+ @Id
+ @Column(name = "path")
+ public String getPath()
+ {
+ return path;
+ }
+
+ public void setPath(String path)
+ {
+ this.path = path;
+ }
+
+ @Column(name = "counter")
+ public long getCounterValue()
+ {
+ return counterValue;
+ }
+
+ public void setCounterValue(long cv)
+ {
+ this.counterValue = cv;
+ }
+
+ public void setOrder(String order)
+ {
+ this.order = order;
+ }
+
+ @Column(name = "sortorder")
+ public String getOrder()
+ {
+ return order;
+ }
+
+ public void setProjectId(String projectId)
+ {
+ this.projectId = projectId;
+ }
+
+ @Column(name = "projectid")
+ public String getProjectId()
+ {
+ return projectId;
+ }
+
+ public void setVisible(boolean visible)
+ {
+ this.visible = visible;
+ }
+
+ @Column(name= "visible")
+ public boolean isVisible()
+ {
+ return visible;
+ }
+}
Added: labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersMDB.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersMDB.java (rev 0)
+++ labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersMDB.java 2007-04-21 23:05:56 UTC (rev 11200)
@@ -0,0 +1,94 @@
+
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * 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.forge.ejb3.counters;
+//
+//import javax.ejb.ActivationConfigProperty;
+//import javax.ejb.MessageDriven;
+//
+//import javax.jms.Message;
+//import javax.jms.TextMessage;
+//
+//import javax.naming.InitialContext;
+//import javax.naming.NamingException;
+//
+//import org.jboss.forge.common.projects.CountersServiceInterface;
+//import org.jboss.logging.Logger;
+//
+//
+///**
+// * EJB3 Message Driven Bean class which is used for receiving messages from
+// * DownloadCounters. It is responsible for incrementing requested counter.
+// * @author Ryszard Kozmik
+// *
+// */
+//@MessageDriven(activationConfig =
+// { @ActivationConfigProperty(propertyName = "destinationType",
+// propertyValue = "javax.jms.Queue")
+// , @ActivationConfigProperty(propertyName = "destination",
+// propertyValue = "queue/jblab_counters")
+// }
+//)
+//public class CountersMDB
+//{
+//
+// private static Logger logger;
+//
+// /**
+// * Just default constructor which gets Logger instance for this class.
+// */
+// public CountersMDB()
+// {
+// if (logger == null)
+// {
+// // Initializing logger.
+// logger = Logger.getLogger(this.getClass());
+// }
+// }
+//
+// /**
+// * Method responsible for getting mesages sent from DownloadCounters.
+// * It gets message and increments a counter for given in message filepath.
+// * @param msg
+// */
+// public void onMessage(Message msg)
+// {
+// try
+// {
+// TextMessage txtMsg = (TextMessage) msg;
+// InitialContext ctx = new InitialContext();
+// CountersService countersService =
+// (CountersService) ctx.lookup(CountersServiceInterface.jndiName);
+// countersService.incrementCounter(txtMsg.getText());
+// }
+// catch (NamingException e)
+// {
+// logger.error("CountersService was not found while incrementing counter.");
+// }
+// catch (Exception e)
+// {
+// logger.error("Problem while processing message in MDB.", e);
+// }
+// }
+//
+//}
Added: labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersService.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersService.java (rev 0)
+++ labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersService.java 2007-04-21 23:05:56 UTC (rev 11200)
@@ -0,0 +1,37 @@
+
+ /*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * 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.forge.ejb3.counters;
+
+import javax.ejb.Local;
+
+import org.jboss.forge.common.projects.CountersServiceInterface;
+
+
+/**
+ * Local interface for CountersServiceBean class.
+ * @author Ryszard Kozmik
+ *
+ */
+ at Local
+public interface CountersService extends CountersServiceInterface {}
Added: labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersServiceBean.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersServiceBean.java (rev 0)
+++ labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersServiceBean.java 2007-04-21 23:05:56 UTC (rev 11200)
@@ -0,0 +1,125 @@
+
+ /*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * 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.forge.ejb3.counters;
+
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+
+import javax.ejb.Stateless;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+import org.jboss.annotation.ejb.LocalBinding;
+import org.jboss.forge.common.projects.Counter;
+import org.jboss.forge.common.projects.CountersServiceInterface;
+import org.jboss.forge.common.projects.Sorting;
+
+
+/**
+ *
+ * @author Ryszard Kozmik
+ *
+ * EJB3 Stateless Session Bean used as a service for managing DownloadCounters persistence.
+ */
+ at Stateless
+ at LocalBinding (jndiBinding=CountersServiceInterface.jndiName)
+public class CountersServiceBean implements CountersService
+{
+
+ // Registering with counters persistence context
+ @PersistenceContext (unitName="property_persistance")
+ protected EntityManager em;
+
+ /**
+ * This method gets all counters from database and returns a Map object with them.
+ * @return Map<String,Counter> containging all download counters from database.
+ */
+ public Map<String,Counter> getCountersMap()
+ {
+ List<CountersEntity> countersRows = em.createQuery("from CountersEntity e").getResultList();
+ Map<String,Counter> countersMap = new Hashtable<String,Counter>(countersRows.size());
+ for (CountersEntity counter : countersRows)
+ {
+ countersMap.put(counter.getPath(),
+ new Counter(counter.getCounterValue(),
+ counter.getProjectId(),
+ counter.isVisible(),
+ Sorting.valueOf(counter.getOrder())));
+ }
+ return countersMap;
+ }
+
+ /**
+ * Persists downloadCounters given in execution parameter to database.
+ * @param downloadCounters
+ * Map<String,Counter> containing all download counters for all projects.
+ */
+ public void updateCounters(Map<String,Counter> downloadCounters)
+ {
+ synchronized (downloadCounters) {
+ for (String path : downloadCounters.keySet())
+ {
+ Counter counter = downloadCounters.get(path);
+ CountersEntity ce = em.find(CountersEntity.class,path);
+ if (ce==null)
+ {
+ ce = new CountersEntity(path,counter.getValue(),counter.getProjectId(),counter.getVisible(),counter.getSorting().name());
+ em.persist(ce);
+ } else {
+ ce.setCounterValue(counter.getValue());
+ ce.setVisible(counter.getVisible());
+ ce.setOrder(counter.getSorting().name());
+ ce.setProjectId(counter.getProjectId());
+ }
+ }
+ List<String> pathsFromDB = em.createQuery("select path from CountersEntity e").getResultList();
+ for (String path : pathsFromDB)
+ {
+ if (!downloadCounters.containsKey(path))
+ {
+ em.remove(em.find(CountersEntity.class,path));
+ }
+ }
+
+ }
+ }
+
+ /**
+ * Method increments counter for specified in parameter file path.
+ * @param path
+ * Filepath for which counter needs to be increased.
+ */
+ public void incrementCounter (String path)
+ {
+ CountersEntity ce = em.find(CountersEntity.class,path);
+ if (ce==null)
+ {
+ return ;
+ }
+ ce.setCounterValue(ce.getCounterValue()+1);
+ em.persist(ce);
+ }
+
+}
More information about the jboss-svn-commits
mailing list