OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
EntityImpl.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  */
21 package org.opensplice.dds.core;
22 
23 import java.util.EventListener;
24 import java.util.Set;
25 import java.util.concurrent.atomic.AtomicInteger;
26 
27 import org.omg.dds.core.EntityQos;
30 import org.omg.dds.core.status.Status;
33 
34 public abstract class EntityImpl<OLD extends DDS.Entity, OLDPARENT, QOS extends EntityQos<?>, LISTENER extends EventListener, LISTENERIMPL extends Listener<LISTENER>>
35  extends AbstractDDSObject implements org.omg.dds.core.Entity<LISTENER, QOS>, Properties {
36  protected final transient OsplServiceEnvironment environment;
37  private final OLDPARENT oldParent;
38  private OLD old;
39  protected LISTENERIMPL listener;
40  private AtomicInteger refCount;
41  private static java.util.HashMap<String, Integer> references = new java.util.HashMap<String, Integer>();
42 
43  private static boolean addReference(String name) {
44  synchronized (references) {
45  Integer refs = references.get(name);
46 
47  if (refs == null) {
48  references.put(name, 1);
49  } else {
50  references.put(name, refs.intValue() + 1);
51  }
52  }
53  return true;
54  }
55 
56  private static boolean removeReference(String name) {
57  synchronized (references) {
58  Integer refs = references.get(name);
59 
60  assert (refs != null);
61  references.put(name, refs.intValue() - 1);
62  }
63  return true;
64  }
65 
66  public static boolean printReferences() {
67  boolean noMoreRefs = true;
68 
69  synchronized (references) {
70  for (String name : references.keySet()) {
71  int count = references.get(name);
72  if (count != 0) {
73  noMoreRefs = false;
74  }
75  System.out.println(name + "=" + count);
76  }
77  if (!noMoreRefs) {
78  System.out.println("----------------");
79 
80  for (String name : references.keySet()) {
81  int count = references.get(name);
82  if (count != 0) {
83  noMoreRefs = false;
84  }
85  System.out.println(name + "=" + count);
86  }
87  System.out.println("----------------");
88  }
89  }
90  return noMoreRefs;
91  }
92 
93  public EntityImpl(OsplServiceEnvironment environment, OLDPARENT oldParent) {
94  this.environment = environment;
95  this.oldParent = oldParent;
96  this.listener = null;
97  this.refCount = new AtomicInteger(1);
98 
99  assert (addReference(this.getClass().getSimpleName()));
100  }
101 
102  public OLD getOld() {
103  final OLD old = this.old;
104 
105  if (old == null) {
106  throw new AlreadyClosedExceptionImpl(this.environment,
107  "Entity already closed.");
108  }
109  return old;
110  }
111 
112  protected void setOld(OLD old) {
113  this.old = old;
114  }
115 
116  public OLDPARENT getOldParent() {
117  return this.oldParent;
118  }
119 
120  @Override
122  return this.environment;
123  }
124 
125  @Override
126  public final void retain() {
127  int newValue = this.refCount.incrementAndGet();
128 
129  if (newValue <= 0) {
130  int refCount = this.refCount.decrementAndGet();
131  throw new AlreadyClosedExceptionImpl(this.environment,
132  "Entity already closed. refcount:" + refCount);
133  }
134  assert (newValue > newValue - 1);
135  assert (newValue > 1);
136  }
137 
138  @Override
139  public final void close() {
140  int newValue = this.refCount.decrementAndGet();
141 
142  if (newValue == 0) {
143  this.destroy();
144  this.old = null;
145  assert (removeReference(this.getClass().getSimpleName()));
146  } else if (newValue < 0) {
147  throw new AlreadyClosedExceptionImpl(this.environment,
148  "Entity already closed.");
149  }
150  }
151 
152  @Override
153  public final void enable() {
154  int rc = this.old.enable();
155  Utilities.checkReturnCode(rc, this.environment,
156  "Entity.enable() failed.");
157  }
158 
159  @Override
160  public final Set<Class<? extends Status>> getStatusChanges() {
161  return StatusConverter.convertMask(this.environment,
162  this.old.get_status_changes());
163  }
164 
165  @Override
167  return new InstanceHandleImpl(this.environment,
168  old.get_instance_handle());
169  }
170 
171  protected abstract void destroy();
172 
173  @Override
174  public final LISTENER getListener(){
175  if (this.listener != null) {
176  return this.listener.getRealListener();
177  }
178  return null;
179  }
180 
181  @Override
182  public void setProperty(String key, String value) {
183  int rc = this.old.set_property(new DDS.Property(key, value));
184  Utilities.checkReturnCode(rc, this.environment,
185  "Properties.setProperty() failed.");
186  }
187 
188  @Override
189  public String getProperty(String key) {
190  DDS.PropertyHolder holder = new DDS.PropertyHolder();
191  holder.value = new DDS.Property(key, null);
192  int rc = this.old.get_property(holder);
193  Utilities.checkReturnCode(rc, this.environment,
194  "Properties.getProperty() failed.");
195  return holder.value.value;
196  }
197 }
void setProperty(String key, String value)
This method sets the property specified by the key to the value given by the value.
final Set< Class<? extends Status > > getStatusChanges()
static Set< Class<? extends Status > > convertMask(OsplServiceEnvironment environment, int state)
An opaque handle that can be used to refer to a local or remote entity.
final InstanceHandle getInstanceHandle()
static void checkReturnCode(int retCode, OsplServiceEnvironment environment, String message)
Definition: Utilities.java:33
ServiceEnvironment getEnvironment()
This class is the abstract base class for all the DCPS objects that support QoS policies, a listener and a status condition.
Definition: Entity.java:36
String getProperty(String key)
Provides access to the current value for a given property.
EntityImpl(OsplServiceEnvironment environment, OLDPARENT oldParent)
Definition: EntityImpl.java:93
final transient OsplServiceEnvironment environment
Definition: EntityImpl.java:36
DDS implementations are rooted in this class, a concrete subclass of which can be instantiated based ...
The Data-Distribution Service (DDS) relies on the use of QoS.
Definition: EntityQos.java:49
Status is the abstract root class for all communication status objects.
Definition: Status.java:41
This type is implemented by all OpenSplice org.omg.dds.core.Entity classes to allow setting and getti...
Definition: Properties.java:31