prickett    2002/11/30 19:38:59

  Modified:    
periodicity/src/plugins-build/database/src/java/org/apache/commons/periodicity/database
                        DatabaseMetaDataImpl.java
  Log:
  Added code to generate a database url
  
  Added a default port field
  
  Added a buildContext method
  
  Added code to return the default port if the port is not specified
  
  Added a getDatabasePortAsString method
  
  Added a getRawUrl()
  
  Revision  Changes    Path
  1.11      +175 -9    
jakarta-commons-sandbox/periodicity/src/plugins-build/database/src/java/org/apache/commons/periodicity/database/DatabaseMetaDataImpl.java
  
  Index: DatabaseMetaDataImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/periodicity/src/plugins-build/database/src/java/org/apache/commons/periodicity/database/DatabaseMetaDataImpl.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DatabaseMetaDataImpl.java 1 Dec 2002 02:23:40 -0000       1.10
  +++ DatabaseMetaDataImpl.java 1 Dec 2002 03:38:59 -0000       1.11
  @@ -61,7 +61,9 @@
    *
    */ 
   
  -
  +import java.io.StringWriter;
  +import org.apache.velocity.VelocityContext;
  +import org.apache.velocity.app.Velocity;
   
   
   public class DatabaseMetaDataImpl implements DatabaseMetaData
  @@ -69,6 +71,24 @@
   
       public static final String DEFAULT_DRIVER_NAME_EXTENSION = ".default";
   
  +    /** The key for the scheme in the velocity context */
  +    public static final String SCHEME_KEY = "scheme";
  +
  +    /** The key for the database name in the velocity context */
  +    public static final String DATABASE_NAME_KEY = "database.name";
  +
  +    /** The key for the database port in the velocity context */
  +    public static final String DATABASE_PORT_KEY = "database.port";
  +
  +    /** The key for the administration path in the velocity context */
  +    public static final String ADMIN_PATH_KEY = "admin.path";
  +
  +    /** The key for the database path in the velocity context */
  +    public static final String DATABASE_PATH_KEY = "database.path";
  +    
  +    /** The key for the database host in the velocity context */
  +    public static final String DATABASE_HOST_KEY = "database.host";
  +
       /** A variable to hold the name of the database */
       private String name = null;
   
  @@ -108,6 +128,9 @@
       /** A variable to hold the default driver name */
       private String defaultDriverName = null;
   
  +    /** A variable to hold the default port for the database */
  +    private int defaultPort = -1;
  +
       /**
        * The purpose of this method is to create a new database meta data
        * implementation given a name and a type.
  @@ -198,11 +221,65 @@
        * The purpose of this method is to return the url of the database.
        * @return The url of the database as a string.
        */
  -    public String getUrl()
  +    public String getUrl() throws Exception
       {
  -        return null;
  +        VelocityContext context = buildContext();
  +        if(context != null)
  +        {
  +            StringWriter swriter = new StringWriter();
  +            if(Velocity.evaluate(context, swriter, "dburl", getRawUrl()))
  +            {
  +                return swriter.toString();
  +            }
  +            else
  +            {
  +                throw new Exception("Velocity error");
  +            }
  +        }
  +        else if(context == null)
  +        {
  +            throw new Exception("context == null");
  +        }
  +        else
  +        {
  +            throw new Exception("UNEXPECTED EXCEPTION");
  +        }    
       }
   
  +    private VelocityContext buildContext() throws Exception
  +    {
  +        VelocityContext context = new VelocityContext();
  +        if(getScheme() != null)
  +        {
  +            context.put(SCHEME_KEY, getScheme());
  +        }
  +        else
  +        {
  +            throw new Exception("getScheme() == null");
  +        }    
  +        if(getName() != null)
  +        {
  +            context.put(DATABASE_NAME_KEY, getName());
  +        }
  +        if(getDatabaseHost() != null)
  +        {
  +            context.put(DATABASE_HOST_KEY, getDatabaseHost());
  +        }
  +        if(getDatabasePortAsString() != null)
  +        {
  +            context.put(DATABASE_PORT_KEY, getDatabasePortAsString());
  +        }
  +        if(getAdminPath() != null)
  +        {
  +            context.put(ADMIN_PATH_KEY, getAdminPath());
  +        }
  +        if(getDatabasePath() != null)
  +        {
  +            context.put(DATABASE_PATH_KEY, getDatabasePath());
  +        }
  +        return context;
  +    }    
  +
       /**
        * The purpose of this method is to return the short description 
        * of the database.
  @@ -340,7 +417,14 @@
        */
       public int getDatabasePort()
       {
  -        return databasePort;
  +        if(defaultPort != -1)
  +        {
  +            return databasePort;
  +        }
  +        else
  +        {
  +            return getDefaultPort();
  +        }    
       }
   
       /**
  @@ -360,6 +444,26 @@
       }
   
       /**
  +     * The purpose of this method is to return the database port as a string.
  +     * @return the database port as a string.
  +     */
  +    public String getDatabasePortAsString()
  +    {
  +        int port = getDatabasePort();
  +        if(port != -1)
  +        {
  +            StringBuffer buffy = new StringBuffer();
  +            buffy.append(":");
  +            buffy.append(String.valueOf(port));
  +            return buffy.toString();
  +        }
  +        else
  +        {
  +            return "";
  +        }
  +    }    
  +
  +    /**
        * The purpose of this method is to return the administration path as
        * a string.
        * @return The administration path as a string.
  @@ -454,6 +558,70 @@
           }    
       }    
   
  +    /**
  +     * The purpose of this method is to return the default port of this 
  +     * database.
  +     * @return The default port as an integer.
  +     */
  +    public int getDefaultPort()
  +    {
  +        return defaultPort;
  +    }
  +
  +    /**
  +     * The purpose of this method is to set the default port of this database.
  +     * @param newval The new value for the port as an integer
  +     */
  +    public void setDefaultPort(int newval) throws Exception
  +    {
  +        if(newval > -2)
  +        {
  +            defaultPort = newval;
  +        }
  +        else
  +        {
  +            throw new Exception("newval is an illegal value.");
  +        }
  +    }    
  +
  +    /**
  +     * The purpose of this method is to return the raw url of the database.
  +     * @return The scheme of the database as a string.
  +     */
  +    public String getRawUrl() throws Exception
  +    {
  +        DriverMetaDataImpl dmeta = 
  +               (DriverMetaDataImpl) PeriodicityDrivers.getMetaData(getDriver());
  +        if(dmeta != null && getProtocol() != null)
  +        {
  +            DriverProtocol protocol = dmeta.getProtocol(getProtocol());
  +            if(protocol != null)
  +            {
  +                return protocol.getUrl();
  +            }
  +            else if(protocol == null)
  +            {
  +                throw new Exception("protocol == null");
  +            }
  +            else
  +            {
  +                throw new Exception("UNEXPECTED EXCEPTION");
  +            }
  +        }
  +        else if(dmeta == null)
  +        {
  +            throw new Exception("dmeta == null");
  +        }
  +        else if(getProtocol() == null)
  +        {
  +            throw new Exception("getProtocol() == null");
  +        }
  +        else
  +        {
  +            throw new Exception("UNEXPECTED EXCEPTION");
  +        }    
  +    }    
  +
       public String toString()
       {
           StringBuffer buffy = new StringBuffer();
  @@ -462,8 +630,6 @@
           buffy.append(getName());
           buffy.append("\"\ngetType()=\"");
           buffy.append(getType());
  -        buffy.append("\"\ngetUrl()=\"");
  -        buffy.append(getUrl());
           buffy.append("\"\ngetShortDescription()=\"");
           buffy.append(getShortDescription());
           buffy.append("\"\ngetDescription()=\"");
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to