Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8790ec98cfa1d21854c7ed79d299ae7b0215de2d (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*******************************************************************************
 * 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.environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;

import org.eclipse.core.runtime.Platform;
import org.eclipse.osee.connection.service.IServiceConnector;
import org.eclipse.osee.connection.service.LocalConnector;
import org.eclipse.osee.framework.jdk.core.reportdata.ReportDataListener;
import org.eclipse.osee.framework.jdk.core.util.EnhancedProperties;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.Message;
import org.eclipse.osee.framework.messaging.MessagingGateway;
import org.eclipse.osee.framework.messaging.NodeInfo;
import org.eclipse.osee.ote.OseeLogStatusCallback;
import org.eclipse.osee.ote.core.GCHelper;
import org.eclipse.osee.ote.core.OseeTestThread;
import org.eclipse.osee.ote.core.OteProperties;
import org.eclipse.osee.ote.core.TestScript;
import org.eclipse.osee.ote.core.cmd.Command;
import org.eclipse.osee.ote.core.environment.interfaces.IAssociatedObjectListener;
import org.eclipse.osee.ote.core.environment.interfaces.ICancelTimer;
import org.eclipse.osee.ote.core.environment.interfaces.IEnvironmentFactory;
import org.eclipse.osee.ote.core.environment.interfaces.IExecutionUnitManagement;
import org.eclipse.osee.ote.core.environment.interfaces.IRuntimeLibraryManager;
import org.eclipse.osee.ote.core.environment.interfaces.IScriptControl;
import org.eclipse.osee.ote.core.environment.interfaces.ITestEnvironment;
import org.eclipse.osee.ote.core.environment.interfaces.ITestEnvironmentAccessor;
import org.eclipse.osee.ote.core.environment.interfaces.ITestEnvironmentListener;
import org.eclipse.osee.ote.core.environment.interfaces.ITestLogger;
import org.eclipse.osee.ote.core.environment.interfaces.ITestStation;
import org.eclipse.osee.ote.core.environment.interfaces.ITimeout;
import org.eclipse.osee.ote.core.environment.interfaces.ITimerControl;
import org.eclipse.osee.ote.core.framework.IRunManager;
import org.eclipse.osee.ote.core.framework.command.ICommandHandle;
import org.eclipse.osee.ote.core.framework.command.ITestContext;
import org.eclipse.osee.ote.core.framework.command.ITestServerCommand;
import org.eclipse.osee.ote.core.internal.Activator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**
 * @author Andrew M. Finkbeiner
 */
public abstract class TestEnvironment implements TestEnvironmentInterface, ITestEnvironmentAccessor, ITestContext {

   private final List<ITestEnvironmentListener> envListeners = new ArrayList<ITestEnvironmentListener>(32);
   private IExecutionUnitManagement executionUnitManagement;

   private File outDir = null;
   private final ITestStation testStation;
   private boolean batchMode = false;
   private final HashMap<String, Remote> controlInterfaces = new HashMap<String, Remote>();
   private final IEnvironmentFactory factory;
   private IServiceConnector connector;
   private final IRuntimeLibraryManager runtimeManager;

   @Deprecated
   private final HashMap<Class<?>, Object> associatedObjects;
   @Deprecated
   private final HashMap<Class<?>, ArrayList<IAssociatedObjectListener>> associatedObjectListeners;
   @Deprecated
   private boolean isEnvSetup = false;
   @Deprecated
   private final List<IScriptCompleteEvent> scriptCompleteListeners = new ArrayList<IScriptCompleteEvent>();
   @Deprecated
   private final List<IScriptSetupEvent> scriptSetupListeners = new ArrayList<IScriptSetupEvent>();

   private OteServerSideEndprointRecieve oteServerSideEndpointRecieve;
   private OteServerSideEndpointSender oteServerSideEndpointSender;
   private final ServiceTracker messagingServiceTracker;

   private volatile boolean isShutdown = false;
   private NodeInfo oteEmbeddedBroker;
   private ServiceRegistration<TestEnvironmentInterface> myRegistration;

   protected TestEnvironment(IEnvironmentFactory factory) {
      GCHelper.getGCHelper().addRefWatch(this);
      try {
         oteEmbeddedBroker = new NodeInfo("OTEEmbeddedBroker", new URI("vm://localhost?broker.persistent=false"));
      } catch (URISyntaxException ex) {
         OseeLog.log(TestEnvironment.class, Level.SEVERE, ex);
      }
      this.factory = factory;
      this.testStation = factory.getTestStation();
      this.runtimeManager = factory.getRuntimeManager();

      this.associatedObjectListeners = new HashMap<Class<?>, ArrayList<IAssociatedObjectListener>>();
      this.associatedObjects = new HashMap<Class<?>, Object>(100);
      this.batchMode = OteProperties.isOseeOteInBatchModeEnabled();

      messagingServiceTracker = setupOteMessagingSenderAndReceiver();
   }

   public void init(IServiceConnector connector) {
      this.connector = connector;
   }

   private void setupDefaultConnector() {
      EnhancedProperties props = new EnhancedProperties();
      try {
         props.setProperty("station", InetAddress.getLocalHost().getHostName());
      } catch (UnknownHostException ex) {
         OseeLog.log(TestEnvironment.class, Level.SEVERE, ex);
      }
      props.setProperty("date", new Date());
      props.setProperty("group", "OSEE Test Environment");
      props.setProperty("owner", System.getProperty("user.name"));
      connector = new LocalConnector(this, Integer.toString(this.getUniqueId()), props);
   }

   private ServiceTracker setupOteMessagingSenderAndReceiver() {
      oteServerSideEndpointRecieve = new OteServerSideEndprointRecieve();
      oteServerSideEndpointSender = new OteServerSideEndpointSender(this);
      BundleContext context = Platform.getBundle("org.eclipse.osee.ote.core").getBundleContext();
      return getServiceTracker(MessagingGateway.class.getName(), new OteEnvironmentTrackerCustomizer(context,
            oteServerSideEndpointRecieve, oteServerSideEndpointSender,
            OteServerSideEndpointSender.OTE_SERVER_SIDE_SEND_PROTOCOL));
   }

   public void sendCommand(Command command) {
      Activator.getInstance().getCommandDistributer().distribute(command);
   }

   public void sendMessageToServer(Message message) {
      oteServerSideEndpointRecieve.recievedMessage(message);
   }

   public ServiceTracker getServiceTracker(String clazz, ServiceTrackerCustomizer customizer) {
      return Activator.getInstance().getServiceTracker(clazz, customizer);
   }

   @Override
   public ServiceTracker getServiceTracker(String clazz) {
      return getServiceTracker(clazz, null);
   }

   @Override
   public ICommandHandle addCommand(ITestServerCommand cmd) throws ExportException {
      return factory.getCommandManager().addCommand(cmd, this);
   }

   @Override
   public IRunManager getRunManager() {
      return factory.getRunManager();
   }

   @Override
   public IRuntimeLibraryManager getRuntimeManager() {
      return this.runtimeManager;
   }

   @Override
   public IEnvironmentFactory getEnvironmentFactory() {
      return factory;
   }

   @Override
   public boolean isInBatchMode() {
      return batchMode;
   }

   @Override
   public void setBatchMode(boolean isInBatchMode) {
      if (!OteProperties.isOseeOteInBatchModeEnabled()) {
         this.batchMode = isInBatchMode;
      }
   }

   @Override
   public void addEnvironmentListener(ITestEnvironmentListener listener) {
      envListeners.add(listener);
   }

   @Override
   public boolean addTask(EnvironmentTask task) {
      factory.getTimerControl().addTask(task, this);
      return true;
   }

   public boolean equals(ITestEnvironment testEnvironment) throws RemoteException {
      if (testEnvironment.getUniqueId() == getUniqueId()) {
         return true;
      } else {
         return false;
      }
   }

   @Override
   public long getEnvTime() {
      return getTimerCtrl().getEnvTime();
   }

   @Override
   public IExecutionUnitManagement getExecutionUnitManagement() {
      return this.executionUnitManagement;
   }

   @Override
   public ITestLogger getLogger() {
      return factory.getTestLogger();
   }

   @Override
   public List<String> getQueueLabels() {
      List<String> list = new ArrayList<String>();
      list.add("Description");
      return list;
   }

   @Override
   public abstract Object getModel(String modelClassName);

   @Override
   public IScriptControl getScriptCtrl() {
      return factory.getScriptControl();
   }

   @Override
   public byte[] getScriptOutfile(String filepath) throws RemoteException {
      try {
         File file = new File(filepath);
         InputStream is = new FileInputStream(file);
         long length = file.length();
         byte[] bytes = new byte[(int) length];

         int numRead = is.read(bytes);
         if (numRead < bytes.length) {
            throw new IOException("Could not completely read file " + file.getName());
         }
         is.close();
         OseeLog.log(TestEnvironment.class, Level.FINE, "going to send " + bytes.length + " bytes to the client");

         return bytes;
      } catch (Exception ex) {
         throw new RemoteException("Error retrieving the script output", ex);
      }
   }

   @Override
   public ITestStation getTestStation() {
      return testStation;
   }

   @Override
   public ITimerControl getTimerCtrl() {
      return factory.getTimerControl();
   }

   @Override
   public int getUniqueId() {
      return this.hashCode();
   }

   private final void removeAllTasks() {
      factory.getTimerControl().cancelAllTasks();
   }

   @Override
   public URL setBatchLibJar(byte[] batchJar) throws IOException {
      String path = System.getProperty("user.home") + File.separator + TestEnvironment.class.getName();

      File dir = new File(path, "batchLibCache");
      if (!dir.isDirectory()) {
         dir.mkdir();
      }
      File jar = File.createTempFile("Batch", ".jar", dir);
      Lib.writeBytesToFile(batchJar, jar);
      return jar.toURI().toURL();
   }

   @Override
   public ICancelTimer setTimerFor(ITimeout listener, int time) {
      return getTimerCtrl().setTimerFor(listener, time);
   }

   @Override
   public void setupOutfileDir(String outfileDir) throws IOException {
      if (Strings.isValid(outfileDir)) {
         outDir = new File(outfileDir);
         if (!outDir.isDirectory()) {
            if (!outDir.mkdirs()) {
               throw new IOException("Failed to create the output directory");
            }
            OseeLog.logf(TestEnvironment.class, Level.INFO,
                  "Outfile Dir [%s] created.", outDir.getAbsolutePath());
         } else {
            OseeLog.logf(TestEnvironment.class, Level.FINE,
                  "Outfile Dir [%s] exists.", outDir.getAbsolutePath());
         }
      } else {
         throw new IOException("A valid outfile directory must be specified.");
      }
   }

   @Override
   public void shutdown() {
      if (isShutdown) {
         return;
      }
      isShutdown = true;
      runtimeManager.uninstall(new OseeLogStatusCallback());
      Activator.getInstance().unregisterTestEnvironment();
      // here we remove all environment tasks (emulators)
      removeAllTasks();
      if (associatedObjects != null) {
         this.associatedObjects.clear();// get rid of all models and support
      }

      messagingServiceTracker.close();

      OseeLog.log(TestEnvironment.class, Level.FINE, "shutting down environment");
      factory.getTimerControl().cancelTimers();
      stop();
      cleanupClassReferences();
      OseeTestThread.clearThreadReferences();
      for (ITestEnvironmentListener listener : envListeners) {
         try {
            listener.onEnvironmentKilled(this);
         } catch (Exception e) {
            OseeLog.log(TestEnvironment.class, Level.SEVERE, "exception during listener notification", e);
         }
      }
      envListeners.clear();
      if (getRunManager() != null) {
         getRunManager().clearAllListeners();
      }
   }

   protected abstract void loadExternalDrivers();

   public void startup(String outfileDir) throws Exception {
      try {
         setupOutfileDir(outfileDir);
      } catch (IOException ex) {
         throw new Exception("Error in directory setup. " + outfileDir, ex);
      }
      if(myRegistration != null){
         myRegistration.unregister();
      }
      myRegistration = FrameworkUtil.getBundle(getClass()).getBundleContext().registerService(TestEnvironmentInterface.class, this, null);
   }

   protected void stop() {
      try{
         myRegistration.unregister();
      } catch (IllegalStateException ex){
         //ignore if it's already unregistered
      }
   }

   protected void cleanupClassReferences() {
      OseeLog.log(TestEnvironment.class, Level.FINE, "cleanupreferences");

      System.out.println("Associated objects that are getting cleaned up.");
      for (Class<?> clazz : associatedObjects.keySet()) {
         System.out.println(clazz.toString());
      }

      if (associatedObjects != null) {
         associatedObjects.clear();
      }
      OseeLog.log(TestEnvironment.class, Level.FINE, "got the other PM REF");
      if (associatedObjectListeners != null) {
         associatedObjectListeners.clear();
      }
      GCHelper.getGCHelper().printLiveReferences();
   }

   public void setExecutionUnitManagement(IExecutionUnitManagement executionUnitManagement) {
      this.executionUnitManagement = executionUnitManagement;
   }

   @Override
   public File getOutDir() {
      return outDir;
   }

   @Override
   public Remote getControlInterface(String id) {
      return controlInterfaces.get(id);
   }

   @Override
   public void registerControlInterface(String id, Remote controlInterface) {
      controlInterfaces.put(id, controlInterface);
   }

   @Override
   public IServiceConnector getConnector() {
      return connector;
   }

   @Deprecated
   public void setEnvSetup(boolean isEnvSetup) {
      this.isEnvSetup = isEnvSetup;
   }

   @Deprecated
   public void addScriptCompleteListener(IScriptCompleteEvent scriptComplete) {
      this.scriptCompleteListeners.add(scriptComplete);
   }

   @Deprecated
   public void removeScriptCompleteListener(IScriptCompleteEvent scriptComplete) {
      this.scriptCompleteListeners.remove(scriptComplete);
   }

   @Deprecated
   public void addScriptSetupListener(IScriptSetupEvent scriptSetup) {
      this.scriptSetupListeners.add(scriptSetup);
   }

   @Deprecated
   public void removeScriptSetupListener(IScriptSetupEvent scriptSetup) {
      this.scriptSetupListeners.remove(scriptSetup);
   }

   @Deprecated
   protected boolean isEnvSetup() {
      return isEnvSetup;
   }

   @Deprecated
   /**
    * alerts the environment of an exception. The environment will take any necessary actions and alert any interested
    * entities of the problem. Any runing test script will be terminated
    * 
    */
   public void handleException(Throwable t, Level logLevel) {
      handleException(t, "An exception has occurred in the environment", logLevel, true);
   }

   @Deprecated
   /**
    * @param abortScript true will cause the currently running script to abort
    */
   public void handleException(Throwable t, Level logLevel, boolean abortScript) {
      handleException(t, "An exception has occurred in the environment", logLevel, abortScript);
   }

   @Deprecated
   /**
    * alerts the environment of an exception. The environment will take any necessary actions and alert any interested
    * entities of the problem
    * 
    * @param t the exception
    * @param logLevel the severity of the exception. Specifing a Level.OFF will
    * @param abortScript cause the exception to not be logged
    */
   public void handleException(Throwable t, String message, Level logLevel, boolean abortScript) {
      if (logLevel != Level.OFF) {
         OseeLog.log(TestEnvironment.class, logLevel, message, t);
      }
      if (getTestScript() != null && abortScript) {
         getTestScript().abortDueToThrowable(t);
      }
      Iterator<ITestEnvironmentListener> iter = envListeners.iterator();
      while (iter.hasNext()) {
         final ITestEnvironmentListener listener = iter.next();
         listener.onException(message, t);
      }
   }

   @Deprecated
   public void testEnvironmentCommandComplete(ICommandHandle handle) {
      for (ITestEnvironmentListener listener : envListeners) {
         try {
            listener.onTestServerCommandFinished(this, handle);
         } catch (Throwable th) {
            System.out.println(listener.getClass().getName());
            th.printStackTrace();
         }
      }
   }

   @Override
   @Deprecated
   /**
    * marks the script as ready as well as clears any objects that are associated with the environment.
    */
   public synchronized void onScriptSetup() {

      for (IScriptSetupEvent listeners : scriptSetupListeners) {
         listeners.scriptSetup();
      }

      this.associatedObjects.clear();
   }

   @Deprecated
   public void removeQueueListener(ReportDataListener listener) throws RemoteException {
      factory.getReportDataControl().removeQueueListener(listener);
   }

   @Override
   @Deprecated
   public void onScriptComplete() throws InterruptedException {
      factory.getScriptControl().setScriptReady(false);

      for (int i = 0; i < scriptCompleteListeners.size(); i++) {
         try {
            scriptCompleteListeners.get(i).scriptComplete();
         } catch (Exception e) {
            OseeLog.log(TestEnvironment.class, Level.SEVERE, "problem with script complete listener", e);
         }
      }

      // here we remove all environment tasks (emulators)
      if (associatedObjects != null) {
         this.associatedObjects.clear();// get rid of all models and support
      }
   }

   @Override
   @Deprecated
   public void associateObject(Class<?> c, Object obj) {
      associatedObjects.put(c, obj);
      ArrayList<IAssociatedObjectListener> listeners = this.associatedObjectListeners.get(c);
      if (listeners != null) {
         for (int i = 0; i < listeners.size(); i++) {
            try {
               listeners.get(i).updateAssociatedListener();
            } catch (RemoteException e) {
               OseeLog.log(TestEnvironment.class, Level.SEVERE, e.getMessage(), e);
            }

         }
      }
   }

   @Override
   @Deprecated
   public Object getAssociatedObject(Class<?> c) {
      return associatedObjects.get(c);
   }

   @Override
   @Deprecated
   public Set<Class<?>> getAssociatedObjects() {
      return associatedObjects.keySet();
   }

   @Override
   @Deprecated
   /**
    * Use getRunManager().getCurrentScript() instead of this method.
    */
   public TestScript getTestScript() {
      return getRunManager().getCurrentScript();
   }

   @Override
   @Deprecated
   public void abortTestScript() {
      getRunManager().abort();
   }

   @Override
   @Deprecated
   public void abortTestScript(Throwable t) {
      getRunManager().abort(t, false);
   }

   public void setOteNodeInfo(NodeInfo oteEmbeddedBroker) {
      this.oteEmbeddedBroker = oteEmbeddedBroker;
   }

   public NodeInfo getOteNodeInfo() {
      return oteEmbeddedBroker;
   }
}

Back to the top