Skip to main content
summaryrefslogtreecommitdiffstats
blob: 02d229d237b7759684e6ee450be271980997944d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.core;

import java.lang.ref.WeakReference;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.ote.core.environment.TestEnvironment;
import org.eclipse.osee.ote.core.internal.Activator;

/**
 * @author Ken J. Aguilar
 */
public abstract class OseeTestThread {

   private static final Logger logger = Logger.getLogger("osee.test.core.OseeTestThread");
   private final Thread thread;
   private final WeakReference<TestEnvironment> env;
   private static final HashSet<OseeTestThread> threadList = new HashSet<OseeTestThread>(32);
   private volatile Throwable causeOfDeath = null;
   private volatile Date timeOfDeath = null;

   /**
    * Creates the thread with the given name and as a non daemon thread
    */
   public OseeTestThread(String name, TestEnvironment env) {
      this(name, false, null, env);
   }

   public OseeTestThread(String name, ThreadGroup group, TestEnvironment env) {
      this(name, false, group, env);
   }

   public Thread.State getState() {
      return thread.getState();
   }

   /**
    * Creates the thread with the given name and daemon flag
    * 
    * @param name the name of this thread
    * @param isDaemon marks the thread as a daemon thread
    */
   public OseeTestThread(String name, boolean isDaemon, ThreadGroup group, TestEnvironment env) {
      GCHelper.getGCHelper().addRefWatch(this);
      this.env = new WeakReference<TestEnvironment>(env);
      thread = new Thread(group, name) {

         @Override
         public void run() {
            try {
               OseeTestThread.this.run();
               synchronized (threadList) {
                  threadList.remove(OseeTestThread.this);
               }
            } catch (TestException e) {
               OseeLog.log(Activator.class, e.getLevel(),
                  "TestException in " + e.getThreadName() + ": " + e.getMessage(), e);
               cleanupAfterException(e);
            } catch (Throwable t) {
               OseeLog.log(Activator.class, Level.SEVERE, "Unhandled exception in " + thread.getName(), t);
               cleanupAfterException(t);
            }
         }
      };
      thread.setDaemon(isDaemon);
      threadList.add(this);
   }

   /**
    * Starts the thread
    */
   public void start() {
      thread.start();
   }

   public void setName(String name) {
      thread.setName(name);
   }

   public String getName() {
      return thread.getName();
   }

   public boolean isAlive() {
      return thread.isAlive();
   }

   public void interrupt() {
      thread.interrupt();
   }

   private static final class Trace extends RuntimeException {
      private static final long serialVersionUID = 8520935964300439490L;

      Trace() {
         super("call trace");
      }
   }

   /**
    * This method will be called upon thread execution
    */
   protected abstract void run() throws Exception;

   public void join() throws InterruptedException {
      thread.join();
   }

   public void join(int milliseconds) throws InterruptedException {
      thread.join(milliseconds);
   }

   public TestEnvironment getEnvironment() {
      return OseeTestThread.this.env.get();
   }

   public static Collection<OseeTestThread> getThreads() {
      return threadList;
   }

   private synchronized void cleanupAfterException(Throwable t) {
      causeOfDeath = t;
      timeOfDeath = Calendar.getInstance().getTime();
      this.env.get().handleException(t, Level.OFF);
   }

   public Throwable getCauseOfDeath() {
      return causeOfDeath;
   }

   public Date getTimeOfDeath() {
      return timeOfDeath;
   }

   public static void clearThreadReferences() {
      synchronized (threadList) {
         threadList.clear();
      }
   }

   public boolean isInterrupted() {
      return thread.isInterrupted();
   }

   public void setDaemon(boolean isDaemon) {
      thread.setDaemon(isDaemon);
   }

   public Thread getThread() {
      return thread;
   }
}

Back to the top