001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.jbi.nmr.flow.jms;
018    
019    import java.lang.reflect.Constructor;
020    
021    import javax.jms.ConnectionFactory;
022    import javax.jms.JMSException;
023    import javax.jms.Message;
024    import javax.jms.MessageListener;
025    import javax.jms.Session;
026    import javax.jms.Topic;
027    
028    /**
029     * Use for message routing among a network of containers. All
030     * routing/registration happens automatically.
031     * 
032     * @version $Revision: 564900 $
033     * @org.apache.xbean.XBean element="jmsFlowTibco"
034     */
035    public class JMSFlowTibco extends AbstractJMSFlow {
036    
037        private static final String TOPIC_NAME_MONITOR_CONSUMER = "$sys.monitor.consumer.*";
038    
039        private static final String PROPERTY_NAME_EVENT_CLASS = "event_class";
040    
041        private static final String PROPERTY_NAME_TARGET_DEST_NAME = "target_dest_name";
042    
043        private static final String PROPERTY_NAME_CONN_CONNID = "conn_connid";
044    
045        private static final String EVENT_CLASS_CONSUMER_CREATE = "consumer.create";
046    
047        protected ConnectionFactory createConnectionFactoryFromUrl(String jmsURL) {
048            try {
049                Class connFactoryClass = Class.forName("com.tibco.tibjms.TibjmsConnectionFactory");
050                if (jmsURL != null) {
051                    Constructor cns = connFactoryClass.getConstructor(new Class[] {String.class });
052                    return (ConnectionFactory) cns.newInstance(new Object[] {jmsURL });
053                } else {
054                    return (ConnectionFactory) connFactoryClass.newInstance();
055                }
056            } catch (Exception e) {
057                throw new RuntimeException("Unable to create Tibco connection factory", e);
058            }
059            /*
060            return (jmsURL != null) ? new com.tibco.tibjms.TibjmsConnectionFactory.TibjmsConnectionFactory(jmsURL) : 
061                                      new com.tibco.tibjms.TibjmsConnectionFactory.TibjmsConnectionFactory();
062            */
063        }
064    
065        public void onConsumerMonitorMessage(Message message) {
066            if (!started.get()) {
067                return;
068            }
069            try {
070                String connectionId = "" + message.getLongProperty(PROPERTY_NAME_CONN_CONNID);
071                String targetDestName = message.getStringProperty(PROPERTY_NAME_TARGET_DEST_NAME);
072                String eventClass = message.getStringProperty(PROPERTY_NAME_EVENT_CLASS);
073                if (getBroadcastDestinationName().equals(targetDestName)) {
074                    if (EVENT_CLASS_CONSUMER_CREATE.equals(eventClass)) {
075                        addClusterNode(connectionId);
076                    } else {
077                        removeClusterNode(connectionId);
078                    }
079                }
080            } catch (JMSException e) {
081                e.printStackTrace();
082            }
083        }
084    
085        public void startConsumerMonitor() throws JMSException {
086            Session broadcastSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
087            Topic createTopic = broadcastSession.createTopic(TOPIC_NAME_MONITOR_CONSUMER);
088            monitorMessageConsumer = broadcastSession.createConsumer(createTopic);
089            monitorMessageConsumer.setMessageListener(new MessageListener() {
090                public void onMessage(Message message) {
091                    onConsumerMonitorMessage(message);
092                }
093            });
094        }
095    
096    }