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