Skip to main content
summaryrefslogtreecommitdiffstats
blob: d877fa19d661636da373318276c07357ad9879a5 (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
/*******************************************************************************
 * 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.List;
import java.util.logging.Level;
import org.eclipse.core.runtime.Assert;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.event.res.RemoteEvent;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.event.filter.BranchUuidEventFilter;
import org.eclipse.osee.framework.skynet.core.event.filter.IEventFilter;
import org.eclipse.osee.framework.skynet.core.event.listener.EventQosType;
import org.eclipse.osee.framework.skynet.core.event.listener.IEventListener;
import org.eclipse.osee.framework.skynet.core.event.model.AccessControlEvent;
import org.eclipse.osee.framework.skynet.core.event.model.AccessTopicEventPayload;
import org.eclipse.osee.framework.skynet.core.event.model.AccessTopicEventType;
import org.eclipse.osee.framework.skynet.core.event.model.ArtifactEvent;
import org.eclipse.osee.framework.skynet.core.event.model.ArtifactEvent.ArtifactEventType;
import org.eclipse.osee.framework.skynet.core.event.model.BranchEvent;
import org.eclipse.osee.framework.skynet.core.event.model.EventBasicGuidArtifact;
import org.eclipse.osee.framework.skynet.core.event.model.EventModType;
import org.eclipse.osee.framework.skynet.core.event.model.RemoteEventServiceEventType;
import org.eclipse.osee.framework.skynet.core.event.model.Sender;
import org.eclipse.osee.framework.skynet.core.event.model.TopicEvent;
import org.eclipse.osee.framework.skynet.core.event.model.TransactionEvent;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.eclipse.osee.framework.skynet.core.internal.ServiceUtil;
import org.eclipse.osee.framework.skynet.core.internal.event.EventListenerRegistry;

/**
 * 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 final class OseeEventManager {

   private OseeEventManager() {
      // Utility Class
   }

   private static OseeEventService getEventService() throws OseeCoreException {
      return ServiceUtil.getEventService();
   }

   private static EventListenerRegistry getEventListeners() {
      return Activator.getEventListeners();
   }

   /**
    * 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) {
      getEventListeners().addListener(EventQosType.PRIORITY, listener);
   }

   public static void addListener(IEventListener listener) {
      getEventListeners().addListener(EventQosType.NORMAL, listener);
   }

   public static void removeAllListeners() {
      getEventListeners().clearAll();
   }

   public static void removeListener(IEventListener listener) {
      getEventListeners().removeListener(listener);
   }

   public static EventSystemPreferences getPreferences() {
      return Activator.getEventPreferences();
   }

   public static boolean isEventManagerConnected() {
      boolean result = false;
      try {
         OseeEventService eventService = getEventService();
         result = eventService.isConnected();
      } catch (Exception ex) {
         // Do Nothing;
      }
      return result;
   }

   public static String getConnectionDetails() {
      EventSystemPreferences preferences = getPreferences();
      StringBuilder sb = new StringBuilder();
      sb.append("oseeEventBrokerUri [" + preferences.getOseeEventBrokerUri() + "]");
      sb.append("eventDebug [" + preferences.getEventDebug() + "]");
      return sb.toString();
   }

   public static int getNumberOfListeners() {
      return getEventListeners().size();
   }

   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   /////////////////////////////////// NEW EVENT MODEL - TOPICS with JSON ////////////////////////////////////////////
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   // Kick LOCAL and REMOTE topic event
   public static void kickTopicEvent(Object source, TopicEvent topicEvent) throws OseeCoreException {
      Assert.isNotNull(source);
      Assert.isNotNull(topicEvent);
      Assert.isNotNull(topicEvent.getEventType(), "TopicEvent.eventType can not be null");
      Assert.isTrue(Strings.isValid(topicEvent.getTopic()), "TopicEvent.topic can not be null.");
      getEventService().send(source, topicEvent);
   }

   // Kick LOCAL and REMOTE topic event with payload
   public static void kickAccessTopicEvent(Object source, AccessTopicEventPayload payload, AccessTopicEventType accessEventTopicType) {
      try {
         TopicEvent topicEvent =
            EventUtil.createTopic(accessEventTopicType.getTopic(), payload, accessEventTopicType.getEventType());
         kickTopicEvent(source, topicEvent);
      } catch (Exception ex) {
         OseeLog.logf(OseeEventManager.class, Level.SEVERE, ex, "Error kicking event [%s][%s]", accessEventTopicType,
            payload);
      }
   }

   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   /////////////////////////////////// LEGACY EVENT MODEL - serialized jaxb object ///////////////////////////////////
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   // Kick LOCAL remote-event event
   public static void kickLocalRemEvent(Object source, RemoteEventServiceEventType remoteEventServiceEventType) throws OseeCoreException {
      getEventService().send(source, remoteEventServiceEventType);
   }

   //Kick LOCAL and REMOTE branch events
   public static void kickBranchEvent(Object source, BranchEvent branchEvent) throws OseeCoreException {
      getEventService().send(source, branchEvent);
   }

   // Kick LOCAL and REMOTE access control events
   public static void kickAccessControlArtifactsEvent(Object source, AccessControlEvent accessControlEvent) throws OseeCoreException {
      getEventService().send(source, accessControlEvent);
   }

   // Kick LOCAL and REMOTE transaction deleted event
   public static void kickTransactionEvent(Object source, final TransactionEvent transactionEvent) throws OseeCoreException {
      getEventService().send(source, transactionEvent);
   }

   // Kick LOCAL and REMOTE transaction event
   public static void kickPersistEvent(Object source, ArtifactEvent artifactEvent) throws OseeCoreException {
      getEventService().send(source, artifactEvent);
   }

   // Kick LOCAL transaction event
   public static void kickLocalArtifactReloadEvent(Object source, Collection<? extends Artifact> artifacts) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      ArtifactEvent artifactEvent =
         new ArtifactEvent(artifacts.iterator().next().getBranch().getGuid(), ArtifactEventType.RELOAD_ARTIFACTS);
      artifactEvent.getArtifacts().addAll(EventBasicGuidArtifact.get(EventModType.Reloaded, artifacts));
      getEventService().send(source, artifactEvent);
   }

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

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

   // Return report showing all listeners registered
   public static String getListenerReport() {
      String toReturn = null;
      if (OseeEventManager.isEventManagerConnected()) {
         toReturn = getEventListeners().toString();
      } else {
         toReturn = "Event system is NOT active";
      }
      return toReturn;
   }

   public static List<IEventFilter> getEventFiltersForBranch(final IOseeBranch branch) {
      try {
         List<IEventFilter> eventFilters = new ArrayList<>(2);
         eventFilters.add(new BranchUuidEventFilter(branch));
         return eventFilters;
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
      return null;
   }

   /////////////////////////////////// LEGACY TEST API ////////////////////////////////////////////
   // Only Used for Testing purposes
   public static void internalTestSendRemoteEvent(final RemoteEvent remoteEvent) throws OseeCoreException {
      getEventService().receive(remoteEvent);
   }

   // Only Used for Testing purposes
   public static void internalTestProcessBranchEvent(Sender sender, BranchEvent branchEvent) throws OseeCoreException {
      getEventService().receive(sender, branchEvent);
   }

   // Only Used for Testing purposes
   public static void internalTestProcessEventArtifactsAndRelations(Sender sender, ArtifactEvent artifactEvent) throws OseeCoreException {
      getEventService().receive(sender, artifactEvent);
   }

}

Back to the top