From: <tim...@us...> - 2007-08-10 15:49:32
|
Revision: 4974 http://smartfrog.svn.sourceforge.net/smartfrog/?rev=4974&view=rev Author: tim_doering Date: 2007-08-10 08:49:30 -0700 (Fri, 10 Aug 2007) Log Message: ----------- SFOS-387: Host state is now updated upon Avalanche's startup and XMPP presence changes SFOS-293: Javadocs here and there Modified Paths: -------------- trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/client/monitor/xmpp/SFAvalancheEventService.java trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/server/ServerSetup.java trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/handlers/DefaultHostStateChangeHandler.java trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/xmpp/LivenessListener.java Added Paths: ----------- trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/ActiveProfileUpdater.java Modified: trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/client/monitor/xmpp/SFAvalancheEventService.java =================================================================== --- trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/client/monitor/xmpp/SFAvalancheEventService.java 2007-08-10 15:48:16 UTC (rev 4973) +++ trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/client/monitor/xmpp/SFAvalancheEventService.java 2007-08-10 15:49:30 UTC (rev 4974) @@ -11,7 +11,6 @@ */ package org.smartfrog.avalanche.client.monitor.xmpp; -import org.jivesoftware.smack.XMPPException; import org.smartfrog.avalanche.shared.MonitoringConstants; import org.smartfrog.avalanche.shared.MonitoringEvent; import org.smartfrog.avalanche.shared.MonitoringEventDefaultImpl; @@ -112,20 +111,16 @@ } } - - public void sendEvent(MonitoringEvent e) throws SmartFrogException{ - try{ - adapter.sendEvent(e); - }catch(XMPPException ex){ - throw new SmartFrogException(ex); - } - } public synchronized void sfStart() throws SmartFrogException, RemoteException { super.sfStart(); } - public void sfTerminate(TerminationRecord arg0) { + /** + * Called upon the daemon's termination?! + * @param arg0 + */ + public void sfTerminate(TerminationRecord arg0) { sfLog().info("Closing XMPP Client Adapter"); adapter.close(); super.sfTerminate(arg0); Modified: trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/server/ServerSetup.java =================================================================== --- trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/server/ServerSetup.java 2007-08-10 15:48:16 UTC (rev 4973) +++ trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/server/ServerSetup.java 2007-08-10 15:49:30 UTC (rev 4974) @@ -12,10 +12,11 @@ package org.smartfrog.avalanche.server; import org.jivesoftware.smack.XMPPException; -import org.smartfrog.avalanche.server.monitor.handlers.ActiveProfileUpdateHandler; -import org.smartfrog.avalanche.server.monitor.handlers.HostUpdateHandler; -import org.smartfrog.avalanche.server.monitor.handlers.HostUpdateRosterHandler; +import org.jivesoftware.smack.Roster; +import org.jivesoftware.smack.packet.Presence; +import org.smartfrog.avalanche.server.monitor.handlers.*; import org.smartfrog.avalanche.shared.xmpp.XMPPAdapter; +import org.smartfrog.avalanche.shared.ActiveProfileUpdater; import org.smartfrog.sfcore.logging.Log; import org.smartfrog.sfcore.logging.LogFactory; @@ -217,34 +218,63 @@ // On adding/deleting hosts, perform the same on the XMPP Server and its users rosters. factory.getHostManager().addHandler(new HostUpdateRosterHandler(adminAdapter, listenerAdapter)); + // Getting the most recent presence of host + Roster roster = this.getListenerAdapter().getRoster(); + String[] hostNames = this.getFactory().getHostManager().listHosts(); + boolean hostAvailable = false; + Presence p = null; + for (int i = 0; i < hostNames.length; i++) { + p = roster.getPresence(hostNames[i] + "@" + this.getXmppServer()); + if (p != null) { + hostAvailable = p.getType().equals(Presence.Type.AVAILABLE); + } else { + hostAvailable = false; + } + ActiveProfileUpdater.setMachineAvailability(factory.getActiveProfileManager(), hostNames[i], hostAvailable); + } + // TODO : Start Smartfrog on server if its not already running using avalancheHome } catch (XMPPException e) { log.fatal("Avalanche InitializAtion failed : ", e); } } - - public XMPPAdapter getAdminAdapter(){ + + /** + * Gets the XMPPAdapter which is logged in to the XMPP Server as admin + * in order to create new users. + * @return XMPPAdapter logged in as 'admin' + */ + public XMPPAdapter getAdminAdapter(){ return adminAdapter ; } - - public XMPPAdapter getListenerAdapter(){ + + /** + * Gets the XMPPAdapter which is logged in as the Avalanche WebUser + * Hosts report to the AVL user (messages, presence updates, etc.) + * @return XMPPAdapter logged in as 'avl' + */ + public XMPPAdapter getListenerAdapter(){ return listenerAdapter ; } /** * Shuts down Avalanche server. - * @throws Exception + * @throws Exception is thrown if anything went wrong */ - public void shutdown() throws Exception{ - System.out.println("Shutting down Avalanche ..."); - log.info("Shutting down Avalanche ..."); + public void shutdown() throws Exception { + // Close the XMPP connections + log.info("Closing the XMPP connections..."); + this.getAdminAdapter().close(); + this.getListenerAdapter().close(); + + // Shutdown the rest of the machine + log.info("Shutting down Avalanche..."); if( null != factory){ factory.close(); factory = null ; - }else{ + } else { log.error("AvalancheContextListener:contextDestroyed() - Shutting down, Avalanche was not initialized completely"); - System.out.println("AvalancheContextListener:contextDestroyed() - Shutting down, Avalanche was not initialized completely"); } /* try{ Added: trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/ActiveProfileUpdater.java =================================================================== --- trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/ActiveProfileUpdater.java (rev 0) +++ trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/ActiveProfileUpdater.java 2007-08-10 15:49:30 UTC (rev 4974) @@ -0,0 +1,56 @@ +package org.smartfrog.avalanche.shared; + +/** +(C) Copyright 1998-2007 Hewlett-Packard Development Company, LP + +This library 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 library 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 library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +For more information: www.smartfrog.org +*/ + +import org.smartfrog.avalanche.server.ActiveProfileManager; +import org.smartfrog.avalanche.core.activeHostProfile.ActiveProfileType; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Modifies the ActiveProfile of a given hosts + */ +public class ActiveProfileUpdater { + private static Log log = LogFactory.getLog(ActiveProfileUpdater.class); + + /** + * Updates the availability record of a given machine + * This needs to be performed e.g. after a XMPP presence change is detected. + * + * @param profileManager is the ActiveProfileManager which holds the Profile of the specifed host. + * @param hostId is the name of the machine + * @param availability true if the machine is available; false if it is not. + */ + public static void setMachineAvailability(ActiveProfileManager profileManager, String hostId, boolean availability) { + try { + ActiveProfileType type = profileManager.getProfile(hostId); + + // No ActiveProfile found - create one + if (type == null) { + try { + type = profileManager.newProfile(hostId); + } catch (Exception x) { + + } + } + + // Profile could not be created - log error + if (type != null) + type.setHostState(availability?"Available":"Not Available"); + else + log.error("Could not retrieve ActiveProfileType for host " + hostId); + + profileManager.setProfile(type); + } catch (Exception ex) { + log.error(ex); + } + } +} Modified: trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/handlers/DefaultHostStateChangeHandler.java =================================================================== --- trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/handlers/DefaultHostStateChangeHandler.java 2007-08-10 15:48:16 UTC (rev 4973) +++ trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/handlers/DefaultHostStateChangeHandler.java 2007-08-10 15:49:30 UTC (rev 4974) @@ -13,13 +13,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.smartfrog.avalanche.core.activeHostProfile.ActiveProfileType; import org.smartfrog.avalanche.server.ActiveProfileManager; import org.smartfrog.avalanche.server.AvalancheFactory; -import org.smartfrog.avalanche.server.DatabaseAccessException; -import org.smartfrog.avalanche.shared.handlers.HostStateChangeHandler; import org.smartfrog.avalanche.server.modules.ModuleCreationException; -import org.smartfrog.avalanche.shared.HostStateEvent; +import org.smartfrog.avalanche.shared.*; /** * Updates state of the host in Active Profile. This is invoked when a host goes down or a @@ -43,39 +40,11 @@ } /** - * updates the host availability status in database. - * TODO: Host state is set as "Available" or "Not Available" change to something - * better. . + * Updates the state of a given host in the database + * @param e is the event that contains the information about the host. */ public void handleEvent(HostStateEvent e) { log.info("Hosts State Changed: " + e.getHostName() + " : " + e.isAvailable()); - - String hostPath = e.getHostName(); - String hostPresence = e.isAvailable() ? "Available" : "Not Available"; - - try { - ActiveProfileType type = profileManager.getProfile(hostPath); - - // No ActiveProfile found - create one - if (type == null) { - log.info("Creating new ActiveProfile for host " + hostPath); - try { - type = profileManager.newProfile(hostPath); - } catch (Exception x) { - - } - } - - // Profile could not be created - log error - if (type != null) - type.setHostState(hostPresence); - else - log.error("Could not retrieve ActiveProfileType for host " + hostPath); - - profileManager.setProfile(type); - } catch (DatabaseAccessException ex) { - log.error(ex); - } + ActiveProfileUpdater.setMachineAvailability(profileManager, e.getHostName(), e.isAvailable()); } - } Modified: trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/xmpp/LivenessListener.java =================================================================== --- trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/xmpp/LivenessListener.java 2007-08-10 15:48:16 UTC (rev 4973) +++ trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/xmpp/LivenessListener.java 2007-08-10 15:49:30 UTC (rev 4974) @@ -88,13 +88,8 @@ log.error("Current presence could not be retrieved." + label); event.setAvailable(false); }else{ - if( p.getType().equals(Presence.Type.AVAILABLE)){ - log.info("Current presence is: AVAILABLE." + label); - event.setAvailable(true); - } else { - log.info("Current presence is: NOT AVAILABLE." + label); - event.setAvailable(false); - } + log.info("Current presence is available:" + p.getType().equals(Presence.Type.AVAILABLE) + label); + event.setAvailable(p.getType().equals(Presence.Type.AVAILABLE)); } log.info("Dispatching event to all other handlers."); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |