Blame | Last modification | View Log | RSS feed
package com.erggroup.buildtool.daemon;import com.erggroup.buildtool.ripple.MutableChar;import com.erggroup.buildtool.ripple.MutableInt;import com.erggroup.buildtool.ripple.MutableString;import com.erggroup.buildtool.ripple.Package;import com.erggroup.buildtool.ripple.ReleaseManager;import java.net.InetAddress;import java.net.UnknownHostException;import java.sql.SQLException;import java.util.Iterator;import java.util.Vector;import org.apache.log4j.Logger;import org.apache.log4j.xml.DOMConfigurator;/**BuildDaemon sub component and entry point (main BuildDaemon thread)*/public class BuildDaemon{/**hostname* @attribute*/static String mHostname = new String();/**Logger* @attribute*/private static final Logger mLogger = Logger.getLogger(BuildDaemon.class);/**Collection of ThreadIdentifier objects.* @attribute*/private Vector mThreadCollection = new Vector();/**mThreadCollection items*/private class ThreadIdentifier{/**rcon_id associated with the thread* @attribute*/private final int mRcon_id;/**thread identifier* @attribute*/private final Thread mThread;/**constructor*/ThreadIdentifier(int rcon_id, Thread thread){mLogger.debug("ThreadIdentifier " + rcon_id);mRcon_id = rcon_id;mThread = thread;}/**accessor*/int get_rcon_id(){mLogger.debug("get_rcon_id");mLogger.info("get_rcon_id returned " + mRcon_id);return mRcon_id;}/**accessor*/Thread get_thread(){mLogger.debug("get_thread");return mThread;}}/**main method for the Build Daemon program* instantiates a BuildDaemon object*/public static void main(String[] args){String abtdXml = System.getenv("ABT_HOME");if ( abtdXml != null ){abtdXml += System.getProperty( "file.separator" ) + "abtd.xml";}else{abtdXml = new String("abtd.xml");}DOMConfigurator.configure(abtdXml);mLogger.debug("main");String usage =new String("Usage: java -jar abtdD.jar -c connectionString -u username -p password");String connectionString = System.getenv("GBE_RM_LOCATION");String username = System.getenv("GBE_RM_USERNAME");String password = System.getenv("GBE_RM_PASSWORD");for (int optind = 0; optind < args.length; optind++){if (args[optind].equals("-c") && optind < (args.length - 1)){connectionString = args[++optind];}else if (args[optind].equals("-u") && optind < (args.length - 1)){username = args[++optind];}else if (args[optind].equals("-p") && optind < (args.length - 1)){password = args[++optind];}}if (connectionString.compareTo("") == 0 ||username.compareTo("") == 0 || password.compareTo("") == 0){mLogger.fatal(usage);System.exit(1);}BuildDaemon buildDaemon = new BuildDaemon(connectionString, username, password);buildDaemon.cleanUp();}/**constructor, implements the sequence diagram spawn thread*/public BuildDaemon(String connectionString, String username,String password){mLogger.debug("BuildDaemon");ReleaseManager releaseManager =new ReleaseManager(connectionString, username, password);boolean run = true;MutableInt rtag_id = new MutableInt();MutableInt rcon_id = new MutableInt();MutableChar daemon_mode = new MutableChar();MutableString gbebuildfilter = new MutableString();String hostname = new String("");try{InetAddress local = InetAddress.getLocalHost();hostname = local.getHostName();mHostname = hostname;mLogger.info("BuildDaemon set hostname " + mHostname);if ( Package.mGenericMachtype == null || Package.mGbeDpkg == null || Package.mGbeDply == null ){mLogger.fatal("run GBE_MACHTYPE or GBE_DPKG or GBE_DPLY not set");throw new Exception();}while (run){try{releaseManager.queryReleaseConfig(hostname);rtag_id.value = 0;rcon_id.value = 0;daemon_mode.value = 'X';gbebuildfilter.value = "";boolean moreReleasesConfigured =releaseManager.getFirstReleaseConfig(rtag_id, rcon_id,daemon_mode, gbebuildfilter);do{if (moreReleasesConfigured){if (!isActive(rcon_id.value)){mLogger.info("BuildDaemon activating " + rtag_id + " " + rcon_id + " " + daemon_mode + " " + gbebuildfilter);// spawn and run the BuildThreadif (daemon_mode.value == 'M'){MasterThread thread = new MasterThread(rtag_id.value, rcon_id.value, gbebuildfilter.value);ThreadIdentifier threadIdentifier =new ThreadIdentifier(rcon_id.value, thread);mThreadCollection.add(threadIdentifier);// begin thread execution and invoke thread.run();thread.start();}else if (daemon_mode.value == 'S'){SlaveThread thread = new SlaveThread(rtag_id.value, rcon_id.value, gbebuildfilter.value);ThreadIdentifier threadIdentifier =new ThreadIdentifier(rcon_id.value, thread);mThreadCollection.add(threadIdentifier);// begin thread execution and invoke thread.run();thread.start();}}moreReleasesConfigured =releaseManager.getNextReleaseConfig(rtag_id, rcon_id,daemon_mode, gbebuildfilter);}}while (moreReleasesConfigured);if ( connectionString.compareTo("unit test spawn thread") == 0){throw new Exception();}mLogger.warn("BuildDaemon sleep for 10 mins");Thread.sleep(600000);mLogger.info("BuildDaemon sleep returned");}catch (SQLException e){mLogger.warn("BuildDaemon caught SQLException");}catch (InterruptedException e){mLogger.warn("BuildDaemon caught InterruptedException");}catch (Exception e){// fatalmLogger.fatal("BuildDaemon caught Exception");run = false;}}}catch( UnknownHostException e ){mLogger.fatal("BuildDaemon caught UnknownHostException");}catch( Exception e ){mLogger.fatal("BuildDaemon caught Exception");}}/**calls isAlive on the Thread object associated with the rcon_id*/public boolean isActive(final int rcon_id){mLogger.debug("isActive " + rcon_id);boolean retVal = false;for (Iterator it = mThreadCollection.iterator(); it.hasNext(); ){ThreadIdentifier threadIdentifier = (ThreadIdentifier) it.next();if (threadIdentifier.get_rcon_id() == rcon_id){if (threadIdentifier.get_thread().isAlive()){retVal = true;}break;}}mLogger.info("isActive returned " + retVal);return retVal;}/**terminates all BuildThreads*/public void cleanUp(){mLogger.debug("cleanUp");for (Iterator it = mThreadCollection.iterator(); it.hasNext(); ){ThreadIdentifier threadIdentifier = (ThreadIdentifier) it.next();if (threadIdentifier.get_thread().isAlive()){try{threadIdentifier.get_thread().interrupt();threadIdentifier.get_thread().join();}catch( InterruptedException e ){}}}}}