OpenSplice Java FACE API  v6.x
OpenSplice Future Airborne Capability Environment (FACE) Java API
ConnectionDescription.java
Go to the documentation of this file.
1 /*
2  * Vortex OpenSplice
3  *
4  * This software and documentation are Copyright 2006 to 2021 ADLINK
5  * Technology Limited, its affiliated companies and licensors. All rights
6  * reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 package org.vortex.FACE;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.HashSet;
25 
26 import javax.xml.XMLConstants;
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.transform.dom.DOMSource;
31 import javax.xml.transform.stream.StreamSource;
32 import javax.xml.validation.Schema;
33 import javax.xml.validation.SchemaFactory;
34 import javax.xml.validation.Validator;
35 
36 import org.omg.dds.core.QosProvider;
37 import org.omg.dds.core.ServiceEnvironment;
38 import org.omg.dds.domain.DomainParticipantQos;
39 import org.omg.dds.pub.DataWriterQos;
40 import org.omg.dds.pub.PublisherQos;
41 import org.omg.dds.sub.DataReaderQos;
42 import org.omg.dds.sub.SubscriberQos;
43 import org.omg.dds.topic.TopicQos;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.Node;
47 import org.w3c.dom.NodeList;
48 import org.xml.sax.SAXException;
49 
50 import FACE.CONNECTION_DIRECTION_TYPE;
51 
52 public class ConnectionDescription {
53  /* file in the root for the ddsface jar */
54  private static final String SCHEMA_PATH = "vortex_face.xsd";
55  private String name;
56  private long id;
57  private int domainId;
58  private CONNECTION_DIRECTION_TYPE direction;
59  private long platformViewGuid;
60  private long refreshPeriod;
61  private String topicName;
62  private String typeName;
63  private ServiceEnvironment env;
64  private final HashSet<ConnectionDescription> configurations = new HashSet<ConnectionDescription>();
65  private QosConfiguration qos = null;
66 
67 
68  public ConnectionDescription(ServiceEnvironment env, String configurationUri)
69  throws ConfigurationException {
70 
71  Schema schema = null;
72  try {
73  String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
74  SchemaFactory factory = SchemaFactory.newInstance(language);
75  schema = factory.newSchema(new StreamSource(this.getClass().getClassLoader().getResourceAsStream(SCHEMA_PATH)));
76  } catch (Exception e) {
77  throw new ConfigurationException(e.getMessage());
78  }
79 
80  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
81  factory.setSchema(schema);
82  factory.setNamespaceAware(true);
83  DocumentBuilder builder;
84  String name;
85  String type;
86  long id;
87  int domainId;
88  CONNECTION_DIRECTION_TYPE direction = null;
89  long platformViewGuid;
90  long refreshPeriod;
91  String topicName;
92  String typeName;
93 
94  try {
95  builder = factory.newDocumentBuilder();
96 
97  Document document = builder.parse(new File(configurationUri));
98  Validator validator = schema.newValidator();
99  validator.validate(new DOMSource(document));
100 
101  NodeList nList = document.getElementsByTagName("connection");
102  for (int temp = 0; temp < nList.getLength(); temp++) {
103  Node nNode = nList.item(temp);
104  if (nNode.getNodeType() == Node.ELEMENT_NODE) {
105  Element eElement = (Element) nNode;
106  type = this.getStringConfig(eElement, "type");
107 
108  if (!"DDS".equals(type)) {
109  throw new ConfigurationException("Only 'DDS' type supported (found '" + type + "')");
110  }
111 
112  String dir = this.getStringConfig(eElement, "direction");
113 
114  if ("SOURCE".equals(dir)) {
115  direction = CONNECTION_DIRECTION_TYPE.SOURCE;
116  } else if ("DESTINATION".equals(dir)) {
117  direction = CONNECTION_DIRECTION_TYPE.DESTINATION;
118  } else {
119  throw new ConfigurationException(
120  "Only SOURCE and DESTINATION connections are supported (found: '"
121  + dir + "').");
122  }
123  NodeList qosList = eElement.getElementsByTagName("qos");
124  QosConfiguration qos = null;
125  if (qosList.getLength() == 0) {
126  qos = new QosConfiguration(env, null, null, null, null, null, null, null, null);
127  } else {
128  Element qosElement = (Element) qosList.item(0);
129  String qosUri = this.getStringConfig(qosElement, "uri");
130  String qosProfile = this.getStringConfig(qosElement, "profile");
131  String domainparticipant_qos_id = this.getStringConfig(qosElement, "domainparticipant_qos_id", true);
132  String topic_qos_id= this.getStringConfig(qosElement, "topic_qos_id", true);
133  String publisher_qos_id = this.getStringConfig(qosElement, "publisher_qos_id", true);
134  String datawriter_qos_id = this.getStringConfig(qosElement, "datawriter_qos_id", true);
135  String subscriber_qos_id = this.getStringConfig(qosElement, "subscriber_qos_id", true);
136  String datareader_qos_id = this.getStringConfig(qosElement, "datareader_qos_id", true);
137  qos = new QosConfiguration(env, qosUri, qosProfile, domainparticipant_qos_id, topic_qos_id, publisher_qos_id, datawriter_qos_id, subscriber_qos_id, datareader_qos_id);
138  }
139  name = this.getStringConfig(eElement, "name").toLowerCase();
140  id = this.hashCode();
141  platformViewGuid = this.getLongConfig(eElement, "platform_view_guid");
142  refreshPeriod = this.getLongConfig(eElement, "refresh_period");
143  domainId = this.getIntConfig(eElement, "domain_id", DDS.DOMAIN_ID_DEFAULT.value);
144  topicName = this.getStringConfig(eElement, "topic_name");
145  typeName = this.getStringConfig(eElement, "type_name");
146  ConnectionDescription config = new ConnectionDescription(env,name,id,domainId,qos,direction,platformViewGuid,refreshPeriod,topicName,typeName);
147  configurations.add(config);
148  }
149  }
150  } catch (ParserConfigurationException e) {
151  throw new ConfigurationException(e.getMessage());
152  } catch (SAXException e) {
153  throw new ConfigurationException(e.getMessage());
154  } catch (IOException e) {
155  throw new ConfigurationException(e.getMessage());
156  }
157  }
158 
159  private long getLongConfig(Element element, String name)
160  throws ConfigurationException {
161  String value = this.getStringConfig(element, name);
162  try {
163  return Long.parseLong(value);
164  } catch (NumberFormatException e) {
165  throw new ConfigurationException(e.getMessage());
166  }
167  }
168 
169  private int getIntConfig(Element element, String name, int defaultValue)
170  throws ConfigurationException {
171  String value = this.getStringConfig(element, name, true);
172  if (value == null) {
173  return defaultValue;
174  } else {
175  try {
176  return Integer.parseInt(value);
177  } catch (NumberFormatException e) {
178  throw new ConfigurationException(e.getMessage());
179  }
180  }
181  }
182 
183  private String getStringConfig(Element element, String name, boolean optional)
184  throws ConfigurationException {
185  String result = null;
186  NodeList list = element.getElementsByTagName(name);
187 
188  if (list.getLength() != 1 && !optional) {
189  throw new ConfigurationException("No element '" + name + "' found.");
190  } else {
191  if (list.getLength() == 1) {
192  Node child = list.item(0).getFirstChild();
193 
194  if (child == null) {
195  throw new ConfigurationException("Element '" + name
196  + "' has no content.");
197  }
198  result = child.getNodeValue();
199  }
200  }
201  return result;
202  }
203 
204  private String getStringConfig(Element element, String name)
205  throws ConfigurationException {
206  NodeList list = element.getElementsByTagName(name);
207 
208  if (list.getLength() != 1) {
209  throw new ConfigurationException("No element '" + name + "' found.");
210  }
211  Node child = list.item(0).getFirstChild();
212 
213  if (child == null) {
214  throw new ConfigurationException("Element '" + name
215  + "' has no content.");
216  }
217  return child.getNodeValue();
218 
219  }
220 
221  public ConnectionDescription(ServiceEnvironment env, String name, long id,
222  int domainId, QosConfiguration qos,
223  CONNECTION_DIRECTION_TYPE direction, long platformViewGuid,
224  long refreshPeriod, String topicName, String typeName) {
225  this.name = name;
226  this.id = id;
227  this.domainId = domainId;
228  this.direction = direction;
229  this.platformViewGuid = platformViewGuid;
230  this.refreshPeriod = refreshPeriod;
231  this.topicName = topicName;
232  this.typeName = typeName;
233  this.qos = qos;
234  this.env = env;
235  }
236 
238  return qos;
239  }
240 
241  public void setQos(QosConfiguration qos) {
242  this.qos = qos;
243  }
244 
245  public HashSet<ConnectionDescription> getConfigurations() {
246  return configurations;
247  }
248 
249  public ServiceEnvironment getEnvironment() {
250  return this.env;
251  }
252 
253  public String getName() {
254  return this.name;
255  }
256 
257  public long getId() {
258  return this.id;
259  }
260 
261  public DomainParticipantQos getDomainParticipantQos() {
262  if (this.getQos().getQP() == null) {
263  return null;
264  }
265  return this.getQos().getDomainparticipant_qos_id() != null
266  ? this.getQos().getQP().getDomainParticipantQos(this.getQos().getDomainparticipant_qos_id())
267  : this.getQos().getQP().getDomainParticipantQos();
268  }
269 
270  public PublisherQos getPublisherQos() {
271  if (this.getQos().getQP() == null) {
272  return null;
273  }
274  return this.getQos().getQP().getPublisherQos(this.getQos().getPublisher_qos_id());
275  }
276 
277  public SubscriberQos getSubscriberQos() {
278  if (this.getQos().getQP() == null) {
279  return null;
280  }
281  return this.getQos().getSubscriber_qos_id() != null
282  ? this.getQos().getQP().getSubscriberQos(this.getQos().getSubscriber_qos_id())
283  : this.getQos().getQP().getSubscriberQos();
284  }
285 
286  public TopicQos getTopicQos() {
287  if (this.getQos().getQP() == null) {
288  return null;
289  }
290  return this.getQos().getTopic_qos_id() != null ? this.getQos().getQP().getTopicQos(this.getQos().getTopic_qos_id())
291  : this.getQos().getQP().getTopicQos();
292  }
293 
294  public DataWriterQos getDataWriterQos() {
295  if (this.getQos().getQP() == null) {
296  return null;
297  }
298  return this.getQos().getDatawriter_qos_id() != null
299  ? this.getQos().getQP().getDataWriterQos(this.getQos().getDatawriter_qos_id())
300  : this.getQos().getQP().getDataWriterQos();
301  }
302 
303  public DataReaderQos getDataReaderQos() {
304  if (this.getQos().getQP() == null) {
305  return null;
306  }
307  return this.getQos().getDatareader_qos_id() != null
308  ? this.getQos().getQP().getDataReaderQos(this.getQos().getDatareader_qos_id())
309  : this.getQos().getQP().getDataReaderQos();
310  }
311 
312  public int getDomainId() {
313  return this.domainId;
314  }
315 
316  public CONNECTION_DIRECTION_TYPE getDirection() {
317  return this.direction;
318  }
319 
320  public long getPlatformViewGuid() {
321  return this.platformViewGuid;
322  }
323 
324  public long getRefreshPeriod() {
325  return this.refreshPeriod;
326  }
327 
328  public String getTopicName() {
329  return this.topicName;
330  }
331 
332  public String getTypeName() {
333  return this.typeName;
334  }
335 
336  @Override
337  public int hashCode() {
338  final int prime = 31;
339  int result = 1;
340  result = prime * result + ((configurations == null) ? 0 : configurations.hashCode());
341  result = prime * result + ((direction == null) ? 0 : direction.hashCode());
342  result = prime * result + domainId;
343  result = prime * result + ((env == null) ? 0 : env.hashCode());
344  result = prime * result + (int) (id ^ (id >>> 32));
345  result = prime * result + ((name == null) ? 0 : name.hashCode());
346  result = prime * result + (int) (platformViewGuid ^ (platformViewGuid >>> 32));
347  result = prime * result + ((qos == null) ? 0 : qos.hashCode());
348  result = prime * result + (int) (refreshPeriod ^ (refreshPeriod >>> 32));
349  result = prime * result + ((topicName == null) ? 0 : topicName.hashCode());
350  result = prime * result + ((typeName == null) ? 0 : typeName.hashCode());
351  return result;
352  }
353 
354  @Override
355  public boolean equals(Object obj) {
356  if (this == obj)
357  return true;
358  if (obj == null)
359  return false;
360  if (getClass() != obj.getClass())
361  return false;
363  if (configurations == null) {
364  if (other.configurations != null)
365  return false;
366  } else if (!configurations.equals(other.configurations))
367  return false;
368  if (direction == null) {
369  if (other.direction != null)
370  return false;
371  } else if (!direction.equals(other.direction))
372  return false;
373  if (domainId != other.domainId)
374  return false;
375  if (env == null) {
376  if (other.env != null)
377  return false;
378  } else if (!env.equals(other.env))
379  return false;
380  if (id != other.id)
381  return false;
382  if (name == null) {
383  if (other.name != null)
384  return false;
385  } else if (!name.equals(other.name))
386  return false;
387  if (platformViewGuid != other.platformViewGuid)
388  return false;
389  if (qos == null) {
390  if (other.qos != null)
391  return false;
392  } else if (!qos.equals(other.qos))
393  return false;
394  if (refreshPeriod != other.refreshPeriod)
395  return false;
396  if (topicName == null) {
397  if (other.topicName != null)
398  return false;
399  } else if (!topicName.equals(other.topicName))
400  return false;
401  if (typeName == null) {
402  if (other.typeName != null)
403  return false;
404  } else if (!typeName.equals(other.typeName))
405  return false;
406  return true;
407  }
408 
409  public static class QosConfiguration {
410 
411  private String uri;
412  private String profile;
413  private String domainparticipant_qos_id;
414  private String topic_qos_id;
415  private String publisher_qos_id;
416  private String datawriter_qos_id;
417  private String subscriber_qos_id;
418  private String datareader_qos_id;
419  private QosProvider qp;
420 
421  public QosConfiguration(ServiceEnvironment env, String uri, String profile, String domainparticipant_qos_id, String topic_qos_id,
422  String publisher_qos_id, String datawriter_qos_id, String subscriber_qos_id, String datareader_qos_id) {
423  this.uri = uri;
424  this.profile = profile;
425  this.domainparticipant_qos_id = domainparticipant_qos_id;
426  this.topic_qos_id = topic_qos_id;
427  this.publisher_qos_id = publisher_qos_id;
428  this.datawriter_qos_id = datawriter_qos_id;
429  this.subscriber_qos_id = subscriber_qos_id;
430  this.datareader_qos_id = datareader_qos_id;
431  if (uri != null && profile != null) {
432  this.qp = QosProvider.newQosProvider(uri, profile, env);
433  } else {
434  this.qp = null;
435  }
436  }
437  public QosProvider getQP() {
438  return qp;
439  }
440  public String getUri() {
441  return uri;
442  }
443  public void setUri(String uri) {
444  this.uri = uri;
445  }
446  public String getProfile() {
447  return profile;
448  }
449  public void setProfile(String profile) {
450  this.profile = profile;
451  }
452  public String getDomainparticipant_qos_id() {
453  return domainparticipant_qos_id;
454  }
455  public void setDomainparticipant_qos_id(String domainparticipant_qos_id) {
456  this.domainparticipant_qos_id = domainparticipant_qos_id;
457  }
458  public String getTopic_qos_id() {
459  return topic_qos_id;
460  }
461  public void setTopic_qos_id(String topic_qos_id) {
462  this.topic_qos_id = topic_qos_id;
463  }
464  public String getPublisher_qos_id() {
465  return publisher_qos_id;
466  }
467  public void setPublisher_qos_id(String publisher_qos_id) {
468  this.publisher_qos_id = publisher_qos_id;
469  }
470  public String getDatawriter_qos_id() {
471  return datawriter_qos_id;
472  }
473  public void setDatawriter_qos_id(String datawriter_qos_id) {
474  this.datawriter_qos_id = datawriter_qos_id;
475  }
476  public String getSubscriber_qos_id() {
477  return subscriber_qos_id;
478  }
479  public void setSubscriber_qos_id(String subscriber_qos_id) {
480  this.subscriber_qos_id = subscriber_qos_id;
481  }
482  public String getDatareader_qos_id() {
483  return datareader_qos_id;
484  }
485  public void setDatareader_qos_id(String datareader_qos_id) {
486  this.datareader_qos_id = datareader_qos_id;
487  }
488 
489  @Override
490  public int hashCode() {
491  final int prime = 31;
492  int result = 1;
493  result = prime * result + ((datareader_qos_id == null) ? 0 : datareader_qos_id.hashCode());
494  result = prime * result + ((datawriter_qos_id == null) ? 0 : datawriter_qos_id.hashCode());
495  result = prime * result + ((domainparticipant_qos_id == null) ? 0 : domainparticipant_qos_id.hashCode());
496  result = prime * result + ((profile == null) ? 0 : profile.hashCode());
497  result = prime * result + ((publisher_qos_id == null) ? 0 : publisher_qos_id.hashCode());
498  result = prime * result + ((subscriber_qos_id == null) ? 0 : subscriber_qos_id.hashCode());
499  result = prime * result + ((topic_qos_id == null) ? 0 : topic_qos_id.hashCode());
500  result = prime * result + ((uri == null) ? 0 : uri.hashCode());
501  result = prime * result + ((qp == null) ? 0 : qp.hashCode());
502  return result;
503  }
504  @Override
505  public boolean equals(Object obj) {
506  if (this == obj)
507  return true;
508  if (obj == null)
509  return false;
510  if (getClass() != obj.getClass())
511  return false;
512  QosConfiguration other = (QosConfiguration) obj;
513  if (datareader_qos_id == null) {
514  if (other.datareader_qos_id != null)
515  return false;
516  } else if (!datareader_qos_id.equals(other.datareader_qos_id))
517  return false;
518  if (datawriter_qos_id == null) {
519  if (other.datawriter_qos_id != null)
520  return false;
521  } else if (!datawriter_qos_id.equals(other.datawriter_qos_id))
522  return false;
523  if (domainparticipant_qos_id == null) {
524  if (other.domainparticipant_qos_id != null)
525  return false;
526  } else if (!domainparticipant_qos_id.equals(other.domainparticipant_qos_id))
527  return false;
528  if (profile == null) {
529  if (other.profile != null)
530  return false;
531  } else if (!profile.equals(other.profile))
532  return false;
533  if (publisher_qos_id == null) {
534  if (other.publisher_qos_id != null)
535  return false;
536  } else if (!publisher_qos_id.equals(other.publisher_qos_id))
537  return false;
538  if (subscriber_qos_id == null) {
539  if (other.subscriber_qos_id != null)
540  return false;
541  } else if (!subscriber_qos_id.equals(other.subscriber_qos_id))
542  return false;
543  if (topic_qos_id == null) {
544  if (other.topic_qos_id != null)
545  return false;
546  } else if (!topic_qos_id.equals(other.topic_qos_id))
547  return false;
548  if (uri == null) {
549  if (other.uri != null)
550  return false;
551  } else if (!uri.equals(other.uri))
552  return false;
553  if (qp == null) {
554  if (other.qp != null)
555  return false;
556  } else if (!qp.equals(other.qp))
557  return false;
558  return true;
559  }
560 
561  @Override
562  public String toString() {
563  return "QosConfiguration [uri=" + uri + ", profile=" + profile + ", domainparticipant_qos_id=" + domainparticipant_qos_id
564  + ", topic_qos_id=" + topic_qos_id + ", publisher_qos_id=" + publisher_qos_id
565  + ", datawriter_qos_id=" + datawriter_qos_id + ", subscriber_qos_id=" + subscriber_qos_id
566  + ", datareader_qos_id=" + datareader_qos_id + "]";
567  }
568  }
569 
570 }
ConnectionDescription(ServiceEnvironment env, String name, long id, int domainId, QosConfiguration qos, CONNECTION_DIRECTION_TYPE direction, long platformViewGuid, long refreshPeriod, String topicName, String typeName)
void setDomainparticipant_qos_id(String domainparticipant_qos_id)
ConnectionDescription(ServiceEnvironment env, String configurationUri)
QosConfiguration(ServiceEnvironment env, String uri, String profile, String domainparticipant_qos_id, String topic_qos_id, String publisher_qos_id, String datawriter_qos_id, String subscriber_qos_id, String datareader_qos_id)
This is a typed class which will be generated by idlpp.
Definition: TS.java:25
HashSet< ConnectionDescription > getConfigurations()