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.jbi;
018    
019    import javax.jbi.JBIException;
020    import javax.jbi.component.ComponentContext;
021    import javax.jbi.servicedesc.ServiceEndpoint;
022    import javax.xml.bind.JAXBException;
023    import javax.xml.namespace.QName;
024    
025    import org.apache.servicemix.wsn.client.AbstractWSAClient;
026    import org.apache.servicemix.wsn.client.NotificationBroker;
027    import org.apache.servicemix.wsn.client.Subscription;
028    import org.apache.servicemix.wsn.component.WSNLifeCycle;
029    import org.apache.servicemix.wsn.jaxws.InvalidTopicExpressionFault;
030    import org.apache.servicemix.wsn.jaxws.PublisherRegistrationFailedFault;
031    import org.apache.servicemix.wsn.jaxws.PublisherRegistrationRejectedFault;
032    import org.apache.servicemix.wsn.jaxws.ResourceUnknownFault;
033    import org.apache.servicemix.wsn.jaxws.TopicNotSupportedFault;
034    import org.apache.servicemix.wsn.jms.JmsPublisher;
035    import org.oasis_open.docs.wsn.br_2.PublisherRegistrationFailedFaultType;
036    import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
037    
038    public class JbiPublisher extends JmsPublisher {
039    
040        private WSNLifeCycle lifeCycle;
041    
042        private ServiceEndpoint endpoint;
043    
044        private String notificationBrokerAddress;
045    
046        public JbiPublisher(String name) {
047            super(name);
048        }
049    
050        public String getNotificationBrokerAddress() {
051            return notificationBrokerAddress;
052        }
053    
054        public void setNotificationBrokerAddress(String notificationBrokerAddress) {
055            this.notificationBrokerAddress = notificationBrokerAddress;
056        }
057    
058        @Override
059        protected Object startSubscription() {
060            Subscription subscription = null;
061            try {
062                NotificationBroker broker = new NotificationBroker(getContext());
063                broker.setResolver(AbstractWSAClient.resolveWSA(publisherReference));
064                subscription = broker.subscribe(AbstractWSAClient.createWSA(notificationBrokerAddress), "noTopic", null);
065            } catch (JBIException e) {
066                // TODO Auto-generated catch block
067                e.printStackTrace();
068            } catch (JAXBException e) {
069                // TODO Auto-generated catch block
070                e.printStackTrace();
071            }
072            return subscription;
073        }
074    
075        @Override
076        protected void destroySubscription(Object subscription) {
077            try {
078                ((Subscription) subscription).unsubscribe();
079            } catch (JBIException e) {
080                // TODO Auto-generated catch block
081                e.printStackTrace();
082            }
083        }
084    
085        @Override
086        protected void validatePublisher(RegisterPublisher registerPublisherRequest) throws InvalidTopicExpressionFault,
087                PublisherRegistrationFailedFault, PublisherRegistrationRejectedFault, ResourceUnknownFault,
088                TopicNotSupportedFault {
089            super.validatePublisher(registerPublisherRequest);
090            String[] parts = split(publisherReference.getAddress().getValue());
091            endpoint = getContext().getEndpoint(new QName(parts[0], parts[1]), parts[2]);
092            if (endpoint == null) {
093                PublisherRegistrationFailedFaultType fault = new PublisherRegistrationFailedFaultType();
094                throw new PublisherRegistrationFailedFault("Unable to resolve consumer reference endpoint", fault);
095            }
096        }
097    
098        protected String[] split(String uri) {
099            char sep;
100            if (uri.indexOf('/') > 0) {
101                sep = '/';
102            } else {
103                sep = ':';
104            }
105            int idx1 = uri.lastIndexOf(sep);
106            int idx2 = uri.lastIndexOf(sep, idx1 - 1);
107            String epName = uri.substring(idx1 + 1);
108            String svcName = uri.substring(idx2 + 1, idx1);
109            String nsUri = uri.substring(0, idx2);
110            return new String[] {nsUri, svcName, epName };
111        }
112    
113        public ComponentContext getContext() {
114            return lifeCycle.getContext();
115        }
116    
117        public WSNLifeCycle getLifeCycle() {
118            return lifeCycle;
119        }
120    
121        public void setLifeCycle(WSNLifeCycle lifeCycle) {
122            this.lifeCycle = lifeCycle;
123        }
124    
125    }