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        ALLOWED_EXTERNAL_CONTENT("fcrepo.external.content.allowed", false),
059        NAMESPACE_REGISTRY("fcrepo.namespace.registry", false);
060
061        private final String text;
062
063        private final boolean setDefaultValue;
064
065        PROPERTIES(final String text) {
066            this(text, true);
067        }
068
069        PROPERTIES(final String text, final boolean setDefaultValue) {
070            this.text = text;
071            this.setDefaultValue = setDefaultValue;
072        }
073
074        public String getValue() {
075            return text;
076        }
077
078        public boolean getSetDefaultValue() {
079            return setDefaultValue;
080        }
081    }
082
083
084    /**
085     * This method loads default System Properties if:
086     * - the context is not an integration-test, and
087     * - the property is not already set
088     * This method mutates explicitly specified system
089     * Properties only if:
090     * - they represent relative paths in order to
091     *   make them relative to the explicit or implicit
092     *   home directory
093     */
094    public void loadSystemProperties() {
095        LOGGER.info("Loading properties");
096
097        if (getProperty("integration-test") == null) {
098            LOGGER.trace("Setting default properties, if necessary.");
099            final String fcrepoHome = getProperty("fcrepo.home");
100            final String baseDir = (fcrepoHome == null
101                    ? getProperty("user.dir") + SEP + "fcrepo4-data" + SEP
102                    : fcrepoHome + SEP);
103            for (final PROPERTIES prop : PROPERTIES.values()) {
104                final String value = getProperty(prop.getValue());
105                if (value == null) {
106                    if (prop.getSetDefaultValue()) {
107                        setProperty(prop.getValue(), baseDir);
108                    }
109                } else {
110                    updateRelativePropertyPath(prop.getValue(), value, baseDir);
111                }
112            }
113        }
114
115        for (final PROPERTIES prop : PROPERTIES.values()) {
116            final String val = prop.getValue();
117            LOGGER.info("{} = {}", val, getProperty(val));
118        }
119    }
120
121    private static void setProperty(final String prop, final String baseDir) {
122        System.setProperty(prop, baseDir + prop);
123    }
124
125    private static void updateRelativePropertyPath(final String prop, final String value, final String baseDir) {
126        if (!new File(value).isAbsolute()) {
127            System.setProperty(prop, baseDir + value);
128        }
129    }
130
131}