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