package ModSQL;
import java.sql.*;
import java.util.*;

/* $Id: DriverConfig.java,v 1.12 2004/01/04 03:07:23 cvs Exp $
 *
 * Copyright (c) 2003 Chris Studholme <chris.studholme@utoronto.ca>
 *
 * May be copied or modified under the terms of the GNU General Public
 * License.  See COPYING for more information.
 */

/**
 * <p>Configuration data for ModSQL.
 *
 * <p>Having this data in a seperate class simplifies package recompiles.
 *
 * @author chris.studholme@utoronto.ca
 */
public final class DriverConfig {
  /** Major version number. */
  public static final int majorVersion=0;
  /** Minor version number. */
  public static final int minorVersion=40;
  /** Text representation of version. */
  public static final String version="v0.40(beta)";

  /** Default size for internal hash tables (if not set in config file). */
  public static final int DEFAULT_HASHTABLE_SIZE = 65536;

  /** Properties read from configuration file. */
  private static Properties props=null;

  /** Debug level (0 for no debug messages). */
  protected static int debugLevel=0;
  
  /** Cached array of functions. */
  private static String[] functions=null;

  /**
   * Read configuration file.
   */
  static {
    // have to instantiate to use getClass() method
    new DriverConfig();
  }

  /**
   * Constructor.
   */
  protected DriverConfig() {
    synchronized (this) {
      if (props!=null)
	return;
      
      Properties defaults = new Properties();
      defaults.setProperty("functionpackage","ModSQL");
      
      props = new Properties(defaults);
      
      // load properties
      try {
	props.load(getClass().getResourceAsStream("ModSQL.conf"));
      }
      catch (Exception e) {
	java.lang.System.err.println("ModSQL.DriverConfig: failed to load settings ("+
				     e.toString()+")");
      }
    }
  }
  
  /**
   * <p>This convenience method can be used to break a color-separated list
   * of strings into an array of String objects.
   *
   * @param colonstring colon-separated list
   * @return an array of strings
   */
  public static String[] makeArray(String colonstring) {
    if (colonstring==null)
      return null;
    
    ArrayList list = new ArrayList();
    
    int start=0;
    int end;
    do {
      end=colonstring.indexOf(':',start);
      if (end>start)
	list.add(colonstring.substring(start,end));
      else if (end<0)
	list.add(colonstring.substring(start));
      start=end+1;
    } while (end>=0);
    
    if (list.isEmpty())
      return null;
    
    String[] result = new String[list.size()];
    return (String[])list.toArray(result);
  }

  /**
   * Get array of function classes to check when parsing expressions.
   *
   * @return array of function classes
   */
  protected static String[] getFunctionClasses() {
    if (functions!=null)
      return functions;
    
    String[] result = makeArray(props.getProperty("functions"));
    if (result==null)
      return null;
    
    String packagename = props.getProperty("functionpackage");
    if (packagename!=null)
      for (int i=0; i<result.length; ++i) {
	if (result[i].indexOf('.')<0)
	  result[i] = packagename+"."+result[i];
      }
    return functions=result;
  }

  /**
   * Get path to index files.
   *
   * @return path to index files
   */
  protected static String getIndexPath() {
    return props.getProperty("indexpath");
  }

  /**
   * Get default size of hash tables used interally.  Reads value from
   * config file but uses constant in source if the config file has not
   * value set.
   *
   * @return default hash table size
   */
  protected static int getHashTableSize() {
    String size = props.getProperty("hashtable_size");
    if (size!=null)
      return Integer.parseInt(size);
    return DEFAULT_HASHTABLE_SIZE;
  }

  /**
   * <p>This method can be used by database modules to obtain properties from
   * the ModSQL properties file.  The database module named, say, Module will
   * have properties of the form:<br><blockquote>
   * Module.property1=value1<br>
   * Module.property2=value2<br>
   * etc.</blockquote>
   *
   * <p>This method will seek out such properties, strip the leading
   * database name, and create a new Properties object containing the
   * properties that apply to the particular database.  If there are no
   * properties for the database stored in the ModSQL properties file, 
   * an empty Properties object will be returned.
   *
   * @param database name of calling database (typically, the package name)
   * @return a Properties object (possibly empty)
   */
  public static Properties getDatabaseProperties(String database) {
    Properties result = new Properties();
    for (Enumeration enum=props.propertyNames(); enum.hasMoreElements(); ) {
      String s = (String)enum.nextElement();
      if (s.startsWith(database+"."))
	result.setProperty(s.substring(database.length()+1),props.getProperty(s));
    }
    return result;
  }
  
} 

