Skip to main content
summaryrefslogtreecommitdiffstats
blob: b88d097862abf2fb727ef3192c328a5df29ca844 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * 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.ui.test.manager.connection;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.ote.core.environment.interfaces.ITestEnvironment;
import org.eclipse.osee.ote.core.environment.status.TestComplete;
import org.eclipse.osee.ote.service.ConnectionEvent;
import org.eclipse.osee.ote.ui.test.manager.core.TestManagerEditor;
import org.eclipse.osee.ote.ui.test.manager.internal.TestManagerPlugin;
import org.eclipse.osee.ote.ui.test.manager.jobs.StoreOutfileJob;
import org.eclipse.osee.ote.ui.test.manager.models.OutputModelJob;
import org.eclipse.osee.ote.ui.test.manager.pages.scriptTable.ScriptTask;
import org.eclipse.osee.ote.ui.test.manager.pages.scriptTable.ScriptTask.ScriptStatusEnum;

/**
 * @author Andrew M. Finkbeiner
 */
public abstract class ScriptManager implements Runnable {
   private final Map<String, ScriptTask> guidToScriptTask = new HashMap<String, ScriptTask>();
   private TestManagerStatusListener statusListenerImpl;
   private final TestManagerEditor testManager;

   private volatile boolean updateScriptTable;
   private StructuredViewer stv;
   private ScheduledExecutorService updater;
   private Set<ScriptTask> tasksToUpdate;
   private ITestEnvironment connectedEnv;
   private UUID sessionKey;

   public ScriptManager(TestManagerEditor testManager, StructuredViewer stv) {
      this.testManager = testManager;
      this.stv = stv;

      tasksToUpdate = new HashSet<ScriptTask>();
      updater = Executors.newScheduledThreadPool(1, new ThreadFactory() {

         @Override
         public Thread newThread(Runnable r) {
            Thread th = new Thread(r, "TM Table updater");
            th.setDaemon(true);
            return th;
         }

      });
      updater.scheduleAtFixedRate(this, 0, 2000, TimeUnit.MILLISECONDS);
      OutputModelJob.createSingleton(this);
   }

   public abstract void abortScript(boolean isBatchAbort) throws RemoteException;

   public void notifyScriptDequeued(String className) {
      ScriptTask task = guidToScriptTask.get(className);
      if (task != null) {
         guidToScriptTask.remove(task);
      }
   }

   /**
    * This should be called after the environment is received in order to configure necessary items.
    * 
    * @return null if successful, otherwise a string describing the error
    */
   public boolean connect(ConnectionEvent event) {

      connectedEnv = event.getEnvironment();
      sessionKey = event.getSessionKey();
      try {
         /*
          * Setup the status listener for commands
          */
         statusListenerImpl = new TestManagerStatusListener(testManager, this);
         return false;
      } catch (Exception e) {
         TestManagerPlugin.log(Level.SEVERE, "failed to connect script manager", e);
         return true;
      }
   }

   /**
    * This should NOT be called directly, users should call the HostDataStore's disconnect.
    */
   public boolean disconnect(ConnectionEvent event) {
      connectedEnv = null;
      sessionKey = null;
      guidToScriptTask.clear();
      statusListenerImpl.unregisterEventListener();
      return false;
   }

   public boolean onConnectionLost() {
      connectedEnv = null;
      sessionKey = null;
      guidToScriptTask.clear();
      statusListenerImpl.unregisterEventListener();
      return false;
   }

   public ScriptTask getScriptTask(String name) {
      return guidToScriptTask.get(name);
   }

   public void notifyScriptQueued(GUID theGUID, final ScriptTask script) {
      guidToScriptTask.put(script.getScriptModel().getTestClass(), script);
      script.setStatus(ScriptStatusEnum.IN_QUEUE);
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            if (stv.getControl().isDisposed()) {
               return;
            }
            stv.refresh(script);
         }
      });
   }

   public void updateScriptTableViewer(final ScriptTask task) {
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            if (stv.getControl().isDisposed()) {
               return;
            }
            stv.refresh(task);
         }
      });
   }

   public void updateScriptTableViewerTimed(ScriptTask task) {
      updateScriptTable = true;
      synchronized (tasksToUpdate) {
         tasksToUpdate.add(task);
      }
   }

   @Override
   public void run() {
      if (updateScriptTable) {
         updateScriptTable = false;
         Displays.ensureInDisplayThread(new Runnable() {
            @Override
            public void run() {
               synchronized (tasksToUpdate) {
                  if (stv.getControl().isDisposed()) {
                     return;
                  }
                  for (ScriptTask task : tasksToUpdate) {
                     stv.refresh(task);
                  }
                  tasksToUpdate.clear();
               }
            }
         });
      }
   }

   protected TestManagerEditor getTestManagerEditor() {
      return testManager;
   }

   public abstract void addTestsToQueue(List<ScriptTask> scripts);

   public void notifyScriptStart(final ScriptTask task) {
      task.setStatus(ScriptStatusEnum.RUNNING);
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            stv.refresh(task);
         }
      });
   }

   public void storeOutFile(ScriptTask task, TestComplete testComplete, boolean isValidRun) {
      if (task.getScriptModel() != null) {
         Job job =
            new StoreOutfileJob(connectedEnv, testManager, this, task, testComplete.getClientOutfilePath(),
               testComplete.getServerOutfilePath(), isValidRun);
         StoreOutfileJob.scheduleJob(job);
      }
   }

   protected UUID getSessionKey() {
      return sessionKey;
   }
}

Back to the top