001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.mm.mmtk;
014
015import java.io.BufferedInputStream;
016import java.io.File;
017import java.io.FileInputStream;
018import java.io.FileNotFoundException;
019import java.io.IOException;
020import java.util.Properties;
021
022import org.jikesrvm.VM;
023import org.jikesrvm.mm.mminterface.Selected;
024
025/**
026 * Read build-time configuration information for MMTk from a Java properties
027 * file.  Properties read through this mechanism should be read at build time
028 * and saved in static final fields.
029 * <p>
030 * This is a wrapper for a java.util.Properties object.
031 */
032public class BuildTimeConfig extends org.mmtk.vm.BuildTimeConfig {
033
034  /**
035   * The underlying properties object
036   */
037  private final Properties props;
038
039  public BuildTimeConfig(String property_file_property, String default_property_file) {
040    props = getProperties(property_file_property,default_property_file);
041  }
042
043  public BuildTimeConfig(String property_file_property) {
044    props = getProperties(property_file_property,null);
045  }
046
047  /**
048   * @param property_file_property The name of the property that sets
049   * the location of the properties file
050   * @param default_property_file The default properties file.
051   * @return the built properties
052   */
053  private Properties getProperties(String property_file_property, String default_property_file) {
054    Properties props = new Properties();
055    String propFileName;
056    if (default_property_file == null) {
057      propFileName = System.getProperty(property_file_property);
058      if (propFileName == null) {
059        System.err.println(property_file_property + " must specify a properties file");
060        VM.sysExit(1);
061      }
062    } else {
063      propFileName = System.getProperty(property_file_property, default_property_file);
064    }
065    File propFile = new File(propFileName);
066
067    try {
068      BufferedInputStream propFileStream = new BufferedInputStream(new FileInputStream(propFile));
069      props.load(propFileStream);
070      propFileStream.close();
071    } catch (FileNotFoundException e) {
072      if (!propFileName.equals(default_property_file)) {
073        System.err.println(propFileName + " not found.");
074        VM.sysExit(1);
075      }
076    } catch (IOException e) {
077      e.printStackTrace();
078      VM.sysExit(1);
079    }
080    return props;
081  }
082
083  @Override
084  public String getPlanName() {
085    return Selected.name;
086  }
087
088  @Override
089  public boolean getBooleanProperty(String name, boolean dflt) {
090    String value = props.getProperty(name,Boolean.toString(dflt));
091    return Boolean.parseBoolean(value);
092  }
093
094  @Override
095  public boolean getBooleanProperty(String name) {
096    String value = props.getProperty(name);
097    if (value == null)
098      throw new RuntimeException("Undefined property " + name);
099    return Boolean.parseBoolean(value);
100  }
101
102  @Override
103  public int getIntProperty(String name, int dflt) {
104    String value = props.getProperty(name,Integer.toString(dflt));
105    return Integer.parseInt(value);
106  }
107
108  @Override
109  public int getIntProperty(String name) {
110    String value = props.getProperty(name);
111    if (value == null)
112      throw new RuntimeException("Undefined property " + name);
113    return Integer.parseInt(value);
114  }
115
116  @Override
117  public String getStringProperty(String name, String dflt) {
118    return props.getProperty(name,dflt);
119  }
120
121  @Override
122  public String getStringProperty(String name) {
123    String value = props.getProperty(name);
124    if (value == null)
125      throw new RuntimeException("Undefined property " + name);
126    return value;
127  }
128
129
130}