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.webapp;
020
021import javax.annotation.PostConstruct;
022
023import org.fcrepo.config.ConditionOnPropertyTrue;
024import org.fcrepo.config.FedoraPropsConfig;
025import org.fcrepo.config.JmsDestination;
026import org.fcrepo.jms.AbstractJMSPublisher;
027import org.fcrepo.jms.DefaultMessageFactory;
028import org.fcrepo.jms.JMSEventMessageFactory;
029import org.fcrepo.jms.JMSQueuePublisher;
030import org.fcrepo.jms.JMSTopicPublisher;
031
032import org.apache.activemq.ActiveMQConnectionFactory;
033import org.apache.activemq.xbean.BrokerFactoryBean;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036import org.springframework.context.annotation.Bean;
037import org.springframework.context.annotation.Conditional;
038import org.springframework.context.annotation.Configuration;
039import org.springframework.context.annotation.DependsOn;
040
041/**
042 * Spring config for jms
043 *
044 * @author pwinckles
045 */
046@Configuration
047@Conditional(JmsConfig.JmsEnabled.class)
048public class JmsConfig {
049
050    private static final Logger LOGGER = LoggerFactory.getLogger(JmsConfig.class);
051
052    static class JmsEnabled extends ConditionOnPropertyTrue {
053        JmsEnabled() {
054            super(FedoraPropsConfig.FCREPO_JMS_ENABLED, true);
055        }
056    }
057
058    @PostConstruct
059    public void postConstruct() {
060        LOGGER.info("JMS messaging enabled");
061    }
062
063    /**
064     * Creates a queue or topic publisher based on the property fcrepo.jms.destination.type. By default, this is a topic
065     *
066     * @param propsConfig config properties
067     * @return jms publisher
068     */
069    @Bean
070    public AbstractJMSPublisher jmsPublisher(final FedoraPropsConfig propsConfig) {
071        if (propsConfig.getJmsDestinationType() == JmsDestination.QUEUE) {
072            return new JMSQueuePublisher(propsConfig.getJmsDestinationName());
073        } else {
074            return new JMSTopicPublisher(propsConfig.getJmsDestinationName());
075        }
076    }
077
078    /**
079     * translates events into JMS header-only format
080     *
081     * @return JMS message factory
082     */
083    @Bean
084    public JMSEventMessageFactory messageFactory() {
085        return new DefaultMessageFactory();
086    }
087
088    /**
089     * JMS Broker configuration
090     *
091     * @param propsConfig config properties
092     * @return jms broker
093     */
094    @Bean
095    public BrokerFactoryBean jmsBroker(final FedoraPropsConfig propsConfig) {
096        final var factory = new BrokerFactoryBean();
097        factory.setConfig(propsConfig.getActiveMQConfiguration());
098        factory.setStart(true);
099        return factory;
100    }
101
102    /**
103     * ActiveMQ connection
104     *
105     * @param propsConfig config properties
106     * @return ActiveMQ connection factory
107     */
108    @Bean
109    @DependsOn("jmsBroker")
110    public ActiveMQConnectionFactory connectionFactory(final FedoraPropsConfig propsConfig) {
111        final var factory = new ActiveMQConnectionFactory();
112        factory.setBrokerURL(String.format("vm://%s:%s?create=false",
113                propsConfig.getJmsHost(), propsConfig.getJmsPort()));
114        return factory;
115    }
116
117}