diff options
Diffstat (limited to 'org.eclipse.osee.ote.connection.jini/src/org/eclipse')
9 files changed, 664 insertions, 0 deletions
diff --git a/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/Activator.java b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/Activator.java new file mode 100644 index 000000000..d73c09496 --- /dev/null +++ b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/Activator.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * 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.util.logging.Level; + +import org.eclipse.core.runtime.Plugin; +import org.eclipse.osee.framework.logging.OseeLog; +import org.eclipse.osee.framework.plugin.core.util.ExportClassLoader; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends Plugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.eclipse.osee.ote.connection.jini"; + + // The shared instance + private static Activator plugin; + + + private ExportClassLoader exportClassLoader; + + /** + * The constructor + */ + public Activator() { + } + + @Override + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + + } + + @Override + public void stop(BundleContext context) throws Exception { + super.stop(context); + exportClassLoader = null; + plugin = null; + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + + ClassLoader getExportClassLoader() { + return exportClassLoader; + } + + public static void log(Level level, String message, Throwable t) { + OseeLog.log(Activator.class, level, message, t); + } + + public static void log(Level level, String message) { + log(level, message, null); + } +} diff --git a/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/ConnectorContribution.java b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/ConnectorContribution.java new file mode 100644 index 000000000..17d6fe946 --- /dev/null +++ b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/ConnectorContribution.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * 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 org.eclipse.osee.connection.service.IConnectorContributor; + +public class ConnectorContribution implements IConnectorContributor { + + public ConnectorContribution() { + } + + @Override + public void init() throws Exception { + } + +} diff --git a/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/IJiniConnectorLink.java b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/IJiniConnectorLink.java new file mode 100644 index 000000000..bd858a816 --- /dev/null +++ b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/IJiniConnectorLink.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * 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.rmi.Remote; +import java.rmi.RemoteException; + +public interface IJiniConnectorLink extends Remote { + boolean ping() throws RemoteException; +} diff --git a/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/IJiniConnectorRegistrar.java b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/IJiniConnectorRegistrar.java new file mode 100644 index 000000000..b2418692b --- /dev/null +++ b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/IJiniConnectorRegistrar.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * 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.IOException; +import java.net.MalformedURLException; +import net.jini.core.discovery.LookupLocator; + +/** + * @author Ken J. Aguilar + */ +public interface IJiniConnectorRegistrar { + void addLocators(String... hosts) throws MalformedURLException, ClassNotFoundException, IOException; + + LookupLocator[] getLocators(); + + void addGroup(String... groups) throws IOException; + + String[] getGroups(); +} diff --git a/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/JiniClientSideConnector.java b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/JiniClientSideConnector.java new file mode 100644 index 000000000..10ff1b693 --- /dev/null +++ b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/JiniClientSideConnector.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * 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.net.URI; +import java.net.UnknownHostException; +import java.rmi.RemoteException; +import java.rmi.server.ExportException; +import net.jini.core.lookup.ServiceItem; + +/** + * @author Ken J. Aguilar + */ +public class JiniClientSideConnector extends JiniConnector { + public static final String TYPE = "jini.client-end"; + private final ServiceItem serviceItem; + private final IJiniConnectorLink link; + + JiniClientSideConnector(ServiceItem serviceItem) { + super(); + this.serviceItem = serviceItem; + link = (IJiniConnectorLink) getProperties().getProperty(LINK_PROPERTY); + } + + @Override + public Object getService() { + return serviceItem.service; + } + + @Override + public String getConnectorType() { + return TYPE; + } + + @Override + public URI upload(File file) throws Exception { + return null; + } + + @Override + public boolean ping() { + try { + return link.ping(); + } catch (RemoteException e) { + return false; + } + } + + @Override + public void init(Object service) throws UnknownHostException, ExportException { + + } + + @Override + public String getUniqueServerId() { + return serviceItem.serviceID.toString(); + } + +} diff --git a/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/JiniConnector.java b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/JiniConnector.java new file mode 100644 index 000000000..276d77ede --- /dev/null +++ b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/JiniConnector.java @@ -0,0 +1,152 @@ +/******************************************************************************* + * 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.Serializable; +import java.net.UnknownHostException; +import java.rmi.Remote; +import java.rmi.server.ExportException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; + +import net.jini.core.entry.Entry; +import net.jini.export.Exporter; +import net.jini.jeri.BasicILFactory; +import net.jini.jeri.BasicJeriExporter; +import net.jini.jeri.tcp.TcpServerEndpoint; + +import org.eclipse.osee.connection.service.IServiceConnector; +import org.eclipse.osee.connection.service.IServicePropertyChangeListener; +import org.eclipse.osee.framework.jdk.core.util.EnhancedProperties; +import org.eclipse.osee.framework.jdk.core.util.Network; + +/** + * @author Ken J. Aguilar + */ +public abstract class JiniConnector implements IServiceConnector { + protected final static String LINK_PROPERTY = "JINI_CONNECTOR_LINK"; + private final HashMap<Object, ExportInfo> exports = new HashMap<>(); + private final EnhancedProperties properties; + private final HashSet<IServicePropertyChangeListener> propertyChangeListeners = + new HashSet<IServicePropertyChangeListener>(); + private boolean connected = false; + + private static final class ExportInfo { + private final Exporter exporter; + private final Object exportedObject; + + private ExportInfo(Exporter exporter, Object exportedObject) { + this.exportedObject = exportedObject; + this.exporter = exporter; + } + } + + protected JiniConnector() { + this(new EnhancedProperties()); + } + + protected JiniConnector(EnhancedProperties properties) { + this.properties = properties; + } + + @Override + public Object export(Object callback) throws ExportException { + try { + Exporter exporter = createExporter(); + Object exportedObject = exporter.export((Remote) callback); + exports.put(callback, new ExportInfo(exporter, exportedObject)); + return exportedObject; + } catch (UnknownHostException e) { + throw new ExportException("failed to export", e); + } + } + + @Override + public void unexport(Object callback) throws Exception { + ExportInfo info = exports.remove(callback); + if (info != null) { + info.exporter.unexport(false); + } + } + + @Override + public Object findExport(Object callback) { + ExportInfo info = exports.get(callback); + if (info != null) { + return info.exportedObject; + } + return null; + } + + @Override + public void stop() throws Exception { + for (ExportInfo info : exports.values()) { + info.exporter.unexport(false); + } + exports.clear(); + } + + private Exporter createExporter() throws UnknownHostException { + return new BasicJeriExporter(TcpServerEndpoint.getInstance(Network.getValidIP().getHostAddress(), 0), + new BasicILFactory(null, null, Activator.getDefault().getExportClassLoader()), false, false); + } + + protected Entry[] createEntries() { + LinkedList<Entry> entries = new LinkedList<>(); + return entries.toArray(new Entry[entries.size()]); + } + + @Override + public Serializable getProperty(String property, Serializable defaultValue) { + return properties.getProperty(property, defaultValue); + } + + @Override + public void addPropertyChangeListener(IServicePropertyChangeListener listener) { + propertyChangeListeners.add(listener); + } + + @Override + public void removePropertyChangeListener(IServicePropertyChangeListener listener) { + propertyChangeListeners.remove(listener); + } + + @Override + public void setProperty(String key, Serializable value) { + properties.setProperty(key, value); + for (IServicePropertyChangeListener listener : propertyChangeListeners) { + listener.propertyChanged(this, key, value); + } + } + + @Override + public EnhancedProperties getProperties() { + return properties; + } + + public void entriesChanged(Entry[] entries) { + EnhancedProperties newProps = new EnhancedProperties(); + for (String key : properties.differences(newProps)) { + for (IServicePropertyChangeListener listener : propertyChangeListeners) { + listener.propertyChanged(this, key, properties.getProperty(key)); + } + } + } + + public void setConnected(boolean connected){ + this.connected = connected; + } + + public boolean isConnected() { + return this.connected; + } +} diff --git a/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/JiniServiceSideConnector.java b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/JiniServiceSideConnector.java new file mode 100644 index 000000000..22d9dd963 --- /dev/null +++ b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/JiniServiceSideConnector.java @@ -0,0 +1,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(); + } + +} diff --git a/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/TestEntry.java b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/TestEntry.java new file mode 100644 index 000000000..4bcc3f666 --- /dev/null +++ b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/TestEntry.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * 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 net.jini.entry.AbstractEntry; + +/** + * @author Ken J. Aguilar + */ +public class TestEntry extends AbstractEntry { + private static final long serialVersionUID = -2239353039479522642L; + public final String data; + + public TestEntry() { + data = "<none>"; + } + + public TestEntry(String data) { + super(); + this.data = data; + } + + /** + * @return the data + */ + public String getData() { + return data; + } + +} diff --git a/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/util/LeaseRenewTask.java b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/util/LeaseRenewTask.java new file mode 100644 index 000000000..49feede69 --- /dev/null +++ b/org.eclipse.osee.ote.connection.jini/src/org/eclipse/osee/ote/connection/jini/util/LeaseRenewTask.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * 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.util; + +import java.util.Timer; +import java.util.TimerTask; +import java.util.logging.Level; +import net.jini.core.lease.Lease; +import net.jini.core.lookup.ServiceRegistrar; +import net.jini.core.lookup.ServiceRegistration; +import org.eclipse.osee.ote.connection.jini.Activator; + +public class LeaseRenewTask extends TimerTask { + /** + * 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 = 1 * 60 * 1000; // 1 minute + + private final ServiceRegistration registration; + private volatile boolean canceled = false; + private final ServiceRegistrar registrar; + + public LeaseRenewTask(Timer timer, ServiceRegistration registration, ServiceRegistrar registrar) { + this.registration = registration; + timer.scheduleAtFixedRate(this, 0, RENEWAL_TIME); + this.registrar = registrar; + } + + @Override + public void run() { + if (canceled) { + return; + } + try { + // Renew for the maximum amount of time allowed + registration.getLease().renew(Lease.FOREVER); + } catch (Throwable ex) { + handleLeaseRenewException(ex); + try { + registration.getLease().renew(Lease.FOREVER); + } catch (Throwable ex1) { + handleLeaseRenewException(ex1); + } + } + } + + private void handleLeaseRenewException(Throwable th) { + String host = "unknown"; + int port = 0; + try { + host = registrar.getLocator().getHost(); + port = registrar.getLocator().getPort(); + } catch (Throwable th2) { + th2.printStackTrace(); + } + System.out.printf("lookup serviceId[%s] host[%s] port[%d]\n", registrar.getServiceID().toString(), host, port); + Activator.log(Level.SEVERE, "error renewing lease", th); + } + + @Override + public boolean cancel() { + canceled = true; + boolean result = super.cancel(); + try { + System.out.println("Canceling lookup lease"); + registration.getLease().cancel(); + } catch (Exception ex) { + throw new RuntimeException("failed to cancel lease", ex); + } + return result; + } + +}
\ No newline at end of file |