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        ARJUNA_DEFAULT_OBJECT_STORE(
052                "com.arjuna.ats.arjuna.common.ObjectStoreEnvironmentBean.default.objectStoreDir"),
053        ARJUNA_OBJECT_STORE("com.arjuna.ats.arjuna.objectstore.objectStoreDir"),
054        OBJECT_STORE("fcrepo.object.directory"),
055        BINARY_STORE("fcrepo.binary.directory"),
056        MODE_INDEX("fcrepo.modeshape.index.directory"),
057        ACTIVE_MQ("fcrepo.activemq.directory");
058
059        private String text;
060
061        private PROPERTIES(final String text) {
062            this.text = text;
063        }
064
065        public String getValue() {
066            return text;
067        }
068    }
069
070
071    /**
072     * This method loads default System Properties if:
073     * - the context is not an integration-test, and
074     * - the property is not already set
075     * This method mutates explicitly specified system
076     * Properties only if:
077     * - they represent relative paths in order to
078     *   make them relative to the explicit or implicit
079     *   home directory
080     */
081    public void loadSystemProperties() {
082        LOGGER.info("Loading properties");
083
084        if (getProperty("integration-test") == null) {
085            LOGGER.trace("Setting default properties, if necessary.");
086            final String fcrepoHome = getProperty("fcrepo.home");
087            final String baseDir = (fcrepoHome == null
088                    ? getProperty("user.dir") + SEP + "fcrepo4-data" + SEP
089                    : fcrepoHome + SEP);
090            for (final PROPERTIES prop : PROPERTIES.values()) {
091                final String value = getProperty(prop.getValue());
092                if (value == null) {
093                    setProperty(prop.getValue(), baseDir);
094                } else {
095                    updateRelativePropertyPath(prop.getValue(), value, baseDir);
096                }
097            }
098        }
099
100        for (final PROPERTIES prop : PROPERTIES.values()) {
101            final String val = prop.getValue();
102            LOGGER.info("{} = {}", val, getProperty(val));
103        }
104    }
105
106    private static void setProperty(final String prop, final String baseDir) {
107        System.setProperty(prop, baseDir + prop);
108    }
109
110    private static void updateRelativePropertyPath(final String prop, final String value, final String baseDir) {
111        if (!new File(value).isAbsolute()) {
112            System.setProperty(prop, baseDir + value);
113        }
114    }
115
116}