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
 
820 mhunt 161
      boolean firstTime = true;
162
 
814 mhunt 163
      while (run)
164
      {
165
        try
166
        {
820 mhunt 167
          if ( !firstTime )
168
          {
169
            mLogger.warn("BuildDaemon sleep for 10 mins");
170
            Thread.sleep(600000);
171
            mLogger.info("BuildDaemon sleep returned");
172
          }
173
          else
174
          {
175
            firstTime = false;    
176
          }
177
 
814 mhunt 178
          releaseManager.queryReleaseConfig(hostname);
179
 
180
          rtag_id.value = 0;
181
          rcon_id.value = 0;
182
          daemon_mode.value = 'X';
183
          gbebuildfilter.value = "";
184
 
185
          boolean moreReleasesConfigured = 
186
            releaseManager.getFirstReleaseConfig(rtag_id, rcon_id, 
187
                                                 daemon_mode, gbebuildfilter);
188
 
189
          do
190
          {
191
            if (moreReleasesConfigured)
192
            {
193
              if (!isActive(rcon_id.value))
194
              {
818 mhunt 195
                mLogger.warn("BuildDaemon activating " + rtag_id + " " + rcon_id + " " + daemon_mode + " " + gbebuildfilter);
814 mhunt 196
 
197
                // spawn and run the BuildThread
198
                if (daemon_mode.value == 'M')
199
                {
200
                  MasterThread thread = new MasterThread(rtag_id.value, rcon_id.value, gbebuildfilter.value);
201
                  ThreadIdentifier threadIdentifier = 
202
                    new ThreadIdentifier(rcon_id.value, thread);
203
                  mThreadCollection.add(threadIdentifier);
204
                  // begin thread execution and invoke thread.run();
205
                  thread.start();
206
                }
207
                else if (daemon_mode.value == 'S')
208
                {
209
                  SlaveThread thread = new SlaveThread(rtag_id.value, rcon_id.value, gbebuildfilter.value);
210
                  ThreadIdentifier threadIdentifier = 
211
                    new ThreadIdentifier(rcon_id.value, thread);
212
                  mThreadCollection.add(threadIdentifier);
213
                  // begin thread execution and invoke thread.run();
214
                  thread.start();
215
                }
216
              }
217
 
218
              moreReleasesConfigured = 
219
                  releaseManager.getNextReleaseConfig(rtag_id, rcon_id, 
220
                                                      daemon_mode, gbebuildfilter);
221
            }
222
          }
223
          while (moreReleasesConfigured);
224
 
225
          if ( connectionString.compareTo("unit test spawn thread") == 0)
226
          {
820 mhunt 227
            run = false;
814 mhunt 228
          }
229
 
230
        }
231
        catch (SQLException e)
232
        {
233
          mLogger.warn("BuildDaemon caught SQLException");
234
        }
235
        catch (InterruptedException e)
236
        {
237
          mLogger.warn("BuildDaemon caught InterruptedException");
238
        }
239
        catch (Exception e)
240
        {
820 mhunt 241
          mLogger.warn("BuildDaemon caught Exception");
814 mhunt 242
        }
243
      }
244
    }
245
    catch( UnknownHostException e )
246
    {
247
      mLogger.fatal("BuildDaemon caught UnknownHostException");
248
    }
249
    catch( Exception e )
250
    {
251
      mLogger.fatal("BuildDaemon caught Exception");
252
    }
253
  }
254
 
255
  /**calls isAlive on the Thread object associated with the rcon_id
256
   */
257
  public boolean isActive(final int rcon_id)
258
  {
259
    mLogger.debug("isActive " + rcon_id);
260
    boolean retVal = false;
261
 
262
    for (Iterator it = mThreadCollection.iterator(); it.hasNext(); )
263
    {
264
      ThreadIdentifier threadIdentifier = (ThreadIdentifier) it.next();
265
 
266
      if (threadIdentifier.get_rcon_id() == rcon_id)
267
      {
268
        if (threadIdentifier.get_thread().isAlive())
269
        {
270
          retVal = true;
271
        }
272
        break;
273
      }
274
    }
275
    mLogger.info("isActive returned " + retVal);
276
    return retVal;
277
  }
278
 
279
  /**terminates all BuildThreads
280
   */
281
  public void cleanUp()
282
  {
283
    mLogger.debug("cleanUp");
284
    for (Iterator it = mThreadCollection.iterator(); it.hasNext(); )
285
    {
286
      ThreadIdentifier threadIdentifier = (ThreadIdentifier) it.next();
287
 
288
      if (threadIdentifier.get_thread().isAlive())
289
      {
290
        try
291
        {
292
          threadIdentifier.get_thread().interrupt();
293
          threadIdentifier.get_thread().join();
294
        }
295
        catch( InterruptedException e )
296
        {
297
        }
298
      }
299
    }
300
  }
301
}