Skip to main content
summaryrefslogtreecommitdiffstats
blob: 22d9dd96330b3d6ff9e28374a7dc7fa173db87da (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
/*******************************************************************************
 * 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.ote.connection.jini;

import java.io.File;
import java.io.Serializable;
import java.net.URI;
import java.net.UnknownHostException;
import java.rmi.Remote;
import java.rmi.server.ExportException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Timer;
import java.util.logging.Level;
import net.jini.core.entry.Entry;
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 net.jini.id.Uuid;
import net.jini.id.UuidFactory;
import net.jini.jeri.BasicILFactory;
import net.jini.jeri.BasicJeriExporter;
import net.jini.jeri.tcp.TcpServerEndpoint;
import org.eclipse.osee.framework.jdk.core.util.EnhancedProperties;
import org.eclipse.osee.framework.jdk.core.util.Network;
import org.eclipse.osee.ote.connection.jini.util.LeaseRenewTask;

/**
 * @author Ken J. Aguilar
 */
public class JiniServiceSideConnector extends JiniConnector implements IJiniConnectorLink {
   public static final String TYPE = "jini.service-end";
   private final HashMap<ServiceRegistration, LeaseRenewTask> registrations =
      new HashMap<ServiceRegistration, LeaseRenewTask>();
   private Remote service;
   private Remote serviceProxy;
   private BasicJeriExporter serviceExporter;
   private ServiceID serviceId;
   private final Timer timer = new Timer();
   private ServiceItem serviceItem;
   private BasicJeriExporter linkExporter;
   private IJiniConnectorLink exportedThis;
   private boolean stopped = false;

   public JiniServiceSideConnector() {
      super();
   }

   @Override
   public void init(Object service) throws UnknownHostException, ExportException {

      this.service = (Remote) service;
      serviceId = generateServiceId();
      serviceExporter =
         new BasicJeriExporter(TcpServerEndpoint.getInstance(Network.getValidIP().getHostAddress(), 0),
            new BasicILFactory(null, null, Activator.getDefault().getExportClassLoader()), false, false);
      linkExporter =
         new BasicJeriExporter(TcpServerEndpoint.getInstance(Network.getValidIP().getHostAddress(), 0),
            new BasicILFactory(null, null, Activator.getDefault().getExportClassLoader()), false, false);
      serviceProxy = serviceExporter.export(this.service);
      exportedThis = (IJiniConnectorLink) linkExporter.export(this);
      setProperty(LINK_PROPERTY, (Serializable) exportedThis);
      serviceItem = new ServiceItem(serviceId, serviceProxy, createEntries());
   }

   public JiniServiceSideConnector(Remote service, EnhancedProperties props) throws UnknownHostException, ExportException {
      super(props);
      this.service = service;
      serviceId = generateServiceId();
      serviceExporter =
         new BasicJeriExporter(TcpServerEndpoint.getInstance(Network.getValidIP().getHostAddress(), 0),
            new BasicILFactory(null, null, Activator.getDefault().getExportClassLoader()), false, false);
      linkExporter =
         new BasicJeriExporter(TcpServerEndpoint.getInstance(Network.getValidIP().getHostAddress(), 0),
            new BasicILFactory(null, null, Activator.getDefault().getExportClassLoader()), false, false);
      serviceProxy = serviceExporter.export(service);
      exportedThis = (IJiniConnectorLink) linkExporter.export(this);
      props.setProperty(LINK_PROPERTY, (Serializable) exportedThis);
      serviceItem = new ServiceItem(serviceId, serviceProxy, createEntries());
   }

   @Override
   public Remote getService() {
      return serviceProxy;
   }

   @Override
   public synchronized void stop() throws Exception {
      if (stopped) {
         return;
      }
      stopped = true;
      super.stop();
      removeAllRegistrations();
      serviceExporter.unexport(true);
      linkExporter.unexport(true);
   }

   /**
    * this method will cancel all current registrations of this connector
    */
   synchronized void removeAllRegistrations() {
      for (ServiceRegistration registration : registrations.keySet()) {
         try {
            LeaseRenewTask task = registrations.get(registration);
            if (task != null) {
               task.cancel();
            }
         } catch (Exception e) {
            Activator.log(Level.SEVERE, "exception removing registration", e);
         }
      }
      registrations.clear();
   }

   private ServiceID generateServiceId() {
      Uuid uuid = UuidFactory.generate();
      Long lsb = new Long(uuid.getLeastSignificantBits());
      Long msb = new Long(uuid.getMostSignificantBits());
      return new ServiceID(msb.longValue(), lsb.longValue());
   }

   synchronized void addRegistration(ServiceRegistration registration, ServiceRegistrar registrar) {
      registrations.put(registration, new LeaseRenewTask(timer, registration, registrar));
   }

   ServiceItem getServiceItem() {
      return serviceItem;
   }

   private synchronized void setAttributes(Entry[] entry) {
      Iterator<ServiceRegistration> iter = registrations.keySet().iterator();
      while (iter.hasNext()) {
         ServiceRegistration registration = iter.next();
         try {
            registration.setAttributes(entry);
         } catch (Exception ex) {
            Activator.log(Level.SEVERE, "exception setting attributes", ex);
            registrations.remove(registration);
         }
      }
   }

   @Override
   public String getConnectorType() {
      return TYPE;
   }

   @Override
   public void setProperty(String key, Serializable value) {
      super.setProperty(key, value);
      setAttributes(createEntries());
   }

   @Override
   public URI upload(File file) throws Exception {
      return null;
   }

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

   @Override
   public String getUniqueServerId() {
      return serviceId.toString();
   }

}

Back to the top