All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nyla.solutions.global.patterns.command.remote.partitioning.PartitionCommasRemoteRegistry Maven / Gradle / Ivy

Go to download

Nyla Solutions Global Java API provides support for basic application utilities (application configuration, data encryption, debugger and text processing).

The newest version!
package nyla.solutions.global.patterns.command.remote.partitioning;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Collection;

import nyla.solutions.global.exception.ConnectionException;
import nyla.solutions.global.exception.RequiredException;
import nyla.solutions.global.net.rmi.RMI;
import nyla.solutions.global.patterns.command.commas.CommandFacts;
import nyla.solutions.global.patterns.loadbalancer.LoadBalanceRegistry;
import nyla.solutions.global.patterns.servicefactory.ServiceFactory;
import nyla.solutions.global.util.Config;
import nyla.solutions.global.util.Debugger;

/**
 * Configuration properties
 * 
 * 
	nyla.solutions.global.patterns.command.remote.partitioning.RemoteCommasRegistry.host=localhost
	nyla.solutions.global.patterns.command.remote.partitioning.RemoteCommasRegistry.port=27001
	nyla.solutions.global.patterns.command.remote.partitioning.RemoteCommasRegistry.name=commasRegistry

 * @author Gregory Green
 *
 */
public class PartitionCommasRemoteRegistry implements CommasRemoteRegistry, Remote
{
	public PartitionCommasRemoteRegistry()
	{
		ServiceFactory factory = ServiceFactory.getInstance(PartitionCommasRemoteRegistry.class);
		
		this.loadBalanceRegistry = factory.create(LoadBalanceRegistry.class);
	}// -------------------------------------------------------
	public String whereIs(String key, CommandFacts commandFacts)
	throws RemoteException
	{
		
		//Build property based on region name
		String lookupKey = constructLookupKey(key, commandFacts);
		
		return this.loadBalanceRegistry.lookup(lookupKey);
			
	}// --------------------------------------------------------

	/**
	 * List registered locations
	 * @see nyla.solutions.global.patterns.command.remote.partitioning.CommasRemoteRegistry#listRegisteredLocations()
	 */
	@Override
	public Collection listRegisteredLocations()
	throws RemoteException
	{
		return this.loadBalanceRegistry.listRegistered();
	}// --------------------------------------------------------
	@Override
	public void unregisterLocation(String location) throws RemoteException
	{
		//remove all keys
		this.loadBalanceRegistry.unregister(location);
	}// --------------------------------------------------------
	
	/**
	 * 
	 * @param type
	 * @param key
	 * @param remote
	 */
	public void registerKey(String key, CommandFacts commandFacts, String location )
	throws RemoteException
	{
		this.loadBalanceRegistry.register(key, location);
	}// --------------------------------------------------------
	/**
	 * Register a location
	 * @see nyla.solutions.global.patterns.command.remote.partitioning.CommasRemoteRegistry#registerLocation(java.lang.Object)
	 */
	public void registerLocation(String location)
	throws RemoteException
	{
		loadBalanceRegistry.register(location);
	}// --------------------------------------------------------

	public static CommasRemoteRegistry getRegistry()
	throws RemoteException
	{
		RMI rmi = new RMI(host,port);
		try
		{
			CommasRemoteRegistry remote = rmi.lookup("commasRegistry");
				
			return remote;
		}
		catch (Exception e)
		{
			throw new ConnectionException("name:"+name+" ERROR:"+e.getMessage()+" rmi"+Debugger.toString(rmi.list()),e);
		}
	}// --------------------------------------------------------

	/**
	 * 
	 * @param args 0=host 1=port 2=name
	 */
	public static void main(String[] args)
	{
		if(args.length != 3)
		{
			System.err.println("Usage "+PartitionCommasRemoteRegistry.class.getSimpleName()+" host port name");
			return;
		}
		
		String host =args[0];
		int port = Integer.parseInt(args[1]);
		String name = args[2];
		
		startRegistry(host,port, name);
	}// --------------------------------------------------------
	/**
	 * Starts the partition commas registry
	 * @param host the host name where the RMI registry is running
	 * @param port the port the RMI port
	 * @param name the name of the PartitionCommasRemoteRegistry to registerb
	 */
	public static void startRegistry(String host,int port, String name)
	{
		try
		{
			 
			RMI rmi = new RMI(host,port);
			PartitionCommasRemoteRegistry registry = new PartitionCommasRemoteRegistry();
			
			rmi.rebind(name, registry);
		
		}
		catch (Exception e)
		{
			Debugger.printError(e);
		}
	}// --------------------------------------------------------
	
	private String constructLookupKey(String key, CommandFacts commandFacts)
	{
		try
		{
			return new StringBuilder(commandFacts.getTargetName()).append(".").append(key).toString();
		}
		catch(NullPointerException e)
		{
			throw new RequiredException("key:"+key+" commandFacts(region):"+commandFacts);
		}
	}// --------------------------------------------------------

	private static final String host = Config.getProperty(PartitionCommasRemoteRegistry.class,"host");
	private static final int port = Config.getPropertyInteger(PartitionCommasRemoteRegistry.class,"port").intValue();
	private static final String name = Config.getProperty(PartitionCommasRemoteRegistry.class,"name");
	private final LoadBalanceRegistry loadBalanceRegistry;
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy