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