OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
Utilities.java
Go to the documentation of this file.
1 /*
2  * Vortex OpenSplice
3  *
4  * This software and documentation are Copyright 2006 to 2024 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.concurrent.TimeoutException;
24 
25 import org.omg.dds.core.Duration;
27 import org.omg.dds.core.Time;
28 
29 import DDS.ErrorInfo;
30 
31 public class Utilities {
32 
33  public static void checkReturnCode(int retCode,
34  OsplServiceEnvironment environment, String message) {
35  try {
36  checkReturnCode(retCode, environment, message, false);
37  } catch (TimeOutExceptionImpl t) {
38  throw new DDSExceptionImpl(environment,
39  "Internal error: TimeOutException caught in unexpected location.");
40  }
41  }
42 
43  private static void checkReturnCode(int retCode,
44  OsplServiceEnvironment environment, String message,
45  boolean withTimeOut) throws TimeOutExceptionImpl {
46  switch (retCode) {
47  case DDS.RETCODE_PRECONDITION_NOT_MET.value:
48  throw new PreconditionNotMetExceptionImpl(environment,
49  getErrorMessage(retCode, message));
50  case DDS.RETCODE_OUT_OF_RESOURCES.value:
51  throw new OutOfResourcesExceptionImpl(environment, getErrorMessage(
52  retCode, message));
53  case DDS.RETCODE_ALREADY_DELETED.value:
54  throw new AlreadyClosedExceptionImpl(environment, getErrorMessage(
55  retCode, message));
56  case DDS.RETCODE_BAD_PARAMETER.value:
57  throw new IllegalArgumentExceptionImpl(environment,
58  getErrorMessage(retCode, message));
59  case DDS.RETCODE_ERROR.value:
60  throw new DDSExceptionImpl(environment, getErrorMessage(retCode,
61  message));
62  case DDS.RETCODE_ILLEGAL_OPERATION.value:
63  throw new IllegalOperationExceptionImpl(environment,
64  getErrorMessage(retCode, message));
65  case DDS.RETCODE_IMMUTABLE_POLICY.value:
66  throw new ImmutablePolicyExceptionImpl(environment,
67  getErrorMessage(retCode, message));
68  case DDS.RETCODE_INCONSISTENT_POLICY.value:
69  throw new InconsistentPolicyExceptionImpl(environment,
70  getErrorMessage(retCode, message));
71  case DDS.RETCODE_NOT_ENABLED.value:
72  throw new NotEnabledExceptionImpl(environment, getErrorMessage(
73  retCode, message));
74  case DDS.RETCODE_UNSUPPORTED.value:
75  throw new UnsupportedOperationExceptionImpl(environment,
76  getErrorMessage(retCode, message));
77  case DDS.RETCODE_TIMEOUT.value:
78  if (withTimeOut) {
79  if (retCode == DDS.RETCODE_TIMEOUT.value) {
80  throw new TimeOutExceptionImpl(environment,
81  getErrorMessage(retCode, message));
82  }
83  }
84  case DDS.RETCODE_OK.value:
85  case DDS.RETCODE_NO_DATA.value:
86  default:
87  break;
88  }
89  }
90 
91  private static String getDetails(DDS.ErrorInfo errorInfo, String message) {
92  String result = "";
93  DDS.StringHolder messageHolder = new DDS.StringHolder();
94 
95  errorInfo.get_message(messageHolder);
96 
97  if (messageHolder.value != null) {
98  if (message != null) {
99  result += message;
100  result += "(" + messageHolder.value + ")";
101  } else {
102  result += messageHolder.value;
103  }
104  } else if (message != null) {
105  result += message;
106  }
107  errorInfo.get_location(messageHolder);
108 
109  if (messageHolder.value != null) {
110  result += " at " + messageHolder.value;
111  }
112  errorInfo.get_source_line(messageHolder);
113 
114  if (messageHolder.value != null) {
115  result += " (" + messageHolder.value + ")";
116  }
117  errorInfo.get_stack_trace(messageHolder);
118 
119  if (messageHolder.value != null) {
120  result += "\nStack trace:\n" + messageHolder.value;
121  }
122  return result;
123  }
124 
125  private static String getErrorMessage(int retCode, String message) {
126  String output;
127  DDS.ErrorInfo errorInfo = new ErrorInfo();
128  int result = errorInfo.update();
129 
130  switch (result) {
131  case DDS.RETCODE_NO_DATA.value:
132  output = message;
133  break;
134  case DDS.RETCODE_OK.value:
135  output = getDetails(errorInfo, message);
136  break;
137  default:
138  if (message != null) {
139  output = " Unable to get extra error information due to internal error.("
140  + message + ")";
141  } else {
142  output = " Unable to get extra error information due to internal error.";
143  }
144  break;
145  }
146  return output;
147  }
148 
149  public static String getOsplExceptionStack(Exception ex,
150  StackTraceElement[] stack) {
151  StringBuffer result = new StringBuffer();
152  int startIndex = 0;
153 
154  result.append(ex.getClass().getSuperclass().getName() + ": "
155  + ex.getMessage() + "\n");
156 
157  while ((stack.length > startIndex)
158  && (Utilities.class.getName().equals(stack[startIndex]
159  .getClassName()))) {
160  startIndex++;
161  }
162  for (int i = startIndex; i < stack.length; i++) {
163  result.append("\tat ");
164  result.append(stack[i].getClassName());
165  result.append(".");
166  result.append(stack[i].getMethodName());
167  result.append(" (");
168  result.append(stack[i].getFileName());
169  result.append(":");
170  result.append(stack[i].getLineNumber());
171  result.append(")\n");
172  }
173  return result.toString();
174  }
175 
176  public static void checkReturnCodeWithTimeout(int retCode,
177  OsplServiceEnvironment environment, String message)
178  throws TimeoutException {
179  checkReturnCode(retCode, environment, message, true);
180  }
181 
182  public static void throwLastErrorException(
183  OsplServiceEnvironment environment) {
184  String message;
185  int code;
186  DDS.ErrorInfo errorInfo = new ErrorInfo();
187  int result = errorInfo.update();
188 
189  switch (result) {
190  case DDS.RETCODE_NO_DATA.value:
191  message = "";
192  code = DDS.RETCODE_ERROR.value;
193  break;
194  case DDS.RETCODE_OK.value:
195  DDS.ReturnCodeHolder errorHolder = new DDS.ReturnCodeHolder();
196  errorInfo.get_code(errorHolder);
197  code = errorHolder.value;
198  message = getDetails(errorInfo, null);
199  break;
200  default:
201  message = "Unable to get extra error information due to internal error.";
202  code = DDS.RETCODE_ERROR.value;
203  }
204 
205  switch (code) {
206  case DDS.RETCODE_PRECONDITION_NOT_MET.value:
207  throw new PreconditionNotMetExceptionImpl(environment, message);
208  case DDS.RETCODE_OUT_OF_RESOURCES.value:
209  throw new OutOfResourcesExceptionImpl(environment, message);
210  case DDS.RETCODE_ALREADY_DELETED.value:
211  throw new AlreadyClosedExceptionImpl(environment, message);
212  case DDS.RETCODE_BAD_PARAMETER.value:
213  throw new IllegalArgumentExceptionImpl(environment, message);
214  case DDS.RETCODE_ILLEGAL_OPERATION.value:
215  throw new IllegalOperationExceptionImpl(environment, message);
216  case DDS.RETCODE_IMMUTABLE_POLICY.value:
217  throw new ImmutablePolicyExceptionImpl(environment, message);
218  case DDS.RETCODE_INCONSISTENT_POLICY.value:
219  throw new InconsistentPolicyExceptionImpl(environment, message);
220  case DDS.RETCODE_NOT_ENABLED.value:
221  throw new NotEnabledExceptionImpl(environment, message);
222  case DDS.RETCODE_UNSUPPORTED.value:
223  throw new UnsupportedOperationExceptionImpl(environment, message);
224  case DDS.RETCODE_ERROR.value:
225  default:
226  throw new DDSExceptionImpl(environment, message);
227 
228  }
229 
230  }
231 
232  public static DDS.Duration_t convert(OsplServiceEnvironment environment,
233  Duration d) {
234  if (d == null) {
235  throw new IllegalArgumentExceptionImpl(environment,
236  "Illegal Duration provided (null).");
237  }
238  try {
239  return ((DurationImpl) d).convert();
240  } catch (ClassCastException e) {
241  throw new IllegalArgumentExceptionImpl(environment,
242  "Usage of non-OpenSplice Duration implementation is not supported.");
243  }
244  }
245 
246  public static Duration convert(OsplServiceEnvironment env, DDS.Duration_t d) {
247  return new DurationImpl(env, d.sec, d.nanosec);
248  }
249 
250  public static long convert(OsplServiceEnvironment environment,
251  InstanceHandle h) {
252  if (h == null) {
253  throw new IllegalArgumentExceptionImpl(environment,
254  "Illegal InstanceHandle provided (null).");
255  }
256  try {
257  return ((InstanceHandleImpl) h).getValue();
258  } catch (ClassCastException e) {
259  throw new IllegalArgumentExceptionImpl(environment,
260  "Usage of non-OpenSplice InstanceHandle implementation is not supported.");
261  }
262  }
263 
264  public static InstanceHandle convert(OsplServiceEnvironment env, long handle) {
265  return new InstanceHandleImpl(env, handle);
266  }
267 
268  public static DDS.Time_t convert(OsplServiceEnvironment environment, Time t) {
269  if (t == null) {
270  throw new IllegalArgumentExceptionImpl(environment,
271  "Illegal Time provided (null).");
272  }
273  try {
274  return ((ModifiableTimeImpl) t).convert();
275  } catch (ClassCastException e) {
276  throw new IllegalArgumentExceptionImpl(environment,
277  "Usage of non-OpenSplice Time implementation is not supported.");
278  }
279  }
280 
281  public static Time convert(OsplServiceEnvironment env, DDS.Time_t t) {
282  return new TimeImpl(env, t.sec, t.nanosec);
283  }
284 }
static InstanceHandle convert(OsplServiceEnvironment env, long handle)
Definition: Utilities.java:264
static void checkReturnCodeWithTimeout(int retCode, OsplServiceEnvironment environment, String message)
Definition: Utilities.java:176
static Time convert(OsplServiceEnvironment env, DDS.Time_t t)
Definition: Utilities.java:281
static String getOsplExceptionStack(Exception ex, StackTraceElement[] stack)
Definition: Utilities.java:149
static DDS.Duration_t convert(OsplServiceEnvironment environment, Duration d)
Definition: Utilities.java:232
static Duration convert(OsplServiceEnvironment env, DDS.Duration_t d)
Definition: Utilities.java:246
An opaque handle that can be used to refer to a local or remote entity.
static void checkReturnCode(int retCode, OsplServiceEnvironment environment, String message)
Definition: Utilities.java:33
static long convert(OsplServiceEnvironment environment, InstanceHandle h)
Definition: Utilities.java:250
static DDS.Time_t convert(OsplServiceEnvironment environment, Time t)
Definition: Utilities.java:268
A span of elapsed time expressed with nanosecond precision.
Definition: Duration.java:35
static void throwLastErrorException(OsplServiceEnvironment environment)
Definition: Utilities.java:182
A moment in time expressed with nanosecond precision (though not necessarily nanosecond accuracy)...
Definition: Time.java:34