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.container;
018    
019    import java.io.Serializable;
020    
021    import javax.jbi.servicedesc.ServiceEndpoint;
022    import javax.xml.namespace.QName;
023    
024    import org.apache.servicemix.jbi.framework.ComponentNameSpace;
025    import org.apache.servicemix.jbi.framework.Registry;
026    import org.apache.servicemix.jbi.messaging.ExchangePacket;
027    import org.apache.servicemix.jbi.messaging.MessageExchangeImpl;
028    import org.apache.servicemix.jbi.resolver.SubscriptionFilter;
029    import org.apache.servicemix.jbi.servicedesc.InternalEndpoint;
030    
031    /**
032     * Represents a subscription to a JBI endpoint.
033     * 
034     * @org.apache.xbean.XBean element="subscriptionSpec"
035     * 
036     * @version $Revision: 564374 $
037     */
038    public class SubscriptionSpec implements Serializable {
039    
040        /**
041         * Generated serial version UID
042         */
043        private static final long serialVersionUID = 8458586342841647313L;
044    
045        private QName service;
046        private QName interfaceName;
047        private QName operation;
048        private String endpoint;
049        private transient SubscriptionFilter filter;
050        private ComponentNameSpace name;
051    
052        public String getEndpoint() {
053            return endpoint;
054        }
055    
056        public void setEndpoint(String endpoint) {
057            this.endpoint = endpoint;
058        }
059    
060        public SubscriptionFilter getFilter() {
061            return filter;
062        }
063    
064        public void setFilter(SubscriptionFilter filter) {
065            this.filter = filter;
066        }
067    
068        public QName getInterfaceName() {
069            return interfaceName;
070        }
071    
072        public void setInterfaceName(QName interfaceName) {
073            this.interfaceName = interfaceName;
074        }
075    
076        public QName getOperation() {
077            return operation;
078        }
079    
080        public void setOperation(QName operation) {
081            this.operation = operation;
082        }
083    
084        public QName getService() {
085            return service;
086        }
087    
088        public void setService(QName service) {
089            this.service = service;
090        }
091    
092        /**
093         * @return Returns the name.
094         */
095        public ComponentNameSpace getName() {
096            return name;
097        }
098    
099        /**
100         * @org.apache.xbean.XBean hide="true"
101         * 
102         * @param name
103         *            The name to set.
104         */
105        public void setName(ComponentNameSpace name) {
106            this.name = name;
107        }
108    
109        /**
110         * Returns true if this subscription matches the given message exchange
111         * 
112         * @param exchange
113         *            the exchange to be matched
114         * @return true if this subscription matches the exchange
115         */
116        public boolean matches(Registry registry, MessageExchangeImpl exchange) {
117            boolean result = false;
118    
119            ExchangePacket packet = exchange.getPacket();
120            ComponentNameSpace sourceId = packet.getSourceId();
121            if (sourceId != null) {
122                // get the list of services
123                if (service != null) {
124                    ServiceEndpoint[] ses = registry.getEndpointsForService(service);
125                    if (ses != null) {
126                        for (int i = 0; i < ses.length; i++) {
127                            InternalEndpoint se = (InternalEndpoint) ses[i];
128                            if (se.getComponentNameSpace() != null && se.getComponentNameSpace().equals(sourceId)) {
129                                result = true;
130                                break;
131                            }
132                        }
133                    }
134                }
135                if (result && interfaceName != null) {
136                    ServiceEndpoint[] ses = registry.getEndpointsForInterface(interfaceName);
137                    if (ses != null) {
138                        result = false;
139                        for (int i = 0; i < ses.length; i++) {
140                            InternalEndpoint se = (InternalEndpoint) ses[i];
141                            if (se.getComponentNameSpace() != null && se.getComponentNameSpace().equals(sourceId)) {
142                                result = true;
143                                break;
144                            }
145                        }
146                    }
147                }
148            }
149            
150            // allow a match all subscription
151            if (service == null && interfaceName == null) {
152                result = true;
153            }
154            if (result && filter != null) {
155                result = filter.matches(exchange);
156            }
157            return result;
158        }
159    
160        /**
161         * @see java.lang.Object#equals(java.lang.Object)
162         */
163        public boolean equals(Object obj) {
164            boolean result = false;
165            if (obj instanceof SubscriptionSpec) {
166                SubscriptionSpec other = (SubscriptionSpec) obj;
167                result = (name == null && other.name == null || name.equals(other.name))
168                        && (service == null && other.service == null)
169                        || (service != null && other.service != null && service.equals(other.service))
170                        && (interfaceName == null && other.interfaceName == null)
171                        || (interfaceName != null && other.interfaceName != null && interfaceName
172                                .equals(other.interfaceName)) && (endpoint == null && other.endpoint == null)
173                        || (endpoint != null && other.endpoint != null && endpoint.equals(other.endpoint));
174    
175            }
176            return result;
177        }
178    
179        /**
180         * @see java.lang.Object#hashCode()
181         */
182        public int hashCode() {
183            return (name != null ? name.hashCode() : 0)
184                    ^ (service != null ? service.hashCode() : (interfaceName != null ? interfaceName.hashCode() : super
185                            .hashCode()));
186        }
187    
188    }