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.wsn.jms;
018    
019    import java.io.StringWriter;
020    
021    import javax.jms.Connection;
022    import javax.jms.JMSException;
023    import javax.jms.Message;
024    import javax.jms.MessageProducer;
025    import javax.jms.Session;
026    import javax.jms.Topic;
027    import javax.xml.bind.JAXBContext;
028    import javax.xml.bind.JAXBException;
029    
030    import org.apache.activemq.advisory.ConsumerEvent;
031    import org.apache.activemq.advisory.ConsumerEventSource;
032    import org.apache.activemq.advisory.ConsumerListener;
033    import org.apache.commons.logging.Log;
034    import org.apache.commons.logging.LogFactory;
035    import org.apache.servicemix.wsn.AbstractPublisher;
036    import org.apache.servicemix.wsn.jaxws.InvalidTopicExpressionFault;
037    import org.apache.servicemix.wsn.jaxws.PublisherRegistrationFailedFault;
038    import org.apache.servicemix.wsn.jaxws.PublisherRegistrationRejectedFault;
039    import org.apache.servicemix.wsn.jaxws.ResourceNotDestroyedFault;
040    import org.apache.servicemix.wsn.jaxws.ResourceUnknownFault;
041    import org.apache.servicemix.wsn.jaxws.TopicNotSupportedFault;
042    import org.oasis_open.docs.wsn.b_2.InvalidTopicExpressionFaultType;
043    import org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType;
044    import org.oasis_open.docs.wsn.b_2.Notify;
045    import org.oasis_open.docs.wsn.br_2.PublisherRegistrationFailedFaultType;
046    import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
047    import org.oasis_open.docs.wsn.br_2.ResourceNotDestroyedFaultType;
048    
049    public abstract class JmsPublisher extends AbstractPublisher implements ConsumerListener {
050    
051        private static Log log = LogFactory.getLog(JmsPublisher.class);
052    
053        private Connection connection;
054    
055        private JmsTopicExpressionConverter topicConverter;
056    
057        private JAXBContext jaxbContext;
058    
059        private Topic jmsTopic;
060    
061        private ConsumerEventSource advisory;
062    
063        private Object subscription;
064    
065        public JmsPublisher(String name) {
066            super(name);
067            topicConverter = new JmsTopicExpressionConverter();
068            try {
069                jaxbContext = JAXBContext.newInstance(Notify.class);
070            } catch (JAXBException e) {
071                throw new RuntimeException("Unable to create JAXB context", e);
072            }
073        }
074    
075        public Connection getConnection() {
076            return connection;
077        }
078    
079        public void setConnection(Connection connection) {
080            this.connection = connection;
081        }
082    
083        @Override
084        public void notify(NotificationMessageHolderType messageHolder) {
085            Session session = null;
086            try {
087                Topic topic = topicConverter.toActiveMQTopic(messageHolder.getTopic());
088                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
089                MessageProducer producer = session.createProducer(topic);
090                Notify notify = new Notify();
091                notify.getNotificationMessage().add(messageHolder);
092                StringWriter writer = new StringWriter();
093                jaxbContext.createMarshaller().marshal(notify, writer);
094                Message message = session.createTextMessage(writer.toString());
095                producer.send(message);
096            } catch (JMSException e) {
097                log.warn("Error dispatching message", e);
098            } catch (JAXBException e) {
099                log.warn("Error dispatching message", e);
100            } catch (InvalidTopicException e) {
101                log.warn("Error dispatching message", e);
102            } finally {
103                if (session != null) {
104                    try {
105                        session.close();
106                    } catch (JMSException e) {
107                        log.debug("Error closing session", e);
108                    }
109                }
110            }
111        }
112    
113        @Override
114        protected void validatePublisher(RegisterPublisher registerPublisherRequest) throws InvalidTopicExpressionFault,
115                PublisherRegistrationFailedFault, PublisherRegistrationRejectedFault, ResourceUnknownFault,
116                TopicNotSupportedFault {
117            super.validatePublisher(registerPublisherRequest);
118            try {
119                jmsTopic = topicConverter.toActiveMQTopic(topic);
120            } catch (InvalidTopicException e) {
121                InvalidTopicExpressionFaultType fault = new InvalidTopicExpressionFaultType();
122                throw new InvalidTopicExpressionFault(e.getMessage(), fault);
123            }
124        }
125    
126        @Override
127        protected void start() throws PublisherRegistrationFailedFault {
128            if (demand) {
129                try {
130                    advisory = new ConsumerEventSource(connection, jmsTopic);
131                    advisory.setConsumerListener(this);
132                    advisory.start();
133                } catch (Exception e) {
134                    PublisherRegistrationFailedFaultType fault = new PublisherRegistrationFailedFaultType();
135                    throw new PublisherRegistrationFailedFault("Error starting demand-based publisher", fault, e);
136                }
137            }
138        }
139    
140        protected void destroy() throws ResourceNotDestroyedFault {
141            try {
142                if (advisory != null) {
143                    advisory.stop();
144                }
145            } catch (Exception e) {
146                ResourceNotDestroyedFaultType fault = new ResourceNotDestroyedFaultType();
147                throw new ResourceNotDestroyedFault("Error destroying publisher", fault, e);
148            } finally {
149                super.destroy();
150            }
151        }
152    
153        public void onConsumerEvent(ConsumerEvent event) {
154            if (event.getConsumerCount() > 0) {
155                if (subscription == null) {
156                    // start subscription
157                    subscription = startSubscription();
158                }
159            } else {
160                if (subscription != null) {
161                    // destroy subscription
162                    Object sub = subscription;
163                    subscription = null;
164                    destroySubscription(sub);
165                }
166            }
167        }
168    
169        protected abstract void destroySubscription(Object sub);
170    
171        protected abstract Object startSubscription();
172    
173    }