[jboss-svn-commits] JBL Code SVN: r5093 - in labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status: service/impl service/impl/protocol tools

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Jul 14 08:30:17 EDT 2006


Author: wrzep
Date: 2006-07-14 08:30:11 -0400 (Fri, 14 Jul 2006)
New Revision: 5093

Added:
   labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresRepositoryManager.java
Modified:
   labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresServiceImpl.java
   labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/protocol/PluginsValuesImpl.java
Log:
http://jira.jboss.org/jira/browse/JBLAB-689
Separated service code and way of saving/retrieving historical scores.
Pawel


Modified: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresServiceImpl.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresServiceImpl.java	2006-07-14 03:11:44 UTC (rev 5092)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresServiceImpl.java	2006-07-14 12:30:11 UTC (rev 5093)
@@ -22,29 +22,18 @@
 
 package org.jboss.forge.status.service.impl;
 
-import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.OutputStream;
-import java.text.SimpleDateFormat;
 import java.util.Calendar;
-import java.util.Date;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import javax.ejb.Local;
 import javax.ejb.Remote;
 import javax.ejb.Stateless;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
 
 import org.jboss.annotation.ejb.LocalBinding;
 import org.jboss.annotation.ejb.RemoteBinding;
 import org.jboss.forge.common.projects.Projects;
 
 import org.jboss.forge.status.common.Constants;
-import org.jboss.forge.status.common.Locations;
 import org.jboss.forge.status.exceptions.GetScoresException;
 import org.jboss.forge.status.exceptions.ScoresSaveException;
 import org.jboss.forge.status.service.ScoresService;
@@ -52,15 +41,11 @@
 import org.jboss.forge.status.service.ScoresServiceRemote;
 import org.jboss.forge.status.service.impl.protocol.PluginsValuesImpl;
 import org.jboss.forge.status.service.protocol.PluginConf;
+import org.jboss.forge.status.service.protocol.PluginsValues;
 import org.jboss.forge.status.tools.Plugins;
+import org.jboss.forge.status.tools.ScoresRepositoryManager;
 
 import org.jboss.logging.Logger;
-import org.jboss.shotoku.ContentManager;
-import org.jboss.shotoku.Directory;
-import org.jboss.shotoku.Node;
-import org.jboss.shotoku.aop.Inject;
-import org.jboss.shotoku.exceptions.RepositoryException;
-import org.jboss.shotoku.exceptions.ResourceDoesNotExist;
 
 /**
 * @author Pawel Wrzeszcz (pawel.wrzeszcz [at] gmail.com)
@@ -74,14 +59,6 @@
 public class ScoresServiceImpl implements ScoresService {
 	
 	private static Logger log = Logger.getLogger(ScoresServiceImpl.class);
-	
-	private Map<String,PluginsValuesImpl> values = new HashMap<String,PluginsValuesImpl>();
-	
-	@Inject
-	private ContentManager cm;
-	
-	@PersistenceContext (unitName="scores")
-    protected EntityManager em;
 
 	/**
 	 * Saves current plugin values.
@@ -94,71 +71,21 @@
 	public void save(Projects projects, List<PluginConf> pluginsConf, 
 			String portalName) throws ScoresSaveException {
 		
-		log.info("Saving...");
+		log.info("Getting plugins...");
 		
 		// Get plugins from conf
 		Plugins plugins = new Plugins(pluginsConf, portalName, projects);
 		
-		// Get directory where we store historical scores
-		Directory rootDir = cm.getRootDirectory();
-		String dirName = Locations.getShotokuStatusPath(portalName);
-		Directory dir = getDir(rootDir, dirName);
-		
 		log.info("Computing scores...");
 		
 		// Get plugin values and save them in proper node
 		PluginsValuesImpl pluginsValues = new PluginsValuesImpl(projects, plugins);
 		
-		// Get node to store scores
-		String nodeName = getCurrentNodeName();
-		Node node = null;
-		
-		log.info("Getting shotoku node...");
-		
-		try {
-			node = dir.getNode(nodeName);
-		} catch (RepositoryException e) {
-			log.error("Error while getting shotoku node: " +
-					dir.getFullName() + "/" + nodeName, e);
-			throw new ScoresSaveException(e);
-		} catch (ResourceDoesNotExist e) {
-			node = createNode(dir, nodeName);
-		}
-		
-		// Save values and store then in cache
-		values.put(getCurrentNodeName(), pluginsValues);
-		save(node, pluginsValues);
-		
-		log.info("Saved succesfully");
-		
-		ScoresEntity se = new ScoresEntity("tttest");
-		em.persist(se);
-		log.info("Saved in DB");
+		log.info("Saving...");
+		ScoresRepositoryManager.save(pluginsValues, portalName);
 	}
 	
-	/**
-	 * Saves plugins values in the given Directory.
-	 * 
-	 * @param dir Directory to save values
-	 * @param pluginsValues PluginsValues to save
-	 * @throws ScoresSaveException 
-	 */
-	private synchronized void save(Node node, PluginsValuesImpl pluginsValues)
-										throws ScoresSaveException {
-		
-		try {
-			OutputStream os = node.getOutputStream();
-			ObjectOutputStream oos = new ObjectOutputStream(os);
-			oos.writeObject((PluginsValuesImpl) pluginsValues);
-			oos.close(); 
-			node.save("update"); /* os is closed inside save() */
-			
-		} catch (Exception e) {
-			log.error("Error while saving shotoku node: " +
-					node.getFullName(), e);
-			throw new ScoresSaveException(e);
-		}
-	}
+	
 
 	/**
 	 * Returns historical scores from the given <code>date</code> in the past.
@@ -168,192 +95,15 @@
 	 * @return scores from the given date
 	 * @throws GetScoresException
 	 */
-	public PluginsValuesImpl getPluginsValues(String portalName, Calendar cal)
+	public PluginsValues getPluginsValues(String portalName, Calendar cal)
 									throws GetScoresException {
 		
-		String key = getNodeName(cal);
-		log.info("Returning plugins values (" + key + ") ...");
-		
-		// null date means today
-		if (cal == null) {
-			cal = Calendar.getInstance();
-		}
-		
-		PluginsValuesImpl v = values.get(key);
-		
-		if (v == null) { // Not in cache => get value and store it
-			
-			log.info("Value not in cache...");
-			Directory rootDir = cm.getRootDirectory();
-			
-			Directory dir;
-			try {
-				dir = rootDir.getDirectory(Locations.
-											getShotokuStatusPath(portalName));	
-			} catch (RepositoryException e) {
-				throw new GetScoresException(e);
-			} catch (ResourceDoesNotExist e) {
-				throw new GetScoresException(e);
-			}
-
-			if (dir != null) {
-				v = getPluginsValues(dir, cal);
-				log.info("Finished.");
-				values.put(key,v);
-			}
-		} else {
-			log.info("Returned cached value.");
-		}
-			
-		return v;
+		return ScoresRepositoryManager.get(portalName, cal);
 	}
 	
-	/**
-	 * Returns historical scores from the specified directory.
-	 * 
-	 * @param dir Directory to read scores from
-	 * @return
-	 * @throws GetScoresException 
-	 */
-	private PluginsValuesImpl getPluginsValues(Directory dir, Calendar cal)
-												throws GetScoresException {
-		
-		// Get appropriate node
-		Node node = null;
-		String nodeName = getNodeName(cal);
-		
-		try {
-			node = dir.getNode(nodeName);
-		} catch (Exception e) {
-			log.info("Unable to get shotoku node " +
-					dir.getFullName() + "/" + nodeName);
-			throw new GetScoresException(e);
-		}
-		
-		// Restore values
-		PluginsValuesImpl values = null;
-		try {
-			InputStream is = node.getContentInputStream();
-			ObjectInputStream ois = new ObjectInputStream(is);
-			values = (PluginsValuesImpl) ois.readObject();
-			ois.close();
-			is.close();
-		} catch (Exception e) {
-			log.error("Error while restoring scores from shotoku node " +
-					dir.getFullName() + "/" + nodeName);
-			throw new GetScoresException(e);
-		}
-		
-		return values;
-	}
-	
-	/**
-	 * Creates new shotoku Node.
-	 * 
-	 * @param dir Directory in which new Node will be created
-	 * @param nodeName name of new Node
-	 * @return Node
-	 * @throws ScoresSaveException 
-	 */
-	private Node createNode(Directory dir, String nodeName)
-											throws ScoresSaveException {
-		
-		Node node = null;
-		
-		try {
-			node = dir.newNode(nodeName);
-			node.save("create");
-		} catch (Exception e) {
-			log.error("Error while creating shotoku node: " +
-					dir.getFullName() + "/" + nodeName, e);
-			throw new ScoresSaveException(e);
-		}
-		
-		return node;
-	}
-
-	/**
-	 * Gets shotoku Directory with name <code>dirName</code>
-	 * from specified shotoku Directory.
-	 * If requested Directory does not exist it is created.
-	 * 
-	 * @param rootDir Directory where search will be performed
-	 * @param dirName name of searched Directory
-	 * @return Directory with the given name
-	 * or null when creating Directory failed.  
-	 * @throws ScoresSaveException 
-	 */
-	private Directory getDir(Directory rootDir, String dirName)
-										throws ScoresSaveException {
-		
-		Directory dir = null;
-			
-		try {
-			dir = rootDir.getDirectory(dirName);
-		} catch (RepositoryException e) {
-			log.error("Error while getting shotoku directory " +
-					rootDir.getFullName() + "/" + dirName);
-			throw new ScoresSaveException(e);
-		} catch (ResourceDoesNotExist e) {
-			dir = createDir(rootDir, dirName);
-		}
-			
-		return dir;
-	}
-
-	/**
-	 * Creates new shotoku Directory.
-	 * 
-	 * @param rootDir Directory in which new subdirectory will be created
-	 * @param dirName name of new Directory
-	 * @return Directory or null if errors occured
-	 * @throws ScoresSaveException 
-	 */
-	private Directory createDir(Directory rootDir, String dirName)
-												throws ScoresSaveException {
-		
-		Directory dir = null;
-		
-		try {
-			dir = rootDir.newDirectory(dirName);
-			dir.save("create");
-		} catch (Exception e) {
-			log.error("Error while creating shotoku directory: " +
-					rootDir.getFullName() + "/" + dirName, e);
-			throw new ScoresSaveException(e);
-		}
-		
-		return dir;
-	}
-
-	/**
-	 * Gets name of Node containing saved data from specified datestamp.
-	 * Notice that only year, month and day matter in passed <code>cal</code>.
-	 * 
-	 * @return Node name
-	 */
-	private String getNodeName(Calendar cal) {
-		
-		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
-		
-		Date date = cal.getTime();
-		
-		return df.format(date);
-	}
-	
-	/**
-	 * @return name of Node containging data saved today
-	 */
-	private String getCurrentNodeName() {
-		
-		Calendar today = Calendar.getInstance();
-		
-		return getNodeName(today); 
-	}
-
 	public boolean hasPluginsValues(String portalName, Calendar cal) {
 		
-		PluginsValuesImpl pluginsValues = null;
+		PluginsValues pluginsValues = null;
 		
 		try {
 			pluginsValues = getPluginsValues(portalName, cal);

Modified: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/protocol/PluginsValuesImpl.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/protocol/PluginsValuesImpl.java	2006-07-14 03:11:44 UTC (rev 5092)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/protocol/PluginsValuesImpl.java	2006-07-14 12:30:11 UTC (rev 5093)
@@ -32,6 +32,7 @@
 
 import org.jboss.forge.status.exceptions.GetScoresException;
 import org.jboss.forge.status.plugins.Plugin;
+import org.jboss.forge.status.service.protocol.PluginValues;
 import org.jboss.forge.status.service.protocol.PluginsValues;
 import org.jboss.forge.status.tools.Plugins;
 
@@ -45,7 +46,7 @@
 	private static final long serialVersionUID = -1669515339062166738L;
 
 	/** Map : pluginId -> pluginValue */
-	private Map<String,PluginValuesImpl> values;
+	private Map<String,PluginValues> values;
 	
 	/** Creation date */
 	private Calendar cal;
@@ -58,7 +59,7 @@
 		
 		cal = Calendar.getInstance();
 		
-		values = new HashMap<String,PluginValuesImpl>();
+		values = new HashMap<String,PluginValues>();
 		
 		Set<String> projectIds =
 			projects.getProjectIds(new NullPermissionsChecker(), null);
@@ -84,7 +85,7 @@
 								throws GetScoresException {
 		
 		Long val = null;
-		PluginValuesImpl pluginValues = values.get(pluginId);
+		PluginValues pluginValues = values.get(pluginId);
 		
 		if (pluginValues != null) {
 			val = pluginValues.getValue(projectId);
@@ -108,7 +109,7 @@
 		return values.containsKey(pluginId);
 	}
 	
-	public PluginValuesImpl getPluginValues(String pluginId) {
+	public PluginValues getPluginValues(String pluginId) {
 		return values.get(pluginId);
 	}
 }

Added: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresRepositoryManager.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresRepositoryManager.java	2006-07-14 03:11:44 UTC (rev 5092)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresRepositoryManager.java	2006-07-14 12:30:11 UTC (rev 5093)
@@ -0,0 +1,308 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, 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.status.tools;
+
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+import org.jboss.forge.status.common.Locations;
+import org.jboss.forge.status.exceptions.GetScoresException;
+import org.jboss.forge.status.exceptions.ScoresSaveException;
+import org.jboss.forge.status.service.impl.ScoresEntity;
+import org.jboss.forge.status.service.impl.ScoresServiceImpl;
+import org.jboss.forge.status.service.impl.protocol.PluginsValuesImpl;
+import org.jboss.forge.status.service.protocol.PluginsValues;
+import org.jboss.logging.Logger;
+import org.jboss.shotoku.ContentManager;
+import org.jboss.shotoku.Directory;
+import org.jboss.shotoku.Node;
+import org.jboss.shotoku.aop.Inject;
+import org.jboss.shotoku.exceptions.RepositoryException;
+import org.jboss.shotoku.exceptions.ResourceDoesNotExist;
+
+/**
+* @author Pawel Wrzeszcz (pawel.wrzeszcz [at] gmail.com)
+*/
+
+public class ScoresRepositoryManager {
+
+	private static Logger log = Logger.getLogger(ScoresServiceImpl.class);
+	
+	private static Map<String,PluginsValues> values = new HashMap<String,PluginsValues>();
+	
+	@Inject
+	private static ContentManager cm;
+	
+	@PersistenceContext (unitName="scores")
+    protected static EntityManager em;
+	
+	public static void save(PluginsValues pluginsValues, String portalName) throws ScoresSaveException {
+		
+		// Get directory where we store historical scores
+		Directory rootDir = cm.getRootDirectory();
+		String dirName = Locations.getShotokuStatusPath(portalName);
+		Directory dir = getDir(rootDir, dirName);
+		
+		// Get node to store scores
+		String nodeName = getCurrentNodeName();
+		Node node = null;
+		
+		log.info("Getting shotoku node...");
+		
+		try {
+			node = dir.getNode(nodeName);
+		} catch (RepositoryException e) {
+			log.error("Error while getting shotoku node: " +
+					dir.getFullName() + "/" + nodeName, e);
+		} catch (ResourceDoesNotExist e) {
+			node = createNode(dir, nodeName);
+		}
+		
+		// Save values and store then in cache
+		values.put(getCurrentNodeName(), pluginsValues);
+		save(node, pluginsValues);
+		
+		log.info("Saved succesfully");
+		/*
+		ScoresEntity se = new ScoresEntity("tttest");
+		em.persist(se);
+		log.info("Saved in DB");*/
+	}
+
+	public static PluginsValues get(String portalName, Calendar cal) throws GetScoresException {
+		
+		String key = getNodeName(cal);
+		log.info("Returning plugins values (" + key + ") ...");
+		
+		// null date means today
+		if (cal == null) {
+			cal = Calendar.getInstance();
+		}
+		
+		PluginsValues v = values.get(key);
+		
+		if (v == null) { // Not in cache => get value and store it
+			
+			log.info("Value not in cache...");
+			Directory rootDir = cm.getRootDirectory();
+			
+			Directory dir;
+			try {
+				dir = rootDir.getDirectory(Locations.
+											getShotokuStatusPath(portalName));	
+			} catch (RepositoryException e) {
+				throw new GetScoresException(e);
+			} catch (ResourceDoesNotExist e) {
+				throw new GetScoresException(e);
+			}
+
+			if (dir != null) {
+				v = getPluginsValues(dir, cal);
+				log.info("Finished.");
+				values.put(key,v);
+			}
+		} else {
+			log.info("Returned cached value.");
+		}
+			
+		return v;
+	}
+	
+	/**
+	 * Saves plugins values in the given Directory.
+	 * 
+	 * @param dir Directory to save values
+	 * @param pluginsValues PluginsValues to save
+	 * @throws ScoresSaveException 
+	 */
+	private synchronized static void save(Node node, PluginsValues pluginsValues)
+										throws ScoresSaveException {
+		
+		try {
+			OutputStream os = node.getOutputStream();
+			ObjectOutputStream oos = new ObjectOutputStream(os);
+			oos.writeObject((PluginsValues) pluginsValues);
+			oos.close(); 
+			node.save("update"); /* os is closed inside save() */
+			
+		} catch (Exception e) {
+			log.error("Error while saving shotoku node: " +
+					node.getFullName(), e);
+			throw new ScoresSaveException(e);
+		}
+	}
+	
+	/**
+	 * Creates new shotoku Node.
+	 * 
+	 * @param dir Directory in which new Node will be created
+	 * @param nodeName name of new Node
+	 * @return Node
+	 * @throws ScoresSaveException 
+	 */
+	private static Node createNode(Directory dir, String nodeName)
+											throws ScoresSaveException {
+		
+		Node node = null;
+		
+		try {
+			node = dir.newNode(nodeName);
+			node.save("create");
+		} catch (Exception e) {
+			log.error("Error while creating shotoku node: " +
+					dir.getFullName() + "/" + nodeName, e);
+			throw new ScoresSaveException(e);
+		}
+		
+		return node;
+	}
+
+	/**
+	 * Gets shotoku Directory with name <code>dirName</code>
+	 * from specified shotoku Directory.
+	 * If requested Directory does not exist it is created.
+	 * 
+	 * @param rootDir Directory where search will be performed
+	 * @param dirName name of searched Directory
+	 * @return Directory with the given name
+	 * or null when creating Directory failed.  
+	 * @throws ScoresSaveException 
+	 */
+	private static Directory getDir(Directory rootDir, String dirName)
+										throws ScoresSaveException {
+		
+		Directory dir = null;
+			
+		try {
+			dir = rootDir.getDirectory(dirName);
+		} catch (RepositoryException e) {
+			log.error("Error while getting shotoku directory " +
+					rootDir.getFullName() + "/" + dirName);
+			throw new ScoresSaveException(e);
+		} catch (ResourceDoesNotExist e) {
+			dir = createDir(rootDir, dirName);
+		}
+			
+		return dir;
+	}
+
+	/**
+	 * Creates new shotoku Directory.
+	 * 
+	 * @param rootDir Directory in which new subdirectory will be created
+	 * @param dirName name of new Directory
+	 * @return Directory or null if errors occured
+	 * @throws ScoresSaveException 
+	 */
+	private static Directory createDir(Directory rootDir, String dirName)
+												throws ScoresSaveException {
+		
+		Directory dir = null;
+		
+		try {
+			dir = rootDir.newDirectory(dirName);
+			dir.save("create");
+		} catch (Exception e) {
+			log.error("Error while creating shotoku directory: " +
+					rootDir.getFullName() + "/" + dirName, e);
+			throw new ScoresSaveException(e);
+		}
+		
+		return dir;
+	}
+
+	/**
+	 * Gets name of Node containing saved data from specified datestamp.
+	 * Notice that only year, month and day matter in passed <code>cal</code>.
+	 * 
+	 * @return Node name
+	 */
+	private static String getNodeName(Calendar cal) {
+		
+		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
+		
+		Date date = cal.getTime();
+		
+		return df.format(date);
+	}
+	
+	/**
+	 * @return name of Node containging data saved today
+	 */
+	private static String getCurrentNodeName() {
+		
+		Calendar today = Calendar.getInstance();
+		
+		return getNodeName(today); 
+	}
+	
+	/**
+	 * Returns historical scores from the specified directory.
+	 * 
+	 * @param dir Directory to read scores from
+	 * @return
+	 * @throws GetScoresException 
+	 */
+	private static PluginsValuesImpl getPluginsValues(Directory dir, Calendar cal)
+												throws GetScoresException {
+		
+		// Get appropriate node
+		Node node = null;
+		String nodeName = getNodeName(cal);
+		
+		try {
+			node = dir.getNode(nodeName);
+		} catch (Exception e) {
+			log.info("Unable to get shotoku node " +
+					dir.getFullName() + "/" + nodeName);
+			throw new GetScoresException(e);
+		}
+		
+		// Restore values
+		PluginsValuesImpl values = null;
+		try {
+			InputStream is = node.getContentInputStream();
+			ObjectInputStream ois = new ObjectInputStream(is);
+			values = (PluginsValuesImpl) ois.readObject();
+			ois.close();
+			is.close();
+		} catch (Exception e) {
+			log.error("Error while restoring scores from shotoku node " +
+					dir.getFullName() + "/" + nodeName);
+			throw new GetScoresException(e);
+		}
+		
+		return values;
+	}
+	
+}




More information about the jboss-svn-commits mailing list