de.fhg.iais.asc.commons.AscConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.commons.AscConfiguration.java

Source

package de.fhg.iais.asc.commons;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;

import de.fhg.iais.asc.commons.state.ASCState;
import de.fhg.iais.commons.dbc.DbcException;
import de.fhg.iais.util.collections.UnmodifiableStringMapAdapter;

/**
 * <p>
 * Contains the configuration used for an ASC run. The configuration also has an {@link ASCState}: although the ASC state is part of the configuration, this
 * design is due to how MyCortex was built upon the ASC. Ideally, this should be changed in the future.
 * </p>
 * 
 * @author cto, kst
 */
public class AscConfiguration implements Iterable<String> {
    public static final String TRANSFORMATIONS_DIR = "transformations.dir";

    public static final String OAI_SOURCE = "oai_source";
    public static final String SOURCE_FOLDER = "source_folder";
    public static final String FORMAT = "md_format";
    public static final String OAI_SET = "oai_set";
    public static final String MAX_FILES_EACH = "maxfilesEach";

    public static final String INBOX = "inbox";
    public static final String WORKSPACE = "workspace";
    public static final String OUTBOX = "outbox";
    public static final String BINARIES = "binaries";
    public static final String BOOKKEEPING = "bookkeeping";

    public static final String TIMEPARSER = "timeparser";
    public static final String TIMEPARSER_RULES = "time_parser_rules";
    public static final String TIMEPARSER_FACETS = "time_parser_facets";
    public static final String TIMEPARSER_TIMEZONE = "time_parser_timezone";

    public static final String GRID_SHIFTS = "gridshifts";
    public static final String GRID_SHIFT_FILE = "grid_shift_file";

    public static final String INGEST_EVENT = "ingest_event";

    public static final String ERRORS = "errors";

    public static final String CLEANUP = "cleanup";

    public static final String STRATEGY = "harvestingstrategy";
    public static final String HARVESTING_CONNECTION_TIMEOUT = "harvesting.connectionTimeout";
    public static final String TMP = ".tmp";
    public static final String META_DATA_FORMAT = "harvesting_format";
    public static final String HARVESTING_FROM_DATE = "harvesting_from";
    public static final String HARVESTING_UNTIL_DATE = "harvesting_until";

    public static final String OAIPMH_PROXYHOST = "oaipmhproxyhost";
    public static final String OAIPMH_PROXYPORT = "oaipmhproxyport";
    public static final String HOST = "host";
    public static final String API_KEY = "api.key";
    public static final String CONTENT_SERVER = "content.server";

    public static final String PROXY = "proxy";

    public static final String DOWNLOADER_PROXY_HOST = "downloader.proxyHost";
    public static final String DOWNLOADER_PROXY_PORT = "downloader.proxyPort";
    public static final String PROVIDERINFOREQUESTER_PROXY_HOST = "providerInfoRequester.proxyHost";
    public static final String PROVIDERINFOREQUESTER_PROXY_PORT = "providerInfoRequester.proxyPort";
    public static final String SUBMITTER_PROXY_HOST = "submitter.proxyHost";
    public static final String SUBMITTER_PROXY_PORT = "submitter.proxyPort";
    public static final String XSLT_PROCESSOR_PROXY_HOST = "xslt.processor.proxyHost";
    public static final String XSLT_PROCESSOR_PROXY_PORT = "xslt.processor.proxyPort";

    public static final String PROVIDER_DB_HOST = "provider_db.host";
    public static final String PROVIDER_INFO_CACHE_LOAD = "provider_info.cache.load";
    public static final String PROVIDER_INFO_CACHE_DIRECTORY = "provider_info.cache.directory";
    public static final String BOOKKEEPING_CONTENT_BATCH_SIZE_IN_LINES = "bookkeeping.content.batch.size";

    public static final String PROVIDER_ID = "provider_id";
    public static final String PROVIDER_DDB_ID = "provider_ddb_id";
    public static final String PROVIDER_NAME = "provider_name";
    public static final String PROVIDER_URI = "provider_uri";
    public static final String PROVIDER_EMAIL = "provider_email";
    public static final String PROVIDER_LOGO = "provider_logo";
    public static final String PROVIDER_PARENT_ID = "provider_parent_id";

    public static final String PROVIDER_LONGITUDE = "provider_longitude";
    public static final String PROVIDER_LATITUDE = "provider_latitude";
    public static final String PROVIDER_STATE = "provider_state";

    public static final String PROVIDER_DOMAIN_1 = "provider_domain1";
    public static final String PROVIDER_DOMAIN_2 = "provider_domain2";
    public static final String PROVIDER_DOMAIN_3 = "provider_domain3";

    public static final String MD_FORMAT = "md_format";

    public static final String PROVIDER_ITEM_ID_TYPE = "providerItemIdType";
    public static final String PROVIDER_ITEM_ID = "providerItemId";

    public static final String REVISION_ID = "revision_id";
    public static final String SEND_FPM = "send.fpm";

    public static final String SEND_USE_MANAGED_CONNECTIONS = "send.useManagedConnections";
    public static final String SEND_EXTRA_DELAY_MS = "send.extra.delay.ms";

    public static final String GENERATEHTMLPREVIEWS = "generateHtmlPreviews";

    public static final String TRANSFORMERS_URL = "transformersurl";
    public static final String DOWNLOAD_BINARIES = "transform.download_binaries";
    public static final String DOWNLOAD_BINARIES_RETRIES = "transform.download.binaries.retries";
    public static final String DOWNLOAD_BINARIES_TIMEOUT = "transform.download.binaries.timeout";
    public static final String BINARIES_CONTINUE_ON_FAIL = "transform.download_binaries.continueOnFail";
    public static final String SCALE_IMAGES = "transform.scale_images";

    public static final String SCALE_IMAGES_MODE = "transform.scale_images.mode";

    public static final String CONVERT_LOCATION = "convert_location";
    public static final String TRANSFORM_VALIDATE_OUTPUT_SCHEMA = "transform.validate.output.schema";
    public static final String TRANSFORM_VALIDATE_OUTPUT_SCHEMA_EDM = "transform.validate.output.schema.edm";

    public static final String TRNS_VALIDATE_INPUT_SCHEMA_FIXED = "transform.validate.input.withFixedSchema";
    public static final String TRNS_VALIDATE_INPUT_SCHEMA_EMBEDDED = "transform.validate.input.withEmbeddedSchema";
    public static final String TRANSFORM_VALIDATE_WITH_SCHEMATRON = "transform.validate.withSchematron";

    public static final String TRANSFORM_TRACE_OUTBOX_XML = "transform.trace.outbox.xml";
    public static final String TRANSFORM_TRACE_INTERMEDIATE = "transform.trace.intermediate";

    public static final String TRANSFORM_GLOBAL_PREPROCESSING_ENABLED = "transform.global.preprocessing.enabled";
    public static final String TRANSFORM_GLOBAL_POSTPROCESSING_ENABLED = "transform.global.postprocessing.enabled";
    public static final String TRANSFORM_GLOBAL_PREPROCESSING_FOLDER = "transform.global.preprocessing.folder";
    public static final String TRANSFORM_GLOBAL_POSTPROCESSING_FOLDER = "transform.global.postprocessing.folder";

    public static final String TRANSFORM_LOCAL_PREPROCESSING_ENABLED = "transform.local.preprocessing.enabled";
    public static final String TRANSFORM_LOCAL_POSTPROCESSING_ENABLED = "transform.local.postprocessing.enabled";
    public static final String TRANSFORM_LOCAL_PREPROCESSING_FOLDER = "transform.local.preprocessing.folder";
    public static final String TRANSFORM_LOCAL_POSTPROCESSING_FOLDER = "transform.local.postprocessing.folder";

    private final Map<String, Object> propertiesMap = new HashMap<String, Object>();

    private static final String[] FIXEDGLOBALPROPERTIES = { CLEANUP, OAIPMH_PROXYHOST, OAIPMH_PROXYPORT, HOST,
            INBOX, WORKSPACE, OUTBOX, ERRORS, SEND_FPM, SEND_EXTRA_DELAY_MS, TRANSFORMERS_URL };

    public static final String TRANSFORM_VALIDATE_WITH_SCHEMATRON_CONTINUE_ON_CRITICAL = "transform.validate.withSchematron.continueOnCritical";

    private ASCState ascState;

    /**
     * Creating an empty context
     */
    public AscConfiguration() {
        // intentionally empty
    }

    /**
     * Creating a context with the global settings and provider settings
     * 
     * @param globalpropertiesFilepath filepath of the global settings (normally asc.properties)
     * @param providerspecificpropertiesFilepath filepath of the provider specific settings (normally
     *        [providername].properties)
     */
    public AscConfiguration(String globalpropertiesFilepath, String providerspecificpropertiesFilepath) {
        readOutPropertiesFromFile(globalpropertiesFilepath, providerspecificpropertiesFilepath);
    }

    /**
     * Read in the properties information to the context
     * 
     * @param globalpropertiesFilepath the global properties
     * @param providerspecificpropertiesFilepath the provider specific properties
     */
    public final void readOutPropertiesFromFile(String globalpropertiesFilepath,
            String providerspecificpropertiesFilepath) {
        if (globalpropertiesFilepath != null) {
            Properties globalproperties = readOnePropertyFile(globalpropertiesFilepath);
            for (Entry<Object, Object> e : globalproperties.entrySet()) {
                this.propertiesMap.put(e.getKey().toString(), e.getValue());
            }
        }

        // local will override global
        if (providerspecificpropertiesFilepath != null) {
            Properties providerspecificproperties = readOnePropertyFile(providerspecificpropertiesFilepath);
            for (Entry<Object, Object> e : providerspecificproperties.entrySet()) {
                this.propertiesMap.put(e.getKey().toString(), e.getValue());
            }
        }
    }

    /**
     * reading the properties from a single file
     * 
     * @param filepath path of the file
     * @return the resulting properties object
     */
    private Properties readOnePropertyFile(String filepath) {
        File source = new File(filepath);
        Properties p = new Properties();
        if (!source.exists()) {
            throw new DbcException("Properties not found: " + source.getAbsolutePath());
        }

        Reader r = null;
        try {
            r = new InputStreamReader(new FileInputStream(source), Charsets.UTF_8);
            p.load(r);
        } catch (Exception e) {
            throw new DbcException("Error reading properties: " + filepath, e);
        } finally {
            if (r != null) {
                IOUtils.closeQuietly(r);
            }
        }
        return p;
    }

    /**
     * writing the current properties in memory back to disk
     * 
     * @param globalpropertiesFilepath
     * @param providerspezificpropertiesFilepath
     */
    public void writeOutPropertiesToFile(String globalpropertiesFilepath,
            String providerspezificpropertiesFilepath) {
        // global properties
        Properties globalProperties = new Properties();
        // provider properties
        Properties providerProperties = new Properties();

        for (Object keyobject : this.propertiesMap.keySet()) {
            String key = (String) keyobject;
            boolean dropped = false;
            for (String oneProp : FIXEDGLOBALPROPERTIES) {
                if (key.equals(oneProp)) {
                    globalProperties.put(key, this.propertiesMap.get(key).toString());
                    dropped = true;
                    break;
                }
            }

            if (!dropped) {
                providerProperties.put(key, this.propertiesMap.get(key).toString());
            }
        }

        if (globalpropertiesFilepath != null) {
            File globalPropertiesFile = new File(globalpropertiesFilepath);
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(globalPropertiesFile);
                globalProperties.store(fileOutputStream, "Global ASC properties");
            } catch (IOException e) {
                throw new DbcException(e);
            } finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        throw new DbcException(e);
                    }
                }
            }
        }

        if (providerspezificpropertiesFilepath != null) {
            File providerspezificPropertiesFile = new File(providerspezificpropertiesFilepath);
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(providerspezificPropertiesFile);
                providerProperties.store(fileOutputStream, "Provider specific ASC properties");
            } catch (IOException e) {
                throw new DbcException(e);
            } finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        throw new DbcException(e);
                    }
                }
            }
        }
    }

    public void writeOutPropertiesToFile(String filePath) {
        Properties property = new Properties();

        for (String key : this.propertiesMap.keySet()) {
            property.put(key, this.propertiesMap.get(key).toString());
        }
        File propertiesFile = new File(filePath);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(propertiesFile);
            property.store(fos, "ASC properties including provider specific properties");
        } catch (Exception e) {
            throw new DbcException(e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {

                    throw new DbcException(e);
                }
            }
        }

    }

    public synchronized ASCState getASCState() {
        if (this.ascState == null) {
            this.ascState = new ASCState();
        }
        return this.ascState;
    }

    public synchronized void resetASCStateObject() {
        if (this.ascState != null) {
            this.ascState.reset();
        }
    }

    /**
     * @see {@link Map#get(Object)} and {@link Strings#toInt(Object, int)}
     * @param key
     * @param dv Default value
     * @return int
     */
    public int get(String key, int dv) {
        Object o = this.propertiesMap.get(key);
        if (o != null) {
            return NumberUtils.toInt(o.toString(), dv);
        }
        return dv;
    }

    /**
     * @see {@link Map#get(Object)}
     * @param key
     * @param dv Default value
     * @return {@link String}
     */
    public String get(String key, String dv) {
        Object o = this.propertiesMap.get(key);
        return o == null ? dv : o.toString();
    }

    /**
     * @see {@link Map#get(Object)} and {@link Strings#toBoolean(Object, boolean)}
     * @param key
     * @param dv Default value
     * @return boolean
     */
    public boolean get(String key, boolean dv) {
        Object o = this.propertiesMap.get(key);
        return o == null ? dv : BooleanUtils.toBooleanDefaultIfNull(BooleanUtils.toBoolean(o.toString()), dv);
    }

    /**
     * @see {@link Map#get(Object)}
     * @param key
     * @return {@link Object}
     */
    public Object get(String key) {
        return this.propertiesMap.get(key);
    }

    /**
     * @see {@link Map#containsKey(Object)}
     * @param key
     * @return
     */
    public boolean hasKey(String key) {
        return this.propertiesMap.containsKey(key);
    }

    public boolean isEmpty(String key) {
        Object obj = this.propertiesMap.get(key);
        return (obj == null) || (obj.toString().trim().length() == 0);
    }

    /**
     * @see {@link Map#put(Object, Object)}
     * @param key
     * @param value
     */
    public void put(String key, Object value) {
        this.propertiesMap.put(key, value);
    }

    @Override
    public Iterator<String> iterator() {
        return this.propertiesMap.keySet().iterator();
    }

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder(this.propertiesMap.size() * 80); // pre-set of guessed size reduces expandCapacity() and
        // increases performance
        for (String key : this) {
            s.append(key).append(":").append(get(key, "")).append("; ");
        }
        return s.toString();
    }

    public Map<String, String> asUnmodifiableStringMap() {
        return new UnmodifiableStringMapAdapter<Object>(this.propertiesMap);
    }
}