// DBAddress.java

package ed.db;

import java.net.*;

import ed.net.*;
import ed.util.*;
import ed.cloud.*;

public class DBAddress {
    
    /**
     * name
     * <host>/name
     * <host>:<port>/name
     */
    public DBAddress( String urlFormat )
        throws UnknownHostException {
        _check( urlFormat , "urlFormat" );
        
        int idx = urlFormat.indexOf( "/" );
        if ( idx < 0 ){
            _host = defaultHost();
            _port = defaultPort();
            _name = urlFormat;
        }
        else {
            _name = urlFormat.substring( idx + 1 ).trim();
            urlFormat = urlFormat.substring( 0 , idx ).trim();
            idx = urlFormat.indexOf( ":" );
            if ( idx < 0 ){
                _host = urlFormat.trim();
                _port = defaultPort();
            }
            else {
                _host = urlFormat.substring( 0 , idx );
                _port = Integer.parseInt( urlFormat.substring( idx + 1 ) );
            }
        }
        
        _check( _host , "host" );
        _check( _name , "name" );
        _addr = _getAddress( _host );

        if ( _name.contains( "." ) )
            throw new RuntimeException( "db names can't have periods in them [" + _name + "]" );
    }

    public DBAddress( DBAddress other , String name )
        throws UnknownHostException {
        this( other._host , other._port , name );
    }

    public DBAddress( String host , String name )
        throws UnknownHostException {
        this( host , DBPort.PORT , name );
    }
    
    public DBAddress( String host , int port , String name )
        throws UnknownHostException {
        _check( host , "host" );
        _check( name , "name" );
        
        _host = host.trim();
        _port = port;
        _name = name.trim();
        _addr = _getAddress( _host );
    }

    static void _check( String thing , String name ){
        if ( thing == null )
            throw new NullPointerException( name + " can't be null " );
        
        thing = thing.trim();
        if ( thing.length() == 0 )
            throw new IllegalArgumentException( name + " can't be empty" );
    }

    public int hashCode(){
        return _host.hashCode() + _port + _name.hashCode();
    }

    public boolean equals( Object other ){
        if ( other instanceof DBAddress ){
            DBAddress a = (DBAddress)other;
            return 
                a._port == _port &&
                a._name.equals( _name ) &&
                a._host.equals( _host );
        }
        return false;
    }

    public String toString(){
        return _host + ":" + _port + "/" + _name;
    }

    public InetSocketAddress getSocketAddress(){
        return new InetSocketAddress( _addr , _port );
    }

    public boolean sameHost( String host ){
        int idx = host.indexOf( ":" );
        int port = defaultPort();
        if ( idx > 0 ){
            port = Integer.parseInt( host.substring( idx + 1 ) );
            host = host.substring( 0 , idx );
        }

        return 
            _port == port &&
            _host.equalsIgnoreCase( host );
    }

    final String _host;
    final int _port;
    final String _name;
    final InetAddress _addr;
    
    private static InetAddress _getAddress( String host )
        throws UnknownHostException {
        return DNSUtil.getByName( _checkCloudHostName( host ) );
    }

    private static String _checkCloudHostName( final String host ){
        if ( host.contains( "." ) )
            return host;

        Cloud c = Cloud.getInstanceIfOnGrid();
        if ( c == null )
            return host;
        
        try {
            String temp = c.getDBHost( host );
            if ( temp != null )
                return temp;
        }
        catch ( Exception e ){} // don't care

        return host;
    }

    static String defaultHost(){
        return Config.get().getTryEnvFirst( "db_ip" , "127.0.0.1" );
    }

    static int defaultPort(){
        return Integer.parseInt(Config.get().getTryEnvFirst("db_port", Integer.toString(DBPort.PORT)));
    }
    
}

@@ -486,32 +486,42 @@
          _name =
+ _fixName(
  urlFormat;%0A    
@@ -514,16 +514,18 @@
 rlFormat
+ )
 ;%0A      
@@ -562,16 +562,26 @@
  _name =
+ _fixName(
  urlForm
@@ -602,32 +602,34 @@
 idx + 1 ).trim()
+ )
 ;%0A            ur
@@ -1138,141 +1138,123 @@
  );%0A
+    %7D
 %0A    
+%0A
     
-if ( _name.contains( %22.%22 ) )%0A            throw new RuntimeException( %22db names can't have periods in them %5B%22 + _name + %22%5D%22 )
+static String _fixName( String name )%7B%0A        name = name.replace( '.' , '-' );%0A        return name
 ;%0A  

. in names replace to -