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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Jul 14 08:44:49 EDT 2006


Author: wrzep
Date: 2006-07-14 08:44:41 -0400 (Fri, 14 Jul 2006)
New Revision: 5094

Added:
   labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresManager.java
   labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresRepositoryManager.java
Removed:
   labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresManager.java
   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/Status.java
   labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/LastNDaysStatusPlugin.java
   labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresServiceImpl.java
Log:
JBLAB-689
Pawel


Modified: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/Status.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/Status.java	2006-07-14 12:30:11 UTC (rev 5093)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/Status.java	2006-07-14 12:44:41 UTC (rev 5094)
@@ -40,6 +40,7 @@
 import org.jboss.forge.status.exceptions.InvalidPluginsConfigurationException;
 import org.jboss.forge.status.exceptions.GetScoresException;
 import org.jboss.forge.status.exceptions.ScoresSaveException;
+import org.jboss.forge.status.service.impl.ScoresManager;
 import org.jboss.forge.status.service.impl.protocol.PluginConfImpl;
 import org.jboss.forge.status.service.impl.protocol.ServiceConfImpl;
 import org.jboss.forge.status.service.protocol.PluginConf;
@@ -48,7 +49,6 @@
 import org.jboss.forge.status.templates.Column;
 import org.jboss.forge.status.templates.Columns;
 import org.jboss.forge.status.templates.MatrixTemplate;
-import org.jboss.forge.status.tools.ScoresManager;
 
 import org.jboss.logging.Logger;
 import org.jboss.portal.common.context.DelegateContext;

Modified: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/LastNDaysStatusPlugin.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/LastNDaysStatusPlugin.java	2006-07-14 12:30:11 UTC (rev 5093)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/LastNDaysStatusPlugin.java	2006-07-14 12:44:41 UTC (rev 5094)
@@ -29,8 +29,8 @@
 import org.jboss.forge.status.common.Tags;
 import org.jboss.forge.status.exceptions.InvalidPluginPropertiesException;
 import org.jboss.forge.status.exceptions.GetScoresException;
+import org.jboss.forge.status.service.impl.ScoresManager;
 import org.jboss.forge.status.tools.Plugins;
-import org.jboss.forge.status.tools.ScoresManager;
 
 /**
 * @author Pawel Wrzeszcz (pawel.wrzeszcz at gmail.com)

Added: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresManager.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresManager.java	2006-07-14 12:30:11 UTC (rev 5093)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresManager.java	2006-07-14 12:44:41 UTC (rev 5094)
@@ -0,0 +1,233 @@
+/*
+ * 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.status.service.impl;
+
+import java.util.Calendar;
+import java.util.List;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.rmi.PortableRemoteObject;
+
+import org.jboss.forge.common.projects.Projects;
+import org.jboss.forge.status.exceptions.GetScoresException;
+import org.jboss.forge.status.exceptions.ScoresSaveException;
+import org.jboss.forge.status.service.ScoresService;
+import org.jboss.forge.status.service.ScoresServiceLocal;
+import org.jboss.forge.status.service.ScoresServiceRemote;
+import org.jboss.forge.status.service.protocol.PluginConf;
+import org.jboss.forge.status.service.protocol.PluginsValues;
+import org.jboss.forge.status.service.protocol.ServiceConf;
+import org.jboss.forge.status.common.Constants;
+import org.jboss.logging.Logger;
+
+/**
+* @author Pawel Wrzeszcz (pawel.wrzeszcz [at] gmail.com)
+*/
+
+public class ScoresManager {
+	
+	private static String portalName;
+	private static Properties properties;
+	private static boolean isRemote;
+	private static Logger log;
+	
+	public static void init(String portalName, ServiceConf serviceConf) {
+		
+		ScoresManager.portalName = portalName;
+		
+		properties = serviceConf.getProperties();
+		isRemote = serviceConf.isRemote();
+		
+		log = Logger.getLogger(ScoresManager.class);
+	}
+	
+	public static void saveAsync(final Projects projects,
+			final List<PluginConf> pluginsConf) {
+		
+		new Thread() {
+			
+			public void run() {
+				ScoresManager.saveAsyncOnce(projects, pluginsConf);
+			}
+			
+		}.start();
+		
+	}
+	
+	private static synchronized void saveAsyncOnce(Projects projects,
+										List<PluginConf> pluginsConf) {
+		try {
+			log.info("Saving scores asynchronously...");
+			ScoresManager.save(projects, pluginsConf);
+			log.info("Finished async save.");
+		} catch (ScoresSaveException e) {
+			log.warn("Async saving scores failed.");
+		}
+	}
+
+	public static void save(Projects projects,
+										List<PluginConf> pluginsConf)
+										throws ScoresSaveException {
+
+		ScoresService service = null;
+
+		try {
+			service = getService();
+		} catch (NamingException e) {
+			throw new ScoresSaveException(e);
+		}
+
+		service.save(projects, pluginsConf, portalName);	
+	}
+	
+	
+	public static boolean hasScores(Calendar cal) throws NamingException {
+		
+		ScoresService service = getService();
+		
+		return service.hasPluginsValues(portalName, cal);
+	}
+	
+	
+	public static PluginsValues getScores(Calendar cal)
+										throws GetScoresException {
+
+		PluginsValues pluginsValues = null;
+
+		try {
+			ScoresService service = getService(); 
+			pluginsValues = service.getPluginsValues(portalName, cal);
+		} catch (Exception e) {
+			throw new GetScoresException(e);
+		}
+
+		if (pluginsValues == null) {
+			throw new GetScoresException();
+		}
+
+		return pluginsValues;
+	}
+	
+	
+	public static PluginsValues getLatestScores()
+									throws GetScoresException {
+		
+		Calendar cal = Calendar.getInstance();
+		int attempts = 0;
+		
+		for(;;) {
+			
+			System.out.println("Trying " + cal.getTime().toString());
+			
+			boolean hasScores;
+			
+			try {
+				hasScores = ScoresManager.hasScores(cal);
+			} catch (NamingException e) {
+				throw new GetScoresException(e);
+			}
+			
+			if (hasScores) {
+				return ScoresManager.getScores(cal);
+			}
+			
+			if ((++attempts) == Constants.MAX_ATTEMPTS) {
+				throw new GetScoresException(
+						"Can not find any recently saved scores. " +
+						"Gave up after " + attempts + " attempts.");
+			}
+			
+			cal.add(Calendar.DAY_OF_MONTH, -1);
+		}
+	}
+
+
+	/**
+	 * For the specified plugin returns historical project score
+	 * from the given <code>date</code> in the past.
+	 * 
+	 * Notice that only year, month and day matter in passed dates.
+	 * 
+	 * @return project score from the given date for specified plugin
+	 * @throws GetScoresException
+	 */
+	public static long getValue(String pluginId, String projectId,
+											Calendar cal)
+			throws GetScoresException {
+		
+		PluginsValues pluginsValues = getScores(cal);
+	    
+		return pluginsValues.getPluginValue(pluginId, projectId);
+	}
+
+	public static long getValue(String id, String projectId,
+			Calendar start, Calendar end) throws GetScoresException {
+		
+		long value;
+		Calendar cal = start;
+		
+		for (;;) {
+			
+			if (cal.after(end)) {
+				throw new GetScoresException();
+			}
+			
+			try {
+				value = getValue(id, projectId, cal);
+				return value;
+			} catch (GetScoresException e) {
+				// try next
+				cal.add(Calendar.DAY_OF_MONTH, 1);
+			}	
+		}
+	}
+	
+	private static ScoresService getService() throws NamingException {
+		
+		Context context = null;
+		if (isRemote) {
+			context = new InitialContext(properties);
+		} else {
+			context = new InitialContext();
+		}
+			
+		//	Perform look up using JNDI name of ejb object
+		String serviceBinding =
+			(isRemote ? Constants.STATUS_REMOTE_SERVICE :
+						Constants.STATUS_LOCAL_SERVICE);
+		
+		Object ref = context.lookup(serviceBinding);
+		
+		// Narrow the reference of the object returned by lookup() method
+		ScoresService service = 
+			(ScoresService) PortableRemoteObject.narrow( ref, 
+									(isRemote ? ScoresServiceRemote.class :
+												ScoresServiceLocal.class));
+		
+		return service;
+	}
+	
+}

Added: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresRepositoryManager.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresRepositoryManager.java	2006-07-14 12:30:11 UTC (rev 5093)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresRepositoryManager.java	2006-07-14 12:44:41 UTC (rev 5094)
@@ -0,0 +1,306 @@
+/*
+ * 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.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.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.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;
+	}
+	
+}

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 12:30:11 UTC (rev 5093)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresServiceImpl.java	2006-07-14 12:44:41 UTC (rev 5094)
@@ -43,7 +43,6 @@
 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;
 

Deleted: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresManager.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresManager.java	2006-07-14 12:30:11 UTC (rev 5093)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresManager.java	2006-07-14 12:44:41 UTC (rev 5094)
@@ -1,233 +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.status.tools;
-
-import java.util.Calendar;
-import java.util.List;
-import java.util.Properties;
-
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import javax.rmi.PortableRemoteObject;
-
-import org.jboss.forge.common.projects.Projects;
-import org.jboss.forge.status.exceptions.GetScoresException;
-import org.jboss.forge.status.exceptions.ScoresSaveException;
-import org.jboss.forge.status.service.ScoresService;
-import org.jboss.forge.status.service.ScoresServiceLocal;
-import org.jboss.forge.status.service.ScoresServiceRemote;
-import org.jboss.forge.status.service.protocol.PluginConf;
-import org.jboss.forge.status.service.protocol.PluginsValues;
-import org.jboss.forge.status.service.protocol.ServiceConf;
-import org.jboss.forge.status.common.Constants;
-import org.jboss.logging.Logger;
-
-/**
-* @author Pawel Wrzeszcz (pawel.wrzeszcz [at] gmail.com)
-*/
-
-public class ScoresManager {
-	
-	private static String portalName;
-	private static Properties properties;
-	private static boolean isRemote;
-	private static Logger log;
-	
-	public static void init(String portalName, ServiceConf serviceConf) {
-		
-		ScoresManager.portalName = portalName;
-		
-		properties = serviceConf.getProperties();
-		isRemote = serviceConf.isRemote();
-		
-		log = Logger.getLogger(ScoresManager.class);
-	}
-	
-	public static void saveAsync(final Projects projects,
-			final List<PluginConf> pluginsConf) {
-		
-		new Thread() {
-			
-			public void run() {
-				ScoresManager.saveAsyncOnce(projects, pluginsConf);
-			}
-			
-		}.start();
-		
-	}
-	
-	private static synchronized void saveAsyncOnce(Projects projects,
-										List<PluginConf> pluginsConf) {
-		try {
-			log.info("Saving scores asynchronously...");
-			ScoresManager.save(projects, pluginsConf);
-			log.info("Finished async save.");
-		} catch (ScoresSaveException e) {
-			log.warn("Async saving scores failed.");
-		}
-	}
-
-	public static void save(Projects projects,
-										List<PluginConf> pluginsConf)
-										throws ScoresSaveException {
-
-		ScoresService service = null;
-
-		try {
-			service = getService();
-		} catch (NamingException e) {
-			throw new ScoresSaveException(e);
-		}
-
-		service.save(projects, pluginsConf, portalName);	
-	}
-	
-	
-	public static boolean hasScores(Calendar cal) throws NamingException {
-		
-		ScoresService service = getService();
-		
-		return service.hasPluginsValues(portalName, cal);
-	}
-	
-	
-	public static PluginsValues getScores(Calendar cal)
-										throws GetScoresException {
-
-		PluginsValues pluginsValues = null;
-
-		try {
-			ScoresService service = getService(); 
-			pluginsValues = service.getPluginsValues(portalName, cal);
-		} catch (Exception e) {
-			throw new GetScoresException(e);
-		}
-
-		if (pluginsValues == null) {
-			throw new GetScoresException();
-		}
-
-		return pluginsValues;
-	}
-	
-	
-	public static PluginsValues getLatestScores()
-									throws GetScoresException {
-		
-		Calendar cal = Calendar.getInstance();
-		int attempts = 0;
-		
-		for(;;) {
-			
-			System.out.println("Trying " + cal.getTime().toString());
-			
-			boolean hasScores;
-			
-			try {
-				hasScores = ScoresManager.hasScores(cal);
-			} catch (NamingException e) {
-				throw new GetScoresException(e);
-			}
-			
-			if (hasScores) {
-				return ScoresManager.getScores(cal);
-			}
-			
-			if ((++attempts) == Constants.MAX_ATTEMPTS) {
-				throw new GetScoresException(
-						"Can not find any recently saved scores. " +
-						"Gave up after " + attempts + " attempts.");
-			}
-			
-			cal.add(Calendar.DAY_OF_MONTH, -1);
-		}
-	}
-
-
-	/**
-	 * For the specified plugin returns historical project score
-	 * from the given <code>date</code> in the past.
-	 * 
-	 * Notice that only year, month and day matter in passed dates.
-	 * 
-	 * @return project score from the given date for specified plugin
-	 * @throws GetScoresException
-	 */
-	public static long getValue(String pluginId, String projectId,
-											Calendar cal)
-			throws GetScoresException {
-		
-		PluginsValues pluginsValues = getScores(cal);
-	    
-		return pluginsValues.getPluginValue(pluginId, projectId);
-	}
-
-	public static long getValue(String id, String projectId,
-			Calendar start, Calendar end) throws GetScoresException {
-		
-		long value;
-		Calendar cal = start;
-		
-		for (;;) {
-			
-			if (cal.after(end)) {
-				throw new GetScoresException();
-			}
-			
-			try {
-				value = getValue(id, projectId, cal);
-				return value;
-			} catch (GetScoresException e) {
-				// try next
-				cal.add(Calendar.DAY_OF_MONTH, 1);
-			}	
-		}
-	}
-	
-	private static ScoresService getService() throws NamingException {
-		
-		Context context = null;
-		if (isRemote) {
-			context = new InitialContext(properties);
-		} else {
-			context = new InitialContext();
-		}
-			
-		//	Perform look up using JNDI name of ejb object
-		String serviceBinding =
-			(isRemote ? Constants.STATUS_REMOTE_SERVICE :
-						Constants.STATUS_LOCAL_SERVICE);
-		
-		Object ref = context.lookup(serviceBinding);
-		
-		// Narrow the reference of the object returned by lookup() method
-		ScoresService service = 
-			(ScoresService) PortableRemoteObject.narrow( ref, 
-									(isRemote ? ScoresServiceRemote.class :
-												ScoresServiceLocal.class));
-		
-		return service;
-	}
-	
-}

Deleted: 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 12:30:11 UTC (rev 5093)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresRepositoryManager.java	2006-07-14 12:44:41 UTC (rev 5094)
@@ -1,308 +0,0 @@
-/*
- * 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