<commit_before>/*
 * $Id: BaseConfigFile.java,v 1.5 2009-07-22 06:36:28 tlipkis Exp $
 */

/*

Copyright (c) 2001-2006 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.config;

import java.io.*;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.lockss.util.*;
import org.lockss.util.urlconn.*;

/**
 * Common functionality for a config file loadable from a URL or filename,
 * and parseable as either XML or props.
 */
public abstract class BaseConfigFile implements ConfigFile {
  protected static Logger log = Logger.getLogger("ConfigFile");

  protected int m_fileType;
  protected String m_lastModified;
  // FileConfigFile assumes the url doesn't change
  protected String m_fileUrl;
  protected String m_loadError = "Not yet loaded";
  protected IOException m_IOException;
  protected long m_lastAttempt;
  protected boolean m_needsReload = true;
  protected boolean m_isPlatformFile = false;
  protected ConfigurationPropTreeImpl m_config;
  protected int m_generation = 0;
  protected Map m_props;
  protected ConfigManager.KeyPredicate keyPred;

  /**
   * Create a ConfigFile for the URL
   */
  public BaseConfigFile(String url) {
    if (StringUtil.endsWithIgnoreCase(url, ".xml")) {
      m_fileType = ConfigFile.XML_FILE;
    } else {
      m_fileType = ConfigFile.PROPERTIES_FILE;
    }
    m_fileUrl = url;
    m_isPlatformFile = StringUtil.endsWithIgnoreCase(m_fileUrl, "local.txt");
  }

  public String getFileUrl() {
    return m_fileUrl;
  }

  /** Return true if this file might contain platform values that are
   * needed in order to properly parse other config files.
   */
  public boolean isPlatformFile() {
    return m_isPlatformFile;
  }

  public int getFileType() {
    return m_fileType;
  }

  public String getLastModified() {
    return m_lastModified;
  }

  public long getLastAttemptTime() {
    return m_lastAttempt;
  }

  public Generation getGeneration() throws IOException {
    ensureLoaded();
    synchronized (this) {
      return new Generation(this, m_config, m_generation);
    }
  }

  public String getLoadErrorMessage() {
    return m_loadError;
  }

  public boolean isLoaded() {
    return m_loadError == null;
  }

  private void ensureLoaded() throws IOException {
    if (m_needsReload || isCheckEachTime()) {
      reload();
    }
  }

  /** Return true if the file should be checked for modification each time
   * getConfiguration() is called.  If false, the file will only be checked
   * on the first call, and after calls to setNeedsReload().  Subclasses
   * use this to modify the default behavior.
   */
  protected boolean isCheckEachTime() {
    return true;
  }

  /**
   * Instruct the ConfigFile to check for modifications the next time it's
   * accessed
   */
  public void setNeedsReload() {
    m_needsReload = true;
  }

  public void setConnectionPool(LockssUrlConnectionPool connPool) {
  }

  public void setProperty(String key, Object val) {
    if (m_props == null) {
      m_props = new HashMap();
    }
    m_props.put(key, val);
  }

  /** Return the Configuration object built from this file
   */
  public Configuration getConfiguration() throws IOException {
    ensureLoaded();
    return m_config;
  }

  /**
   * Reload the contents if changed.
   */
  protected void reload() throws IOException {
    m_lastAttempt = TimeBase.nowMs();
    try {
      InputStream in = openInputStream();
      if (in != null) {
	try {
	  setConfigFrom(in);
	} finally {
	  IOUtil.safeClose(in);
	}
      }
    } catch (FileNotFoundException ex) {
      log.debug2("File not found: " + m_fileUrl);
      m_IOException = ex;
      m_loadError = ex.toString();
      throw ex;
    } catch (IOException ex) {
      log.warning("Unexpected exception loading " + m_fileUrl + ": " + ex);
      m_IOException = ex;
      if (m_loadError == null ||
	  !StringUtil.equalStrings(ex.getMessage(), m_loadError)) {
	// Some subs set m_loadError to exception message.  Don't overwrite
	// those with message that includes java exception class
	m_loadError = ex.toString();
      }
      throw ex;
    }
  }

  protected void setConfigFrom(InputStream in) throws IOException {
    ConfigurationPropTreeImpl newConfig = new ConfigurationPropTreeImpl();

    try {
      // Load the configuration
      if (m_fileType == XML_FILE) {
	XmlPropertyLoader.load(newConfig.getPropertyTree(), in);
      } else {
	newConfig.getPropertyTree().load(in);
      }
      filterConfig(newConfig);
      // update stored configuration atomically
      newConfig.seal();
      m_config = newConfig;
      m_loadError = null;
      m_IOException = null;
      m_lastModified = calcNewLastModified();
      m_generation++;
      m_needsReload = false;
    } catch (IOException ex) {
      throw ex;
    } catch (Exception ex) {
      log.debug("Unexpected non-IO error loading configuration", ex);
      throw new IOException(ex.toString());
    }<commit_msg>Use new for of XmlPropertyLoader.load() to pass Tdb object.<commit_after>/*
 * $Id: BaseConfigFile.java,v 1.6 2010-04-02 23:05:45 pgust Exp $
 */

/*

Copyright (c) 2001-2006 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.config;

import java.io.*;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.lockss.util.*;
import org.lockss.util.urlconn.*;

/**
 * Common functionality for a config file loadable from a URL or filename,
 * and parseable as either XML or props.
 */
public abstract class BaseConfigFile implements ConfigFile {
  protected static Logger log = Logger.getLogger("ConfigFile");

  protected int m_fileType;
  protected String m_lastModified;
  // FileConfigFile assumes the url doesn't change
  protected String m_fileUrl;
  protected String m_loadError = "Not yet loaded";
  protected IOException m_IOException;
  protected long m_lastAttempt;
  protected boolean m_needsReload = true;
  protected boolean m_isPlatformFile = false;
  protected ConfigurationPropTreeImpl m_config;
  protected int m_generation = 0;
  protected Map m_props;
  protected ConfigManager.KeyPredicate keyPred;

  /**
   * Create a ConfigFile for the URL
   */
  public BaseConfigFile(String url) {
    if (StringUtil.endsWithIgnoreCase(url, ".xml")) {
      m_fileType = ConfigFile.XML_FILE;
    } else {
      m_fileType = ConfigFile.PROPERTIES_FILE;
    }
    m_fileUrl = url;
    m_isPlatformFile = StringUtil.endsWithIgnoreCase(m_fileUrl, "local.txt");
  }

  public String getFileUrl() {
    return m_fileUrl;
  }

  /** Return true if this file might contain platform values that are
   * needed in order to properly parse other config files.
   */
  public boolean isPlatformFile() {
    return m_isPlatformFile;
  }

  public int getFileType() {
    return m_fileType;
  }

  public String getLastModified() {
    return m_lastModified;
  }

  public long getLastAttemptTime() {
    return m_lastAttempt;
  }

  public Generation getGeneration() throws IOException {
    ensureLoaded();
    synchronized (this) {
      return new Generation(this, m_config, m_generation);
    }
  }

  public String getLoadErrorMessage() {
    return m_loadError;
  }

  public boolean isLoaded() {
    return m_loadError == null;
  }

  private void ensureLoaded() throws IOException {
    if (m_needsReload || isCheckEachTime()) {
      reload();
    }
  }

  /** Return true if the file should be checked for modification each time
   * getConfiguration() is called.  If false, the file will only be checked
   * on the first call, and after calls to setNeedsReload().  Subclasses
   * use this to modify the default behavior.
   */
  protected boolean isCheckEachTime() {
    return true;
  }

  /**
   * Instruct the ConfigFile to check for modifications the next time it's
   * accessed
   */
  public void setNeedsReload() {
    m_needsReload = true;
  }

  public void setConnectionPool(LockssUrlConnectionPool connPool) {
  }

  public void setProperty(String key, Object val) {
    if (m_props == null) {
      m_props = new HashMap();
    }
    m_props.put(key, val);
  }

  /** Return the Configuration object built from this file
   */
  public Configuration getConfiguration() throws IOException {
    ensureLoaded();
    return m_config;
  }

  /**
   * Reload the contents if changed.
   */
  protected void reload() throws IOException {
    m_lastAttempt = TimeBase.nowMs();
    try {
      InputStream in = openInputStream();
      if (in != null) {
	try {
	  setConfigFrom(in);
	} finally {
	  IOUtil.safeClose(in);
	}
      }
    } catch (FileNotFoundException ex) {
      log.debug2("File not found: " + m_fileUrl);
      m_IOException = ex;
      m_loadError = ex.toString();
      throw ex;
    } catch (IOException ex) {
      log.warning("Unexpected exception loading " + m_fileUrl + ": " + ex);
      m_IOException = ex;
      if (m_loadError == null ||
	  !StringUtil.equalStrings(ex.getMessage(), m_loadError)) {
	// Some subs set m_loadError to exception message.  Don't overwrite
	// those with message that includes java exception class
	m_loadError = ex.toString();
      }
      throw ex;
    }
  }

  protected void setConfigFrom(InputStream in) throws IOException {
    ConfigurationPropTreeImpl newConfig = new ConfigurationPropTreeImpl();
    try {
      // Load the configuration
      if (m_fileType == XML_FILE) {
        Tdb tdb = new Tdb();
	XmlPropertyLoader.load(newConfig.getPropertyTree(), tdb, in);
	if (!tdb.isEmpty()) {
	  newConfig.setTdb(tdb);
	}
      } else {
	newConfig.getPropertyTree().load(in);
      }
      filterConfig(newConfig);
      // update stored configuration atomically
      newConfig.seal();
      m_config = newConfig;
      m_loadError = null;
      m_IOException = null;
      m_lastModified = calcNewLastModified();
      m_generation++;
      m_needsReload = false;
    } catch (IOException ex) {
      throw ex;
    } catch (Exception ex) {
      log.debug("Unexpected non-IO error loading configuration", ex);
      throw new IOException(ex.toString());
    }
{'fraction_non_alphanumeric': 0.08184204247196374, 'fraction_numerical': 0.004135846655531695, 'mean_word_length': 3.4462517680339464, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5205009', 'n_tokens_mistral': 3614, 'n_tokens_neox': 3474, 'n_words': 1278}