You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@river.apache.org by th...@apache.org on 2012/04/19 16:41:17 UTC

svn commit: r1327969 - in /river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery: ./ AbstractDiscovery.java DiscoveryException.java EasyDiscovery.java MulticastDiscovery.java UnicastDiscovery.java

Author: thobbs
Date: Thu Apr 19 14:41:16 2012
New Revision: 1327969

URL: http://svn.apache.org/viewvc?rev=1327969&view=rev
Log:
tidying up svn

Added:
    river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/
    river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/AbstractDiscovery.java
    river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/DiscoveryException.java
    river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/EasyDiscovery.java
    river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/MulticastDiscovery.java
    river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/UnicastDiscovery.java

Added: river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/AbstractDiscovery.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/AbstractDiscovery.java?rev=1327969&view=auto
==============================================================================
--- river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/AbstractDiscovery.java (added)
+++ river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/AbstractDiscovery.java Thu Apr 19 14:41:16 2012
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.river.extra.discovery;
+
+import java.io.IOException;
+import java.rmi.RMISecurityManager;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.jini.discovery.DiscoveryManagement;
+import net.jini.lease.LeaseRenewalManager;
+import net.jini.lookup.LookupCache;
+import net.jini.lookup.ServiceDiscoveryListener;
+import net.jini.lookup.ServiceDiscoveryManager;
+
+/**
+ * Base class for unicast and multicast implementations of {@link EasyDiscovery}
+ * 
+ * Provides serivce discovery according to a {@link LookupCache}.
+ * 
+ * @author Tom Hobbs
+ */
+abstract class AbstractDiscovery implements EasyDiscovery {
+
+	private static final Logger log = Logger.getLogger(AbstractDiscovery.class.getSimpleName());
+	
+	private final DiscoveryManagement disco;
+	private final ServiceDiscoveryManager sdm;
+	private final LookupCache cache;
+	
+	protected AbstractDiscovery(DiscoveryManagement dm) throws IOException {
+		System.setSecurityManager(new RMISecurityManager());
+		
+		this.disco = dm;
+		
+		LeaseRenewalManager lrm = new LeaseRenewalManager();
+		sdm = new ServiceDiscoveryManager(dm, lrm);
+		cache = sdm.createLookupCache(EVERYTHING, NO_FILTER, NO_LISTENER);
+	}
+	
+	protected DiscoveryManagement getDiscoveryManagement() {
+		return this.disco;
+	}
+	
+	protected ServiceDiscoveryManager getServiceDiscoveryManager() {
+		return this.sdm;
+	}
+	
+	protected LookupCache getCache() {
+		return this.cache;
+	}
+	
+	@Override
+	public void addServiceDiscoveryListener(ServiceDiscoveryListener sdl) {
+		if(null != sdl) {
+			cache.addListener(sdl);
+		}
+	}
+	
+	@Override
+	public void removeServiceDiscoveryListener(ServiceDiscoveryListener sdl) {
+		if(null != sdl) {
+			cache.removeListener(sdl);
+		}
+	}
+	
+	@Override
+	public void terminate() {
+		try {
+			cache.terminate();
+		} catch (Exception e) {
+			log.log(Level.WARNING, "Failed to terminate LookupCache", e);
+		}
+		try {
+			sdm.terminate();
+		} catch (Exception e) {
+			log.log(Level.WARNING, "Failed to terminate ServiceDiscoveryManager", e);
+		}
+		try {
+			disco.terminate();
+		} catch (Exception e) {
+			log.log(Level.WARNING, "Failed to terminate DiscoveryManagement", e);
+		}
+	}
+}

Added: river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/DiscoveryException.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/DiscoveryException.java?rev=1327969&view=auto
==============================================================================
--- river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/DiscoveryException.java (added)
+++ river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/DiscoveryException.java Thu Apr 19 14:41:16 2012
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.river.extra.discovery;
+
+import java.rmi.RemoteException;
+
+/**
+ * Simple unchecked wrapper exception for a checked RemoteException or general
+ * error message.
+ * 
+ * @author Tom Hobbs
+ *
+ */
+public class DiscoveryException extends RuntimeException {
+
+	private static final long serialVersionUID = -5785894817597158158L;
+
+	/**
+	 * Wraps a checked RuntimeException
+	 * 
+	 * @param e
+	 * 		  The exception to wrap
+	 */
+	public DiscoveryException(RemoteException e) {
+		super(e);
+	}
+
+	/**
+	 * General discovery exception with an explanation message
+	 * @param msg
+	 */
+	public DiscoveryException(String msg) {
+		super(msg);
+	}
+
+
+}

Added: river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/EasyDiscovery.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/EasyDiscovery.java?rev=1327969&view=auto
==============================================================================
--- river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/EasyDiscovery.java (added)
+++ river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/EasyDiscovery.java Thu Apr 19 14:41:16 2012
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.river.extra.discovery;
+
+import java.rmi.RemoteException;
+import java.util.Collection;
+import java.util.Iterator;
+
+import net.jini.core.lookup.ServiceItem;
+import net.jini.core.lookup.ServiceTemplate;
+import net.jini.lookup.ServiceDiscoveryEvent;
+import net.jini.lookup.ServiceDiscoveryListener;
+import net.jini.lookup.ServiceItemFilter;
+
+/**
+ * 
+ * This is a simple interface which can be used to describe discovery of 
+ * services regardless of the underlying mechanism (unicast or multicase.)
+ * 
+ * @author Tom Hobbs
+ *
+ */
+public interface EasyDiscovery extends Iterable<ServiceItem> {
+
+	/**
+	 * Convenience field to signify no discovery filter, or rather, a filter
+	 * that passes all {@link ServiceItem}s
+	 */
+	public final static ServiceItemFilter NO_FILTER = new ServiceItemFilter() {
+		@Override
+		public boolean check(ServiceItem item) {
+			return true;
+		}
+	};
+	
+	/**
+	 * Convenience field to signify no discovery listener
+	 */
+	public final static ServiceDiscoveryListener NO_LISTENER = new ServiceDiscoveryListener() {
+		
+		@Override
+		public void serviceRemoved(ServiceDiscoveryEvent event) {
+			// do nothing
+		}
+		
+		@Override
+		public void serviceChanged(ServiceDiscoveryEvent event) {
+			// do nothing
+		}
+		
+		@Override
+		public void serviceAdded(ServiceDiscoveryEvent event) {
+			// do nothing
+		}
+	};
+	
+	/**
+	 * Convenience field to match all service types 
+	 */
+	public final static ServiceTemplate EVERYTHING = new ServiceTemplate(null, null, null);
+	
+	/**
+	 * Terminates the discovery mechanism.
+	 */
+	public abstract void terminate();
+
+	/**
+	 * Returns a collection of all the matching services according to the
+	 * supplied template.
+	 *  
+	 * @param tmpl
+	 * 		  The template to match
+	 * 
+	 * @return The matching templates
+	 * 
+	 * @throws RemoteException
+	 * 		   If discovery has failed for some reason
+	 */
+	public abstract Collection<ServiceItem> lookupAll(ServiceTemplate tmpl) throws RemoteException;
+
+	/**
+	 * @return An iterator over all of the services currently existing in the
+	 * djinn.
+	 */
+	@Override
+	public abstract Iterator<ServiceItem> iterator();
+	
+	/**
+	 * Allows the addition of a {@link ServiceDiscoveryListener} to be informed
+	 * when the state of the djinn changes.
+	 * 
+	 * @param sdl
+	 */
+	public abstract void addServiceDiscoveryListener(ServiceDiscoveryListener sdl);
+	
+	public abstract void removeServiceDiscoveryListener(ServiceDiscoveryListener sdl);
+
+}

Added: river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/MulticastDiscovery.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/MulticastDiscovery.java?rev=1327969&view=auto
==============================================================================
--- river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/MulticastDiscovery.java (added)
+++ river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/MulticastDiscovery.java Thu Apr 19 14:41:16 2012
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.river.extra.discovery;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import net.jini.core.lookup.ServiceItem;
+import net.jini.core.lookup.ServiceTemplate;
+import net.jini.discovery.LookupDiscovery;
+
+/**
+ * Provides a simply discovery mechanism using multicast.
+ * 
+ * @author Tom Hobbs
+ *
+ */
+public class MulticastDiscovery extends AbstractDiscovery {
+
+	/**
+	 * Provides lookup mechanism for all lookup groups.
+	 * 
+	 * @throws IOException
+	 * @see {@link LookupDiscovery.ALL_GROUPS}
+	 */
+	public MulticastDiscovery() throws IOException {
+		this(LookupDiscovery.ALL_GROUPS);
+	}
+
+	/**
+	 * Provides lookup mechanism for supplied lookup groups only.
+	 * 
+	 * @param groups
+	 * @throws IOException
+	 */
+	public MulticastDiscovery(String[] groups) throws IOException {
+		super(new LookupDiscovery(groups));
+	}
+
+	@Override
+	public Iterator<ServiceItem> iterator() {
+		ServiceItem[] services = getCache().lookup(EasyDiscovery.NO_FILTER, 
+												   Integer.MAX_VALUE);
+		return Arrays.asList(services).iterator();
+	}
+
+	@Override
+	public List<ServiceItem> lookupAll(ServiceTemplate tmpl) {
+		return Arrays.asList(getServiceDiscoveryManager().lookup(tmpl, 
+																 Integer.MAX_VALUE, 
+																 EasyDiscovery.NO_FILTER));
+	}
+
+}

Added: river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/UnicastDiscovery.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/UnicastDiscovery.java?rev=1327969&view=auto
==============================================================================
--- river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/UnicastDiscovery.java (added)
+++ river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/discovery/UnicastDiscovery.java Thu Apr 19 14:41:16 2012
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.river.extra.discovery;
+
+import java.io.IOException;
+import java.rmi.RemoteException;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import net.jini.core.discovery.LookupLocator;
+import net.jini.core.lookup.ServiceItem;
+import net.jini.core.lookup.ServiceMatches;
+import net.jini.core.lookup.ServiceRegistrar;
+import net.jini.core.lookup.ServiceTemplate;
+import net.jini.discovery.LookupLocatorDiscovery;
+
+/**
+ * Provides a simply discovery mechanism using unicast.
+ * 
+ * @author Tom Hobbs
+ *
+ */
+public class UnicastDiscovery extends AbstractDiscovery {
+
+	/**
+	 * Provides lookup using the specified {@link LookupLocator}s.
+	 * 
+	 * @param locators
+	 * 		  The unicast lookup locators to use
+	 * @throws IOException
+	 */
+	public UnicastDiscovery(LookupLocator... locators) throws IOException {
+		super(new LookupLocatorDiscovery(locators));
+	}
+
+	@Override
+	public List<ServiceItem> lookupAll(ServiceTemplate tmpl) throws RemoteException {
+		ServiceRegistrar[] registrars = getDiscoveryManagement().getRegistrars();
+		if(null != registrars && 0 != registrars.length) {
+			ServiceMatches matches = registrars[0].lookup(tmpl, Integer.MAX_VALUE);
+			return Arrays.asList(matches.items);
+		} else {
+			return null;
+		}
+	}
+
+	@Override
+	public Iterator<ServiceItem> iterator() throws DiscoveryException {
+		try {
+			List<ServiceItem> all = lookupAll(EVERYTHING);
+			if(null != all) {
+				return all.iterator();
+			}
+		} catch (RemoteException e) {
+			throw new DiscoveryException(e);
+		}
+		return null;
+	}
+
+}