river-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tho...@apache.org
Subject svn commit: r993002 - in /incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra: ./ selfhealing/
Date Mon, 06 Sep 2010 11:39:14 GMT
Author: thobbs
Date: Mon Sep  6 11:39:14 2010
New Revision: 993002

URL: http://svn.apache.org/viewvc?rev=993002&view=rev
Log:
First pass at a decent self-healing proxy.  Casting to get administrable interface currently
does not work

Added:
    incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/
    incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/
    incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/MulticastServiceFinder.java
    incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/SelfHealingServiceFactory.java
    incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/ServiceFinder.java
    incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/ServiceWrapper.java
    incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/UnicastServiceFinder.java

Added: incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/MulticastServiceFinder.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/MulticastServiceFinder.java?rev=993002&view=auto
==============================================================================
--- incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/MulticastServiceFinder.java
(added)
+++ incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/MulticastServiceFinder.java
Mon Sep  6 11:39:14 2010
@@ -0,0 +1,60 @@
+package org.apache.river.extra.selfhealing;
+
+import java.io.IOException;
+import java.rmi.RemoteException;
+import java.util.logging.Logger;
+
+import net.jini.core.lookup.ServiceItem;
+import net.jini.core.lookup.ServiceTemplate;
+import net.jini.discovery.DiscoveryListenerManagement;
+import net.jini.discovery.DiscoveryManagement;
+import net.jini.discovery.LookupDiscovery;
+import net.jini.lease.LeaseRenewalManager;
+import net.jini.lookup.ServiceDiscoveryManager;
+import net.jini.lookup.ServiceItemFilter;
+
+
+public class MulticastServiceFinder implements ServiceFinder {
+
+    private static final Logger logger = Logger.getLogger(MulticastServiceFinder.class.getSimpleName());
+
+    private final ServiceDiscoveryManager serviceDiscovery;
+
+    public MulticastServiceFinder() throws IOException {
+        this(new LookupDiscovery(LookupDiscovery.ALL_GROUPS), new LeaseRenewalManager());
+    }
+
+    public MulticastServiceFinder(final DiscoveryListenerManagement dlm) throws IOException
{
+        this((DiscoveryManagement) dlm, new LeaseRenewalManager());
+    }
+
+    public MulticastServiceFinder(final DiscoveryManagement dlm, final LeaseRenewalManager
lrm) throws IOException {
+        this.serviceDiscovery = new ServiceDiscoveryManager(dlm, lrm);
+        try {
+            Thread.sleep(2000);
+        } catch (InterruptedException ie) {}
+    }
+
+    public Object findNewService(final ServiceTemplate template) throws RemoteException {
+        return findNewService(template, new ServiceItemFilter() {
+            public boolean check(final ServiceItem item) {
+                return true;
+            }
+        });
+    }
+
+    public Object findNewService(final ServiceTemplate template, final ServiceItemFilter
filter) throws RemoteException {
+        ServiceItem[] services = this.serviceDiscovery.lookup(template, Integer.MAX_VALUE,
filter);
+
+        if(null == services || 0 == services.length) {
+            throw new RemoteException("Cannot find valid service");
+        }
+
+        return services[0].service;
+    }
+
+    public void terminate() {
+        logger.info("Terminating service finder");
+        this.serviceDiscovery.terminate();
+    }
+}
\ No newline at end of file

Added: incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/SelfHealingServiceFactory.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/SelfHealingServiceFactory.java?rev=993002&view=auto
==============================================================================
--- incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/SelfHealingServiceFactory.java
(added)
+++ incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/SelfHealingServiceFactory.java
Mon Sep  6 11:39:14 2010
@@ -0,0 +1,16 @@
+package org.apache.river.extra.selfhealing;
+
+import java.lang.reflect.Proxy;
+
+import net.jini.core.lookup.ServiceTemplate;
+
+public class SelfHealingServiceFactory {
+
+    public static Object lookup(final ServiceTemplate template, final ServiceFinder finder)
{
+        return Proxy.newProxyInstance(template.serviceTypes[0].getClassLoader(),
+                                      template.serviceTypes,
+                                      new ServiceWrapper(finder, template));
+
+    }
+
+}
\ No newline at end of file

Added: incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/ServiceFinder.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/ServiceFinder.java?rev=993002&view=auto
==============================================================================
--- incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/ServiceFinder.java
(added)
+++ incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/ServiceFinder.java
Mon Sep  6 11:39:14 2010
@@ -0,0 +1,13 @@
+package org.apache.river.extra.selfhealing;
+
+import java.rmi.RemoteException;
+
+import net.jini.core.lookup.ServiceTemplate;
+
+public interface ServiceFinder {
+
+    Object findNewService(ServiceTemplate template) throws RemoteException;
+
+    void terminate();
+
+}
\ No newline at end of file

Added: incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/ServiceWrapper.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/ServiceWrapper.java?rev=993002&view=auto
==============================================================================
--- incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/ServiceWrapper.java
(added)
+++ incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/ServiceWrapper.java
Mon Sep  6 11:39:14 2010
@@ -0,0 +1,88 @@
+package org.apache.river.extra.selfhealing;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.rmi.RemoteException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.jini.core.lookup.ServiceTemplate;
+
+public class ServiceWrapper implements InvocationHandler {
+
+    private static final Logger logger = Logger.getLogger(ServiceWrapper.class.getSimpleName());
+
+    private ServiceFinder finder;
+    private ServiceTemplate template;
+    private Object proxy;
+
+    public ServiceWrapper(final ServiceFinder finder, final ServiceTemplate template) {
+        setServiceFinder(finder);
+        setServiceTemplate(template);
+    }
+
+    public Object invoke(final Object ignored, final Method method, final Object[] args)
throws Throwable {
+        initServiceProxy();
+
+        Object response = null;
+        boolean serviceCallFailed = false;
+
+        try {
+            response = execute(method, args);
+        } catch (RemoteException re) {
+            logger.log(Level.WARNING, "Service invocation failed: "+re.getMessage(), re);
+            serviceCallFailed = true;
+        } catch (Throwable t) {
+            logger.log(Level.WARNING, "Service invocation failed: "+t.getMessage(), t);
+            serviceCallFailed = true;
+        }
+
+        //attempt to find a replacement service and reissue the call
+        if(serviceCallFailed) {
+            logger.info("Service call failed.  Looking for replacement service");
+            initServiceProxy();
+            response = execute(method, args);
+        }
+
+        return response;
+    }
+
+
+    protected Object execute(final Method method, final Object[] args) throws RemoteException
{
+        try {
+
+            System.out.println("Invoking method "+method.getName()+" on object "+this.proxy);
+
+            return method.invoke(this.proxy, args);
+        } catch (IllegalAccessException iae) {
+            throw new RemoteException("Cannot execute method because "+iae.getMessage(),
iae);
+        } catch (IllegalArgumentException iae) {
+            throw new RemoteException("Cannot execute method because "+iae.getMessage(),
iae);
+        } catch (InvocationTargetException ite) {
+            throw new RemoteException("Cannot execute method because "+ite.getMessage(),
ite);
+        }
+    }
+
+    private void initServiceProxy() throws RemoteException {
+        if(null == this.proxy) {
+            logger.finer("Looking for a service proxy");
+            this.proxy = this.finder.findNewService(this.template);
+        }
+    }
+
+    private void setServiceFinder(final ServiceFinder finder) {
+        if(null == finder) {
+            throw new IllegalArgumentException("ServiceFinder cannot be null");
+        }
+        this.finder = finder;
+    }
+
+    private void setServiceTemplate(final ServiceTemplate template) {
+        if(null == template) {
+            throw new IllegalArgumentException("ServiceTemplate cannot be null");
+        }
+        this.template = template;
+    }
+
+}

Added: incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/UnicastServiceFinder.java
URL: http://svn.apache.org/viewvc/incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/UnicastServiceFinder.java?rev=993002&view=auto
==============================================================================
--- incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/UnicastServiceFinder.java
(added)
+++ incubator/river/jtsk/skunk/Extra_SelfHealingProxy/src/org/apache/river/extra/selfhealing/UnicastServiceFinder.java
Mon Sep  6 11:39:14 2010
@@ -0,0 +1,57 @@
+package org.apache.river.extra.selfhealing;
+
+import java.io.IOException;
+import java.rmi.RemoteException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.jini.core.discovery.LookupLocator;
+import net.jini.core.lookup.ServiceTemplate;
+import net.jini.core.lookup.StreamServiceRegistrar;
+
+public class UnicastServiceFinder implements ServiceFinder {
+
+    private static final Logger logger = Logger.getLogger(UnicastServiceFinder.class.getSimpleName());
+
+    private LookupLocator[] lookupLocators;
+
+    public UnicastServiceFinder(final LookupLocator[] lookupLocators) {
+        setLookupLocators(lookupLocators);
+    }
+
+    public Object findNewService(final ServiceTemplate template) throws RemoteException {
+        Object proxy = null;
+
+        for(int i=0 ; i<this.lookupLocators.length && null == proxy ; i++) {
+
+            try {
+                StreamServiceRegistrar ssr = this.lookupLocators[i].getStreamRegistrar();
+                proxy = ssr.lookup(template);
+            } catch (IOException ioe) {
+                logger.log(Level.WARNING, "Unable to lookup service on jini://"+this.lookupLocators[i].getHost()+":"+this.lookupLocators[i].getPort(),
ioe);
+            } catch (ClassNotFoundException cce) {
+                logger.log(Level.WARNING, "Unable to lookup service on jini://"+this.lookupLocators[i].getHost()+":"+this.lookupLocators[i].getPort(),
cce);
+            }
+
+        }
+
+        if(null == proxy) {
+            throw new RemoteException("Cannot find valid service");
+        }
+
+        return proxy;
+    }
+
+    public void terminate() {
+    }
+
+    private void setLookupLocators(final LookupLocator[] lookupLocators) {
+        if(null == lookupLocators) {
+            throw new IllegalArgumentException("LookupLocator array cannot be null");
+        }
+        if(0 == lookupLocators.length) {
+            throw new IllegalArgumentException("LookupLocator array must have length >
0");
+        }
+        this.lookupLocators = lookupLocators;
+    }
+}



Mime
View raw message