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 */
018
019package org.fcrepo.config;
020
021import java.io.IOException;
022import java.nio.file.Files;
023import java.nio.file.Path;
024import java.time.Duration;
025
026import javax.annotation.PostConstruct;
027
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.springframework.beans.factory.annotation.Value;
031import org.springframework.context.annotation.Configuration;
032import org.springframework.core.io.Resource;
033
034/**
035 * General Fedora properties
036 *
037 * @author pwinckles
038 * @since 6.0.0
039 */
040@Configuration
041public class FedoraPropsConfig extends BasePropsConfig {
042
043    private static final Logger LOGGER = LoggerFactory.getLogger(FedoraPropsConfig.class);
044
045    public static final String FCREPO_JMS_HOST = "fcrepo.jms.host";
046    public static final String FCREPO_DYNAMIC_JMS_PORT = "fcrepo.dynamic.jms.port";
047    public static final String FCREPO_DYNAMIC_STOMP_PORT = "fcrepo.dynamic.stomp.port";
048    public static final String FCREPO_ACTIVEMQ_CONFIGURATION = "fcrepo.activemq.configuration";
049    public static final String FCREPO_NAMESPACE_REGISTRY = "fcrepo.namespace.registry";
050    public static final String FCREPO_EXTERNAL_CONTENT_ALLOWED = "fcrepo.external.content.allowed";
051    private static final String FCREPO_ACTIVEMQ_DIRECTORY = "fcrepo.activemq.directory";
052    private static final String FCREPO_SESSION_TIMEOUT = "fcrepo.session.timeout";
053    private static final String FCREPO_VELOCITY_RUNTIME_LOG = "fcrepo.velocity.runtime.log";
054    private static final String FCREPO_REBUILD_VALIDATION_FIXITY = "fcrepo.rebuild.validation.fixity";
055    private static final String FCREPO_REBUILD_ON_START = "fcrepo.rebuild.on.start";
056    private static final String FCREPO_JMS_BASEURL = "fcrepo.jms.baseUrl";
057    private static final String FCREPO_SERVER_MANAGED_PROPS_MODE = "fcrepo.properties.management";
058    private static final String FCREPO_JMS_DESTINATION_TYPE = "fcrepo.jms.destination.type";
059    private static final String FCREPO_JMS_DESTINATION_NAME = "fcrepo.jms.destination.name";
060    public static final String FCREPO_JMS_ENABLED = "fcrepo.jms.enabled";
061
062    private static final String DATA_DIR_DEFAULT_VALUE = "data";
063    private static final String LOG_DIR_DEFAULT_VALUE = "logs";
064    private static final String ACTIVE_MQ_DIR_DEFAULT_VALUE = "ActiveMQ/kahadb";
065
066    @Value("${" + FCREPO_HOME_PROPERTY + ":" + DEFAULT_FCREPO_HOME_VALUE + "}")
067    protected Path fedoraHome;
068
069    @Value("#{fedoraPropsConfig.fedoraHome.resolve('" + DATA_DIR_DEFAULT_VALUE + "')}")
070    private Path fedoraData;
071
072    @Value("#{fedoraPropsConfig.fedoraHome.resolve('" + LOG_DIR_DEFAULT_VALUE + "')}")
073    private Path fedoraLogs;
074
075    @Value("${" + FCREPO_JMS_HOST + ":localhost}")
076    private String jmsHost;
077
078    @Value("${" + FCREPO_DYNAMIC_JMS_PORT + ":61616}")
079    private String jmsPort;
080
081    @Value("${" + FCREPO_DYNAMIC_STOMP_PORT + ":61613}")
082    private String stompPort;
083
084    @Value("${" + FCREPO_ACTIVEMQ_CONFIGURATION + ":classpath:/config/activemq.xml}")
085    private Resource activeMQConfiguration;
086
087    @Value("${" + FCREPO_ACTIVEMQ_DIRECTORY + ":#{fedoraPropsConfig.fedoraData.resolve('" +
088            ACTIVE_MQ_DIR_DEFAULT_VALUE + "').toAbsolutePath().toString()}}")
089    private String activeMqDirectory;
090
091    @Value("${" + FCREPO_NAMESPACE_REGISTRY + ":classpath:/namespaces.yml}")
092    private String namespaceRegistry;
093
094    @Value("${" + FCREPO_EXTERNAL_CONTENT_ALLOWED + ":#{null}}")
095    private String externalContentAllowed;
096
097    @Value("${" + FCREPO_SESSION_TIMEOUT + ":180000}")
098    private Long sessionTimeoutLong;
099    private Duration sessionTimeout;
100
101    @Value("${" + FCREPO_VELOCITY_RUNTIME_LOG + ":" +
102            "#{fedoraPropsConfig.fedoraLogs.resolve('velocity.log').toString()}}")
103    private Path velocityLog;
104
105    @Value("${" + FCREPO_REBUILD_VALIDATION_FIXITY + ":false}")
106    private boolean rebuildFixityCheck;
107
108    @Value("${" + FCREPO_REBUILD_ON_START + ":false}")
109    private boolean rebuildOnStart;
110
111    @Value("${" + FCREPO_JMS_BASEURL + ":#{null}}")
112    private String jmsBaseUrl;
113
114    @Value("${" + FCREPO_SERVER_MANAGED_PROPS_MODE + ":strict}")
115    private String serverManagedPropsModeStr;
116    private ServerManagedPropsMode serverManagedPropsMode;
117
118    @Value("${" + FCREPO_JMS_DESTINATION_TYPE + ":topic}")
119    private String jmsDestinationTypeStr;
120    private JmsDestination jmsDestinationType;
121
122    @Value("${" + FCREPO_JMS_DESTINATION_NAME + ":fedora}")
123    private String jmsDestinationName;
124
125    @PostConstruct
126    private void postConstruct() throws IOException {
127        LOGGER.info("Fedora home: {}", fedoraHome);
128        LOGGER.debug("Fedora home data: {}", fedoraData);
129        try {
130            Files.createDirectories(fedoraHome);
131        } catch (final IOException e) {
132            throw new IOException(String.format("Failed to create Fedora home directory at %s." +
133                    " Fedora home can be configured by setting the %s property.", fedoraHome, FCREPO_HOME_PROPERTY), e);
134        }
135        Files.createDirectories(fedoraData);
136        serverManagedPropsMode = ServerManagedPropsMode.fromString(serverManagedPropsModeStr);
137        sessionTimeout = Duration.ofMillis(sessionTimeoutLong);
138        jmsDestinationType = JmsDestination.fromString(jmsDestinationTypeStr);
139    }
140
141    /**
142     * @return Path to Fedora home directory
143     */
144    public Path getFedoraHome() {
145        return fedoraHome;
146    }
147
148    /**
149     * Sets the path to the Fedora home directory -- should only be used for testing purposes.
150     *
151     * @param fedoraHome Path to Fedora home directory
152     */
153    public void setFedoraHome(final Path fedoraHome) {
154        this.fedoraHome = fedoraHome;
155    }
156
157    /**
158     * @return Path to Fedora home data directory
159     */
160    public Path getFedoraData() {
161        return fedoraData;
162    }
163
164    /**
165     * @return Path to Fedora home logs directory
166     */
167    public Path getFedoraLogs() {
168        return fedoraLogs;
169    }
170
171    /**
172     * Sets the path to the Fedora home data directory -- should only be used for testing purposes.
173     *
174     * @param fedoraData Path to Fedora home data directory
175     */
176    public void setFedoraData(final Path fedoraData) {
177        this.fedoraData = fedoraData;
178    }
179
180    /**
181     * @return The JMS host
182     */
183    public String getJmsHost() {
184        return jmsHost;
185    }
186
187    /**
188     * @return The JMS/Open Wire port
189     */
190    public String getJmsPort() {
191        return jmsPort;
192    }
193
194    /**
195     * @return The STOMP protocol port
196     */
197    public String getStompPort() {
198        return stompPort;
199    }
200
201    /**
202     * @return The ActiveMQ data directory
203     */
204    public String getActiveMqDirectory() {
205        return activeMqDirectory;
206    }
207
208    /**
209     * @return The path to the ActiveMQ xml spring configuration.
210     */
211    public Resource getActiveMQConfiguration() {
212        return activeMQConfiguration;
213    }
214
215    /**
216     * @return The path to the allowed external content pattern definitions.
217     */
218    public String getExternalContentAllowed() {
219        return externalContentAllowed;
220    }
221
222    /**
223     * @return The path to the namespace registry file.
224     */
225    public String getNamespaceRegistry() {
226        return namespaceRegistry;
227    }
228
229    /**
230     * @return The timeout in milliseconds of the persistence session
231     */
232    public Duration getSessionTimeout() {
233        return sessionTimeout;
234    }
235
236    /**
237     * @param sessionTimeout the session timeout duration
238     */
239    public void setSessionTimeout(final Duration sessionTimeout) {
240        this.sessionTimeout = sessionTimeout;
241    }
242
243    /**
244     * @return The path to the velocity log.
245     */
246    public Path getVelocityLog() {
247        return velocityLog;
248    }
249
250    /**
251     * @return true if the rebuild validation should also check file fixity
252     */
253    public boolean isRebuildFixityCheck() {
254        return rebuildFixityCheck;
255    }
256
257    /**
258     * @return true if the internal indices should be rebuilt when Fedora starts up.
259     */
260    public boolean isRebuildOnStart() {
261        return rebuildOnStart;
262    }
263
264    /**
265     * @param rebuildOnStart A boolean flag indicating whether or not to rebuild on start
266     */
267    public void setRebuildOnStart(final boolean rebuildOnStart) {
268        this.rebuildOnStart = rebuildOnStart;
269    }
270
271    /**
272     * @return the JMS base url, if specified
273     */
274    public String getJmsBaseUrl() {
275        return jmsBaseUrl;
276    }
277
278    /**
279     * @return the server managed properties mode, default strict
280     */
281    public ServerManagedPropsMode getServerManagedPropsMode() {
282        return serverManagedPropsMode;
283    }
284
285    /**
286     * @param serverManagedPropsMode the server managed props mode
287     */
288    public void setServerManagedPropsMode(final ServerManagedPropsMode serverManagedPropsMode) {
289        this.serverManagedPropsMode = serverManagedPropsMode;
290    }
291
292    /**
293     * @return the jms destination type
294     */
295    public JmsDestination getJmsDestinationType() {
296        return jmsDestinationType;
297    }
298
299    /**
300     * @return the jms destination name
301     */
302    public String getJmsDestinationName() {
303        return jmsDestinationName;
304    }
305
306}