Skip to main content
summaryrefslogtreecommitdiffstats
blob: a9c19e5cf29b35458d6e788a97f94856423c82fb (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
/*******************************************************************************
 * 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.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.data.OseeBranch;
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.model.Branch;
import org.eclipse.osee.framework.jdk.core.util.OseeProperties;
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.BranchGuidEventFilter;
import org.eclipse.osee.framework.skynet.core.event.filter.IEventFilter;
import org.eclipse.osee.framework.skynet.core.event.listener.IBranchEventListener;
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.ArtifactEvent;
import org.eclipse.osee.framework.skynet.core.event.model.BranchEvent;
import org.eclipse.osee.framework.skynet.core.event.model.BroadcastEvent;
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.TransactionEvent;
import org.eclipse.osee.framework.skynet.core.event.systems.EventManagerData;
import org.eclipse.osee.framework.skynet.core.event.systems.InternalEventManager;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.eclipse.osee.framework.skynet.core.utility.DbUtil;

/**
 * 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 List<IEventFilter> commonBranchEventFilter;
   private static BranchGuidEventFilter commonBranchGuidEvenFilter;
   private static IBranchEventListener testBranchEventListener;
   private static EventManagerData eventManagerData;

   private OseeEventManager() {
      // Static methods only;
   }

   public static void setEventManagerData(EventManagerData eventManagerData) {
      OseeEventManager.eventManagerData = eventManagerData;
   }

   /**
    * 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 (listener == null) {
         throw new IllegalArgumentException("listener can not be null");
      }
      Collection<IEventListener> priorityListeners = eventManagerData.getPriorityListeners();
      priorityListeners.add(listener);
   }

   public static void addListener(IEventListener listener) {
      if (listener == null) {
         throw new IllegalArgumentException("listener can not be null");
      }
      Collection<IEventListener> listeners = eventManagerData.getListeners();
      listeners.add(listener);
   }

   public static void removeAllListeners() {
      eventManagerData.getListeners().clear();
      eventManagerData.getPriorityListeners().clear();
   }

   public static void removeListener(IEventListener listener) {
      eventManagerData.getListeners().remove(listener);
      eventManagerData.getPriorityListeners().remove(listener);
   }

   private static InternalEventManager getEventManager() {
      return eventManagerData.getMessageEventManager();
   }

   public static EventSystemPreferences getPreferences() {
      return eventManagerData.getPreferences();
   }

   public static boolean isEventManagerConnected() {
      return getEventManager() != null ? getEventManager().isConnected() : false;
   }

   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 eventManagerData.getListeners().size();
   }

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

   // Only Used for Testing purposes
   public static void internalTestSendRemoteEvent(final RemoteEvent remoteEvent) throws RemoteException {
      getEventManager().testSendRemoteEventThroughFrameworkListener(remoteEvent);
   }

   // Only Used for Testing purposes
   public static void internalTestProcessBranchEvent(Sender sender, BranchEvent branchEvent) {
      getEventManager().processBranchEvent(sender, branchEvent);
   }

   // Only Used for Testing purposes
   public static void internalTestProcessEventArtifactsAndRelations(Sender sender, ArtifactEvent artifactEvent) {
      getEventManager().processEventArtifactsAndRelations(sender, artifactEvent);
   }

   // Kick LOCAL remote-event event
   public static void kickLocalRemEvent(Object source, RemoteEventServiceEventType remoteEventServiceEventType) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      if (!DbUtil.isDbInit()) {
         getEventManager().kickLocalRemEvent(createSender(source), remoteEventServiceEventType);
      }
   }

   // Kick LOCAL and REMOTE broadcast event
   public static void kickBroadcastEvent(Object source, BroadcastEvent broadcastEvent) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      getEventManager().kickBroadcastEvent(createSender(source), broadcastEvent);
   }

   //Kick LOCAL and REMOTE branch events
   public static void kickBranchEvent(Object source, BranchEvent branchEvent, int branchId) throws OseeCoreException {
      EventUtil.eventLog("OEM: kickBranchEvent: type: " + branchEvent.getEventType() + " guid: " + branchEvent.getBranchGuid() + " - " + source);
      if (isDisableEvents()) {
         return;
      }

      Sender sender = createSender(source);
      if (testBranchEventListener != null) {
         testBranchEventListener.handleBranchEvent(sender, branchEvent);
      }
      branchEvent.setNetworkSender(sender.getNetworkSender());
      getEventManager().kickBranchEvent(sender, branchEvent);
   }

   // Kick LOCAL and REMOTE access control events
   public static void kickAccessControlArtifactsEvent(Object source, AccessControlEvent accessControlEvent) throws OseeAuthenticationRequiredException {
      if (isDisableEvents()) {
         return;
      }
      Sender sender = createSender(source);
      accessControlEvent.setNetworkSender(sender.getNetworkSender());
      getEventManager().kickAccessControlArtifactsEvent(sender, accessControlEvent);
   }

   // Kick LOCAL and REMOTE transaction deleted event
   public static void kickTransactionEvent(Object source, final TransactionEvent transactionEvent) throws OseeCoreException {
      if (isDisableEvents()) {
         return;
      }
      Sender sender = createSender(source);
      transactionEvent.setNetworkSender(sender.getNetworkSender());
      getEventManager().kickTransactionEvent(sender, transactionEvent);
   }

   // Kick LOCAL and REMOTE transaction event
   public static void kickPersistEvent(Object source, ArtifactEvent artifactEvent) throws OseeAuthenticationRequiredException {
      if (isDisableEvents()) {
         return;
      }
      Sender sender = createSender(source);
      artifactEvent.setNetworkSender(sender.getNetworkSender());
      getEventManager().kickArtifactEvent(sender, 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());
      artifactEvent.getArtifacts().addAll(EventBasicGuidArtifact.get(EventModType.Reloaded, artifacts));
      getEventManager().kickLocalArtifactReloadEvent(createSender(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;
      if (OseeEventManager.isEventManagerConnected()) {
         toReturn =
            EventUtil.getListenerReport(eventManagerData.getListeners(), eventManagerData.getPriorityListeners());
      } else {
         toReturn = "Event system is NOT active";
      }
      return toReturn;
   }

   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 OseeBranch(branchName, 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;
   }

   // 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;
   }

}

Back to the top