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