[jboss-svn-commits] JBL Code SVN: r15309 - in labs/jbosslabs/trunk/portal-extensions: forge-portal-attr/src/java/org/jboss/forge/portal and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sun Sep 23 15:39:40 EDT 2007
Author: dejp
Date: 2007-09-23 15:39:40 -0400 (Sun, 23 Sep 2007)
New Revision: 15309
Removed:
labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterDescriptor.java
labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDescriptor.java
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/CountersServiceInterface.java
labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterTools.java
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/DownloadCountersWatcher.java
labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java
labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal/DownloadingFilter.java
Log:
JBLAB-887
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 2007-09-23 17:03:42 UTC (rev 15308)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Counter.java 2007-09-23 19:39:40 UTC (rev 15309)
@@ -34,7 +34,7 @@
*/
public class Counter implements Serializable {
-
+
/**
* Current value of a download counter.
*/
@@ -64,8 +64,8 @@
* Name of project for this counter.
*/
public Counter (long value,String projectId,Boolean visible,Sorting sorting) {
- this.value=value;
- this.projectId=projectId;
+ this.value = value;
+ this.projectId = projectId;
this.visible = visible;
this.sorting = sorting;
}
@@ -76,8 +76,8 @@
public void increment () {
value++;
}
-
- /**
+
+ /**
* Method returns project id name for this counter.
*
* @return Project id name for this counter.
@@ -134,4 +134,5 @@
public void setSorting(Sorting sorting) {
this.sorting = sorting;
}
+
}
Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/CountersServiceInterface.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/CountersServiceInterface.java 2007-09-23 17:03:42 UTC (rev 15308)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/CountersServiceInterface.java 2007-09-23 19:39:40 UTC (rev 15309)
@@ -39,7 +39,7 @@
//public void updateCounters(Map<String,Counter> downloadCounters);
- public void incrementCounter (String path);
+ public void incrementCounter(String path);
public void addCounter(Counter newCounter, String path);
Deleted: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterDescriptor.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterDescriptor.java 2007-09-23 17:03:42 UTC (rev 15308)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterDescriptor.java 2007-09-23 19:39:40 UTC (rev 15309)
@@ -1,173 +0,0 @@
-
- /*
- * 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.io.IOException;
-import java.io.InputStream;
-import java.util.Hashtable;
-import java.util.Map;
-
-import org.apache.xerces.parsers.DOMParser;
-import org.jboss.forge.common.XmlTools;
-import org.jboss.forge.common.projects.XmlInputFactory.XmlNotFoundException;
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-
-/**
- * DownloadCounterDescriptor is an object representing a project download counter descriptor.
- * It parses the xml descriptor and then is used for collecting data achieved from parsing.
- *
- * @author Ryszard Kozmik
- */
-
-public class DownloadCounterDescriptor {
-
- /**
- * This variable contains all links, which need to be tracked, collected from xml descriptor.
- */
- private Map<String,Boolean> links;
-
- /**
- * Describes the order in which counters are requested to be displayed.
- */
- private Sorting sorting;
-
- /**
- * Name of tag in counter.xml containing counter link.
- */
- public static final String LINK = "link";
-
- /**
- * Name of tag in counter.xml containing counter.
- */
- public static final String COUNTER = "counter";
-
- /**
- * This variable contains name of the visibility attribute.
- */
- public static final String VISIBILITY = "visible";
-
- /**
- * This variable contains name of the sorting attribute.
- */
- public static final String SORTING = "sorting";
-
- /**
- * This variable contains value for attributes which means TRUE.
- */
- public static final String TRUE = "true";
-
- /**
- * This variable contains value for attributes which means FALSE.
- */
- public static final String FALSE = "false";
-
- /**
- * Main DownloadCounterDescriptor constructor which parses the xml descriptor for a project
- * and collects data from it.
- *
- * @param counterDesc
- * This Node points to the download counter project descriptor.
- * @throws SAXException
- * XML parser exception.
- * @throws IOException
- * File reading exception.
- * @throws XmlNotFoundException
- * File not found exception.
- */
- public DownloadCounterDescriptor (org.jboss.shotoku.Node counterDesc)
- throws SAXException, IOException,XmlNotFoundException {
-
- // Opening the download counter descriptor.
- DOMParser parser = new DOMParser();
- InputStream is = counterDesc.getContentInputStream();
- parser.parse(new InputSource(is));
- Document doc = parser.getDocument();
- Node n=null,property=null;
- NodeList nodes = doc.getDocumentElement().getChildNodes();
-
- // Parsing and gettting download links which are requested to be tracked by download counter.
- links = new Hashtable<String,Boolean>();
-
- // Resolving way of sorting counters.
- sorting = Sorting.RANDOM;
- String sortingAtt = XmlTools.getAttributeValue(doc.getDocumentElement(),SORTING);
- if (sortingAtt!=null &&
- (sortingAtt.compareToIgnoreCase(Sorting.ASCENDING.name())==0
- || sortingAtt.compareToIgnoreCase(Sorting.ASC.name())==0)) {
- sorting = Sorting.ASCENDING;
- } else if (sortingAtt!=null &&
- (sortingAtt.compareToIgnoreCase(Sorting.DESCENDING.name())==0
- || sortingAtt.compareToIgnoreCase(Sorting.DESC.name())==0)) {
- sorting = Sorting.DESCENDING;
- }
-
- for (int i = 0; i < nodes.getLength(); i++) {
- n = nodes.item(i);
- if (n.getNodeType() == Node.ELEMENT_NODE) {
- if (n.getNodeName().equals(COUNTER)) {
- NodeList counterProps = n.getChildNodes();
- for (int j=0;j< counterProps.getLength() ; j++) {
- property = counterProps.item(j);
- if (property.getNodeType()== Node.ELEMENT_NODE){
- String visibilityAtt = XmlTools.getAttributeValue(property,VISIBILITY);
- boolean visibility = true;
- if (visibilityAtt!=null && visibilityAtt.trim().compareToIgnoreCase(FALSE)==0) {
- visibility = false;
- }
- if (property.getNodeName().equals(LINK) && XmlTools.unmarshallText(property) != null
- && !XmlTools.unmarshallText(property).trim().equals("")) {
- links.put(XmlTools.unmarshallText(property).trim(),visibility);
- }
-
- }
- }
- }
- }
- }
-
- }
-
- /**
- * Returns sorting description for counters.
- * @return
- * Requested sorting order.
- */
- public Sorting getSorting () {
- return sorting;
- }
-
- /**
- * Method simply returns links from the xml descriptor which are requested to be tracked.
- * @return
- * List<String> of download links.
- */
- public Map<String,Boolean> getLinks () {
- return links;
- }
-
-}
Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterTools.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterTools.java 2007-09-23 17:03:42 UTC (rev 15308)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterTools.java 2007-09-23 19:39:40 UTC (rev 15309)
@@ -23,11 +23,7 @@
package org.jboss.forge.common.projects;
-import java.io.File;
import java.util.Map;
-
-import org.jboss.forge.common.ForgeHelper;
-import org.jboss.portal.core.servlet.jsp.taglib.context.DelegateContext;
import org.jboss.shotoku.aop.CacheItemInject;
import org.jboss.shotoku.cache.CacheItemUser;
@@ -39,132 +35,19 @@
*/
public class DownloadCounterTools {
-
- /**
- * Dir in portal where the JSP view file is.
- */
- public static final String FORGE_FILE_ACCESS_DIR="file-access";
-
- /**
- * Name of the directory in dynamicly changing portal content dir containing download counters' files.
- */
- public static final String DOWNLOAD_COUNTERS_DIR="downloads";
- /**
- * Name of JSP view file.
- */
- public static final String DOWNLOADCOUNTER_JSP="normal.jsp";
-
- /**
- * Child of a counter node where value of a counter.
- */
- public static final String VALUE = "value";
-
- /**
- * Child of a counter node containing download counter's tracked link.
- */
- public static final String LINK = "link";
-
- /**
- * Child nodes of counters head context.
- */
- public static final String COUNTER = "counter";
-
- /**
- * Head context name.
- */
- public static final String COUNTERS = "counters";
-
- /**
- * Method prepares DelegateContext object for DownloadCountersPortlet view JSP file.
- *
- * @param portalName
- * @param projectId
- * Project id name for which tracked links should be added to the context.
- * @return DelegateContext object containing content for JSP file.
- * @throws Exception
- */
- public static DelegateContext getContext (final String portalName,final String projectId) throws Exception {
-
- // If the projectId is null method returns empty DelegateContext object.
- if (projectId==null) {
- return new DelegateContext();
- }
-
- // Creating new empty context.
- DelegateContext ctx = new DelegateContext();
-
- // Getting the DownloadCountersDescriptor object containing counters.
- DownloadCountersDescriptor desc = getDesc(portalName);
-
- // If there isn't a descriptor in cache, return empty context.
- if (desc==null) {
- return ctx;
- }
-
- // If there is no tracked link for projectId return empty context.
- if (!desc.checkForProjectCounters(projectId)) {
- return ctx;
- }
-
- // Getting the counters values.
- Map<String,Long> values = desc.getValuesForPortlet(projectId);
-
- // Filling the context for portlet.
- DelegateContext counters = ctx.next("counters");
- for (String keyLink:values.keySet()) {
- DelegateContext counter = counters.next("counter");
- // Resolving the file name.
- String fileName = keyLink.split(File.separator)[keyLink.split(File.separator).length-1];
- counter.put("link",fileName);
- counter.put("value",Long.toString(values.get(keyLink)));
- }
- return ctx;
- }
- private static @CacheItemInject(dataSource=DownloadCountersWatcher.class) CacheItemUser<String, DownloadCountersDescriptor> dcw;
+ private static @CacheItemInject(dataSource=DownloadCountersWatcher.class) CacheItemUser<String, DownloadCountersDB> dcw;
/**
* Method gets DownloadCountersDescriptor object from the forge cache.
* @param portalName
* @return DownloadCountersDescriptor object.
*/
- public static DownloadCountersDescriptor getDesc(final String portalName) {
+ public static DownloadCountersDB getDesc(final String portalName) {
return dcw.get(portalName);
}
/**
- * Method constructs path to main download counters descriptor.
- *
- * @param portalName
- * @return Path to main download counters descriptor.
- */
- public static String getMainXmlPath (String portalName) {
- return ForgeHelper.DYNAMIC_CONTENT_DIR+File.separator+portalName+File.separator+DOWNLOAD_COUNTERS_DIR+File.separator+ProjectsHelper.DOWNLOADCOUNTERMAIN_DESC;
- }
-
- /**
- * Method constructs path to project's download counters descriptor.
- *
- * @param portalName
- * @param projectId
- * Project id name for which the download counter descriptor path will be constructed
- * @return Path to project's download counters descriptor.
- */
- public static String getProjectXmlPath(String portalName,String projectId) {
- return portalName+File.separator+ProjectsHelper.MEMBERS_DIR+File.separator+projectId+
- File.separator+ProjectsHelper.DOWNLOADCOUNTER_DESC;
- }
-
- /**
- * Method constructs path to the DownloadCounterPortlet JSP view file.
- *
- * @return Path to the DownloadCounterPortlet's JSP view file.
- */
- public static String getJspCmPath() {
- return FORGE_FILE_ACCESS_DIR + File.separator + DOWNLOADCOUNTER_JSP;
- }
-
- /**
* Method returns overall sum of all downloads for specified <code>projectId</code> name.
* @param projectId
* Project id name for which sum must be generated.
@@ -174,7 +57,7 @@
* Returns sum of all downloads.
*/
public static long getOverallDownloadsForProject (String projectId,String portalName) {
- DownloadCountersDescriptor desc = getDesc(portalName);
+ DownloadCountersDB desc = getDesc(portalName);
Map<String,Long> values = desc!=null?desc.getValuesForPortlet(projectId):null;
if (values==null || values.size()==0) {
return 0;
Modified: 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 2007-09-23 17:03:42 UTC (rev 15308)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDB.java 2007-09-23 19:39:40 UTC (rev 15309)
@@ -22,16 +22,17 @@
package org.jboss.forge.common.projects;
+import java.util.Comparator;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.TreeMap;
import javax.naming.InitialContext;
-
-import javax.jms.JMSException;
-import javax.jms.Queue;
-import javax.jms.QueueConnectionFactory;
-import javax.jms.QueueSession;
-import javax.jms.TextMessage;
-
import javax.naming.NamingException;
-
+import org.jboss.forge.common.ejb3.LabsServices;
+import org.jboss.forge.common.projects.project.Downloads;
+import org.jboss.forge.common.projects.project.File;
+import org.jboss.forge.common.projects.project.Files;
+import org.jboss.logging.Logger;
import org.jboss.shotoku.ContentManager;
@@ -41,14 +42,29 @@
*
* @author Ryszard Kozmik
*/
-public class DownloadCountersDB
- extends DownloadCountersDescriptor
-{
+public class DownloadCountersDB {
- // InitialContext is used for getting CountersEntity bean.
- protected InitialContext ctx;
+ /**
+ * DownloadCountersDB logger.
+ */
+ private static Logger logger = Logger.getLogger(DownloadCountersDB.class);
+
+ /**
+ * InitialContext is used for getting CountersEntity bean.
+ */
+ private InitialContext ctx;
/**
+ * This Map contains all counters for the portal.
+ */
+ protected Map<String,Counter> downloadCounters;
+
+ /**
+ * ContentManager is used for getting to file resources.
+ */
+ protected ContentManager contentManager;
+
+ /**
* Main constructor of this class which gets all counters from database,
* then it compares them with information gained from xml projects' descriptors
* and finally after synchronization database is updated with the changes.
@@ -56,31 +72,47 @@
* @param cm
* ContentManager instance needed to get xml descriptors.
*/
- public DownloadCountersDB(String portalName, ContentManager cm)
- {
- super();
+ public DownloadCountersDB() {
try {
- ctx = new InitialContext();
- CountersServiceInterface countersService =
- (CountersServiceInterface) ctx.lookup(CountersServiceInterface.jndiName);
- downloadCounters = countersService.getCountersMap();
+ ctx = new InitialContext();
+ CountersServiceInterface countersService =
+ (CountersServiceInterface) ctx.lookup(CountersServiceInterface.jndiName);
+ downloadCounters = countersService.getCountersMap();
} catch (Exception e) {
- getLogger().error("Failed to initialize downloadCounters.", e);
+ logger.error("Failed to initialize downloadCounters.", e);
}
}
+ public DownloadCountersDB(final String portalName, ContentManager contentManager) {
+ this();
+ this.contentManager = contentManager;
+ }
+
/**
- * This empty method is overriding the method from DownloadCountersDescriptor.
- * @param portalName
+ * Method only returns all download counters.
+ * @return Returns a Map containing tracked download counters.
*/
- public void synchronizeWithFile(String portalName)
- {
- // do nothing
+ public Map<String, Counter> getCountersMap() {
+ return downloadCounters;
}
- public synchronized void increment(final String link) {
-
- increment(link, null);
+ /**
+ * Method returns download counters for given project.
+ * @param projectId
+ * @return Returns a Map containing tracked download counters.
+ */
+ public Map<String, Counter> getCountersMapForProject(String projectId) {
+ Map<String, Counter> counters = null;
+
+ if (downloadCounters != null) {
+ counters = new TreeMap<String, Counter>();
+
+ for (String link : getProjectLinks(projectId).keySet()) {
+ counters.put(link, downloadCounters.get(link));
+ }
+ }
+
+ return counters;
}
/**
@@ -92,7 +124,6 @@
*/
public synchronized void increment(final String link, final String projectId) {
-
if (downloadCounters == null) {
return;
}
@@ -103,11 +134,32 @@
if (!downloadCounters.containsKey(link)) {
- Counter newCounter = new Counter(1, projectId, false,
- Sorting.RANDOM);
- downloadCounters.put(link, newCounter);
+ Downloads downloads = LabsServices.getProjectsService().getProjectDownloads(projectId);
+ Files files = downloads.getFiles();
+
+ boolean exists = false;
+
+ for (File file : files.getFile()) {
+ if (link.startsWith("http://") || link.startsWith("https://")) {
+ if (link.endsWith(file.getId())) {
+ exists = true;
+ break;
+ }
+ } else {
+ if (link.endsWith(file.getFullName())) {
+ exists = true;
+ break;
+ }
+ }
+ }
+
+ if (exists) {
+ Counter newCounter = new Counter(1, projectId, false,
+ Sorting.RANDOM);
+ downloadCounters.put(link, newCounter);
- countersService.addCounter(newCounter, link);
+ countersService.addCounter(newCounter, link);
+ }
} else {
@@ -116,18 +168,108 @@
}
} catch (NamingException e) {
- getLogger()
- .error(
- "CountersService was not found while incrementing a counter.",
- e);
+ logger.error(
+ "CountersService was not found while incrementing a counter.", e);
} catch (Exception e) {
- getLogger()
- .error(
- "CountersService problem while incrementing/creating a counter.",
- e);
+ logger.error(
+ "CountersService problem while incrementing/creating a counter.", e);
}
+ }
+ /**
+ * This method returns a List<String> of links which
+ * are now tracked for project given by <projectId> parameter.
+ *
+ * @param projectId
+ * Id of a project for which the links will be searched.
+ * @return List<String> Containing tracked links.
+ */
+ private synchronized Map<String, Boolean> getProjectLinks(String projectId) {
+ Map<String,Boolean> links = new Hashtable<String,Boolean>();
+
+ if (downloadCounters == null) {
+ return links;
+ }
+
+ for (String link : downloadCounters.keySet()) {
+ String id = downloadCounters.get(link).getProjectId();
+ if (id != null) {
+ if (id.equals(projectId)) {
+ links.put(new String(link),downloadCounters.get(link).getVisible());
+ }
+ }
+ }
+ return links;
}
+
+ /**
+ * Method gives a Map<String,Long> containing pairs of link to tracked file
+ * and the Long value object which contains number of downloads of this file.
+ * @param projectId
+ * Project id name for which Map of values is reuqested.
+ * @return
+ * Map<String,Long> containing link:value pairs.
+ */
+ public synchronized Map<String,Long> getValuesForPortlet(String projectId) {
+ Map<String,Boolean> links = getProjectLinks(projectId);
+ Map<String,Long> values = null;
+ if (links.size()==0) {
+ values = new Hashtable<String,Long>(0);
+ return values;
+ }
+
+ // Resolving way of sorting for counters.
+ Sorting sorting = downloadCounters.get(links.keySet().iterator().next()).getSorting();
+ if (sorting==Sorting.ASC || sorting==Sorting.ASCENDING) {
+ values = new TreeMap<String,Long>(new Comparator<String>() {
+ /**
+ * This method is used for comparing counters taking to consideration their values.
+ * If defines ascending order.
+ * @param o1
+ * First link to compare.
+ * @param o2
+ * Second link to compare.
+ * @return
+ * negative integer - when counter for the first link is lower than for second one
+ * zero - when counters for both links are equal
+ * positive integer - when counter for the first link is greater than for second one
+ */
+ public synchronized int compare(String link1, String link2) {
+ int valueCompare = (int)(downloadCounters.get(link1).getValue()-downloadCounters.get(link2).getValue());
+ return valueCompare==0?link1.compareTo(link2):valueCompare;
+ }
+ });
+ } else if (sorting==Sorting.DESC || sorting==Sorting.DESCENDING) {
+ values = new TreeMap<String,Long>(new Comparator<String>() {
+ /**
+ * This method is used for comparing counters taking to consideration their values.
+ * It defines descending order.
+ * @param o1
+ * First link to compare.
+ * @param o2
+ * Second link to compare.
+ * @return
+ * negative integer - when counter for the first link is greater than for second one
+ * zero - when counters for both links are equal
+ * positive integer - when counter for the first link is lower than for second one
+ */
+ public synchronized int compare(String link1, String link2) {
+ int valueCompare = (int)(downloadCounters.get(link2).getValue()-downloadCounters.get(link1).getValue());
+ return valueCompare==0?link2.compareTo(link1):valueCompare;
+ }
+ });
+ } else {
+ values = new Hashtable<String,Long>(links.size());
+ }
+ for (String link : links.keySet()) {
+ if (links.get(link)) {
+ values.put(link,downloadCounters.get(link).getValue());
+ }
+ }
+
+ return values;
+ }
+
}
Deleted: 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 2007-09-23 17:03:42 UTC (rev 15308)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDescriptor.java 2007-09-23 19:39:40 UTC (rev 15309)
@@ -1,682 +0,0 @@
-
- /*
- * 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.io.File;
-import java.io.InputStream;
-import java.util.Comparator;
-import java.util.HashSet;
-import java.util.Hashtable;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-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;
-import org.jboss.shotoku.ContentManager;
-import org.jboss.shotoku.Directory;
-import org.jboss.shotoku.exceptions.ResourceDoesNotExist;
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.xml.sax.InputSource;
-
-/**
- * DownloadCountersDescriptor is a main class used for counting downloads.
- * It contains code for synchronizing counters with main download counters xml
- * as well as synchronizing the tracked links with projects dowload counter descriptors.
- *
- * @author Ryszard Kozmik
- */
-
-public class DownloadCountersDescriptor {
-
- /**
- * Name of tag in counters.xml containing counter value.
- */
- public static final String VALUE = "value";
-
- /**
- * Name of tag in counters.xml containing counter link.
- */
- public static final String LINK = "link";
-
- /**
- * Name of tag in counters.xml containing counter.
- */
- public static final String COUNTER = "counter";
-
- /**
- * Name of tag in counters.xml containing project id.
- */
- public static final String ID = "id";
-
- /**
- * This variable contains name of the visibility attribute.
- */
- public static final String VISIBILITY = "visible";
-
- /**
- * This variable contains name of the sorting attribute.
- */
- public static final String SORTING = "sorting";
-
- /**
- * This variable contains value for attributes which means TRUE.
- */
- public static final String TRUE = "true";
-
- /**
- * This variable contains value for attributes which means FALSE.
- */
- public static final String FALSE = "false";
-
- /**
- * This Map contains all counters for the portal.
- */
- protected Map<String,Counter> downloadCounters;
-
- /**
- * ContentManager is used for getting to file resources.
- */
- protected ContentManager contentManager;
-
- /**
- * Logger is used for displaying messages.
- */
- private Logger logger;
-
- /**
- * This boolean variable turns to true if one of the counters
- * has been incremented. If value is true the downloadCounters Map
- * will be synchronized with a main xml descriptor
- */
- private boolean changeStatus;
-
- /**
- * Constructor for DownloadCountersDescriptor.
- * While constructing object all information from projects descriptors
- * is being collected and the downloadCounters Map is filled with values
- * from main xml download counters descriptor.
- *
- * @param portalName
- * @param contentManager
- */
- public DownloadCountersDescriptor (final String portalName, ContentManager contentManager) {
-
- this();
-
- // Saving the ContentManager.
- this.contentManager = contentManager;
-
- // Getting the path to main dowload counters descriptor.
- String pathToCountersXml = File.separator + DownloadCounterTools.getMainXmlPath(portalName);
-
- 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);
-
- // 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) {
- 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.
- */
- public Map<String,Counter> getCountersMap () {
- return downloadCounters;
- }
-
-
- /**
- * This method searches through projects direcories in order to find all download counter descriptors.
- * Method returns Map containing pairs of String projectId and
- * org.jboss.shotoku.Node download counter descriptor file node.
- * @param portalName
- * @return A Map<String,org.jboss.shotoku.Node> containing project ids with their download
- * counter's Node.
- */
- public Map<String,org.jboss.shotoku.Node> getDownloadDescriptors (String portalName) {
- Map<String,org.jboss.shotoku.Node> nodes = new Hashtable<String,org.jboss.shotoku.Node>();
-
- // Getting the members dir.
- Directory membersDir;
- try {
- membersDir = contentManager.getDirectory(portalName+File.separator+ProjectsHelper.MEMBERS_DIR);
- } catch (ResourceDoesNotExist e) {
- logger.error ("Members directory not exists.",e);
- return null;
- }
-
- // Getting all projects dirs in members directory.
- List<Directory> membersProjectDirs = membersDir.getDirectories();
-
- for (Directory projectDir:membersProjectDirs) {
- for (org.jboss.shotoku.Node n:projectDir.getNodes().toList()) {
- // Checking if node is a node containing download counter descriptor.
- if (n.getName().compareTo(ProjectsHelper.DOWNLOADCOUNTER_DESC)==0) {
- org.jboss.shotoku.Node counter;
- try {
- counter = projectDir.getNode(ProjectsHelper.DOWNLOADCOUNTER_DESC);
- } catch (ResourceDoesNotExist e) {
- break;
- }
- String projectId = projectDir.getName();
- nodes.put(projectId,counter);
- break;
- }
- }
-
- }
- return nodes;
- }
-
- /**
- * Method removes links which were disabled from counting
- * and adds those which was added in their project's download counter descriptor.
- *
- * @param counters
- * This Map<Strin,org.jboss.shotoku.Node> contains project ids with their download
- * counter descriptor's Node.
- */
- 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.
- Set<String> projects = getTrackedProjects();
- if (!counters.keySet().containsAll(projects)) {
- projects.removeAll(counters.keySet());
- removeLinksForProjects (projects);
- }
-
- // Iterating through projects nodes containing download counter descriptors.
- for (String projectId:counters.keySet()){
- try {
- // Parsing project download counter descriptor.
- DownloadCounterDescriptor counterDesc =
- new DownloadCounterDescriptor(counters.get(projectId));
-
- // Getting list of links which are now tracked by downloadCounters.
- Map<String,Boolean> xmlProjectLinks = getProjectLinks(projectId);
-
- // Getting the list of links from download counter descriptor.
- Map<String,Boolean> links = counterDesc.getLinks();
-
- // This list will be used for collecting links which must be
- // removed from downloadCounters.
- List<String> linksToDelete = new LinkedList<String>();
-
- // Searching for links to remove and links which are already tracked.
- for (String link:xmlProjectLinks.keySet()) {
- if (links.keySet().contains(link)) {
- setCounterVisibility (link,links.get(link));
- setCounterSorting (link,counterDesc.getSorting());
- links.keySet().remove(link);
- } else {
- linksToDelete.add(link);
- }
- }
-
- removeLinksFromCounting(linksToDelete);
-
- addLinksForCounting (links,projectId,counterDesc.getSorting());
-
- } catch (Exception e) {
- logger.warn("Problem with opening project "+
- projectId+" download counter descriptor.",e);
- }
-
- }
-
- // Changing status to false becouse downloadCounters Map
- // is now synchronized with projects' download counter descriptors.
- changeStatus=false;
- }
-
- /**
- * Method removes links specified in <code>linksToDelete</code>
- * from tracking their downloads.
- *
- * @param linksToDelete
- * List<String> of links which should be removed.
- */
- private synchronized void removeLinksFromCounting (List<String> linksToDelete) {
- // Removing links which shouldn't be tracked any more.
- for (String link:linksToDelete) {
- downloadCounters.remove(link);
- }
- }
-
- /**
- * Method sets visibility for counter in downloadCounters Map as requested
- * in <code>visibility</code> parameter.
- * @param link
- * The link to the counter.
- * @param visibility
- * Visibility which counter should have.
- */
- private synchronized void setCounterVisibility (String link, Boolean visibility) {
- downloadCounters.get(link).setVisible(visibility);
- }
-
- /**
- * Method sets sorting order in which this counter will be displayed.
- * @param link
- * The link to the counter.
- * @param sorting
- * Requested sorting order.
- */
- private synchronized void setCounterSorting (String link,Sorting sorting) {
- downloadCounters.get(link).setSorting(sorting);
- }
-
- /**
- * Method adds links which are wished to be tracked.
- *
- * @param links
- * List<String> of links wished to be tracked
- * @param projectId
- * Project id name for which the links are added.
- */
- private synchronized void addLinksForCounting (Map<String,Boolean> links,String projectId,Sorting sorting) {
- // Adding new download links for tracking.
- for (String link:links.keySet()) {
- downloadCounters.put(link,new Counter(0,projectId,links.get(link),sorting));
- }
- }
-
- private synchronized void removeLinksForProjects (Set<String> projects) {
- Set<String> countersLinks = downloadCounters.keySet();
- List<String> linksForRemoval = new LinkedList<String>();
- for (String link: countersLinks) {
- if (projects.contains(downloadCounters.get(link).getProjectId())){
- linksForRemoval.add(new String(link));
- }
- }
- downloadCounters.keySet().removeAll(linksForRemoval);
- }
-
- private synchronized Set<String> getTrackedProjects () {
- Set<String> projects = new HashSet<String>();
- if (downloadCounters==null)
- return projects;
- for (String link:downloadCounters.keySet()) {
- projects.add(downloadCounters.get(link).getProjectId());
- }
- return projects;
- }
-
- /**
- * This method returns a List<String> of links which
- * are now tracked for project given by <projectId> parameter.
- *
- * @param projectId
- * Id of a project for which the links will be searched.
- * @return List<String> Containing tracked links.
- */
- private synchronized Map<String,Boolean> getProjectLinks (String projectId) {
- Map<String,Boolean> links = new Hashtable<String,Boolean>();
-
- if (downloadCounters == null) {
- return links;
- }
-
- for (String link:downloadCounters.keySet()) {
- if (downloadCounters.get(link).getProjectId().equals(projectId)) {
- links.put(new String(link),downloadCounters.get(link).getVisible());
- }
- }
- return links;
- }
-
- /**
- * Method gives a Map<String,Long> containing pairs of link to tracked file
- * and the Long value object which contains number of downloads of this file.
- * @param projectId
- * Project id name for which Map of values is reuqested.
- * @return
- * Map<String,Long> containing link:value pairs.
- */
- public synchronized Map<String,Long> getValuesForPortlet (String projectId) {
- Map<String,Boolean> links = getProjectLinks(projectId);
- Map<String,Long> values = null;
- if (links.size()==0) {
- values = new Hashtable<String,Long>(0);
- return values;
- }
-
- // Resolving way of sorting for counters.
- Sorting sorting = downloadCounters.get(links.keySet().iterator().next()).getSorting();
- if (sorting==Sorting.ASC || sorting==Sorting.ASCENDING) {
- values = new TreeMap<String,Long>(new Comparator<String>() {
- /**
- * This method is used for comparing counters taking to consideration their values.
- * If defines ascending order.
- * @param o1
- * First link to compare.
- * @param o2
- * Second link to compare.
- * @return
- * negative integer - when counter for the first link is lower than for second one
- * zero - when counters for both links are equal
- * positive integer - when counter for the first link is greater than for second one
- */
- public synchronized int compare(String link1, String link2) {
- int valueCompare = (int)(downloadCounters.get(link1).getValue()-downloadCounters.get(link2).getValue());
- return valueCompare==0?link1.compareTo(link2):valueCompare;
- }
- });
- } else if (sorting==Sorting.DESC || sorting==Sorting.DESCENDING) {
- values = new TreeMap<String,Long>(new Comparator<String>() {
- /**
- * This method is used for comparing counters taking to consideration their values.
- * It defines descending order.
- * @param o1
- * First link to compare.
- * @param o2
- * Second link to compare.
- * @return
- * negative integer - when counter for the first link is greater than for second one
- * zero - when counters for both links are equal
- * positive integer - when counter for the first link is lower than for second one
- */
- public synchronized int compare(String link1, String link2) {
- int valueCompare = (int)(downloadCounters.get(link2).getValue()-downloadCounters.get(link1).getValue());
- return valueCompare==0?link2.compareTo(link1):valueCompare;
- }
- });
- } else {
- values = new Hashtable<String,Long>(links.size());
- }
- for (String link : links.keySet()) {
- if (links.get(link)) {
- values.put(link,downloadCounters.get(link).getValue());
- }
- }
- return values;
- }
-
- /**
- * Method synchronizes the downloadCounters Map with a main download counters descriptor file.
- *
- * @param portalName
- */
- public void synchronizeWithFile(String portalName) {
-
- String pathToCountersXml = File.separator + DownloadCounterTools.getMainXmlPath(portalName);
-
- try {
- DOMParser parser = new DOMParser();
-
- // Opening main download counters descriptor file.
- InputStream is = contentManager.getNode(pathToCountersXml).getContentInputStream();
- parser.parse(new InputSource(is));
- Document doc = parser.getDocument();
- Node mainTag = doc.createElement("counters");
-
- // Replacing old main descriptor tag with new, empty.
- doc.replaceChild(mainTag,doc.getDocumentElement());
-
- // Filling the descriptor with new counter.
- createDocument(doc);
-
- DomToXmlTransformer xht = new DomToXmlTransformer();
-
- // Getting string containing whole xml.
- String xmlString = xht.transformNode(doc.getDocumentElement());
-
- // Saving the main download counter xml descriptor with a new content.
- org.jboss.shotoku.Node xmlFile = contentManager.getNode(pathToCountersXml);
- xmlFile.setContent(xmlString);
- xmlFile.save ("[DownlaodCounter] Main xml descriptor file update.");
- changeStatus=false;
- } catch (Exception e) {
- logger.error("Failed to persist downloadCounters",e);
- }
- }
-
- /**
- * Method creates content of download counters main descriptor by
- * copying values from downloadCounters Map.
- *
- * @param doc
- * This Document object points to download counters main descriptor xml.
- */
- private void createDocument (Document doc) {
- Set<String> keys = downloadCounters.keySet();
- Counter temporary=null;
- for (String link:keys) {
- temporary = downloadCounters.get(link);
- Node newCounter = doc.createElement(COUNTER);
- Node counterVisibility = doc.createAttribute(VISIBILITY);
- counterVisibility.appendChild(doc.createTextNode(temporary.getVisible().toString()));
- newCounter.getAttributes().setNamedItem(counterVisibility);
- Node counterSorting = doc.createAttribute(SORTING);
- counterSorting.appendChild(doc.createTextNode(temporary.getSorting().name()));
- newCounter.getAttributes().setNamedItem(counterSorting);
-
- Node newLink = doc.createElement(LINK);
- Node newLinkText = doc.createTextNode(link);
- newLink.appendChild(newLinkText);
-
- Node newProjectId = doc.createElement(ID);
- Node newProjectIdText = doc.createTextNode(temporary.getProjectId());
- newProjectId.appendChild(newProjectIdText);
-
- Node newValue = doc.createElement(VALUE);
- Node newValueText = doc.createTextNode(Long.toString(temporary.getValue()));
- newValue.appendChild(newValueText);
-
- newCounter.appendChild(newLink);
- newCounter.appendChild(newProjectId);
- newCounter.appendChild(newValue);
-
- 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
- * 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.
- changeStatus=true;
- downloadCounters.get(link).increment();
- }
- }
-
- /**
- * This method returns true if there is at least one link,
- * connected with given in parameter <projectId>, tracked.
- * If there is no link for this <projectId> tracked method returns false.
- *
- * @param projectId
- * The id of a project for which occurence of the links will bechecked.
- * @return Bool value whether there is a link to this project tracked or not.
- */
- public synchronized boolean checkForProjectCounters(String projectId) {
- for (String link:downloadCounters.keySet()) {
- if (downloadCounters.get(link).getProjectId().equals(projectId)) {
- return true;
- }
- }
- return false;
- }
-
- /**
- * Method returns true if there was change in downloadCounters Map.
- * Othervise it returns false.
- *
- * @return Returns true if the status of downloadCounters has changed.
- */
- 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.
- * @param nodes
- * NodeList containing conent of xml descriptor
- * @return
- * Map<String,Couner> containing counters for links which will be tracked.
- */
- private static Map<String,Counter> getValuesFromNodes (NodeList nodes) {
- Map<String,Counter> values = new Hashtable<String,Counter>();
-
- // Temporary variables used for parsing.
- Node n=null,property=null;
-
- // Parsing main download counters descriptor.
- for (int i = 0; i < nodes.getLength(); i++) {
- n = nodes.item(i);
- if (n.getNodeType() == Node.ELEMENT_NODE) {
- if (n.getNodeName().equals(DownloadCountersDescriptor.COUNTER)) {
- Sorting tempSorting = Sorting.RANDOM;
- String sortingAtt = XmlTools.getAttributeValue(n,SORTING);
- if (sortingAtt!=null &&
- (sortingAtt.compareToIgnoreCase(Sorting.ASCENDING.name())==0
- || sortingAtt.compareToIgnoreCase(Sorting.ASC.name())==0)) {
- tempSorting = Sorting.ASCENDING;
- } else if (sortingAtt!=null &&
- (sortingAtt.compareToIgnoreCase(Sorting.DESCENDING.name())==0
- || sortingAtt.compareToIgnoreCase(Sorting.DESC.name())==0)) {
- tempSorting = Sorting.DESCENDING;
- }
- NodeList counterProps = n.getChildNodes();
- String tempLink = null;
- String tempValue = null;
- String tempId = null;
- boolean visibility = true;
- for (int j=0;j< counterProps.getLength() ; j++) {
- property = counterProps.item(j);
- if (property.getNodeType()== Node.ELEMENT_NODE){
- String nodeTextCnt = XmlTools.unmarshallText(property);
- if (property.getNodeName().equals(LINK) && nodeTextCnt != null && !nodeTextCnt.trim().equals("")) {
- String visibilityAtt = XmlTools.getAttributeValue(property,VISIBILITY);
- if (visibilityAtt!=null && visibilityAtt.equals(FALSE)) {
- visibility = false;
- }
- tempLink = nodeTextCnt.trim();
- } else if (property.getNodeName().equals(VALUE) && nodeTextCnt !=null && !nodeTextCnt.trim().equals("")) {
- tempValue = nodeTextCnt.trim();
- } else if (property.getNodeName().equals(ID) && nodeTextCnt !=null && !nodeTextCnt.trim().equals("")) {
- tempId = nodeTextCnt.trim();
- }
- }
- }
- if (tempLink!=null && tempValue!=null && tempId!=null) {
- values.put(tempLink,new Counter(Long.valueOf(tempValue),tempId,visibility,tempSorting));
- }
- }
- }
- }
- return values;
- }
-
-
-
-}
Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersWatcher.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersWatcher.java 2007-09-23 17:03:42 UTC (rev 15308)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersWatcher.java 2007-09-23 19:39:40 UTC (rev 15309)
@@ -23,11 +23,9 @@
package org.jboss.forge.common.projects;
-import java.util.Set;
import java.util.Map;
-
-import org.jboss.shotoku.cache.ShotokuResourceWatcher;
import org.jboss.shotoku.cache.ChangeType;
+import org.jboss.shotoku.cache.ShotokuResourceWatcher;
import org.jboss.shotoku.cache.ValueChange;
import org.jboss.shotoku.cache.ValueInit;
@@ -39,7 +37,8 @@
* @author Ryszard Kozmik
*
*/
-public class DownloadCountersWatcher extends ShotokuResourceWatcher<String, DownloadCountersDescriptor> {
+public class DownloadCountersWatcher extends ShotokuResourceWatcher<String, DownloadCountersDB>
+{
/**
@@ -48,30 +47,17 @@
*
* @return DownloadCountersDescriptor object.
*/
- private DownloadCountersDescriptor getDescriptor (String key) {
- DownloadCountersDescriptor descriptor =
- new DownloadCountersDB(key, getContentManager(key));
-
- // Getting the project ids names where are poll descriptors.
- Set<String> projects = ProjectsHelper.getProjectIds(key);
-
- // Adding found project poll descriptors to the ResourceWatcher to watch
- // for their changes.
- resetWatchedPaths(key);
- for (String projectId : projects) {
- addWatchedPath(key, DownloadCounterTools.getProjectXmlPath(key, projectId));
- }
-
- descriptor.synchronizeWithFile(key);
-
+ private DownloadCountersDB getDescriptor (String key) {
+ DownloadCountersDB descriptor = new DownloadCountersDB(key, getContentManager(key));
+
return descriptor;
}
- protected ValueChange<DownloadCountersDescriptor> update(String key, DownloadCountersDescriptor currentObject, Map<String, ChangeType> changes) {
+ protected ValueChange<DownloadCountersDB> update(String key, DownloadCountersDB currentObject, Map<String, ChangeType> changes) {
return ValueChange.changeTo(getDescriptor(key));
}
- public ValueInit<DownloadCountersDescriptor> init(String key) {
+ public ValueInit<DownloadCountersDB> init(String key) {
return ValueInit.realValue(getDescriptor(key));
}
}
Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java 2007-09-23 17:03:42 UTC (rev 15308)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java 2007-09-23 19:39:40 UTC (rev 15309)
@@ -68,8 +68,8 @@
public final static String PROJECT_DESC = "project.xml";
public final static String PROJECTS_DESC = "projects.xml";
- protected final static String DOWNLOADCOUNTER_DESC = "counter.xml";
- protected final static String DOWNLOADCOUNTERMAIN_DESC = "counters.xml";
+ //protected final static String DOWNLOADCOUNTER_DESC = "counter.xml";
+ //protected final static String DOWNLOADCOUNTERMAIN_DESC = "counters.xml";
public final static String POLLS_DESC = "polls.xml";
public final static String POLL_DESC = "poll.xml";
Modified: labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal/DownloadingFilter.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal/DownloadingFilter.java 2007-09-23 17:03:42 UTC (rev 15308)
+++ labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal/DownloadingFilter.java 2007-09-23 19:39:40 UTC (rev 15309)
@@ -41,13 +41,15 @@
* @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
* @author Adam Warski (adamw at aster.pl)
* @author <a href="mailto:tomasz.szymanski at jboss.com">Tomasz Szymanski</a>
+ * @author Przemyslaw Dej (pdej at jboss.org)
*/
public class DownloadingFilter implements Filter {
+ private static final String PROJECT_ID_PARAM = "projectId";
private static final String URL_PARAM = "url";
private static final String ROBOT_SPECIAL_NAME = "$$$_LABS_ROBOT_COUNTER_$$$";
-
+
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
@@ -55,7 +57,8 @@
HttpServletRequest htReq = (HttpServletRequest) request;
- String link = htReq.getParameter(URL_PARAM);
+ String projectId = htReq.getParameter(PROJECT_ID_PARAM);
+ String link = resolveLink(htReq.getQueryString());
if ((response instanceof HttpServletResponse) &&
(link != null) && (link.length() > 0)) {
@@ -63,7 +66,7 @@
boolean desired = DownloadingTools.isDesired(htReq);
if (desired) {
- String projectId = resolveProjectId(link);
+ //String projectId = resolveProjectId(link);
count(link, projectId, Constants.LABS_PORTAL);
} else {
// Count robots
@@ -78,27 +81,32 @@
chain.doFilter(request, response);
}
- private static String resolveProjectId(String link) {
-
- String[] splited = link.split("/");
-
- return (splited.length >= 2) ? splited[1] : null;
- }
+ private String resolveLink(String query) {
+ String link = null;
+
+ if (query != null && query.length() > 0) {
+ int index = query.indexOf("&" + URL_PARAM + "=");
+
+ if (index != -1) {
+ link = query.substring(index + 5);
+ }
+ }
+
+ return link;
+ }
-
private static void count(String url, String projectId, String portalName) {
// Trying to get DownloadCountersDescriptor object from the
// cache
- DownloadCountersDB downloadCounters =
- (DownloadCountersDB) DownloadCounterTools.getDesc(portalName);
+ DownloadCountersDB downloadCounters = DownloadCounterTools.getDesc(portalName);
// Sending the request link to the DownloadCounter to
// increment
// counter for this link if it's one of the tracked links.
if (downloadCounters != null) {
- downloadCounters.increment(url, projectId);
+ downloadCounters.increment(url, projectId);
}
}
More information about the jboss-svn-commits
mailing list