001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006
007package org.fcrepo.webapp;
008
009import javax.annotation.PostConstruct;
010
011import org.fcrepo.config.ConditionOnPropertyTrue;
012import org.fcrepo.config.FedoraPropsConfig;
013import org.fcrepo.config.JmsDestination;
014import org.fcrepo.jms.AbstractJMSPublisher;
015import org.fcrepo.jms.DefaultMessageFactory;
016import org.fcrepo.jms.JMSEventMessageFactory;
017import org.fcrepo.jms.JMSQueuePublisher;
018import org.fcrepo.jms.JMSTopicPublisher;
019
020import org.apache.activemq.ActiveMQConnectionFactory;
021import org.apache.activemq.xbean.BrokerFactoryBean;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024import org.springframework.context.annotation.Bean;
025import org.springframework.context.annotation.Conditional;
026import org.springframework.context.annotation.Configuration;
027import org.springframework.context.annotation.DependsOn;
028
029/**
030 * Spring config for jms
031 *
032 * @author pwinckles
033 */
034@Configuration
035@Conditional(JmsConfig.JmsEnabled.class)
036public class JmsConfig {
037
038    private static final Logger LOGGER = LoggerFactory.getLogger(JmsConfig.class);
039
040    static class JmsEnabled extends ConditionOnPropertyTrue {
041        JmsEnabled() {
042            super(FedoraPropsConfig.FCREPO_JMS_ENABLED, true);
043        }
044    }
045
046    @PostConstruct
047    public void postConstruct() {
048        LOGGER.info("JMS messaging enabled");
049    }
050
051    /**
052     * Creates a queue or topic publisher based on the property fcrepo.jms.destination.type. By default, this is a topic
053     *
054     * @param propsConfig config properties
055     * @return jms publisher
056     */
057    @Bean
058    public AbstractJMSPublisher jmsPublisher(final FedoraPropsConfig propsConfig) {
059        if (propsConfig.getJmsDestinationType() == JmsDestination.QUEUE) {
060            return new JMSQueuePublisher(propsConfig.getJmsDestinationName());
061        } else {
062            return new JMSTopicPublisher(propsConfig.getJmsDestinationName());
063        }
064    }
065
066    /**
067     * translates events into JMS header-only format
068     *
069     * @return JMS message factory
070     */
071    @Bean
072    public JMSEventMessageFactory messageFactory() {
073        return new DefaultMessageFactory();
074    }
075
076    /**
077     * JMS Broker configuration
078     *
079     * @param propsConfig config properties
080     * @return jms broker
081     */
082    @Bean
083    public BrokerFactoryBean jmsBroker(final FedoraPropsConfig propsConfig) {
084        final var factory = new BrokerFactoryBean();
085        factory.setConfig(propsConfig.getActiveMQConfiguration());
086        factory.setStart(true);
087        return factory;
088    }
089
090    /**
091     * ActiveMQ connection
092     *
093     * @param propsConfig config properties
094     * @return ActiveMQ connection factory
095     */
096    @Bean
097    @DependsOn("jmsBroker")
098    public ActiveMQConnectionFactory connectionFactory(final FedoraPropsConfig propsConfig) {
099        final var factory = new ActiveMQConnectionFactory();
100        factory.setBrokerURL(String.format("vm://%s:%s?create=false",
101                propsConfig.getJmsHost(), propsConfig.getJmsPort()));
102        return factory;
103    }
104
105}