KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > container > SubscriptionSpec


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.container;
18
19 import org.apache.servicemix.jbi.framework.ComponentNameSpace;
20 import org.apache.servicemix.jbi.framework.Registry;
21 import org.apache.servicemix.jbi.messaging.ExchangePacket;
22 import org.apache.servicemix.jbi.messaging.MessageExchangeImpl;
23 import org.apache.servicemix.jbi.resolver.SubscriptionFilter;
24 import org.apache.servicemix.jbi.servicedesc.InternalEndpoint;
25
26 import javax.jbi.servicedesc.ServiceEndpoint;
27 import javax.xml.namespace.QName JavaDoc;
28
29 import java.io.Serializable JavaDoc;
30
31 /**
32  * Represents a subscription to a JBI endpoint.
33  *
34  * @org.apache.xbean.XBean element="subscriptionSpec"
35  *
36  * @version $Revision: 426415 $
37  */

38 public class SubscriptionSpec implements Serializable JavaDoc {
39
40     /**
41      * Generated serial version UID
42      */

43     private static final long serialVersionUID = 8458586342841647313L;
44
45     private QName JavaDoc service;
46     private QName JavaDoc interfaceName;
47     private QName JavaDoc operation;
48     private String JavaDoc endpoint;
49     private transient SubscriptionFilter filter;
50     private ComponentNameSpace name;
51
52     public String JavaDoc getEndpoint() {
53         return endpoint;
54     }
55
56     public void setEndpoint(String JavaDoc endpoint) {
57         this.endpoint = endpoint;
58     }
59
60     public SubscriptionFilter getFilter() {
61         return filter;
62     }
63
64     public void setFilter(SubscriptionFilter filter) {
65         this.filter = filter;
66     }
67
68     public QName JavaDoc getInterfaceName() {
69         return interfaceName;
70     }
71
72     public void setInterfaceName(QName JavaDoc interfaceName) {
73         this.interfaceName = interfaceName;
74     }
75
76     public QName JavaDoc getOperation() {
77         return operation;
78     }
79
80     public void setOperation(QName JavaDoc operation) {
81         this.operation = operation;
82     }
83
84     public QName JavaDoc getService() {
85         return service;
86     }
87
88     public void setService(QName JavaDoc service) {
89         this.service = service;
90     }
91
92     /**
93      * @return Returns the name.
94      */

95     public ComponentNameSpace getName() {
96         return name;
97     }
98
99     /**
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 JavaDoc 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 }
189
Popular Tags