summaryrefslogtreecommitdiffstats
blob: a9fa56198da2c8d2a65d5e41155a15939341b6dd (plain)
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
/*******************************************************************************
 * 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.messaging.event.skynet.service;

import java.util.Collections;
import java.util.Dictionary;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Level;
import net.jini.core.entry.Entry;
import net.jini.lookup.entry.Comment;
import net.jini.lookup.entry.Name;
import net.jini.lookup.entry.ServiceInfo;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jini.JiniClassServer;
import org.eclipse.osee.framework.jini.service.core.JiniService;
import org.eclipse.osee.framework.jini.service.core.SimpleFormattedEntry;
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.messaging.event.skynet.SkynetEventPlugin;
import org.eclipse.osee.framework.messaging.event.skynet.filter.IEventFilter;
import org.eclipse.osee.framework.plugin.core.OseeActivator;
import org.eclipse.osee.framework.plugin.core.server.BundleResourceFinder;

/**
 * Skynet Event Service handles event distribution to provide network assisted cache consistency for Skynet.
 * 
 * @author Robert A. Fisher
 */
public class SkynetEventService extends JiniService implements ISkynetEventService {
   private static JiniClassServer jiniClassServer;

   private final HashCollection<IEventFilter, ISkynetEventListener> listenerMap;
   private final ExecutorService executorService;
   private final AcceptAllEventFilter defaultEventFilter;

   protected SkynetEventService(String dbConfig) {
      this.defaultEventFilter = new AcceptAllEventFilter();
      this.executorService = Executors.newFixedThreadPool(3, new ThreadFactory() {
         @Override
         public Thread newThread(Runnable r) {
            return new Thread(r, "Event Dispatcher");
         }
      });
      this.listenerMap = new HashCollection<IEventFilter, ISkynetEventListener>();
      try {
         registerWithJini(dbConfig);
      } catch (Exception ex) {
         OseeLog.log(SkynetEventPlugin.class, Level.SEVERE, ex);
      }
   }

   private final IEventFilter getDefaultEventFilter() {
      return defaultEventFilter;
   }

   @SuppressWarnings("unchecked")
   private void registerWithJini(String dbConfig) throws Exception {
      jiniClassServer = JiniClassServer.getInstance();
      jiniClassServer.addResourceFinder(new BundleResourceFinder(new String[] {
         "org.eclipse.osee.framework.messaging.event.skynet",
         "org.eclipse.osee.framework.jdk.core"}));

      Dictionary dictionary = null;
      String name = "OSEE Event Service";
      String manufacturer = "Eclipse.org";
      String vendor = "Eclipse.org";
      String version = "0.0";
      String model = "SES";
      String serialNumber = "0.0";
      String description = "OSEE Event Service - OSEE Cache Network Manager";

      OseeActivator plugin = SkynetEventPlugin.getInstance();
      if (plugin != null) {
         dictionary = plugin.getBundle().getHeaders();
      }

      this.registerService(new Entry[] {
         new SimpleFormattedEntry("db", dbConfig),
         new ServiceInfo(name, manufacturer, vendor, version, model, serialNumber),
         new Name(name),
         new Comment(description)}, dictionary);

      System.out.println("....................OSEE Event Service(" + dbConfig + ") is Alive....................");
      this.stayAlive();
   }

   @Override
   public void register(ISkynetEventListener listener, IEventFilter... filters) {
      addListeners(listener, filters);
   }

   @Override
   public void deregister(ISkynetEventListener listener, IEventFilter... filters) {
      removeListeners(listener, filters);
   }

   private synchronized void removeListeners(ISkynetEventListener listener, IEventFilter... filters) {
      if (listener != null) {
         for (IEventFilter filter : listenerMap.keySet()) {
            listenerMap.removeValue(filter, listener);
         }
      }
   }

   private synchronized void addListeners(ISkynetEventListener listener, IEventFilter... filters) {
      if (listener != null) {
         if (filters != null && filters.length > 0) {
            for (IEventFilter filter : filters) {
               listenerMap.put(filter, listener);
            }
         } else {
            listenerMap.put(getDefaultEventFilter(), listener);
         }
      }
   }

   private synchronized HashCollection<ISkynetEventListener, ISkynetEvent> getEventAndListenersToSendTo(final ISkynetEvent[] events, Set<ISkynetEventListener> filterOutSet) {
      HashCollection<ISkynetEventListener, ISkynetEvent> eventSets =
         new HashCollection<ISkynetEventListener, ISkynetEvent>(false, HashSet.class);
      // Build sets according to the filters that the event matches
      for (IEventFilter filter : listenerMap.keySet()) {
         for (ISkynetEvent event : events) {
            if (filter.accepts(event)) {
               // When a filter accepts an event, the event to everyone listening for it
               for (ISkynetEventListener listener : listenerMap.getValues(filter)) {
                  if (!filterOutSet.contains(listener)) {
                     eventSets.put(listener, event);
                  }
               }
            }
         }
      }
      return eventSets;
   }

   private Set<ISkynetEventListener> toFilteredSet(final ISkynetEventListener... filterOut) {
      if (filterOut != null && filterOut.length > 0) {
         Set<ISkynetEventListener> filterOutSet =
            new HashSet<ISkynetEventListener>((int) (filterOut.length / .75f) + 1);
         for (ISkynetEventListener listener : filterOut) {
            if (listener != null) {
               filterOutSet.add(listener);
            }
         }
         return filterOutSet;
      }
      return Collections.emptySet();
   }

   @Override
   public void kick(final ISkynetEvent[] events, final ISkynetEventListener... except) {
      if (events != null && events.length > 0) {
         executorService.submit(new EventDispatchRunnable(this, getEventAndListenersToSendTo(events,
            toFilteredSet(except))));
      }
   }

   public static void main(String[] args) {
      if (args.length != 2) {
         throw new IllegalArgumentException("Must supply the db name for the service, such as \"dbinstance:schema\"");
      }

      new SkynetEventService(args[1]);
   }

   @Override
   public void kill() {
      try {
         this.deregisterService();
         this.commitSuicide();
      } finally {
         this.executorService.shutdown();
      }
   }

   @Override
   public boolean isAlive() {
      return true;
   }
}