Skip to main content
summaryrefslogtreecommitdiffstats
blob: 225461d54d6903c1297e2929a6109c70e788fa92 (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
/*******************************************************************************
 * 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.ui.skynet;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.exception.OseeAuthenticationRequiredException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.services.IOseeCachingService;
import org.eclipse.osee.framework.core.util.ServiceDependencyTracker;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.IOseeDatabaseServiceProvider;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.listener.IBroadcastEventListener;
import org.eclipse.osee.framework.skynet.core.event.model.BroadcastEvent;
import org.eclipse.osee.framework.skynet.core.event.model.BroadcastEventType;
import org.eclipse.osee.framework.skynet.core.event.model.Sender;
import org.eclipse.osee.framework.skynet.core.utility.DbUtil;
import org.eclipse.osee.framework.ui.plugin.OseeFormActivator;
import org.eclipse.osee.framework.ui.skynet.artifact.ArtifactSaveNotificationHandler;
import org.eclipse.osee.framework.ui.skynet.artifact.IAccessPolicyHandlerService;
import org.eclipse.osee.framework.ui.skynet.blam.operation.SetWorkbenchOverrideIconBlam;
import org.eclipse.osee.framework.ui.skynet.cm.IOseeCmService;
import org.eclipse.osee.framework.ui.skynet.internal.AccessPolicyServiceRegHandler;
import org.eclipse.osee.framework.ui.skynet.internal.ArtifactPromptService;
import org.eclipse.osee.framework.ui.skynet.internal.ArtifactPromptServiceRegHandler;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchListener;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.BundleContext;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;

public class SkynetGuiPlugin extends OseeFormActivator implements IBroadcastEventListener, IOseeDatabaseServiceProvider {
   private static SkynetGuiPlugin pluginInstance; // The shared instance.
   public static final String PLUGIN_ID = "org.eclipse.osee.framework.ui.skynet";
   public static final String CHANGE_REPORT_ATTRIBUTES_PREF =
      "org.eclipse.osee.framework.ui.skynet.changeReportAttributes";
   public static final String ARTIFACT_EXPLORER_ATTRIBUTES_PREF =
      "org.eclipse.osee.framework.ui.skynet.artifactExplorerAttributes";

   public static final String ARTIFACT_SEARCH_RESULTS_ATTRIBUTES_PREF =
      "org.eclipse.osee.framework.ui.skynet.artifactSearchResultsAttributes";
   private ServiceTracker packageAdminTracker;
   private ServiceTracker cacheServiceTracker;
   private ServiceTracker databaseServiceTracker;
   private ServiceTracker cmServiceTracker;

   private final Map<String, ServiceDependencyTracker> trackers = new HashMap<String, ServiceDependencyTracker>();

   public SkynetGuiPlugin() {
      super(PLUGIN_ID);
      pluginInstance = this;
   }

   @Override
   public void stop(BundleContext context) throws Exception {
      super.stop(context);
      packageAdminTracker.close();
      cacheServiceTracker.close();
      databaseServiceTracker.close();
      cmServiceTracker.close();

      for (ServiceDependencyTracker tracker : trackers.values()) {
         Lib.close(tracker);
      }
   }

   @Override
   public void start(BundleContext context) throws Exception {
      super.start(context);

      packageAdminTracker = new ServiceTracker(context, PackageAdmin.class.getName(), null);
      packageAdminTracker.open();

      cacheServiceTracker = new ServiceTracker(context, IOseeCachingService.class.getName(), null);
      cacheServiceTracker.open();

      databaseServiceTracker = new ServiceTracker(context, IOseeDatabaseService.class.getName(), null);
      databaseServiceTracker.open();

      cmServiceTracker = new ServiceTracker(context, IOseeCmService.class.getName(), null);
      cmServiceTracker.open();

      trackers.put(IAccessPolicyHandlerService.class.getName(), new ServiceDependencyTracker(context,
         new AccessPolicyServiceRegHandler()));

      trackers.put(ArtifactPromptService.class.getName(), new ServiceDependencyTracker(context,
         new ArtifactPromptServiceRegHandler()));

      for (ServiceDependencyTracker tracker : trackers.values()) {
         tracker.open();
      }

      SetWorkbenchOverrideIconBlam.reloadOverrideImage();

      OseeEventManager.addListener(this);

      if (PlatformUI.isWorkbenchRunning()) {

         OseeLog.registerLoggerListener(new DialogPopupLoggerListener());

         IWorkbench workbench = PlatformUI.getWorkbench();
         workbench.addWorkbenchListener(new IWorkbenchListener() {

            @Override
            public void postShutdown(IWorkbench workbench) {
               // do nothing
            }

            @Override
            public boolean preShutdown(IWorkbench workbench, boolean forced) {
               if (!DbUtil.isDbInit()) {
                  try {
                     UserManager.getUser().saveSettings();
                  } catch (Throwable th) {
                     th.printStackTrace();
                  }
               }
               return true;
            }
         });

         PlatformUI.getWorkbench().addWorkbenchListener(new ArtifactSaveNotificationHandler());
      }
   }

   public static SkynetGuiPlugin getInstance() {
      return pluginInstance;
   }

   public PackageAdmin getPackageAdmin() {
      return (PackageAdmin) this.packageAdminTracker.getService();
   }

   public IOseeCachingService getOseeCacheService() {
      return (IOseeCachingService) cacheServiceTracker.getService();
   }

   @Override
   public IOseeDatabaseService getOseeDatabaseService() {
      return (IOseeDatabaseService) databaseServiceTracker.getService();
   }

   public IOseeCmService getOseeCmService() {
      return (IOseeCmService) cmServiceTracker.getService();
   }

   public IAccessPolicyHandlerService getPolicyHandlerService() throws OseeCoreException {
      return ((AccessPolicyServiceRegHandler) trackers.get(IAccessPolicyHandlerService.class.getName()).getHandler()).getService();
   }

   public ArtifactPromptService getArtifactPromptService() throws OseeCoreException {
      return ((ArtifactPromptServiceRegHandler) trackers.get(ArtifactPromptService.class.getName()).getHandler()).getService();
   }

   @Override
   public void handleBroadcastEvent(Sender sender, final BroadcastEvent broadcastEvent) {

      // Determine whether this is a shutdown event
      // Prevent shutting down users without a valid message
      if (broadcastEvent.getBroadcastEventType() == BroadcastEventType.Force_Shutdown) {
         if (broadcastEvent.getMessage() == null || broadcastEvent.getMessage().length() == 0) {
            return;
         }
         Displays.ensureInDisplayThread(new Runnable() {
            @Override
            public void run() {
               boolean isShutdownRequest = false;
               try {
                  isShutdownRequest = broadcastEvent.getUsers().contains(UserManager.getUser());
               } catch (OseeCoreException ex) {
                  // do nothing
               }
               if (isShutdownRequest) {
                  MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                     "Shutdown Requested", broadcastEvent.getMessage());
                  // Shutdown the bench when this event is received
                  PlatformUI.getWorkbench().close();
               }
            }
         });
      } else if (broadcastEvent.getBroadcastEventType() == BroadcastEventType.Message) {
         if (broadcastEvent.getMessage() == null || broadcastEvent.getMessage().length() == 0) {
            return;
         }
         Displays.ensureInDisplayThread(new Runnable() {
            @Override
            public void run() {
               MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                  "Remote Message", broadcastEvent.getMessage());
            }
         });
      } else if (broadcastEvent.getBroadcastEventType() == BroadcastEventType.Ping) {
         // Another client ping'd this client for session information; Pong back with
         // original client's session id so it can be identified as the correct pong
         try {
            OseeEventManager.kickBroadcastEvent(this, new BroadcastEvent(BroadcastEventType.Pong, null,
               sender.getOseeSession().toString()));
         } catch (Exception ex) {
            OseeLog.log(SkynetGuiPlugin.class, OseeLevel.SEVERE_POPUP, ex);
         }
      } else if (broadcastEvent.getBroadcastEventType() == BroadcastEventType.Pong) {
         // Got pong from another client; If message == this client's sessionId, then it's
         // the response from this client's ping
         try {
            if (broadcastEvent.getMessage() != null && broadcastEvent.getMessage().equals(
               ClientSessionManager.getSession().toString())) {
               OseeLog.log(SkynetGuiPlugin.class, Level.INFO, "Pong: " + sender.toString());
            }
         } catch (OseeAuthenticationRequiredException ex) {
            OseeLog.log(SkynetGuiPlugin.class, Level.SEVERE, "Pong: " + sender.toString(), ex);
         }
      }
   }

}

Back to the top