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
 
192
      if ( Package.mGenericMachtype == null || Package.mGbeDpkg == null || Package.mGbeDply == null )
193
      {
194
        mLogger.fatal("run GBE_MACHTYPE or GBE_DPKG or GBE_DPLY not set");
868 mhunt 195
        throw new Exception("run GBE_MACHTYPE or GBE_DPKG or GBE_DPLY not set");
814 mhunt 196
      }
197
 
820 mhunt 198
      boolean firstTime = true;
199
 
814 mhunt 200
      while (run)
201
      {
202
        try
203
        {
820 mhunt 204
          if ( !firstTime )
205
          {
206
            mLogger.warn("BuildDaemon sleep for 10 mins");
207
            Thread.sleep(600000);
208
            mLogger.info("BuildDaemon sleep returned");
209
          }
210
          else
211
          {
212
            firstTime = false;    
213
          }
214
 
814 mhunt 215
          releaseManager.queryReleaseConfig(hostname);
216
 
217
          rtag_id.value = 0;
218
          rcon_id.value = 0;
219
          daemon_mode.value = 'X';
220
 
221
          boolean moreReleasesConfigured = 
222
            releaseManager.getFirstReleaseConfig(rtag_id, rcon_id, 
896 mhunt 223
                                                 daemon_mode);
814 mhunt 224
 
225
          do
226
          {
227
            if (moreReleasesConfigured)
228
            {
229
              if (!isActive(rcon_id.value))
230
              {
896 mhunt 231
                mLogger.warn("BuildDaemon activating " + rtag_id.value + " " + rcon_id.value + " " + daemon_mode.value);
814 mhunt 232
 
896 mhunt 233
                String utf = null;
234
 
235
                if ( connectionString.compareTo("unit test spawn thread") == 0 )
236
                {
237
                  utf = connectionString;
238
                }
239
 
814 mhunt 240
                // spawn and run the BuildThread
241
                if (daemon_mode.value == 'M')
242
                {
896 mhunt 243
                  MasterThread thread = new MasterThread(rtag_id.value, rcon_id.value, utf);
814 mhunt 244
                  ThreadIdentifier threadIdentifier = 
245
                    new ThreadIdentifier(rcon_id.value, thread);
246
                  mThreadCollection.add(threadIdentifier);
247
                  // begin thread execution and invoke thread.run();
248
                  thread.start();
249
                }
250
                else if (daemon_mode.value == 'S')
251
                {
896 mhunt 252
                  SlaveThread thread = new SlaveThread(rtag_id.value, rcon_id.value, utf);
814 mhunt 253
                  ThreadIdentifier threadIdentifier = 
254
                    new ThreadIdentifier(rcon_id.value, thread);
255
                  mThreadCollection.add(threadIdentifier);
256
                  // begin thread execution and invoke thread.run();
257
                  thread.start();
258
                }
259
              }
260
 
261
              moreReleasesConfigured = 
262
                  releaseManager.getNextReleaseConfig(rtag_id, rcon_id, 
896 mhunt 263
                                                      daemon_mode);
814 mhunt 264
            }
265
          }
266
          while (moreReleasesConfigured);
267
 
268
          if ( connectionString.compareTo("unit test spawn thread") == 0)
269
          {
820 mhunt 270
            run = false;
814 mhunt 271
          }
272
 
273
        }
274
        catch (SQLException e)
275
        {
276
          mLogger.warn("BuildDaemon caught SQLException");
277
        }
278
        catch (InterruptedException e)
279
        {
280
          mLogger.warn("BuildDaemon caught InterruptedException");
281
        }
282
        catch (Exception e)
283
        {
820 mhunt 284
          mLogger.warn("BuildDaemon caught Exception");
814 mhunt 285
        }
286
      }
287
    }
288
    catch( UnknownHostException e )
289
    {
290
      mLogger.fatal("BuildDaemon caught UnknownHostException");
291
    }
292
    catch( Exception e )
293
    {
294
      mLogger.fatal("BuildDaemon caught Exception");
295
    }
296
  }
297
 
298
  /**calls isAlive on the Thread object associated with the rcon_id
299
   */
300
  public boolean isActive(final int rcon_id)
301
  {
302
    mLogger.debug("isActive " + rcon_id);
303
    boolean retVal = false;
304
 
864 mhunt 305
    for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
814 mhunt 306
    {
864 mhunt 307
      ThreadIdentifier threadIdentifier = it.next();
814 mhunt 308
 
309
      if (threadIdentifier.get_rcon_id() == rcon_id)
310
      {
311
        if (threadIdentifier.get_thread().isAlive())
312
        {
313
          retVal = true;
844 dpurdie 314
          break;
814 mhunt 315
        }
844 dpurdie 316
        else
317
        {
318
            mLogger.warn("isActive found dead thread " + rcon_id );
319
        }
814 mhunt 320
      }
321
    }
322
    mLogger.info("isActive returned " + retVal);
323
    return retVal;
324
  }
325
 
326
  /**terminates all BuildThreads
327
   */
328
  public void cleanUp()
329
  {
330
    mLogger.debug("cleanUp");
864 mhunt 331
    for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
814 mhunt 332
    {
864 mhunt 333
      ThreadIdentifier threadIdentifier = it.next();
814 mhunt 334
 
335
      if (threadIdentifier.get_thread().isAlive())
336
      {
337
        try
338
        {
339
          threadIdentifier.get_thread().interrupt();
340
          threadIdentifier.get_thread().join();
341
        }
342
        catch( InterruptedException e )
343
        {
344
        }
345
      }
346
    }
347
  }
348
}