You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by sc...@apache.org on 2009/04/17 17:29:16 UTC

svn commit: r766050 - in /incubator/etch/branches/name-service/services/ns/src: main/java/org/apache/etch/services/ns/support/ test/java/org/apache/etch/services/ns/support/

Author: sccomer
Date: Fri Apr 17 15:29:16 2009
New Revision: 766050

URL: http://svn.apache.org/viewvc?rev=766050&view=rev
Log:
name service patch from manoj ganesan.

Added:
    incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestEtchTransportFactory.java   (with props)
Modified:
    incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/DefaultNSLib.java
    incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/EtchTransportFactory.java
    incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/NSLib.java
    incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestNSLib.java

Modified: incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/DefaultNSLib.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/DefaultNSLib.java?rev=766050&r1=766049&r2=766050&view=diff
==============================================================================
--- incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/DefaultNSLib.java (original)
+++ incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/DefaultNSLib.java Fri Apr 17 15:29:16 2009
@@ -42,23 +42,14 @@
 {
 	private final static int INITIAL_ALARM_DELAY = 1;
 	
-	public NSLib getImplInstance()
-	{
-		return new DefaultNSLib();
-	}
-	
-	/**
-	 * Collection of <session, record> entries to keep track of all listeners connected 
-	 * to this instance of NSLib
-	 */
+	// Collection of <session, record> entries to keep track of all listeners connected 
+	// to this instance of NSLib
 	public Map<Session, Record> recordsBySession = Collections.synchronizedMap( 
 			new HashMap<Session, Record>() );
 	
-	/*
-	 * Map of <nsUri, RemoteNameServiceServer> 
-	 */
+	// Map of <nsUri, RemoteNameServiceServer>
 	private Map<String, RemoteNameServiceServer> serversByNsUri = 
-		new HashMap<String, RemoteNameServiceServer>();
+		Collections.synchronizedMap( new HashMap<String, RemoteNameServiceServer>() );
 	
 	/**
 	 * Create a new RemoteNameServiceServer with the nsUri supplied
@@ -120,6 +111,12 @@
 	 * @return appropriate instance of RemoteNameServiceServer
 	 * @throws Exception
 	 */
+	/**
+	 * Get server object to communicate with the name service
+	 * @param nsUri name service uri
+	 * @return server object
+	 * @throws Exception
+	 */
 	public RemoteNameServiceServer getServer( String nsUri ) throws Exception
 	{
 		synchronized( serversByNsUri )
@@ -135,9 +132,8 @@
 		}
 	}
 
-	/**
-	 * Convenience object holding relevant listener data
-	 */
+	// Convenience object holding relevant listener data
+	// Also listens to alarm events
 	private class Record implements AlarmListener
 	{
 		String nsUri;
@@ -176,16 +172,16 @@
 
 		URL u = new URL( uri );
 
+		String sourceUri = u.getUri();
+		if ( sourceUri == null || sourceUri.isEmpty() )
+			throw new IllegalArgumentException( "sourceUri == null" );
+		
 		if ( ! u.hasTerm( EtchTransportFactory.LISTENER_REGISTERED_URI ) )
 			throw new IllegalArgumentException( "listener registered uri " +
 			"not configured properly within the etch uri" );
-		
-		String sourceUri = u.getUri();
 		String targetUri = u.getTerm( EtchTransportFactory.LISTENER_REGISTERED_URI );
-		String nsUri = null;
 		
-		if ( sourceUri == null || sourceUri.isEmpty() )
-			throw new IllegalArgumentException( "sourceUri == null" );
+		String nsUri = null;
 		
 		// extract if present in uri
 		if ( u.hasTerm( EtchTransportFactory.NS_URI ) )
@@ -195,7 +191,7 @@
 			nsUri = defaultNsUri;
 		
 		Record record = new Record( nsUri, sourceUri, qualities, targetUri, ttl );
-		//Assertion.check( recordsBySession.containsKey( session ), "session already exists" );
+		// org.apache.etch.util.Assertion.check( recordsBySession.containsKey( session ), "session already exists" );
 		recordsBySession.put( session, record );
 		
 		// add register task to the alarm manager

Modified: incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/EtchTransportFactory.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/EtchTransportFactory.java?rev=766050&r1=766049&r2=766050&view=diff
==============================================================================
--- incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/EtchTransportFactory.java (original)
+++ incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/EtchTransportFactory.java Fri Apr 17 15:29:16 2009
@@ -63,10 +63,12 @@
 		URL u = new URL( uri );
 		
 		// Extract uri for the listener to listen on 
-		if ( !u.hasTerm( LISTENER_URI ) )
+		if ( !u.hasTerm( LISTENER_URI ) || (listenerUri = u.getTerm( LISTENER_URI )).isEmpty() )
 			throw new IllegalArgumentException( "listener uri not configured properly within the etch uri" );
 		
-		listenerUri = u.getTerm( LISTENER_URI ); 
+		if ( !u.hasTerm( LISTENER_REGISTERED_URI ) || u.getTerm( LISTENER_REGISTERED_URI).isEmpty() )
+			throw new IllegalArgumentException( "listener registered uri " +
+			"not configured properly within the etch uri" );
 		
 		// call to create transport stack underneath
 		Transport<ServerFactory> transport = TransportFactory.getListener( listenerUri, resources );
@@ -103,9 +105,12 @@
 			this.uri = uri;
 			
 			URL u = new URL( uri );
+
+			if ( u.getUri() == null || u.getUri().isEmpty() )
+				throw new IllegalArgumentException( "source uri == null" );
+
 			if ( u.hasTerm( RECONNECT_TERM ) )
 				reconnect = u.getIntegerTerm( RECONNECT_TERM, 0 );
-			
 		}
 		
 		public void transportMessage( Who recipient, Message msg )
@@ -345,7 +350,7 @@
 			if ( control == START || control == START_AND_WAIT_UP )
 			{	
 				// register to the naming service here
-				DefaultNSLib.staticRegister( getSession(), uri, null, 0 );
+				NSLib.staticRegister( getSession(), uri, null, 0 );
 				
 				return;
 			}
@@ -353,7 +358,7 @@
 			if ( control == STOP || control == STOP_AND_WAIT_DOWN )
 			{
 				// unregister from the name service
-				DefaultNSLib.staticUnregister( getSession(), uri, false );
+				NSLib.staticUnregister( getSession(), uri, false );
 				
 				return;
 			}

Modified: incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/NSLib.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/NSLib.java?rev=766050&r1=766049&r2=766050&view=diff
==============================================================================
--- incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/NSLib.java (original)
+++ incubator/etch/branches/name-service/services/ns/src/main/java/org/apache/etch/services/ns/support/NSLib.java Fri Apr 17 15:29:16 2009
@@ -29,8 +29,9 @@
 /**
  * 
  * Class that acts as an interface between core etch and the naming service.
- *
- * NOTE: One NSLib instance deals with only one connection to the name service
+ * 
+ * Several implementations of this class can exist, depending upon the need.
+ * Currently, DefaultNSLib is the default implementation used for NSLib.
  * 
  */
 public abstract class NSLib
@@ -49,14 +50,14 @@
 	public static NSLib getInstance()
 	{
 		
-		/*if ( instance == null )
+		if ( instance == null )
 		{
 			synchronized ( instanceSync )
 			{
 				if ( instance == null )
-					instance = instance.getImplInstance();		
+					instance = new DefaultNSLib();		
 			}
-		}*/
+		}
 		return instance;
 	}
 
@@ -71,6 +72,11 @@
 	
 	/**
 	 * Sets the default NSUri for this NSLib
+	 * 
+	 * This uri will be used when a naming service uri is not 
+	 * specified in the url or if such a uri cannot be 
+	 * extracted using other means.
+	 * 
 	 * @param defaultNsUri the new default NsUri
 	 */
 	public static void staticSetDefaultNsUri( String defaultNsUri )
@@ -79,7 +85,10 @@
 	}
 	
 	/**
-	 * 
+	 * Sets the specific version of NSLib to be used for forwarding 
+	 * calls. By default, DefaultNSLib is used and there's no need to 
+	 * set it explicitly using this method.
+	 *  
 	 * @param newInstance
 	 * @return old instance 
 	 */
@@ -93,6 +102,9 @@
 		}
 	}
 
+	/**
+	 * Shut down method to release resources etc.
+	 */
 	public static void staticShutdown()
 	{
 		NSLib oldInstance = setInstance( null );
@@ -101,12 +113,6 @@
 	}
 	
 	/**
-	 * Obtain instance of the specific NSLib implementation
-	 * @return
-	 */
-	public abstract NSLib getImplInstance();
-	
-	/**
 	 * Cleanup method for the NSLib implementation
 	 */
 	public abstract void shutdown();

Added: incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestEtchTransportFactory.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestEtchTransportFactory.java?rev=766050&view=auto
==============================================================================
--- incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestEtchTransportFactory.java (added)
+++ incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestEtchTransportFactory.java Fri Apr 17 15:29:16 2009
@@ -0,0 +1,313 @@
+package org.apache.etch.services.ns.support;
+
+import java.util.Map;
+
+import org.apache.etch.bindings.java.msg.Message;
+import org.apache.etch.bindings.java.msg.TypeMap;
+import org.apache.etch.bindings.java.msg.ValueFactory;
+import org.apache.etch.bindings.java.support.Class2TypeMap;
+import org.apache.etch.bindings.java.support.DefaultValueFactory;
+import org.apache.etch.bindings.java.support.ServerFactory;
+import org.apache.etch.bindings.java.support.TransportFactory;
+import org.apache.etch.bindings.java.support.TransportHelper;
+import org.apache.etch.bindings.java.transport.SessionMessage;
+import org.apache.etch.bindings.java.transport.TransportMessage;
+import org.apache.etch.services.ns.NameService.Entry;
+import org.apache.etch.util.Monitor;
+import org.apache.etch.util.Resources;
+import org.apache.etch.util.core.Who;
+import org.apache.etch.util.core.io.Session;
+import org.apache.etch.util.core.io.Transport;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/** Test of EtchTransportFactory */
+public class TestEtchTransportFactory
+{
+	private MockNSLib nsLibInstance;
+	
+	/** @throws Exception */
+	@BeforeClass
+	public static void setup() throws Exception
+	{
+		Class.forName( "org.apache.etch.services.ns.support.EtchTransportFactory" );
+	}
+
+	/** @throws Exception */
+	@Before
+	public void setupNsLibInstance() throws Exception
+	{
+		nsLibInstance = new MockNSLib();
+		NSLib.setInstance( nsLibInstance );
+	}
+
+	/** @throws Exception */
+	@Test( expected = IllegalArgumentException.class )
+	public void testListenerUri1() throws Exception
+	{
+		String uri = "etch:sourceListener?suri=&curi=tcp://10.89.30.202:4001&nsuri=namingServiceUri";
+		TransportFactory.getListener( uri, null );
+	}
+
+	/** @throws Exception */
+	@Test( expected = IllegalArgumentException.class )
+	public void testListenerUri2() throws Exception
+	{
+		String uri = "etch:sourceListener?curi=tcp://10.89.30.202:4001&nsuri=namingServiceUri";
+		TransportFactory.getListener( uri, null );
+	}
+
+	/** @throws Exception */
+	@Test( expected = IllegalArgumentException.class )
+	public void testListenerUri3() throws Exception
+	{
+		String uri = "etch:sourceListener?suri=tcp://0.0.0.0:4001&nsuri=namingServiceUri";
+		TransportFactory.getListener( uri, null );
+	}
+
+	/** @throws Exception */
+	@Test( expected = IllegalArgumentException.class )
+	public void testListenerUri4() throws Exception
+	{
+		String uri = "etch:sourceListener?suri=tcp://0.0.0.0:4001&curi=&nsuri=namingServiceUri";
+		TransportFactory.getListener( uri, null );
+	}
+
+	/** @throws Exception */
+	@Test( expected = IllegalArgumentException.class )
+	public void testListenerUri5() throws Exception
+	{
+		String uri = "etch:suri=tcp://0.0.0.0:4001&curi=tcp://10.89.30.202:4001&nsuri=namingServiceUri";
+		TransportFactory.getListener( uri, null );
+	}
+
+	/** @throws Exception */
+	@Test( expected = IllegalArgumentException.class )
+	public void testClientUri1() throws Exception 
+	{
+		String uri = "etch://127.0.0.1:8002/";
+		TransportFactory.getTransport( uri, null );
+	}
+
+	/** @throws Exception */
+	@Test( expected = IllegalArgumentException.class )
+	public void testClientUri2() throws Exception 
+	{
+		String uri = "etch:";
+		TransportFactory.getTransport( uri, null );
+	}
+
+	/** @throws Exception */
+	@Test
+	public void testListener1() throws Exception
+	{
+		String uri = "etch:sourceListener?suri=tcp://0.0.0.0:4001&curi=tcp://10.89.30.202:4001&nsuri=namingServiceUri";
+		Transport<ServerFactory> tsf = TransportFactory.getListener( uri, null );
+		tsf.setSession( new MySession() );
+		
+		// register should not be called before start is called on the transport
+		nsLibInstance.method.waitUntilEq( "getDefaultNsUri" );
+	}
+
+	/** @throws Exception */
+	@Test
+	public void testListener2() throws Exception
+	{
+		String uri = "etch:sourceListener?suri=tcp://0.0.0.0:4001&curi=tcp://10.89.30.202:4001&nsuri=namingServiceUri";
+		Transport<ServerFactory> tsf = TransportFactory.getListener( uri, null );
+		tsf.setSession( new MySession() );
+		tsf.transportControl( Transport.START_AND_WAIT_UP, 4000 );
+		
+		nsLibInstance.method.waitUntilEq( "register" );
+	}
+
+	/** @throws Exception */
+	@Test
+	public void testClient1() throws Exception
+	{
+		String uri = "etch:sourceListener";
+		TransportFactory.getTransport( uri, null );
+		
+		// no lookup should've been done so far
+		nsLibInstance.method.waitUntilEq( "getDefaultNsUri" );
+	}
+
+	/** @throws Exception */
+	@Test @Ignore
+	public void testCommunication1() throws Exception
+	{
+		String uri = "etch:sourceListener?suri=tcp://0.0.0.0:8001&curi=tcp://10.89.30.202:4001&nsuri=namingServiceUri";
+		
+		Resources res = TransportHelper.initResources( null );
+		res.put( Transport.VALUE_FACTORY, new DefaultValueFactory( uri, new TypeMap(), new Class2TypeMap() ) );
+		Transport<ServerFactory> tsf = TransportFactory.getListener( uri, res );
+		tsf.setSession( new MySession() );
+		tsf.transportControl( Transport.START_AND_WAIT_UP, 4000 );
+		
+		nsLibInstance.method.waitUntilEq( "register" );
+		
+		String clientUri = "etch:sourceListener";
+		
+		Resources r = TransportHelper.initResources( null );
+		r.put( Transport.VALUE_FACTORY, new DefaultValueFactory( clientUri, new TypeMap(), new Class2TypeMap() ) );
+		
+		TransportMessage tm = TransportFactory.getTransport( clientUri, r );
+		tm.setSession( new MockSessionMessage() );
+		tm.transportControl( Transport.START_AND_WAIT_UP, 4000 );
+		
+		nsLibInstance.method.waitUntilEq( "lookup" );
+		
+		Thread.sleep( 1000 );
+		
+		tsf.transportControl( Transport.STOP_AND_WAIT_DOWN, 4000 );
+		
+		System.in.read();
+	}
+	
+	/**
+	 * Mock NSLib implementation
+	 */
+	public class MockNSLib extends NSLib
+	{
+		private Monitor<String> method = new Monitor<String>( "method", "getDefaultNsUri" );
+
+		@Override
+		public String getDefaultNsUri()
+		{
+			method.set( "getDefaultNsUri" );
+			return null;
+		}
+
+		@Override
+		public Entry lookup( String uri )
+		{
+			method.set( "lookup" );
+			return new Entry( "sourceListener", null, "tcp://127.0.0.1:8001", 0, null, null, false);
+		}
+
+		@Override
+		public void register( Session session, String uri, Map<?, ?> qualities,
+				int ttl )
+		{
+			method.set( "register" );
+		}
+
+		@Override
+		public void setDefaultNsUri( String defaultNsUri )
+		{
+			method.set( "setDefaultNsUri" );		
+		}
+
+		@Override
+		public void shutdown()
+		{
+			method.set( "shutdown" );		
+		}
+
+		@Override
+		public void unregister( Session session, String uri, boolean deleteEntry )
+		{
+			method.set( "unregister" );
+		}
+	}
+	
+	/**
+	 * Mock session 
+	 *
+	 */
+	private class MySession implements ServerFactory
+	{
+		public void sessionControl( Object arg0, Object arg1 ) throws Exception
+		{
+			System.out.println( "session control" );
+		}
+
+		public void sessionNotify( Object arg0 ) throws Exception
+		{	
+			System.out.println( "session notify: " + arg0 );
+		}
+
+		public Object sessionQuery( Object arg0 ) throws Exception
+		{
+			System.out.println( "sessionQuery: " + arg0 );
+			return null;
+		}
+
+		public void newServer( String uri, Resources resources,
+				TransportMessage transport ) throws Exception
+		{
+			// TODO Auto-generated method stub
+			
+		}
+
+		public ValueFactory newValueFactory()
+		{
+			// TODO Auto-generated method stub
+			return null;
+		}
+
+		public Session getSession()
+		{
+			// TODO Auto-generated method stub
+			return null;
+		}
+
+		public void setSession( Session session )
+		{
+			// TODO Auto-generated method stub
+			
+		}
+
+		public void transportControl( Object control, Object value )
+				throws Exception
+		{
+			// TODO Auto-generated method stub
+			
+		}
+
+		public void transportNotify( Object event ) throws Exception
+		{
+			// TODO Auto-generated method stub
+			
+		}
+
+		public Object transportQuery( Object query ) throws Exception
+		{
+			// TODO Auto-generated method stub
+			return null;
+		}
+	}
+	
+	/** SessionMessage for testing */
+	public class MockSessionMessage implements SessionMessage
+	{
+		public boolean sessionMessage( Who sender, Message msg )
+				throws Exception
+		{
+			// TODO Auto-generated method stub
+			return false;
+		}
+
+		public void sessionControl( Object control, Object value )
+				throws Exception
+		{
+			// TODO Auto-generated method stub
+			
+		}
+
+		public void sessionNotify( Object event ) throws Exception
+		{
+			// TODO Auto-generated method stub
+			
+		}
+
+		public Object sessionQuery( Object query ) throws Exception
+		{
+			// TODO Auto-generated method stub
+			return null;
+		}
+	}
+	
+}

Propchange: incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestEtchTransportFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestEtchTransportFactory.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestNSLib.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestNSLib.java?rev=766050&r1=766049&r2=766050&view=diff
==============================================================================
--- incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestNSLib.java (original)
+++ incubator/etch/branches/name-service/services/ns/src/test/java/org/apache/etch/services/ns/support/TestNSLib.java Fri Apr 17 15:29:16 2009
@@ -27,7 +27,6 @@
 import org.apache.etch.services.ns.MainNameServiceListener;
 import org.apache.etch.services.ns.NameServiceHelper;
 import org.apache.etch.services.ns.NameService.Entry;
-import org.apache.etch.services.ns.support.DefaultNSLib;
 import org.apache.etch.services.ns.support.NSLib;
 import org.apache.etch.util.core.io.Session;
 import org.apache.etch.util.core.io.Transport;
@@ -48,7 +47,6 @@
 	@BeforeClass
 	public static void startNS()
 	{
-		NSLib.setInstance( new DefaultNSLib() );
 		defaultNSUri = NSLib.staticGetDefaultNsUri();
 		try
 		{