KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > registry > AbstractEndpoint


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: AbstractEndpoint.java 1060 2006-09-13 15:33:37Z alouis $
20  * -------------------------------------------------------------------------
21  * $Log: AbstractEndpoint.java,v $
22  */

23 package org.objectweb.petals.jbi.registry;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.Serializable JavaDoc;
27
28 import javax.jbi.JBIException;
29 import javax.jbi.servicedesc.ServiceEndpoint;
30 import javax.xml.namespace.QName JavaDoc;
31 import javax.xml.parsers.DocumentBuilder JavaDoc;
32 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
33
34 import org.objectweb.petals.jbi.management.service.EndpointService;
35 import org.objectweb.petals.tools.jbicommon.util.XMLUtil;
36 import org.objectweb.petals.util.NamingHelper;
37 import org.objectweb.petals.util.StringHelper;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.DocumentFragment JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41
42 /**
43  * @version $Rev: 1060 $ $Date: 2006-09-13 17:33:37 +0200 (mer, 13 sep 2006) $
44  * @since Petals 1.0
45  * @author alouis,<a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
46  */

47 public abstract class AbstractEndpoint implements ServiceEndpoint, Serializable JavaDoc {
48
49     public enum EndpointType {
50         CONSUMER, EXTERNAL, INTERNAL, LINKED
51     }
52
53     public static final String JavaDoc EPR_NAMESPACE = "http://java.sun.com/xml/ns/jbi";;
54
55     protected String JavaDoc componentName;
56
57     protected String JavaDoc containerName;
58
59     protected String JavaDoc description;
60
61     protected String JavaDoc endpointName;
62
63     protected transient EndpointService endpointService;
64
65     protected QName JavaDoc[] interfaces;
66
67     protected QName JavaDoc serviceName;
68
69     protected EndpointType type;
70
71     protected long containerUID;
72
73     public AbstractEndpoint() {
74         // Do nothing
75
}
76
77     /**
78      *
79      * @param serviceName
80      * @param endpointName
81      * @param componentName
82      * @param containerName
83      */

84     public AbstractEndpoint(QName JavaDoc serviceName, String JavaDoc endpointName,
85         String JavaDoc componentName, String JavaDoc containerName, EndpointType type,
86         EndpointService endpointService) {
87         super();
88         this.serviceName = serviceName;
89         this.endpointName = endpointName;
90         this.componentName = componentName;
91         this.containerName = containerName;
92         this.type = type;
93         this.endpointService = endpointService;
94     }
95
96     /**
97      * @see javax.jbi.servicedesc.ServiceEndpoint#getAsReference(javax.xml.namespace.QName)
98      */

99     public DocumentFragment JavaDoc getAsReference(QName JavaDoc operationName) {
100         DocumentFragment JavaDoc result = null;
101         try {
102             String JavaDoc reference = "<end-point-reference xmlns='" + EPR_NAMESPACE
103                 + "' service-name='" + serviceName.getLocalPart()
104                 + "' end-point-name='" + endpointName
105                 + "'></end-point-reference>";
106             InputSource JavaDoc source = createSource(reference);
107             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory
108                 .newInstance();
109             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
110             Document JavaDoc document = builder.parse(source);
111             result = document.createDocumentFragment();
112         } catch (Exception JavaDoc e) {
113             // Do nothing
114
}
115         return result;
116     }
117
118     public String JavaDoc getComponentName() {
119         return componentName;
120     }
121
122     public String JavaDoc getContainerName() {
123         return containerName;
124     }
125
126     public String JavaDoc getDescription() {
127         return description;
128     }
129
130     public Document JavaDoc getEndpointDescriptor() {
131         Document JavaDoc doc = null;
132         if (description != null) {
133             doc = XMLUtil.createDocumentFromString(description);
134         }
135         if (endpointService != null) {
136             try {
137                 doc = endpointService.getEndpointDescriptorForEndpoint(this);
138             } catch (JBIException e) {
139                 // Do nothing
140
}
141         }
142         return doc;
143     }
144
145     /**
146      * @see javax.jbi.servicedesc.ServiceEndpoint#getEndpointName()
147      */

148     public String JavaDoc getEndpointName() {
149         return endpointName;
150     }
151
152     public EndpointService getEndpointService() {
153         return endpointService;
154     }
155
156     /**
157      * @see javax.jbi.servicedesc.ServiceEndpoint#getInterfaces()
158      */

159     public QName JavaDoc[] getInterfaces() {
160         if (interfaces != null) {
161             return interfaces;
162         }
163         if (endpointService != null) {
164             return endpointService.getInterfacesForEndpoint(this);
165         }
166         return new QName JavaDoc[0];
167     }
168
169     /**
170      * @see javax.jbi.servicedesc.ServiceEndpoint#getServiceName()
171      */

172     public QName JavaDoc getServiceName() {
173         return serviceName;
174     }
175
176     public EndpointType getType() {
177         return type;
178     }
179
180     /**
181      *
182      * @param interfaceName
183      * @return
184      */

185     public boolean implementsInterface(QName JavaDoc interfaceName) {
186
187         boolean implement = false;
188
189         for (QName JavaDoc anInterface : getInterfaces()) {
190             if (anInterface.equals(interfaceName)) {
191                 implement = true;
192                 break;
193             }
194         }
195
196         return implement;
197     }
198
199     public void setComponentName(String JavaDoc componentName) {
200         this.componentName = componentName;
201     }
202
203     public void setContainerName(String JavaDoc containerName) {
204         this.containerName = containerName;
205     }
206
207     public void setDescription(String JavaDoc description) {
208         this.description = description;
209     }
210
211     public void setEndpointName(String JavaDoc endpointName) {
212         this.endpointName = endpointName;
213     }
214
215     public void setEndpointService(EndpointService endpointService) {
216         this.endpointService = endpointService;
217     }
218
219     public void setInterfaces(QName JavaDoc[] interfaces) {
220         this.interfaces = interfaces;
221     }
222
223     public void setServiceName(QName JavaDoc serviceName) {
224         this.serviceName = serviceName;
225     }
226
227     public void setType(EndpointType type) {
228         this.type = type;
229     }
230
231     public String JavaDoc toString() {
232         return serviceName + " ->" + endpointName + " (" + type.name() + "):"
233             + componentName+","+containerName;
234     }
235
236     /**
237      * Return true if o is a ServiceEndpoint and endpointName and serviceName
238      * are equals
239      */

240     public boolean equals(Object JavaDoc o) {
241         boolean result = false;
242
243         if (o instanceof ServiceEndpoint) {
244             ServiceEndpoint other = (ServiceEndpoint) o;
245
246             boolean endpointsEqual = StringHelper.equal(endpointName, other
247                 .getEndpointName());
248
249             String JavaDoc serviceNameStr = NamingHelper.qNameToString(serviceName);
250             String JavaDoc otherServiceNameStr = NamingHelper.qNameToString(other
251                 .getServiceName());
252             boolean servicesEqual = StringHelper.equal(serviceNameStr,
253                 otherServiceNameStr);
254
255             result = endpointsEqual && servicesEqual;
256         }
257         return result;
258     }
259
260     public int hashCode() {
261         int hashCode = 0;
262         if (serviceName != null) {
263             hashCode += serviceName.hashCode();
264         }
265         if (endpointName != null) {
266             hashCode += endpointName.hashCode();
267         }
268         return hashCode;
269     }
270
271     private InputSource JavaDoc createSource(String JavaDoc msg) {
272         InputSource JavaDoc source = new InputSource JavaDoc();
273         byte[] msgByte = msg.getBytes();
274         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(msgByte);
275         source.setByteStream(in);
276         return source;
277     }
278
279     public long getContainerUID() {
280         return containerUID;
281     }
282
283     public void setContainerUID(long componentUID) {
284         this.containerUID = componentUID;
285     }
286
287 }
288
Popular Tags