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.spring;
018    
019    import javax.jbi.JBIException;
020    import javax.jbi.component.ComponentContext;
021    import javax.jbi.messaging.ExchangeStatus;
022    import javax.jbi.messaging.MessageExchange;
023    import javax.jbi.messaging.MessagingException;
024    import javax.annotation.Resource;
025    import javax.annotation.PostConstruct;
026    import javax.annotation.PreDestroy;
027    
028    import org.w3c.dom.Element;
029    
030    import org.apache.servicemix.jbi.listener.MessageExchangeListener;
031    import org.apache.servicemix.jbi.jaxp.SourceTransformer;
032    import org.apache.servicemix.wsn.client.NotificationBroker;
033    
034    /**
035     * A simple bean acting as a WS-Notification publisher.
036     * All messages sent to it will be forwarded to the NotificationBroker as Notify requests.
037     * This beans should be used and deployed onto servicemix-bean as a pojo for a bean endpoint. 
038     *
039     * @author gnodet
040     * @version $Revision: 376451 $
041     * @org.apache.xbean.XBean element="publisher-proxy"
042     */
043    public class PublisherProxyBean implements MessageExchangeListener {
044    
045        private NotificationBroker wsnBroker;
046    
047        private String topic;
048    
049        //private Publisher publisher;
050    
051        @Resource
052        private ComponentContext context;
053    
054        private SourceTransformer sourceTransformer = new SourceTransformer();
055    
056    
057        /**
058         * @return Returns the topic.
059         */
060        public String getTopic() {
061            return topic;
062        }
063    
064        /**
065         * @param topic The topic to set.
066         */
067        public void setTopic(String topic) {
068            this.topic = topic;
069        }
070    
071        @PostConstruct
072        public void start() throws JBIException {
073            wsnBroker = new NotificationBroker(context);
074            //publisher = wsnBroker.registerPublisher(null, topic, false);
075        }
076    
077        @PreDestroy
078        public void stop() throws JBIException {
079            //publisher.destroy();
080        }
081    
082        /* (non-Javadoc)
083         * @see org.apache.servicemix.MessageExchangeListener#onMessageExchange(javax.jbi.messaging.MessageExchange)
084         */
085        public void onMessageExchange(MessageExchange exchange) throws MessagingException {
086            if (exchange.getStatus() != ExchangeStatus.ACTIVE) {
087                return;
088            }
089            // This is a notification from the WSN broker
090            try {
091                Element elem = sourceTransformer.toDOMElement(exchange.getMessage("in"));
092                wsnBroker.notify(topic, elem);
093                exchange.setStatus(ExchangeStatus.DONE);
094            } catch (Exception e) {
095                exchange.setError(e);
096            }
097            context.getDeliveryChannel().send(exchange);
098        }
099    
100    }