Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
814 mhunt 1
package com.erggroup.buildtool.daemon;
2
 
3
import com.erggroup.buildtool.ripple.MutableChar;
4
import com.erggroup.buildtool.ripple.MutableInt;
5
import com.erggroup.buildtool.ripple.MutableString;
6
import com.erggroup.buildtool.ripple.Package;
7
import com.erggroup.buildtool.ripple.ReleaseManager;
8
 
9
import java.net.InetAddress;
10
 
11
import java.net.UnknownHostException;
12
 
13
import java.sql.SQLException;
14
 
15
import java.util.Iterator;
16
import java.util.Vector;
17
 
18
import org.apache.log4j.Logger;
19
import org.apache.log4j.xml.DOMConfigurator;
20
 
21
/**BuildDaemon sub component and entry point (main BuildDaemon thread)
22
 */
23
public class BuildDaemon
24
{
25
 
26
  /**hostname
27
   * @attribute
28
   */
29
  static String mHostname = new String();
30
 
31
  /**Logger
32
   * @attribute
33
   */
34
  private static final Logger mLogger = Logger.getLogger(BuildDaemon.class);
35
 
36
  /**Collection of ThreadIdentifier objects.
37
   * @attribute
38
   */
39
  private Vector mThreadCollection = new Vector();
40
 
41
  /**mThreadCollection items
42
   */
43
  private class ThreadIdentifier
44
  {
45
    /**rcon_id associated with the thread
46
     * @attribute
47
     */
48
    private final int mRcon_id;
49
 
50
    /**thread identifier
51
     * @attribute
52
     */
53
    private final Thread mThread;
54
 
55
    /**constructor
56
     */
57
    ThreadIdentifier(int rcon_id, Thread thread)
58
    {
59
      mLogger.debug("ThreadIdentifier " + rcon_id);
60
      mRcon_id = rcon_id;
61
      mThread = thread;
62
    }
63
 
64
    /**accessor
65
     */
66
    int get_rcon_id()
67
    {
68
      mLogger.debug("get_rcon_id");
69
      mLogger.info("get_rcon_id returned " + mRcon_id);
70
      return mRcon_id;
71
    }
72
 
73
    /**accessor
74
     */
75
    Thread get_thread()
76
    {
77
      mLogger.debug("get_thread");
78
      return mThread;
79
    }
80
  }
81
 
82
  /**main method for the Build Daemon program
83
   * instantiates a BuildDaemon object
84
   */
85
  public static void main(String[] args)
86
  {
87
    String abtdXml = System.getenv("ABT_HOME");
88
 
89
    if ( abtdXml != null )
90
    {
91
      abtdXml += System.getProperty( "file.separator" ) + "abtd.xml";
92
    }
93
    else
94
    {
95
      abtdXml = new String("abtd.xml");
96
    }
97
    DOMConfigurator.configure(abtdXml);
98
    mLogger.debug("main");
99
    String usage = 
100
      new String("Usage: java -jar abtdD.jar -c connectionString -u username -p password");
101
    String connectionString = System.getenv("GBE_RM_LOCATION");
102
    String username = System.getenv("GBE_RM_USERNAME");
103
    String password = System.getenv("GBE_RM_PASSWORD");
104
 
105
    for (int optind = 0; optind < args.length; optind++)
106
    {
107
      if (args[optind].equals("-c") && optind < (args.length - 1))
108
      {
109
        connectionString = args[++optind];
110
      }
111
      else if (args[optind].equals("-u") && optind < (args.length - 1))
112
      {
113
        username = args[++optind];
114
      }
115
      else if (args[optind].equals("-p") && optind < (args.length - 1))
116
      {
117
        password = args[++optind];
118
      }
119
    }
120
 
121
    if (connectionString.compareTo("") == 0 || 
122
        username.compareTo("") == 0 || password.compareTo("") == 0)
123
    {
124
      mLogger.fatal(usage);
125
      System.exit(1);
126
    }
127
 
128
    BuildDaemon buildDaemon = new BuildDaemon(connectionString, username, password);
129
    buildDaemon.cleanUp();
130
 
131
  }
132
 
133
  /**constructor, implements the sequence diagram spawn thread
134
   */
135
  public BuildDaemon(String connectionString, String username, 
136
                     String password)
137
  {
138
    mLogger.debug("BuildDaemon");
139
    ReleaseManager releaseManager = 
140
      new ReleaseManager(connectionString, username, password);
141
    boolean run = true;
142
    MutableInt rtag_id = new MutableInt();
143
    MutableInt rcon_id = new MutableInt();
144
    MutableChar daemon_mode = new MutableChar();
145
    MutableString gbebuildfilter = new MutableString();
146
    String hostname = new String("");
147
 
148
    try
149
    {
150
      InetAddress local = InetAddress.getLocalHost();
151
      hostname = local.getHostName();
152
      mHostname = hostname;
153
      mLogger.info("BuildDaemon set hostname " + mHostname);
154
 
155
      if ( Package.mGenericMachtype == null || Package.mGbeDpkg == null || Package.mGbeDply == null )
156
      {
157
        mLogger.fatal("run GBE_MACHTYPE or GBE_DPKG or GBE_DPLY not set");
158
        throw new Exception();
159
      }
160
 
161
      while (run)
162
      {
163
        try
164
        {
165
          releaseManager.queryReleaseConfig(hostname);
166
 
167
          rtag_id.value = 0;
168
          rcon_id.value = 0;
169
          daemon_mode.value = 'X';
170
          gbebuildfilter.value = "";
171
 
172
          boolean moreReleasesConfigured = 
173
            releaseManager.getFirstReleaseConfig(rtag_id, rcon_id, 
174
                                                 daemon_mode, gbebuildfilter);
175
 
176
          do
177
          {
178
            if (moreReleasesConfigured)
179
            {
180
              if (!isActive(rcon_id.value))
181
              {
182
                mLogger.info("BuildDaemon activating " + rtag_id + " " + rcon_id + " " + daemon_mode + " " + gbebuildfilter);
183
 
184
                // spawn and run the BuildThread
185
                if (daemon_mode.value == 'M')
186
                {
187
                  MasterThread thread = new MasterThread(rtag_id.value, rcon_id.value, gbebuildfilter.value);
188
                  ThreadIdentifier threadIdentifier = 
189
                    new ThreadIdentifier(rcon_id.value, thread);
190
                  mThreadCollection.add(threadIdentifier);
191
                  // begin thread execution and invoke thread.run();
192
                  thread.start();
193
                }
194
                else if (daemon_mode.value == 'S')
195
                {
196
                  SlaveThread thread = new SlaveThread(rtag_id.value, rcon_id.value, gbebuildfilter.value);
197
                  ThreadIdentifier threadIdentifier = 
198
                    new ThreadIdentifier(rcon_id.value, thread);
199
                  mThreadCollection.add(threadIdentifier);
200
                  // begin thread execution and invoke thread.run();
201
                  thread.start();
202
                }
203
              }
204
 
205
              moreReleasesConfigured = 
206
                  releaseManager.getNextReleaseConfig(rtag_id, rcon_id, 
207
                                                      daemon_mode, gbebuildfilter);
208
            }
209
          }
210
          while (moreReleasesConfigured);
211
 
212
          if ( connectionString.compareTo("unit test spawn thread") == 0)
213
          {
214
            throw new Exception();
215
          }
216
 
217
          mLogger.warn("BuildDaemon sleep for 10 mins");
218
          Thread.sleep(600000);
219
          mLogger.info("BuildDaemon sleep returned");
220
        }
221
        catch (SQLException e)
222
        {
223
          mLogger.warn("BuildDaemon caught SQLException");
224
        }
225
        catch (InterruptedException e)
226
        {
227
          mLogger.warn("BuildDaemon caught InterruptedException");
228
        }
229
        catch (Exception e)
230
        {
231
          // fatal
232
          mLogger.fatal("BuildDaemon caught Exception");
233
          run = false;
234
        }
235
      }
236
    }
237
    catch( UnknownHostException e )
238
    {
239
      mLogger.fatal("BuildDaemon caught UnknownHostException");
240
    }
241
    catch( Exception e )
242
    {
243
      mLogger.fatal("BuildDaemon caught Exception");
244
    }
245
  }
246
 
247
  /**calls isAlive on the Thread object associated with the rcon_id
248
   */
249
  public boolean isActive(final int rcon_id)
250
  {
251
    mLogger.debug("isActive " + rcon_id);
252
    boolean retVal = false;
253
 
254
    for (Iterator it = mThreadCollection.iterator(); it.hasNext(); )
255
    {
256
      ThreadIdentifier threadIdentifier = (ThreadIdentifier) it.next();
257
 
258
      if (threadIdentifier.get_rcon_id() == rcon_id)
259
      {
260
        if (threadIdentifier.get_thread().isAlive())
261
        {
262
          retVal = true;
263
        }
264
        break;
265
      }
266
    }
267
    mLogger.info("isActive returned " + retVal);
268
    return retVal;
269
  }
270
 
271
  /**terminates all BuildThreads
272
   */
273
  public void cleanUp()
274
  {
275
    mLogger.debug("cleanUp");
276
    for (Iterator it = mThreadCollection.iterator(); it.hasNext(); )
277
    {
278
      ThreadIdentifier threadIdentifier = (ThreadIdentifier) it.next();
279
 
280
      if (threadIdentifier.get_thread().isAlive())
281
      {
282
        try
283
        {
284
          threadIdentifier.get_thread().interrupt();
285
          threadIdentifier.get_thread().join();
286
        }
287
        catch( InterruptedException e )
288
        {
289
        }
290
      }
291
    }
292
  }
293
}