1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.metricshub.wbem.sblim.cimclient.internal.cim;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 import java.math.BigInteger;
49 import java.net.URI;
50 import java.net.URISyntaxException;
51 import org.metricshub.wbem.javax.cim.CIMDataType;
52 import org.metricshub.wbem.javax.cim.CIMInstance;
53 import org.metricshub.wbem.javax.cim.CIMObjectPath;
54 import org.metricshub.wbem.javax.cim.CIMProperty;
55 import org.metricshub.wbem.javax.cim.UnsignedInteger16;
56 import org.metricshub.wbem.javax.cim.UnsignedInteger32;
57 import org.metricshub.wbem.javax.cim.UnsignedInteger64;
58 import org.metricshub.wbem.javax.cim.UnsignedInteger8;
59 import org.metricshub.wbem.sblim.cimclient.internal.util.WBEMConstants;
60
61
62
63
64
65
66 public abstract class CIMHelper {
67
68 private CIMHelper() {
69
70 }
71
72
73
74
75
76
77
78
79
80
81 public static URI createCimomUri(CIMObjectPath pPath) throws URISyntaxException {
82 String scheme = pPath.getScheme();
83 String host = pPath.getHost();
84 int port = WBEMConstants.DEFAULT_WBEM_PORT;
85 try {
86 port = Integer.parseInt(pPath.getPort());
87 } catch (NumberFormatException e) {
88
89 }
90 return new URI(scheme, null, host, port, WBEMConstants.CIMOM_PATH, null, null);
91 }
92
93
94
95
96
97
98
99
100
101
102 public static URI createCimomUri(URI pUri) throws URISyntaxException {
103 String scheme = pUri.getScheme();
104 String host = pUri.getHost();
105 int port = pUri.getPort();
106 if (port == -1) {
107
108 port = WBEMConstants.DEFAULT_WBEM_PORT;
109 }
110 return new URI(scheme, null, host, port, WBEMConstants.CIMOM_PATH, null, null);
111 }
112
113 private static CIMDataType CIMScalarDataTypes[] = {
114 CIMDataType.UINT8_T,
115 CIMDataType.SINT8_T,
116 CIMDataType.UINT16_T,
117 CIMDataType.SINT16_T,
118 CIMDataType.UINT32_T,
119 CIMDataType.SINT32_T,
120 CIMDataType.UINT64_T,
121 CIMDataType.SINT64_T,
122 CIMDataType.STRING_T,
123 CIMDataType.BOOLEAN_T,
124 CIMDataType.REAL32_T,
125 CIMDataType.REAL64_T,
126 CIMDataType.DATETIME_T,
127 CIMDataType.CHAR16_T,
128 new CIMDataType(""),
129 CIMDataType.OBJECT_T,
130 null,
131 CIMDataType.CLASS_T
132 };
133
134
135
136
137
138
139
140
141
142
143 public static CIMDataType ScalarDataType(int pType) {
144 if (pType < 0 || pType >= CIMScalarDataTypes.length) return null;
145 return CIMScalarDataTypes[pType];
146 }
147
148 private static CIMDataType CIMArrayDataTypes[] = {
149 CIMDataType.UINT8_ARRAY_T,
150 CIMDataType.SINT8_ARRAY_T,
151 CIMDataType.UINT16_ARRAY_T,
152 CIMDataType.SINT16_ARRAY_T,
153 CIMDataType.UINT32_ARRAY_T,
154 CIMDataType.SINT32_ARRAY_T,
155 CIMDataType.UINT64_ARRAY_T,
156 CIMDataType.SINT64_ARRAY_T,
157 CIMDataType.STRING_ARRAY_T,
158 CIMDataType.BOOLEAN_ARRAY_T,
159 CIMDataType.REAL32_ARRAY_T,
160 CIMDataType.REAL64_ARRAY_T,
161 CIMDataType.DATETIME_ARRAY_T,
162 CIMDataType.CHAR16_ARRAY_T,
163 new CIMDataType("", 0),
164 CIMDataType.OBJECT_ARRAY_T,
165 null,
166 CIMDataType.CLASS_ARRAY_T
167 };
168
169
170
171
172
173
174
175
176
177
178 public static CIMDataType UnboundedArrayDataType(int pType) {
179 if (pType < 0 || pType >= CIMArrayDataTypes.length) return null;
180 return CIMArrayDataTypes[pType];
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public static CIMInstance CIMInstanceWithSynchonizedNumericKeyDataTypes(
204 CIMObjectPath pObjectPath,
205 CIMProperty<?>[] pProps
206 ) {
207 CIMInstance inst = new CIMInstance(pObjectPath, pProps);
208 CIMProperty<?>[] oldKeys = inst.getKeys();
209 CIMProperty<?>[] newKeys = new CIMProperty<?>[oldKeys.length];
210 boolean update = false;
211
212 for (int i = 0; i < oldKeys.length; i++) {
213 CIMDataType oldType = oldKeys[i].getDataType();
214 CIMProperty<?> prop = inst.getProperty(oldKeys[i].getName());
215 if (
216 oldType != null &&
217 prop != null &&
218 prop.getDataType() != null &&
219 !prop.getDataType().equals(oldType) &&
220 isNumericObject(oldType) &&
221 isNumericObject(prop.getDataType())
222 ) {
223 update = true;
224 newKeys[i] =
225 new CIMProperty<Object>(
226 oldKeys[i].getName(),
227 prop.getDataType(),
228 translateNumericObject(oldKeys[i].getValue(), oldType, prop.getDataType()),
229 oldKeys[i].isKey(),
230 oldKeys[i].isPropagated(),
231 oldKeys[i].getOriginClass()
232 );
233 } else {
234 newKeys[i] = oldKeys[i];
235 }
236 }
237
238 return (
239 update
240 ? inst.deriveInstance(
241 new CIMObjectPath(
242 pObjectPath.getScheme(),
243 pObjectPath.getHost(),
244 pObjectPath.getPort(),
245 pObjectPath.getNamespace(),
246 pObjectPath.getObjectName(),
247 newKeys
248 )
249 )
250 : inst
251 );
252 }
253
254 private static boolean isNumericObject(CIMDataType type) {
255 switch (type.getType()) {
256 case CIMDataType.SINT8:
257 case CIMDataType.SINT16:
258 case CIMDataType.SINT32:
259 case CIMDataType.SINT64:
260 case CIMDataType.UINT8:
261 case CIMDataType.UINT16:
262 case CIMDataType.UINT32:
263 case CIMDataType.UINT64:
264 case CIMDataType.REAL32:
265 case CIMDataType.REAL64:
266 return true;
267 }
268 return false;
269 }
270
271 private static Object translateNumericObject(Object oldValue, CIMDataType oldType, CIMDataType newType) {
272 if (oldValue == null) return null;
273
274 int from = oldType.getType(), to = newType.getType();
275 long newInt = 0;
276 double newDec = 0;
277 Object o = null;
278 boolean useInt = true;
279
280 switch (from) {
281 case CIMDataType.SINT8:
282 Byte b = (Byte) oldValue;
283 newInt = b.longValue();
284 break;
285 case CIMDataType.SINT16:
286 Short s = (Short) oldValue;
287 newInt = s.longValue();
288 break;
289 case CIMDataType.SINT32:
290 Integer i = (Integer) oldValue;
291 newInt = i.longValue();
292 break;
293 case CIMDataType.SINT64:
294 Long l = (Long) oldValue;
295 newInt = l.longValue();
296 break;
297 case CIMDataType.UINT8:
298 UnsignedInteger8 u8 = (UnsignedInteger8) oldValue;
299 newInt = u8.longValue();
300 break;
301 case CIMDataType.UINT16:
302 UnsignedInteger16 u16 = (UnsignedInteger16) oldValue;
303 newInt = u16.longValue();
304 break;
305 case CIMDataType.UINT32:
306 UnsignedInteger32 u32 = (UnsignedInteger32) oldValue;
307 newInt = u32.longValue();
308 break;
309 case CIMDataType.UINT64:
310 UnsignedInteger64 u64 = (UnsignedInteger64) oldValue;
311 newInt = u64.longValue();
312 break;
313 case CIMDataType.REAL32:
314 Float f = (Float) oldValue;
315 newDec = f.doubleValue();
316 useInt = false;
317 break;
318 case CIMDataType.REAL64:
319 Double d = (Double) oldValue;
320 newDec = d.doubleValue();
321 useInt = false;
322 break;
323 }
324
325 switch (to) {
326 case CIMDataType.SINT8:
327 byte b = (byte) (useInt ? newInt : newDec);
328 o = new Byte(b);
329 break;
330 case CIMDataType.SINT16:
331 short s = (short) (useInt ? newInt : newDec);
332 o = new Short(s);
333 break;
334 case CIMDataType.SINT32:
335 int i = (int) (useInt ? newInt : newDec);
336 o = new Integer(i);
337 break;
338 case CIMDataType.SINT64:
339 long l = (long) (useInt ? newInt : newDec);
340 o = new Long(l);
341 break;
342 case CIMDataType.UINT8:
343 byte u8 = (byte) (useInt ? newInt : newDec);
344 o = new UnsignedInteger8(u8);
345 break;
346 case CIMDataType.UINT16:
347 short u16 = (short) (useInt ? newInt : newDec);
348 o = new UnsignedInteger16(u16);
349 break;
350 case CIMDataType.UINT32:
351 int u32 = (int) (useInt ? newInt : newDec);
352 o = new UnsignedInteger32(u32);
353 break;
354 case CIMDataType.UINT64:
355 long u64 = (long) (useInt ? newInt : newDec);
356 o = new UnsignedInteger64(BigInteger.valueOf(u64));
357 break;
358 case CIMDataType.REAL32:
359 float f = (float) (useInt ? newInt : newDec);
360 o = new Float(f);
361 break;
362 case CIMDataType.REAL64:
363 double d = useInt ? (double) newInt : newDec;
364 o = new Double(d);
365 break;
366 }
367
368 return o;
369 }
370 }