Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9656b8177b2fccb12d2e4a27040242e53129b6d0 (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
/*******************************************************************************
 * 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.jini.service.core;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import net.jini.core.entry.Entry;
import net.jini.core.lease.Lease;
import net.jini.core.lease.LeaseDeniedException;
import net.jini.core.lease.UnknownLeaseException;
import net.jini.core.lookup.ServiceID;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceRegistration;
import org.eclipse.osee.framework.jini.discovery.IRegistrarListener;
import org.eclipse.osee.framework.jini.discovery.ServiceDataStore;
import org.eclipse.osee.framework.jini.service.interfaces.IService;
import org.eclipse.osee.framework.jini.util.OseeJini;

/**
 * @author Andrew M. Finkbeiner
 */
public class JiniJoinManager implements IRegistrarListener {

   /**
    * The amount of time before a lease expires to first attempt renewal. This amount of time should be sufficiently
    * large to account for delays in communication (i.e. network delays), and allow for at least a few retries in the
    * event the service is not reachable. This time is specified in milliseconds.
    */
   private static final long RENEWAL_TIME = 2 * 60 * 1000; // 2 minutes

   //  private HashMap<ServiceID, ServiceRegistrar> idToReggie;
   private final HashMap<ServiceID, ServiceRegistrar> idToReggie;
   //  private ArrayList<ServiceRegistration> registrations;
   private final ArrayList<ServiceRegistration> registrations;
   private final Timer renewTimer;
   private final Remote proxy;
   private final Entry[] entry;
   private final ServiceID serviceID;
   private final ServiceDataStore serviceDataStore;

   public JiniJoinManager(ServiceID serviceID, JiniService js, Entry[] entry) throws IOException {
      proxy = OseeJini.getRemoteReference(js);
      this.entry = entry;
      this.serviceID = serviceID;
      registrations = new ArrayList<ServiceRegistration>();
      idToReggie = new HashMap<ServiceID, ServiceRegistrar>();
      renewTimer = new Timer();
      serviceDataStore = ServiceDataStore.getNonEclipseInstance();
      serviceDataStore.addListener(this);
   }

   public JiniJoinManager(ServiceID serviceID, IService service, Entry[] entry) {
      proxy = service;
      this.entry = entry;
      this.serviceID = serviceID;
      registrations = new ArrayList<ServiceRegistration>();
      idToReggie = new HashMap<ServiceID, ServiceRegistrar>();
      renewTimer = new Timer();
      serviceDataStore = ServiceDataStore.getNonEclipseInstance();
      serviceDataStore.addListener(this);
   }

   public void terminate() throws RemoteException {
      renewTimer.cancel();
      for (int i = 0; i < registrations.size(); i++) {
         ServiceRegistration registration = registrations.get(i);
         try {
            registration.getLease().cancel();
         } catch (UnknownLeaseException ex) {

         }
      }
   }

   public void setAttributes(Entry[] entry) {
      for (int i = 0; i < registrations.size(); i++) {
         ServiceRegistration registration = registrations.get(i);
         try {
            registration.setAttributes(entry);
         } catch (UnknownLeaseException ex) {
            registrations.remove(i);
            i--;
            //		ex.printStackTrace();
         } catch (RemoteException ex) {
            registrations.remove(i);
            i--;
            //		ex.printStackTrace();
         }
      }
   }

   private class RenewLease extends TimerTask {
      ServiceRegistration registration;

      public RenewLease(ServiceRegistration registration) {
         this.registration = registration;
      }

      @Override
      public void run() {
         try {
            // Renew for the maximum amount of time allowed
            registration.getLease().renew(Lease.FOREVER);
            renewTimer.schedule(new RenewLease(registration),
               registration.getLease().getExpiration() - System.currentTimeMillis() - RENEWAL_TIME);
         } catch (LeaseDeniedException ex) {
            //		ex.printStackTrace();
         } catch (UnknownLeaseException ex) {
            //		ex.printStackTrace();
         } catch (RemoteException ex) {
            //		ex.printStackTrace();
         }
         //	    finally{
         //	    renewTimer.schedule(new RenewLease(registration), registration.getLease().getExpiration() - System.currentTimeMillis() - RENEWAL_TIME);
         //	    }
      }

   }

   public void addGroup(String... groups) throws IOException {
      serviceDataStore.addGroup(groups);
   }

   public String[] getGroups() {
      return serviceDataStore.getGroups();
   }

   @Override
   public void reggieAdded(List<ServiceRegistrar> serviceRegistrars) {
      ServiceRegistrar[] reggies = serviceRegistrars.toArray(new ServiceRegistrar[serviceRegistrars.size()]);
      try {
         for (int i = 0; i < reggies.length; i++) {
            ServiceRegistration registration;
            registration = reggies[i].register(new ServiceItem(serviceID, proxy, entry), Long.MAX_VALUE);
            idToReggie.put(reggies[i].getServiceID(), reggies[i]);
            registrations.add(registration);
            renewTimer.schedule(new RenewLease(registration),
               registration.getLease().getExpiration() - System.currentTimeMillis() - RENEWAL_TIME);
         }
      } catch (ExportException ex) {
         ex.printStackTrace();
      } catch (RemoteException ex) {
         ex.printStackTrace();
      } catch (Throwable t) {
         t.printStackTrace();
      }
   }

   @Override
   public void reggieRemoved(List<ServiceRegistrar> serviceRegistrars) {
      ServiceRegistrar[] reggies = serviceRegistrars.toArray(new ServiceRegistrar[serviceRegistrars.size()]);
      for (int i = 0; i < reggies.length; i++) {
         idToReggie.remove(reggies[i].getServiceID());
      }
   }

   @Override
   public void reggieChanged(List<ServiceRegistrar> serviceRegistrars) {
   }

   public Entry[] getEntry() {
      return entry;
   }

   public Collection<ServiceRegistrar> getRegistrations() {
      return Collections.unmodifiableCollection(idToReggie.values());
   }

   public Remote getProxy() {
      return proxy;
   }

   public ServiceRegistrar findRegistrar(String host) throws MalformedURLException, ClassNotFoundException, IOException {
      return serviceDataStore.findRegistrar(host);
   }
}

Back to the top