Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2b5c36ab4c8dcffa5a121cf3114131303304643b (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
/*******************************************************************************
 * 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.framework.skynet.core.event;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.exception.OseeAuthenticationRequiredException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.database.core.OseeInfo;
import org.eclipse.osee.framework.jdk.core.util.OseeProperties;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactModType;
import org.eclipse.osee.framework.skynet.core.event2.AccessControlEvent;
import org.eclipse.osee.framework.skynet.core.event2.ArtifactEvent;
import org.eclipse.osee.framework.skynet.core.event2.BranchEvent;
import org.eclipse.osee.framework.skynet.core.event2.BroadcastEvent;
import org.eclipse.osee.framework.skynet.core.event2.TransactionChange;
import org.eclipse.osee.framework.skynet.core.event2.TransactionEvent;
import org.eclipse.osee.framework.skynet.core.event2.TransactionEventType;
import org.eclipse.osee.framework.skynet.core.event2.artifact.EventBasicGuidArtifact;
import org.eclipse.osee.framework.skynet.core.event2.artifact.EventModType;
import org.eclipse.osee.framework.skynet.core.event2.filter.BranchGuidEventFilter;
import org.eclipse.osee.framework.skynet.core.event2.filter.IEventFilter;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.eclipse.osee.framework.skynet.core.relation.RelationEventType;
import org.eclipse.osee.framework.skynet.core.relation.RelationLink;
import org.eclipse.osee.framework.skynet.core.utility.LoadedArtifacts;

/**
 * Front end to OSEE events. Provides ability to add and remove different event listeners as well as the ability to kick
 * framework events.
 * 
 * @author Donald G. Dunne
 */
public class OseeEventManager {

   private static IBranchEventListener testBranchEventListener;
   private static List<IEventFilter> commonBranchEventFilter;
   private static BranchGuidEventFilter commonBranchGuidEvenFilter;

   private static Sender getSender(Object sourceObject) throws OseeAuthenticationRequiredException {
      // Sender came from Remote Event Manager if source == sender
      if (sourceObject instanceof Sender && ((Sender) sourceObject).isRemote()) {
         return (Sender) sourceObject;
      }
      // Else, create new sender based on sourceObject
      return new Sender(sourceObject, ClientSessionManager.getSession());
   }

   // Kick LOCAL remote-event event
   public static void kickLocalRemEvent(Object source, RemoteEventServiceEventType remoteEventServiceEventType) throws OseeCoreException {
      if (InternalEventManager.isDisableEvents()) {
         return;
      }
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.kickRemoteEventManagerEvent(getSender(source), remoteEventServiceEventType);
      }
      if (OseeEventManager.isNewEvents()) {
         InternalEventManager2.kickLocalRemEvent(getSender(source), remoteEventServiceEventType);
      }
   }

   // Kick LOCAL and REMOTE broadcast event
   public static void kickBroadcastEvent(Object source, BroadcastEvent broadcastEvent) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.kickBroadcastEvent(getSender(source), broadcastEvent.getBroadcastEventType(),
            broadcastEvent.getUsers().toArray(new String[broadcastEvent.getUsers().size()]),
            broadcastEvent.getMessage());
      }
      if (OseeEventManager.isNewEvents()) {
         InternalEventManager2.kickBroadcastEvent(getSender(source), broadcastEvent);
      }
   }

   //Kick LOCAL and REMOTE branch events
   public static void kickBranchEvent(Object source, BranchEvent branchEvent, int branchId) throws OseeCoreException {
      eventLog("OEM: kickBranchEvent: type: " + branchEvent.getEventType() + " guid: " + branchEvent.getBranchGuid() + " - " + source);
      if (testBranchEventListener != null) {
         testBranchEventListener.handleBranchEventREM1(getSender(source), branchEvent.getEventType(), branchId);
      }
      if (isDisableEvents()) {
         return;
      }
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.kickBranchEvent(getSender(source), branchEvent.getEventType(), branchId);
      }
      branchEvent.setNetworkSender(getSender(source).getNetworkSender());
      if (OseeEventManager.isNewEvents()) {
         InternalEventManager2.kickBranchEvent(getSender(source), branchEvent);
      }
   }

   // Kick LOCAL and REMOTE branch events
   public static void kickMergeBranchEvent(Object source, MergeBranchEventType branchEventType, int branchId) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.kickMergeBranchEvent(getSender(source), branchEventType, branchId);
         // Handled by kickMergeBranchEvent for new Events
      }
   }

   // Kick LOCAL and REMOTE access control events
   public static void kickAccessControlArtifactsEvent(Object source, AccessControlEvent accessControlEvent, final LoadedArtifacts loadedArtifacts) throws OseeAuthenticationRequiredException {
      if (isDisableEvents()) {
         return;
      }
      accessControlEvent.setNetworkSender(getSender(source).getNetworkSender());
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.kickAccessControlArtifactsEvent(getSender(source), accessControlEvent, loadedArtifacts);
      }
      if (OseeEventManager.isNewEvents()) {
         InternalEventManager2.kickAccessControlArtifactsEvent(getSender(source), accessControlEvent);
      }
   }

   // Kick LOCAL artifact modified event; This event does NOT go external
   public static void kickArtifactModifiedEvent(Object source, ArtifactModType artifactModType, Artifact artifact) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.kickArtifactModifiedEvent(getSender(source), artifactModType, artifact);
      }
   }

   // Kick LOCAL relation modified event; This event does NOT go external
   public static void kickRelationModifiedEvent(Object source, RelationEventType relationEventType, RelationLink link, Branch branch, String relationType) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.kickRelationModifiedEvent(getSender(source), relationEventType, link, branch,
            relationType);
      }
   }

   // Kick LOCAL and REMOTE purged event depending on sender
   public static void kickArtifactsPurgedEvent(Object source, LoadedArtifacts loadedArtifacts, Set<EventBasicGuidArtifact> artifactChanges) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.kickArtifactsPurgedEvent(getSender(source), loadedArtifacts);
         // Handled by kickTransactionEvent for new Events
      }
   }

   // Kick LOCAL and REMOTE artifact change type depending on sender
   public static void kickArtifactsChangeTypeEvent(Object source, int toArtifactTypeId, String toArtifactTypeGuid, LoadedArtifacts loadedArtifacts, Set<EventBasicGuidArtifact> artifactChanges) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.kickArtifactsChangeTypeEvent(getSender(source), toArtifactTypeId, loadedArtifacts);
         // Handled by kickTransactionEvent for new Events
      }
   }

   // Kick LOCAL and REMOTE transaction deleted event
   public static void kickTransactionEvent(Object source, final TransactionEvent transactionEvent) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      Set<Integer> transactionIds = new HashSet<Integer>();
      for (TransactionChange transChange : transactionEvent.getTransactions()) {
         transactionIds.add(transChange.getTransactionId());
      }
      int[] transIds = new int[transactionIds.size()];
      int x = 0;
      for (Integer value : transactionIds) {
         transIds[x++] = value.intValue();
      }
      if (transactionEvent.getEventType() == TransactionEventType.Purged && OseeEventManager.isOldEvents()) {
         InternalEventManager.kickTransactionsPurgedEvent(getSender(source), transIds);
      }
      transactionEvent.setNetworkSender(getSender(source).getNetworkSender());
      if (OseeEventManager.isNewEvents()) {
         InternalEventManager2.kickTransactionEvent(getSender(source), transactionEvent);
      }
   }

   // Kick LOCAL and REMOTE transaction event
   public static void kickPersistEvent(Object source, ArtifactEvent artifactEvent) throws OseeAuthenticationRequiredException {
      if (isDisableEvents()) {
         return;
      }
      if (artifactEvent.getSkynetTransactionDetails() != null && OseeEventManager.isOldEvents()) {
         InternalEventManager.kickPersistEvent(getSender(source), artifactEvent.getSkynetTransactionDetails());
      }
      artifactEvent.setNetworkSender(getSender(source).getNetworkSender());
      if (OseeEventManager.isNewEvents()) {
         InternalEventManager2.kickPersistEvent(getSender(source), artifactEvent);
      }
   }

   // Kick LOCAL transaction event
   public static void kickLocalArtifactReloadEvent(Object source, Collection<? extends Artifact> artifacts) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.kickArtifactReloadEvent(getSender(source), artifacts);
      }
      ArtifactEvent artifactEvent = new ArtifactEvent();
      artifactEvent.getArtifacts().addAll(EventBasicGuidArtifact.get(EventModType.Reloaded, artifacts));
      if (OseeEventManager.isNewEvents()) {
         InternalEventManager2.kickLocalArtifactReloadEvent(getSender(source), artifactEvent);
      }
   }

   /**
    * Add a priority listener. This should only be done for caches where they need to be updated before all other
    * listeners are called.
    */
   public static void addPriorityListener(IEventListener listener) {
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.addPriorityListener(listener);
      }
      if (OseeEventManager.isNewEvents()) {
         InternalEventManager2.addPriorityListener(listener);
      }
   }

   public static void addListener(IEventListener listener) {
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.addListener(listener);
      }
      if (OseeEventManager.isNewEvents()) {
         InternalEventManager2.addListener(listener);
      }
   }

   public static void removeListener(IEventListener listener) {
      if (OseeEventManager.isOldEvents()) {
         InternalEventManager.removeListeners(listener);
      }
      if (OseeEventManager.isNewEvents()) {
         InternalEventManager2.removeListeners(listener);
      }
   }

   public static boolean isDisableEvents() {
      return InternalEventManager.isDisableEvents();
   }

   // Turn off all event processing including LOCAL and REMOTE
   public static void setDisableEvents(boolean disableEvents) {
      InternalEventManager.setDisableEvents(disableEvents);
      InternalEventManager2.setDisableEvents(disableEvents);
   }

   // Return report showing all listeners registered
   public static String getListenerReport() {
      if (OseeEventManager.isOldEvents()) {
         return InternalEventManager.getListenerReport();
      }
      if (OseeEventManager.isNewEvents()) {
         return InternalEventManager2.getListenerReport();
      }
      return "Neither event system is active";
   }

   // Registration for branch events; for test only
   public static void registerBranchEventListenerForTest(IBranchEventListener branchEventListener) {
      if (!OseeProperties.isInTest()) {
         throw new IllegalStateException("Invalid registration for production");
      }
      testBranchEventListener = branchEventListener;
   }

   public static boolean isEventDebugConsole() {
      if (!Strings.isValid(System.getProperty("eventDebug"))) {
         return false;
      }
      return System.getProperty("eventDebug").equals("console");
   }

   public static boolean isEventDebugErrorLog() {
      if (!Strings.isValid(System.getProperty("eventDebug"))) {
         return false;
      }
      return System.getProperty("eventDebug").equals("log") || "TRUE".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.osee.framework.skynet.core/debug/Events"));
   }

   /**
    * If old event kicks and listens should be used
    */
   public static boolean isOldEvents() {
      return !isNewEvents();
   }

   /**
    * If new event kicks and listens should be used
    */
   public static boolean isNewEvents() {
      try {
         String dbProperty = OseeInfo.getCachedValue("eventSystem");
         if (Strings.isValid(dbProperty)) {
            return dbProperty.equals("new");
         }
      } catch (OseeDataStoreException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
      if (!Strings.isValid(System.getProperty("eventSystem"))) {
         return false;
      }
      return System.getProperty("eventSystem").equals("new");
   }

   public static void setNewEvents(boolean enabled) {
      System.setProperty("eventSystem", enabled ? "new" : "old");
   }

   public static void eventLog(String output) {
      eventLog(output, null);
   }

   public static void eventLog(String output, Exception ex) {
      try {
         if (isEventDebugConsole()) {
            System.err.println(output + (ex != null ? " <<ERROR>> " + ex.toString() : ""));
         } else if (isEventDebugErrorLog()) {
            if (ex != null) {
               OseeLog.log(Activator.class, Level.SEVERE, output, ex);
            } else {
               OseeLog.log(Activator.class, Level.FINE, output);
            }
         }
      } catch (Exception ex1) {
         OseeLog.log(Activator.class, Level.SEVERE, ex1);
      }
   }

   public static List<IEventFilter> getEventFiltersForBranch(Branch branch) {
      return getEventFiltersForBranch(branch.getName(), branch.getGuid());
   }

   public static List<IEventFilter> getEventFiltersForBranch(final String branchName, final String branchGuid) {
      try {
         List<IEventFilter> eventFilters = new ArrayList<IEventFilter>(2);
         eventFilters.add(new BranchGuidEventFilter(new IOseeBranch() {

            @Override
            public String getName() {
               return branchName;
            }

            @Override
            public String getGuid() {
               return branchGuid;
            }
         }));
         return eventFilters;
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
      return null;
   }

   public static List<IEventFilter> getCommonBranchEventFilters() {
      try {
         if (commonBranchEventFilter == null) {
            commonBranchEventFilter = new ArrayList<IEventFilter>(2);
            commonBranchEventFilter.add(getCommonBranchFilter());
         }
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
      return commonBranchEventFilter;
   }

   public static BranchGuidEventFilter getCommonBranchFilter() {
      if (commonBranchGuidEvenFilter == null) {
         commonBranchGuidEvenFilter = new BranchGuidEventFilter(CoreBranches.COMMON);
      }
      return commonBranchGuidEvenFilter;
   }

}

Back to the top