All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.boot.util.PropertyFile Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * Created by : s3595
 */
package at.spardat.xma.boot.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;


/**
 * PropertiesFile which automatically flushes its properties into its file.
 *
 * @author s3595 Chris Sch?fer (CGS)
 * @version $Id: PropertyFile.java 2084 2007-11-27 14:53:31Z s3460 $
 */
public class PropertyFile extends Properties {

    /** header info for property files */
    private String strHeader = "XMA-File Properties";

    /** the file_ to save to */
    private File file_;

    /** extended file output */
    private boolean debug;

    /**
     * ProertyFile backed by the specified "file"
     *
     * @param file the file to save and load to
     * @param header the file header
     * @throws IOException containing the filename
     */
    public PropertyFile(File file, String header) throws IOException {
       init( file, header, false);
    }
    /**
     * ProertyFile backed by the specified "file"
     * @param file the file to save and load to
     * @param debug if true trace information is writen into the log file
     * @throws IOException containing the filename
     */
    public PropertyFile(File file, boolean debug) throws IOException {
       init( file, this.strHeader, debug);
    }

    /**
     * @param file the file to load from and save to
     * @param header header info
     * @throws IOException containing the filename
     */
    private void init( File file, String header, boolean debug) throws IOException {
        this.file_        = file;
        this.strHeader    = header;
        this.debug        = debug;
        this.load();
    }

    /**
     * Returns the value of the specified key, or null if the key
     * does not exist.
     *
     * @param key key to look for
     */
    public String getProperty(String key) {
      return super.getProperty(key);
    }

    /**
     * Returns the value of the specified key, or the default value
     * if the key does not exist.
     */
    public String getProperty(String key, String defaultValue) {
      return super.getProperty(key, defaultValue);
    }

    /**
     * Sets the value for the specified key and updates the corresponding .ifo file.
     * @return the previous value
     * @throws RuntimeException if the property could not be persisted
     */
    public Object setProperty(String key, String value) {
        Object o = super.setProperty(key, value);
        try {
            this.store();
        }
        catch (IOException e) {
          throw new RuntimeException("io/error on underlying property-file '"+file_.getAbsolutePath()+"'",e);
        }
      return o;
    }

    /**
     * Sets the value for the specified key without updating the file now.
     * @return the previous value
     * @since 1.3.1
     * @author s2877
     */
    public Object setPropertyNoflush(String key,String value) {
        return super.setProperty(key,value);
    }

    /** get the underlying file_
     */
    public File getStoreFile() {
      return file_;
    }

    /**
     * load from file_
     * @throws IOException containing the filename
     */
    public void load() throws IOException {
      try {
          if( !file_.exists()) {
            file_.getParentFile().mkdirs();
            file_.createNewFile();
            this.store();
          } else {
              InputStream s = null;
              try {
                s = new FileInputStream(file_);
                super.load(s);
              } finally {
                  Util.close(s,file_.getAbsolutePath());
              }
          }
      } catch (Exception exc) {
          IOException ne = new IOException("error loading "+file_.getAbsolutePath());
          ne.initCause(exc);
          throw ne;
      }
    }// load

    /**
     * Saves the properties to the file_.
     * @throws IOException containing the filename
     */
    public void store() throws IOException {
        OutputStream s = null;
        try {
            s = new FileOutputStream(file_);
            store(s, strHeader);
        } catch(IOException exc) {
            IOException ne = new IOException("error storing "+file_.getAbsolutePath());
            ne.initCause(exc);
            throw ne;
        } finally {
            Util.close(s,file_.getAbsolutePath());
        }
    }// store

}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy