[jboss-svn-commits] JBL Code SVN: r14868 - in labs/jbosslabs/trunk/portal-extensions: forge-common/src/java/org/jboss/forge/common/projects and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Sep 4 12:05:26 EDT 2007


Author: wrzep
Date: 2007-09-04 12:05:26 -0400 (Tue, 04 Sep 2007)
New Revision: 14868

Modified:
   labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/DownloadingTools.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/DownloadCountersDB.java
   labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersServiceBean.java
   labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal/DownloadingFilter.java
Log:
JBLAB-889


Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/DownloadingTools.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/DownloadingTools.java	2007-09-04 15:52:34 UTC (rev 14867)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/DownloadingTools.java	2007-09-04 16:05:26 UTC (rev 14868)
@@ -37,7 +37,7 @@
 	private static final String USER_AGENT_HEADER = "User-Agent";
 
 	private static final String[] desiredUserAgents = {
-				"gecko", "opera", "wget", "msie" };
+				"gecko", "firefox", "opera", "wget", "msie" };
 	// Don't put "mozilla" here. Some bots (including googlebot) have "mozilla"
 	// string as a combatibility flag
 	
@@ -45,6 +45,8 @@
 								= "com.opensymphony.module.sitemesh.mapper."
 								+ "RobotDecoratorMapper";
 	private static final String IS_BOT_METHOD = "isBot";
+	
+	private static Method isBotMethod = null;
 
 	private static Logger log = Logger.getLogger(DownloadingTools.class);
 	
@@ -53,29 +55,31 @@
     	
     	String userAgent = request.getHeader(USER_AGENT_HEADER);
 		
-		if (userAgent != null) {
+    	if (userAgent == null) {
+    		return false;
+    	}
 			
-			userAgent = userAgent.trim().toLowerCase();
-			
-			for (String agent : desiredUserAgents) {
-				if (userAgent.indexOf(agent) != -1) {
-					return true;
-				}
-			}
-		}
-		
+    	userAgent = userAgent.trim().toLowerCase();
+
+    	for (String agent : desiredUserAgents) {
+    		if (userAgent.indexOf(agent) != -1) {
+    			return true;
+    		}
+    	}
+    	
     	// Not sure, ask sitemesh
 		try {
 			// Reflection in necessary since "isBot" method is private
-			Class<RobotDecoratorMapper> rdmClass = (Class<RobotDecoratorMapper>)
-    		Thread.currentThread().getContextClassLoader().loadClass(ROBOT_DECORADOR_MAPPER_CLASS);
 			
-			Method m = rdmClass.getDeclaredMethod(IS_BOT_METHOD, HttpServletRequest.class);
-			m.setAccessible(true);
+			if (isBotMethod == null) {
+				Class<RobotDecoratorMapper> rdmClass = (Class<RobotDecoratorMapper>)
+				Thread.currentThread().getContextClassLoader().loadClass(ROBOT_DECORADOR_MAPPER_CLASS);
 			
-			//TODO logging??
+				isBotMethod = rdmClass.getDeclaredMethod(IS_BOT_METHOD, HttpServletRequest.class);
+				isBotMethod.setAccessible(true);
+			}
 	
-			return ! ((Boolean) m.invoke(null, request));
+			return ! ((Boolean) isBotMethod.invoke(null, request));
 		} catch (Exception e) {
 			log.warn(e);
 		}

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-04 15:52:34 UTC (rev 14867)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/CountersServiceInterface.java	2007-09-04 16:05:26 UTC (rev 14868)
@@ -37,9 +37,11 @@
     
     public Map<String,Counter> getCountersMap();
     
-    public void updateCounters(Map<String,Counter> downloadCounters);
+    //public void updateCounters(Map<String,Counter> downloadCounters);
     
     public void incrementCounter (String path);
 
 	public void addCounter(Counter newCounter, String path);
+	
+	public boolean hasCounter(String path);
 }

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-04 15:52:34 UTC (rev 14867)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDB.java	2007-09-04 16:05:26 UTC (rev 14868)
@@ -22,8 +22,6 @@
 
 package org.jboss.forge.common.projects;
 
-import java.util.Map;
-
 import javax.naming.InitialContext;
 
 import javax.jms.JMSException;
@@ -61,51 +59,23 @@
     public DownloadCountersDB(String portalName, ContentManager cm)
     {
         super();
-        this.setContentManager(cm);
-        try
-        {
-            ctx = new InitialContext();
-            CountersServiceInterface countersService =
-                (CountersServiceInterface) ctx.lookup(CountersServiceInterface.jndiName);
-            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);
-            
-            countersService.updateCounters(downloadCounters);
-
-        }
-        catch (Exception e)
-        {
-            getLogger().error("Failed to initialize downloadCounters.", e);
-        }
-
+        try {
+			ctx = new InitialContext();
+			 CountersServiceInterface countersService =
+	                (CountersServiceInterface) ctx.lookup(CountersServiceInterface.jndiName);
+	         downloadCounters = countersService.getCountersMap();
+        } 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.
+     * This empty method is overriding the method from DownloadCountersDescriptor.
      * @param portalName
      */
     public void synchronizeWithFile(String portalName)
     {
-        try
-        {
-            CountersServiceInterface countersService =
-                (CountersServiceInterface) ctx.lookup(CountersServiceInterface.jndiName);
-            countersService.updateCounters(downloadCounters);
-        }
-        catch (Exception e)
-        {
-            getLogger().error("Failed to persist downloadCounters", e);
-        }
+    	// do nothing
     }
 
     public synchronized void increment(final String link) {
@@ -122,6 +92,7 @@
      */
     public synchronized void increment(final String link, final String projectId) {
 
+  
     	if (downloadCounters == null) {
     		return;
     	}
@@ -129,10 +100,9 @@
     	try {
     		CountersServiceInterface countersService = 
     				(CountersServiceInterface) ctx.lookup(CountersServiceInterface.jndiName);
-    		countersService.incrementCounter(link);
-
-    		if (!downloadCounters.containsKey(link)) { // Item not counted yet
-
+    		
+    		if (!downloadCounters.containsKey(link)) {
+    		
     			Counter newCounter = new Counter(1, projectId, false,
     					Sorting.RANDOM);
     			downloadCounters.put(link, newCounter);
@@ -141,7 +111,7 @@
 
     		} else {
 
-    			// Status change to inform about counters modification.
+    			countersService.incrementCounter(link);
     			downloadCounters.get(link).increment();
     		}
 

Modified: 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	2007-09-04 15:52:34 UTC (rev 14867)
+++ labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/counters/CountersServiceBean.java	2007-09-04 16:05:26 UTC (rev 14868)
@@ -56,6 +56,7 @@
      * This method gets all counters from database and returns a Map object with them.
      * @return Map<String,Counter> containging all download counters from database.
      */
+    @SuppressWarnings("unchecked")
     public Map<String,Counter> getCountersMap() 
     {
         List<CountersEntity> countersRows = em.createQuery("from CountersEntity e").getResultList();
@@ -71,11 +72,38 @@
         return countersMap;
     }
     
+    public void addCounter(Counter counter, String path) {
+    	
+    	CountersEntity ce = new CountersEntity(path,counter.getValue(),counter.getProjectId(),counter.getVisible(),counter.getSorting().name());
+        em.persist(ce);
+    }
+    
     /**
+     * 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);
+    }
+    
+    public boolean hasCounter(String path) {
+    	return em.find(CountersEntity.class,path) != null;
+    }
+    
+    /**
      * 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) {
@@ -94,6 +122,7 @@
                     ce.setProjectId(counter.getProjectId());
                 }
             }
+            // remove counters that are not included in xml conf file
             List<String> pathsFromDB = em.createQuery("select path from CountersEntity e").getResultList();
             for (String path : pathsFromDB) 
             {
@@ -104,28 +133,5 @@
             }
             
         }
-    }
-    
-    public void addCounter(Counter counter, String path) {
-    	
-    	CountersEntity ce = new CountersEntity(path,counter.getValue(),counter.getProjectId(),counter.getVisible(),counter.getSorting().name());
-        em.persist(ce);
-    }
-    
-    /**
-     * 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);
-    }
-    
+    }*/
 }

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-04 15:52:34 UTC (rev 14867)
+++ labs/jbosslabs/trunk/portal-extensions/forge-portal-attr/src/java/org/jboss/forge/portal/DownloadingFilter.java	2007-09-04 16:05:26 UTC (rev 14868)
@@ -22,7 +22,6 @@
 
 package org.jboss.forge.portal;
 
-import java.io.File;
 import java.io.IOException;
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
@@ -47,6 +46,7 @@
 public class DownloadingFilter implements Filter {
 
     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 {
@@ -60,13 +60,14 @@
         	if ((response instanceof HttpServletResponse) &&
         						(link != null) && (link.length() > 0)) {
         		
-        		   String portalName = Constants.LABS_PORTAL;
-        		   String projectId = resolveProjectId(link);
-        		
         		   boolean desired = DownloadingTools.isDesired(htReq);
         		   
         		   if (desired) {
-        			   count(link, projectId, portalName);
+            		   String projectId = resolveProjectId(link);
+        			   count(link, projectId, Constants.LABS_PORTAL);
+        		   } else {
+        			   // Count robots
+        			   count(ROBOT_SPECIAL_NAME, "default", Constants.LABS_PORTAL);
         		   }
         			   
         		   ((HttpServletResponse) response).sendRedirect(link);
@@ -78,8 +79,10 @@
     }
 
 	private static String resolveProjectId(String link) {
- 
-    	return link.split(File.separator)[0];
+		
+		String[] splited = link.split("/");
+		
+    	return (splited.length >= 2) ? splited[1] : null;
 	}
 
 




More information about the jboss-svn-commits mailing list