001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.kernel.impl.spring;
017
018import org.slf4j.Logger;
019
020import java.io.File;
021
022import static java.lang.System.getProperty;
023import static org.slf4j.LoggerFactory.getLogger;
024
025/**
026 * This class loads System Properties only if:
027 * - the context is not an integration-test, and
028 * - the property is not already set
029 * This class mutates explicitly specified system
030 * Properties only if:
031 * - they represent relative paths in order to
032 *   make them relative to the explicit or implicit
033 *   home directory
034 *
035 * @author Andrew Woods
036 *         Date: 10/15/13
037 */
038public class DefaultPropertiesLoader {
039
040    private static final Logger LOGGER = getLogger(DefaultPropertiesLoader.class);
041
042    private static final String SEP = getProperty("file.separator");
043
044    /**
045     * @author awoods
046     * @since 2013
047     */
048    private enum PROPERTIES {
049        DEFAULT_OBJECT_STORE(
050                "com.arjuna.ats.arjuna.common.ObjectStoreEnvironmentBean.default.objectStoreDir"),
051        OBJECT_STORE("com.arjuna.ats.arjuna.objectstore.objectStoreDir"),
052        ISPN_CACHE("fcrepo.ispn.cache"),
053        ISPN_BIN_CACHE("fcrepo.ispn.binary.cache"),
054        BIN_STORE_PATH("fcrepo.binary.directory"),
055        MODE_INDEX("fcrepo.modeshape.index.directory"),
056        ISPN_ALT_CACHE("fcrepo.ispn.alternative.cache"),
057        ISPN_BIN_ALT_CACHE("fcrepo.ispn.binary.alternative.cache"),
058        ISPN_REPO_CACHE("fcrepo.ispn.repo.cache"),
059        ACTIVE_MQ("fcrepo.activemq.directory");
060
061        private String text;
062
063        private PROPERTIES(final String text) {
064            this.text = text;
065        }
066
067        public String getValue() {
068            return text;
069        }
070    }
071
072
073    /**
074     * This method loads default System Properties if:
075     * - the context is not an integration-test, and
076     * - the property is not already set
077     * This method mutates explicitly specified system
078     * Properties only if:
079     * - they represent relative paths in order to
080     *   make them relative to the explicit or implicit
081     *   home directory
082     */
083    public void loadSystemProperties() {
084        LOGGER.info("Loading properties");
085
086        if (getProperty("integration-test") == null) {
087            LOGGER.trace("Setting default properties, if necessary.");
088            final String fcrepoHome = getProperty("fcrepo.home");
089            final String baseDir = (fcrepoHome == null
090                    ? getProperty("user.dir") + SEP + "fcrepo4-data" + SEP
091                    : fcrepoHome + SEP);
092            for (final PROPERTIES prop : PROPERTIES.values()) {
093                final String value = getProperty(prop.getValue());
094                if (value == null) {
095                    setProperty(prop.getValue(), baseDir);
096                } else {
097                    updateRelativePropertyPath(prop.getValue(), value, baseDir);
098                }
099            }
100        }
101
102        for (final PROPERTIES prop : PROPERTIES.values()) {
103            final String val = prop.getValue();
104            LOGGER.info("{} = {}", val, getProperty(val));
105        }
106    }
107
108    private static void setProperty(final String prop, final String baseDir) {
109        System.setProperty(prop, baseDir + prop);
110    }
111
112    private static void updateRelativePropertyPath(final String prop, final String value, final String baseDir) {
113        if (!new File(value).isAbsolute()) {
114            System.setProperty(prop, baseDir + value);
115        }
116    }
117
118}