1 23 package org.objectweb.petals.jbi.registry; 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.Serializable ; 27 28 import javax.jbi.JBIException; 29 import javax.jbi.servicedesc.ServiceEndpoint; 30 import javax.xml.namespace.QName ; 31 import javax.xml.parsers.DocumentBuilder ; 32 import javax.xml.parsers.DocumentBuilderFactory ; 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 ; 39 import org.w3c.dom.DocumentFragment ; 40 import org.xml.sax.InputSource ; 41 42 47 public abstract class AbstractEndpoint implements ServiceEndpoint, Serializable { 48 49 public enum EndpointType { 50 CONSUMER, EXTERNAL, INTERNAL, LINKED 51 } 52 53 public static final String EPR_NAMESPACE = "http://java.sun.com/xml/ns/jbi";; 54 55 protected String componentName; 56 57 protected String containerName; 58 59 protected String description; 60 61 protected String endpointName; 62 63 protected transient EndpointService endpointService; 64 65 protected QName [] interfaces; 66 67 protected QName serviceName; 68 69 protected EndpointType type; 70 71 protected long containerUID; 72 73 public AbstractEndpoint() { 74 } 76 77 84 public AbstractEndpoint(QName serviceName, String endpointName, 85 String componentName, String 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 99 public DocumentFragment getAsReference(QName operationName) { 100 DocumentFragment result = null; 101 try { 102 String reference = "<end-point-reference xmlns='" + EPR_NAMESPACE 103 + "' service-name='" + serviceName.getLocalPart() 104 + "' end-point-name='" + endpointName 105 + "'></end-point-reference>"; 106 InputSource source = createSource(reference); 107 DocumentBuilderFactory factory = DocumentBuilderFactory 108 .newInstance(); 109 DocumentBuilder builder = factory.newDocumentBuilder(); 110 Document document = builder.parse(source); 111 result = document.createDocumentFragment(); 112 } catch (Exception e) { 113 } 115 return result; 116 } 117 118 public String getComponentName() { 119 return componentName; 120 } 121 122 public String getContainerName() { 123 return containerName; 124 } 125 126 public String getDescription() { 127 return description; 128 } 129 130 public Document getEndpointDescriptor() { 131 Document 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 } 141 } 142 return doc; 143 } 144 145 148 public String getEndpointName() { 149 return endpointName; 150 } 151 152 public EndpointService getEndpointService() { 153 return endpointService; 154 } 155 156 159 public QName [] getInterfaces() { 160 if (interfaces != null) { 161 return interfaces; 162 } 163 if (endpointService != null) { 164 return endpointService.getInterfacesForEndpoint(this); 165 } 166 return new QName [0]; 167 } 168 169 172 public QName getServiceName() { 173 return serviceName; 174 } 175 176 public EndpointType getType() { 177 return type; 178 } 179 180 185 public boolean implementsInterface(QName interfaceName) { 186 187 boolean implement = false; 188 189 for (QName 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 componentName) { 200 this.componentName = componentName; 201 } 202 203 public void setContainerName(String containerName) { 204 this.containerName = containerName; 205 } 206 207 public void setDescription(String description) { 208 this.description = description; 209 } 210 211 public void setEndpointName(String endpointName) { 212 this.endpointName = endpointName; 213 } 214 215 public void setEndpointService(EndpointService endpointService) { 216 this.endpointService = endpointService; 217 } 218 219 public void setInterfaces(QName [] interfaces) { 220 this.interfaces = interfaces; 221 } 222 223 public void setServiceName(QName serviceName) { 224 this.serviceName = serviceName; 225 } 226 227 public void setType(EndpointType type) { 228 this.type = type; 229 } 230 231 public String toString() { 232 return serviceName + " ->" + endpointName + " (" + type.name() + "):" 233 + componentName+","+containerName; 234 } 235 236 240 public boolean equals(Object 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 serviceNameStr = NamingHelper.qNameToString(serviceName); 250 String 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 createSource(String msg) { 272 InputSource source = new InputSource (); 273 byte[] msgByte = msg.getBytes(); 274 ByteArrayInputStream in = new ByteArrayInputStream (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 |