From: <jbo...@li...> - 2006-05-12 23:03:10
|
Author: wrzep Date: 2006-05-12 19:02:59 -0400 (Fri, 12 May 2006) New Revision: 4211 Added: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresMDB.java labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresManagerAsync.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/service/model/ScoresServiceLocal.java Log: JBLAB-599 MDB for JMM 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-05-12 20:02:22 UTC (rev 4210) +++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/Status.java 2006-05-12 23:02:59 UTC (rev 4211) @@ -23,7 +23,6 @@ package org.jboss.forge.status; import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -46,6 +45,7 @@ import org.jboss.forge.status.templates.Columns; import org.jboss.forge.status.templates.MatrixTemplate; import org.jboss.forge.status.tools.ScoresManager; +import org.jboss.forge.status.tools.ScoresManagerAsync; import org.jboss.forge.status.tools.ServiceConf; import org.jboss.forge.status.tools.Tags; @@ -91,15 +91,15 @@ PluginsValues pluginsValues; try { + //<debug> + ScoresManagerAsync.init(portalName, serviceConf); + ScoresManagerAsync.save(projects, pluginsConf); + // </debug> + // Compute and save current scores ScoresManager.save(projects, pluginsConf); - // <debug>; - //long v = ScoresManager.getValue("downloads", "jbosswiki", new Date(), new Date()); - //System.out.println("value = " + v); - // </debug> - pluginsValues = ScoresManager.getCurrentScores(); } catch (ScoresSaveException e) { Added: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresMDB.java =================================================================== --- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresMDB.java 2006-05-12 20:02:22 UTC (rev 4210) +++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/impl/ScoresMDB.java 2006-05-12 23:02:59 UTC (rev 4211) @@ -0,0 +1,362 @@ +/* + * 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.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.ActivationConfigProperty; +import javax.ejb.MessageDriven; +import javax.jms.JMSException; +import javax.jms.MapMessage; +import javax.jms.Message; +import javax.jms.MessageListener; + +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.model.PluginConf; +import org.jboss.forge.status.tools.Plugins; +import org.jboss.forge.status.tools.Tools; + +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) +*/ + +@MessageDriven(activateConfig = +{ + @ActivationConfigProperty(propertyName="destinationType", + propertyValue="javax.jms.Queue"), + @ActivationConfigProperty(propertyName="destination", + propertyValue="queue/jmm") +}) +public class ScoresMDB implements MessageListener { + + private static Logger log = Logger.getLogger(ScoresServiceImpl.class); + + private Map<String,PluginsValuesImpl> values = new HashMap<String,PluginsValuesImpl>(); + + @Inject + private ContentManager cm; + + public void onMessage(Message msg) { + + MapMessage message = (MapMessage) msg; + + try { + String request = message.getString("name"); + + System.out.println("request name: " + request); + } catch (JMSException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + } + + /** + * Saves current plugin values. + * + * @param projects Projects for which plugin values will be saved + * @param plugins Plugins for which values will be saved + * @param portalName + * @return true iff saving was successful + */ + public void save(Projects projects, List<PluginConf> pluginsConf, + String portalName) throws ScoresSaveException { + + log.info("Saving..."); + + // Get plugins from conf + Plugins plugins = new Plugins(pluginsConf, portalName, projects); + + // Get directory where we store historical scores + Directory rootDir = cm.getRootDirectory(); + String dirName = Tools.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); + } + + /** + * Saves plugins values in the given Directory. + * + * @param dir Directory to save values + * @param pluginsValues PluginsValues to save + * @throws ScoresSaveException + */ + private 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() */ + log.info("Saved succesfully"); + } 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. + * + * Notice that only year, month and day matter in passed dates. + * + * @return scores from the given date + * @throws GetScoresException + */ + public PluginsValuesImpl getPluginsValues(String portalName, Date date) + throws GetScoresException { + + log.info("Returning plugins values..."); + + // null date means today + if (date == null) { + Calendar cal = Calendar.getInstance(); + date = cal.getTime(); + } + + String key = getNodeName(date); + 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(Tools.getShotokuStatusPath(portalName)); + } catch (RepositoryException e) { + throw new GetScoresException(e); + } catch (ResourceDoesNotExist e) { + throw new GetScoresException(e); + } + + if (dir != null) { + v = getPluginsValues(dir, date); + log.info("Finished."); + values.put(key,v); + } + } + + return v; + } + + /** + * Returns historical scores from the specified directory. + * + * @param dir Directory to read scores from + * @return + * @throws GetScoresException + */ + private PluginsValuesImpl getPluginsValues(Directory dir, Date date) + throws GetScoresException { + + // Get appropriate node + Node node = null; + String nodeName = getNodeName(date); + + try { + node = dir.getNode(nodeName); + } catch (Exception e) { + log.error("Error while getting 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>date</code>. + * + * @param date datestamp + * @return Node name + */ + private String getNodeName(Date date) { + + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + + return df.format(date); + } + + /** + * @return name of Node containging data saved today + */ + private String getCurrentNodeName() { + + Calendar cal = Calendar.getInstance(); + Date today = cal.getTime(); + + return getNodeName(today); + } + + public PluginValuesImpl get(String portalName, String pluginId, Date date) + throws GetScoresException { + + PluginsValuesImpl pv = getPluginsValues(portalName, date); + return pv.getPluginValues(pluginId); + } + +} Modified: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/model/ScoresServiceLocal.java =================================================================== --- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/model/ScoresServiceLocal.java 2006-05-12 20:02:22 UTC (rev 4210) +++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/service/model/ScoresServiceLocal.java 2006-05-12 23:02:59 UTC (rev 4211) @@ -25,6 +25,7 @@ /** * @author Pawel Wrzeszcz (pawel.wrzeszcz [at] gmail.com) */ + public interface ScoresServiceLocal extends ScoresService { } \ No newline at end of file Added: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresManagerAsync.java =================================================================== --- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresManagerAsync.java 2006-05-12 20:02:22 UTC (rev 4210) +++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/tools/ScoresManagerAsync.java 2006-05-12 23:02:59 UTC (rev 4211) @@ -0,0 +1,172 @@ +/* + * 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.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import javax.jms.JMSException; +import javax.jms.MapMessage; +import javax.jms.Queue; +import javax.jms.QueueConnection; +import javax.jms.QueueConnectionFactory; +import javax.jms.QueueSender; +import javax.jms.QueueSession; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +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.model.PluginConf; +import org.jboss.forge.status.service.model.PluginsValues; + +/** +* @author Pawel Wrzeszcz (pawel.wrzeszcz [at] gmail.com) +*/ + +public class ScoresManagerAsync { + + private static String portalName; + private static boolean isRemote; + private static Properties properties; + + public static void init(String portalName, ServiceConf serviceConf) { + + ScoresManagerAsync.portalName = portalName; + + isRemote = serviceConf.isRemote(); + System.out.println("remote: " + isRemote); + + properties = serviceConf.getProperties(); + } + + public static void save(Projects projects, List<PluginConf> pluginsConf) + throws ScoresSaveException { + + // Fill arguments + Map<String,Object> args = new HashMap<String,Object>(); + args.put("projects", projects); //TODO store these args names somewhere ;-) + args.put("pluginsConf", pluginsConf); + args.put("portalName", portalName); + + // Send message + try { + sendMessage("save", args); + } catch (Exception e) { + throw new ScoresSaveException(e); + } + + // TODO poll/wait for results + } + + public static PluginsValues getScores(Date date, Date toDate) + throws GetScoresException { + //TODO toDate + + + // Fill arguments + Map<String,Object> args = new HashMap<String,Object>(); + args.put("date", date); + args.put("portalName", portalName); + + // Send message + try { + sendMessage("getPluginsValues", args); + } catch (Exception e) { + throw new GetScoresException(e); + } + + // TODO poll/wait for results + + PluginsValues pluginsValues = null; + + if (pluginsValues == null) { + throw new GetScoresException(); + } + + return pluginsValues; + } + + /** + * For the specified plugin returns historical project score + * from the given <code>date</code> in the past. + * If scores from specified day are not available, + * the next available scores saved at the day before or equal + * to <code>toDate</code> are returned. + * + * 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, + Date date, Date toDate) + throws GetScoresException { + + PluginsValues pluginsValues = getScores(date, toDate); + + return pluginsValues.getPluginValue(pluginId, projectId); + } + + public static PluginsValues getCurrentScores() + throws GetScoresException { + + Date now = new Date(); + + return getScores(now, now); + } + + private static void sendMessage(String name, Map<String,Object> args) throws + NamingException, JMSException { + + Queue queue = null; + QueueConnection connection = null; + QueueSession session = null; + QueueSender sender = null; + Context context = new InitialContext(properties); + + queue = (Queue) context.lookup("queue/jmm"); + QueueConnectionFactory factory = + (QueueConnectionFactory) context.lookup("ConnectionFactory"); + connection = factory.createQueueConnection(); + session = connection.createQueueSession(false, + QueueSession.AUTO_ACKNOWLEDGE); + + // Copy arguments into message + MapMessage message = session.createMapMessage(); + message.setString("name", name); + for (String argName : args.keySet()) { + message.setObject(argName, args.get(argName)); + } + + sender = session.createSender(queue); + sender.send(message); + // sess.commit (); TODO closing vs committing + session.close (); + } + +} |