From: <jbo...@li...> - 2006-05-01 01:49:09
|
Author: unibrew Date: 2006-04-30 21:49:01 -0400 (Sun, 30 Apr 2006) New Revision: 4023 Added: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/CountersService.java labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDB.java Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/project.xml labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Counter.java labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDescriptor.java Log: [JBLAB-655] Adding new files for DownloadCounters in database and modifying old files a little Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/project.xml =================================================================== --- labs/jbosslabs/trunk/portal-extensions/forge-common/project.xml 2006-05-01 01:47:21 UTC (rev 4022) +++ labs/jbosslabs/trunk/portal-extensions/forge-common/project.xml 2006-05-01 01:49:01 UTC (rev 4023) @@ -73,6 +73,19 @@ <jar>portal-server-lib.jar</jar> </dependency> + <dependency> + <groupId>jboss</groupId> + <artifactId>ejb3-persistence</artifactId> + <version>3.0RC6</version> + <jar>ejb3-persistence.jar</jar> + </dependency> + <dependency> + <groupId>jboss</groupId> + <artifactId>jboss-ejb3x</artifactId> + <version>3.0RC6</version> + <jar>jboss-ejb3x.jar</jar> + </dependency> + <dependency> <groupId>jboss</groupId> <artifactId>activation</artifactId> Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Counter.java =================================================================== --- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Counter.java 2006-05-01 01:47:21 UTC (rev 4022) +++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Counter.java 2006-05-01 01:49:01 UTC (rev 4023) @@ -61,7 +61,7 @@ * @param projectId * Name of project for this counter. */ - Counter (long value,String projectId,Boolean visible,Sorting sorting) { + public Counter (long value,String projectId,Boolean visible,Sorting sorting) { this.value=value; this.projectId=projectId; this.visible = visible; Added: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/CountersService.java =================================================================== --- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/CountersService.java 2006-05-01 01:47:21 UTC (rev 4022) +++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/CountersService.java 2006-05-01 01:49:01 UTC (rev 4023) @@ -0,0 +1,46 @@ + + /* + * 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.common.projects; + +import java.util.Map; + +import javax.ejb.Local; + + +/** + * + * @author Ryszard Kozmik + * + */ +@Local +public interface CountersService +{ + + public void createCounterWithPath(String path); + + public Map<String,Counter> getCountersMap(); + + public void updateCounters(Map<String,Counter> downloadCounters); + +} Added: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDB.java =================================================================== --- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDB.java 2006-05-01 01:47:21 UTC (rev 4022) +++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDB.java 2006-05-01 01:49:01 UTC (rev 4023) @@ -0,0 +1,99 @@ + /* + * 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.common.projects; + +import java.util.Map; + +import javax.naming.InitialContext; + +import org.jboss.logging.Logger; +import org.jboss.shotoku.ContentManager; + + +/** + * DownloadCountersDB is a main class used for counting downloads by using EJB3 and database. + * It overrides persistence of DownloadCountersDescriptor class. + * + * @author Ryszard Kozmik + */ +public class DownloadCountersDB + extends DownloadCountersDescriptor +{ + + protected InitialContext ctx; + + public DownloadCountersDB (String portalName, ContentManager cm) + { + this.setContentManager(cm); + try + { + ctx = new InitialContext(); + CountersService countersService = (CountersService)ctx.lookup(CountersService.class.getName()); + downloadCounters=countersService.getCountersMap(); + + // Getting Map containing pairs of ProjectId and shotoku Node + // which refers to projects' download counter descriptor. + Map <String,org.jboss.shotoku.Node> descriptors = getDownloadDescriptors(portalName); + + // Synchronizing tracked links in downloadConters with links from + // projects download counter descriptors. + synchronizeCounters(descriptors); + + } catch (Exception e) + { + getLogger().error ("Failed to initialize downloadCounters.",e); + } + + } + + /** + * This method is overriding the method from DownloadCountersDescriptor and + * because of that name is missleading. It just executes proper method on + * CountersService to persist all counters in database. + * @param portalName + */ + public void synchronizeWithFile(String portalName) { + try { + CountersService countersService = (CountersService)ctx.lookup(CountersService.class.getName()); + countersService.updateCounters(downloadCounters); + } catch (Exception e) + { + getLogger().error("Failed to persist downloadCounters",e); + } + } + + /** + * Method simply just increments the counter value for + * given in parameter <link>. + * + * @param link + * Counter value for this <code>link</code> will be incremented. + */ + synchronized public void increment (final String link) { + if (downloadCounters!=null && downloadCounters.get(link)!=null) { + // Status change to inform about counters modification. + downloadCounters.get(link).increment(); + } + } + +} Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDescriptor.java =================================================================== --- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDescriptor.java 2006-05-01 01:47:21 UTC (rev 4022) +++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDescriptor.java 2006-05-01 01:49:01 UTC (rev 4023) @@ -34,6 +34,8 @@ import java.util.Set; import java.util.TreeMap; +import javax.naming.InitialContext; + import org.apache.xerces.parsers.DOMParser; import org.jboss.forge.common.XmlTools; import org.jboss.logging.Logger; @@ -98,12 +100,12 @@ /** * This Map contains all counters for the portal. */ - private Map<String,Counter> downloadCounters; + protected Map<String,Counter> downloadCounters; /** * ContentManager is used for getting to file resources. */ - private ContentManager contentManager; + protected ContentManager contentManager; /** * Logger is used for displaying messages. @@ -128,41 +130,51 @@ */ public DownloadCountersDescriptor (final String portalName, ContentManager contentManager) { - // Just initializing the changeStatus variable - changeStatus=false; + this(); + + // Saving the ContentManager. + this.contentManager = contentManager; + + // Getting the path to main dowload counters descriptor. + String pathToCountersXml = File.separator + DownloadCounterTools.getMainXmlPath(portalName); - // Initializing logger. - logger = Logger.getLogger(this.getClass()); - - // Getting the path to main dowload counters descriptor. - String pathToCountersXml = File.separator + DownloadCounterTools.getMainXmlPath(portalName); - - // Saving the ContentManager. - this.contentManager = contentManager; - try { - // Parsing main download counters descriptor. - DOMParser parser = new DOMParser(); - InputStream is = contentManager.getNode(pathToCountersXml).getContentInputStream(); - parser.parse(new InputSource(is)); - Document doc = parser.getDocument(); - NodeList nodes = doc.getDocumentElement().getChildNodes(); - downloadCounters=getValuesFromNodes(nodes); + // Parsing main download counters descriptor. + DOMParser parser = new DOMParser(); + InputStream is = contentManager.getNode(pathToCountersXml).getContentInputStream(); + parser.parse(new InputSource(is)); + Document doc = parser.getDocument(); + NodeList nodes = doc.getDocumentElement().getChildNodes(); + downloadCounters=getValuesFromNodes(nodes); - // Getting Map containing pairs of ProjectId and shotoku Node - // which refers to projects' download counter descriptor. - Map <String,org.jboss.shotoku.Node> descriptors = getDownloadDescriptors(portalName); + // Getting Map containing pairs of ProjectId and shotoku Node + // which refers to projects' download counter descriptor. + Map <String,org.jboss.shotoku.Node> descriptors = getDownloadDescriptors(portalName); - // Synchronizing tracked links in downloadConters with links from - // projects download counter descriptors. - synchronizeCounters(descriptors); + // Synchronizing tracked links in downloadConters with links from + // projects download counter descriptors. + synchronizeCounters(descriptors); } catch (Exception e) { logger.error ("Failed to initialize downloadCounters.",e); } } - + + /** + * Just default constructor whic does almost nothing. + */ + protected DownloadCountersDescriptor () + { + // Just initializing the changeStatus variable + changeStatus=false; + + // Initializing logger. + logger = Logger.getLogger(this.getClass()); + + } + + /** * Method only returns all download counters. * @return Returns a Map containing tracked download counters. @@ -223,7 +235,7 @@ * This Map<Strin,org.jboss.shotoku.Node> contains project ids with their download * counter descriptor's Node. */ - private void synchronizeCounters (Map<String,org.jboss.shotoku.Node> counters) { + protected void synchronizeCounters (Map<String,org.jboss.shotoku.Node> counters) { // Checking if tracked projects still have their counter.xml descriptors. // If not deleting all tracked links for them. @@ -468,7 +480,7 @@ xmlFile.save ("[DownlaodCounter] Main xml descriptor file update."); changeStatus=false; } catch (Exception e) { - e.printStackTrace(); + logger.error("Failed to persist downloadCounters",e); } } @@ -511,6 +523,39 @@ doc.getDocumentElement().appendChild(newCounter); } } + + /** + * Just a method to get Logger, used by subclasses. + */ + protected Logger getLogger () + { + return logger; + } + + /** + * Just a method to set Logger, used by subclasses. + */ + protected void setLogger (Logger logger) + { + this.logger = logger; + } + + /** + * Just a method to get ContentManager, used by subclasses. + */ + protected ContentManager getContentManager () + { + return this.contentManager; + } + + /** + * Just a method to set ContentManager, used by subclasses. + */ + protected void setContentManager (ContentManager cm) + { + this.contentManager=cm; + } + /** * Method simply just increments the counter value for @@ -554,8 +599,16 @@ public boolean hasChanged () { return changeStatus; } - - + + /** + * Method for setting changed status. + */ + protected void setChanged(boolean changeStatus) + { + this.changeStatus = changeStatus; + } + + /** * Method used for pulling the links,counter values and projectIds out from the * xml file nodes to the <String,Counter> Map. |