001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.gravity.adapters;
022
023import javax.jms.Session;
024
025import org.apache.activemq.ActiveMQConnectionFactory;
026import org.apache.activemq.command.ActiveMQTopic;
027import org.granite.logging.Logger;
028import org.granite.messaging.service.ServiceException;
029import org.granite.util.XMap;
030
031/**
032 * @author William DRAI
033 */
034public class ActiveMQServiceAdapter extends JMSServiceAdapter {
035
036    private static final Logger log = Logger.getLogger(ActiveMQServiceAdapter.class);
037
038    @Override
039    public void configure(XMap adapterProperties, XMap destinationProperties) throws ServiceException {
040        try {
041            destinationName = destinationProperties.get("jms/destination-name");
042            if (Boolean.TRUE.toString().equals(destinationProperties.get("jms/transacted-sessions")))
043                transactedSessions = true;
044            if ("AUTO_ACKNOWLEDGE".equals(destinationProperties.get("jms/acknowledge-mode")))
045                acknowledgeMode = Session.AUTO_ACKNOWLEDGE;
046            else if ("CLIENT_ACKNOWLEDGE".equals(destinationProperties.get("jms/acknowledge-mode")))
047                acknowledgeMode = Session.CLIENT_ACKNOWLEDGE;
048            else if ("DUPS_OK_ACKNOWLEDGE".equals(destinationProperties.get("jms/acknowledge-mode")))
049                acknowledgeMode = Session.DUPS_OK_ACKNOWLEDGE;
050            if ("javax.jms.TextMessage".equals(destinationProperties.get("jms/message-type")))
051                textMessages = true;
052            
053            if (Boolean.TRUE.toString().equals(destinationProperties.get("jms/no-local")))
054                noLocal = true;
055
056            if (Boolean.TRUE.toString().equals(destinationProperties.get("session-selector")))
057                sessionSelector = true;
058            
059            failoverRetryInterval = destinationProperties.get("jms/failover-retry-interval", Long.TYPE, DEFAULT_FAILOVER_RETRY_INTERVAL);
060            if (failoverRetryInterval <= 0) {
061                log.warn("Illegal failover retry interval: %d (using default %d)", failoverRetryInterval, DEFAULT_FAILOVER_RETRY_INTERVAL);
062                failoverRetryInterval = DEFAULT_FAILOVER_RETRY_INTERVAL;
063            }
064            
065            failoverRetryCount = destinationProperties.get("jms/failover-retry-count", Integer.TYPE, DEFAULT_FAILOVER_RETRY_COUNT);
066            if (failoverRetryCount <= 0) {
067                log.warn("Illegal failover retry count: %s (using default %d)", failoverRetryCount, DEFAULT_FAILOVER_RETRY_COUNT);
068                failoverRetryCount = DEFAULT_FAILOVER_RETRY_COUNT;
069            }
070
071            StringBuilder sb = null;
072            if (destinationProperties.get("server/broker-url") != null && !"".equals(destinationProperties.get("server/broker-url").trim())) {
073                sb = new StringBuilder(destinationProperties.get("server/broker-url"));
074            }
075            else {
076                    sb = new StringBuilder("vm://");
077                    sb.append(getId());
078                    if (Boolean.FALSE.toString().equals(destinationProperties.get("server/create-broker"))) {
079                        sb.append("?create=false");
080                        String startupWait = destinationProperties.get("server/wait-for-start");
081                        if (startupWait != null)
082                            sb.append("&waitForStart=" + startupWait);
083                    } 
084                    else
085                        sb.append("?create=true");
086                    
087                    if (Boolean.TRUE.toString().equals(destinationProperties.get("server/durable"))) {
088                        sb.append("&broker.persistent=true");
089                        if (destinationProperties.containsKey("server/file-store-root"))
090                            sb.append("&broker.dataDirectory=").append(destinationProperties.get("server/file-store-root"));
091                    }
092                    else
093                        sb.append("&broker.persistent=false");
094            }
095
096            String brokerURL = sb.toString();
097            if (destinationProperties.get("server/username") != null && !"".equals(destinationProperties.get("server/username").trim()) 
098                        && destinationProperties.get("server/password") != null && !"".equals(destinationProperties.get("server/password").trim())) {
099                String username = destinationProperties.get("server/username");
100                String password = destinationProperties.get("server/password"); 
101                jmsConnectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL);
102            }
103            else
104                jmsConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
105        }
106        catch (Exception e) {
107            throw new ServiceException("Error when configuring JMS Adapter", e);
108        }
109    }
110
111    @Override
112    protected javax.jms.Destination getProducerDestination(String topic) {
113        return new ActiveMQTopic(topic != null ? destinationName + "." + topic.replaceAll("\\*\\*", ">") : destinationName);
114    }
115
116    @Override
117    protected javax.jms.Destination getConsumerDestination(String topic) {
118        return new ActiveMQTopic(topic != null ? destinationName + "." + topic.replaceAll("\\*\\*", ">") : destinationName);
119    }
120}