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/03/02 23:06:17 UTC

svn commit: r749453 - in /incubator/etch/branches/config/services/config: ./ src/main/java/org/apache/etch/services/config/ src/test/java/org/apache/etch/services/config/

Author: sccomer
Date: Mon Mar  2 22:06:17 2009
New Revision: 749453

URL: http://svn.apache.org/viewvc?rev=749453&view=rev
Log:
updated

Added:
    incubator/etch/branches/config/services/config/empty.yml
Modified:
    incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/MainConfigurationListener.java
    incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/YamlConfig.java
    incubator/etch/branches/config/services/config/src/test/java/org/apache/etch/services/config/TestYamlConfig.java

Added: incubator/etch/branches/config/services/config/empty.yml
URL: http://svn.apache.org/viewvc/incubator/etch/branches/config/services/config/empty.yml?rev=749453&view=auto
==============================================================================
    (empty)

Modified: incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/MainConfigurationListener.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/MainConfigurationListener.java?rev=749453&r1=749452&r2=749453&view=diff
==============================================================================
--- incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/MainConfigurationListener.java (original)
+++ incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/MainConfigurationListener.java Mon Mar  2 22:06:17 2009
@@ -52,14 +52,24 @@
 		listener.transportControl( Transport.START_AND_WAIT_UP, 4000 );
 	}
 
-	public ConfigurationServer newConfigurationServer( RemoteConfigurationClient client ) throws ConfigurationException
+	public ConfigurationServer newConfigurationServer(
+		RemoteConfigurationClient client ) throws ConfigurationException
 	{
 		return new MyYamlConfig( client );
 	}
 	
+	/**
+	 * Wrapper around YamlConfig which watches for {@link Session#DOWN} and
+	 * unloads the config (thus canceling all subscriptions).
+	 */
 	public static class MyYamlConfig extends YamlConfig implements ObjSession
 	{
-		public MyYamlConfig( RemoteConfigurationClient client ) throws ConfigurationException
+		/**
+		 * Constructs MyYamlConfig.
+		 * @param client
+		 * @throws ConfigurationException
+		 */
+		public MyYamlConfig( ConfigurationClient client ) throws ConfigurationException
 		{
 			super( client );
 		}

Modified: incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/YamlConfig.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/YamlConfig.java?rev=749453&r1=749452&r2=749453&view=diff
==============================================================================
--- incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/YamlConfig.java (original)
+++ incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/YamlConfig.java Mon Mar  2 22:06:17 2009
@@ -38,6 +38,7 @@
 import org.apache.etch.util.AlarmManager;
 import org.apache.etch.util.Assertion;
 import org.ho.yaml.Yaml;
+import org.ho.yaml.exception.YamlException;
 
 
 /** Local Yaml implementation of ConfigurationServer */
@@ -96,7 +97,7 @@
 				file = f;
 				lastModified = t0;
 				configs = importConfigs( o );
-				return getConfigPath( null, "/" );
+				return getRoot();
 			}
 			
 			try
@@ -134,7 +135,11 @@
 
 	public Object getRoot()
 	{
-		return getConfigPath( null, "/" );
+		if (!isLoaded())
+			throw new IllegalStateException( "no config loaded" );
+		if (configs.size() == 0)
+			throw new IllegalStateException( "config is empty" );
+		return toId( 0 );
 	}
 	
 	/////////////////////
@@ -158,7 +163,11 @@
 
 	public Integer getIndex( Object id )
 	{
-		// TODO Auto-generated method stub
+		Object i = getConf( id ).name;
+		if (i == null)
+			return null;
+		if (i instanceof Integer)
+			return (Integer) i;
 		return null;
 	}
 
@@ -167,9 +176,14 @@
 		Conf c = getConf( id );
 		
 		if (c.isRoot())
-			return "";
+			return "/";
 		
-		return getPath( toId( c.parent ) ) + '/' + c.name;
+		String s = getPath( toId( c.parent ) );
+		
+		if (s.equals( "/" ))
+			return s + c.name;
+		
+		return s + '/' + c.name;
 	}
 
 	public Boolean isRoot( Object id )
@@ -242,13 +256,34 @@
 
 	public Object getConfigPath( Object id, String path )
 	{
-		if (path == null || path.length() == 0)
+		checkPath( path );
+		// path is not null.
+		
+		// id == null: path must be absolute.
+		// id != null: path may be relative or absolute.
+		
+		if (id == null)
 		{
-			getConf( id );
-			return id;
+			if (!isAbsolute( path ))
+				throw new IllegalArgumentException( "path must be absolute when id == null" );
+			
+			id = getRoot();
+			path = abs2rel( path );
+		}
+		else
+		{
+			if (isAbsolute( path ))
+			{
+				id = getRoot();
+				path = abs2rel( path );
+			}
+			else
+			{
+				getConf( id );
+			}
 		}
 		
-		// path is not null and not empty.
+		// id might be null.
 		
 		Integer iid = fromId( id );
 		
@@ -266,6 +301,31 @@
 		
 		return toId( iid );
 	}
+	
+	private String abs2rel( String path )
+	{
+		if (!isAbsolute( path ))
+			return path;
+		return path.substring( 1 );
+	}
+
+	private static boolean isAbsolute( String path )
+	{
+		checkPath( path );
+		return path.startsWith( "/" );
+	}
+	
+	private static boolean isRelative( String path )
+	{
+		checkPath( path );
+		return !path.startsWith( "/" );
+	}
+	
+	private static void checkPath( String path )
+	{
+		if (path == null)
+			throw new IllegalArgumentException( "path == null" );
+	}
 
 	////////////////////////
 	// NODE / PATH ACCESS //
@@ -278,8 +338,7 @@
 
 	public Object getValuePath( Object id, String path )
 	{
-		// TODO Auto-generated method stub
-		return null;
+		return getValue( getConfigPath( id, path ) );
 	}
 
 	public Boolean getBooleanPath( Object id, String path )
@@ -323,61 +382,46 @@
 
 	public Boolean hasValue( Object id )
 	{
-		return id != null && getConf( id ).hasValue();
+		return getConf( id ).hasValue();
 	}
 
 	public Object getValue( Object id )
 	{
-		// TODO Auto-generated method stub
-		return null;
+		return getConf( id ).getValue();
 	}
 
 	public Boolean getBoolean( Object id )
 	{
-		if (id == null)
-			return null;
 		return getConf( id ).getBoolean();
 	}
 
 	public Integer getInteger( Object id )
 	{
-		if (id == null)
-			return null;
 		return getConf( id ).getInteger();
 	}
 
 	public Double getDouble( Object id )
 	{
-		if (id == null)
-			return null;
 		return getConf( id ).getDouble();
 	}
 
 	public String getString( Object id )
 	{
-		if (id == null)
-			return null;
 		return getConf( id ).getString();
 	}
 
 	public Date getDate( Object id )
 	{
-		if (id == null)
-			return null;
 		return getConf( id ).getDate();
 	}
 
 	public List<?> getList( Object id, Integer depth )
 	{
-		if (id == null)
-			return null;
 		return getConf( id ).getList( depth );
 	}
 
 	public Map<?, ?> getMap( Object id, Integer depth )
 	{
-		if (id == null)
-			return null;
 		return getConf( id ).getMap( depth );
 	}
 	
@@ -447,13 +491,15 @@
 
 	private Object toId( Integer iid )
 	{
+		if (iid == null)
+			throw new NullPointerException( "iid == null" );
 		return iid;
 	}
 
-	private Integer fromId( Object id )
+	private int fromId( Object id )
 	{
 		if (id == null)
-			return null;
+			throw new IllegalArgumentException( "id == null" );
 		if (id instanceof Integer)
 			return (Integer) id;
 		return ((Number) id).intValue();
@@ -504,6 +550,10 @@
 		{
 			throw new ConfigurationException( "file not found: " + file );
 		}
+		catch ( YamlException e )
+		{
+			throw new ConfigurationException( "file not loaded: " + file + "; "+e );
+		}
 	}
 
 	private static List<Conf> importConfigs( Object root )
@@ -617,15 +667,11 @@
 		return k;
 	}
 	
-	private Conf getConf( Integer iid )
+	private Conf getConf( int iid )
 	{
 		if (!isLoaded())
 			throw new IllegalStateException( "no config loaded" );
 		
-		if (iid == null)
-			iid = 0;
-//			throw new IllegalArgumentException( "id == null" );
-		
 		if (iid < 0 || iid >= configs.size())
 			throw new IllegalArgumentException( "id < 0 || id >= configs.size()" );
 		
@@ -704,6 +750,12 @@
 			this.value = value;
 		}
 		
+		public Object getValue()
+		{
+			// TODO Auto-generated method stub
+			return null;
+		}
+
 		public Integer parent;
 		
 		public Object name;

Modified: incubator/etch/branches/config/services/config/src/test/java/org/apache/etch/services/config/TestYamlConfig.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/config/services/config/src/test/java/org/apache/etch/services/config/TestYamlConfig.java?rev=749453&r1=749452&r2=749453&view=diff
==============================================================================
--- incubator/etch/branches/config/services/config/src/test/java/org/apache/etch/services/config/TestYamlConfig.java (original)
+++ incubator/etch/branches/config/services/config/src/test/java/org/apache/etch/services/config/TestYamlConfig.java Mon Mar  2 22:06:17 2009
@@ -11,6 +11,8 @@
 	
 	private static final String BOGUS = "services/config/bogus";
 	
+	private static final String EMPTY = "services/config/empty";
+	
 	private static final String DIR = "services/config/dir";
 	
 	private final MyConfigurationClient client = new MyConfigurationClient();
@@ -65,6 +67,13 @@
 	}
 	
 	/** @throws Exception */
+	@Test( expected = ConfigurationException.class)
+	public void construct4d() throws Exception
+	{
+		new YamlConfig( null, EMPTY );
+	}
+	
+	/** @throws Exception */
 	@Test
 	public void construct5() throws Exception
 	{
@@ -100,6 +109,13 @@
 	}
 	
 	/** @throws Exception */
+	@Test( expected = ConfigurationException.class)
+	public void construct6d() throws Exception
+	{
+		new YamlConfig( client, EMPTY );
+	}
+	
+	/** @throws Exception */
 	@Test
 	public void canload() throws Exception
 	{
@@ -108,6 +124,7 @@
 		Assert.assertFalse( c.canLoad( "" ) );
 		Assert.assertFalse( c.canLoad( DIR ) );
 		Assert.assertFalse( c.canLoad( BOGUS ) );
+		Assert.assertTrue( c.canLoad( EMPTY ) );
 		Assert.assertTrue( c.canLoad( REMOTE ) );
 	}
 	
@@ -116,7 +133,8 @@
 	public void notloaded1() throws Exception
 	{
 		ConfigurationServer c = new YamlConfig( null );
-		c.getPath( null );
+		Assert.assertFalse( c.isLoaded() );
+		c.getRoot();
 	}
 	
 	/** @throws Exception */
@@ -124,7 +142,8 @@
 	public void notloaded2() throws Exception
 	{
 		ConfigurationServer c = new YamlConfig( null, null );
-		c.getPath( null );
+		Assert.assertFalse( c.isLoaded() );
+		c.getRoot();
 	}
 	
 	/** @throws Exception */
@@ -176,8 +195,8 @@
 	public void loaded1() throws Exception
 	{
 		ConfigurationServer c = new YamlConfig( null );
-		c.loadConfig( REMOTE );
-		Assert.assertEquals( "", c.getPath( null ) );
+		Object r = c.loadConfig( REMOTE );
+		Assert.assertEquals( "/", c.getPath( r ) );
 	}
 	
 	/** @throws Exception */
@@ -185,7 +204,7 @@
 	public void loaded2() throws Exception
 	{
 		ConfigurationServer c = new YamlConfig( null, REMOTE );
-		Assert.assertEquals( "", c.getPath( null ) );
+		Assert.assertEquals( "/", c.getPath( c.getRoot() ) );
 	}
 	
 	/** @throws Exception */
@@ -193,10 +212,10 @@
 	public void unloaded1() throws Exception
 	{
 		ConfigurationServer c = new YamlConfig( null );
-		c.loadConfig( REMOTE );
-		Assert.assertEquals( "", c.getPath( null ) );
+		Object r = c.loadConfig( REMOTE );
+		Assert.assertEquals( "/", c.getPath( r ) );
 		c.unloadConfig();
-		c.getPath( null );
+		c.getPath( r );
 	}
 	
 	/** @throws Exception */