Skip to main content
summaryrefslogtreecommitdiffstats
blob: e3bca76dfee7297cc6d7bcfb3df4413e3a4dc186 (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
/*******************************************************************************
 * 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.systems;

import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.util.logging.Level;
import net.jini.core.entry.Entry;
import net.jini.core.lookup.ServiceItem;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.jini.discovery.EclipseJiniClassloader;
import org.eclipse.osee.framework.jini.discovery.IServiceLookupListener;
import org.eclipse.osee.framework.jini.discovery.ServiceDataStore;
import org.eclipse.osee.framework.jini.service.core.SimpleFormattedEntry;
import org.eclipse.osee.framework.jini.util.OseeJini;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.event.skynet.ISkynetEvent;
import org.eclipse.osee.framework.messaging.event.skynet.ISkynetEventListener;
import org.eclipse.osee.framework.messaging.event.skynet.ISkynetEventService;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.event.EventSystemPreferences;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.RemoteEventServiceEventType;
import org.eclipse.osee.framework.skynet.core.internal.Activator;

/**
 * @author Donald G. Dunne
 */
public final class JiniSkynetEventServiceLookup implements IServiceLookupListener {

   private final ISkynetEventListener clientEventListener;
   private String acceptableServiceName;
   private ISkynetEventService currentEventService;
   private ISkynetEventListener clientEventListenerRemoteReference;
   private final EventSystemPreferences preferences;

   public JiniSkynetEventServiceLookup(EventSystemPreferences preferences, ISkynetEventListener clientEventListener) {
      this.preferences = preferences;
      this.clientEventListener = clientEventListener;
      clear();
   }

   private void clear() {
      currentEventService = null;
      clientEventListenerRemoteReference = null;
      acceptableServiceName = null;
   }

   public void start() {
      getClientEventListenerRemoteReference();
   }

   public void stop() {
      if (clientEventListenerRemoteReference != null) {
         ServiceDataStore.getEclipseInstance(EclipseJiniClassloader.getInstance()).removeListener(this);
      }
      reset();
   }

   public ISkynetEventListener getClientEventListenerRemoteReference() {
      checkJiniRegistration();
      return clientEventListenerRemoteReference;
   }

   public void checkJiniRegistration() {
      if (preferences.isNewEvents()) {
         return;
      }
      if (clientEventListenerRemoteReference == null) {
         try {
            // We need to trigger authentication before attempting to get database information from client session manager.
            UserManager.getUser();
            acceptableServiceName =
               ClientSessionManager.getDataStoreName() + ":" + ClientSessionManager.getDataStoreLoginName();
            clientEventListenerRemoteReference =
               (ISkynetEventListener) OseeJini.getRemoteReference(clientEventListener);
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
            clientEventListenerRemoteReference = null;
         }

         if (clientEventListenerRemoteReference != null) {
            ServiceDataStore.getEclipseInstance(EclipseJiniClassloader.getInstance()).addListener(this,
               ISkynetEventService.class);
         }
      }
   }

   private ISkynetEventService getReference() {
      return currentEventService;
   }

   public boolean isValid() {
      return isValidService(currentEventService);
   }

   public void reset() {
      setEventService(null);
   }

   public void kick(ISkynetEvent[] events, ISkynetEventListener... except) {
      try {
         getReference().kick(events, except);
      } catch (ExportException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      } catch (RemoteException ex) {
         disconnectService(ex);
      }
   }

   private boolean isValidService(ISkynetEventService service) {
      boolean result = false;
      try {
         if (service != null) {
            result = service.isAlive();
         }
      } catch (Exception ex) {
         // Do Nothing
         result = false;
      }
      return result;
   }

   private synchronized void setEventService(ISkynetEventService service) {
      if (isValidService(currentEventService)) {
         try {
            currentEventService.deregister(getClientEventListenerRemoteReference());
         } catch (RemoteException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
      currentEventService = service;
   }

   private void disconnectService(Exception e) {
      if (preferences.isNewEvents()) {
         return;
      }
      OseeLog.log(Activator.class, Level.WARNING, "Skynet Event Service connection lost\n" + e.toString(), e);
      setEventService(null);
      try {
         OseeEventManager.kickLocalRemEvent(this, RemoteEventServiceEventType.Rem1_DisConnected);
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
   }

   private void connectToService(ISkynetEventService service) {
      if (preferences.isNewEvents()) {
         return;
      }
      try {
         ISkynetEventListener clientListener = getClientEventListenerRemoteReference();
         if (clientListener != null) {
            service.register(clientListener);
            setEventService(service);
            OseeLog.log(Activator.class, Level.INFO,
               "Skynet Event Service connection established " + acceptableServiceName);
            OseeEventManager.kickLocalRemEvent(this, RemoteEventServiceEventType.Rem1_Connected);
         } else {
            OseeLog.log(Activator.class, Level.SEVERE, "Client listener reference was null");
         }
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
   }

   @Override
   public void serviceAdded(ServiceItem serviceItem) {
      if (preferences.isNewEvents()) {
         return;
      }
      if (serviceItem.service instanceof ISkynetEventService) {
         ISkynetEventService service = (ISkynetEventService) serviceItem.service;
         if (isValidService(service)) {
            // Check if the service is for the database we are using
            for (Entry entry : serviceItem.attributeSets) {
               if (entry instanceof SimpleFormattedEntry) {
                  SimpleFormattedEntry simpleEntry = (SimpleFormattedEntry) entry;
                  if ("db".equals(simpleEntry.name) && acceptableServiceName.equals(simpleEntry.value)) {
                     connectToService(service);
                     break;
                  }
               }
            }
         }
      }
   }

   @Override
   public void serviceChanged(ServiceItem serviceItem) {
      serviceAdded(serviceItem);
   }

   @Override
   public void serviceRemoved(ServiceItem serviceItem) {
      // do nothing
   }
}

Back to the top