Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c9006f55528a85361b50e5b0f79b8502c77a4ca4 (plain) (blame)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*******************************************************************************
 * Copyright (c) 2012 Juergen Haug
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * CONTRIBUTORS:
 * 		Juergen Haug
 * 
 *******************************************************************************/

package org.eclipse.etrice.runtime.java.config;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public abstract class AbstractVariableService implements IVariableService {

	private final static String POLLINGTIMER_KEY = "polling_timer[ms]";
	private final static int defaultPollingTimer = 5000;
	private final boolean logging = true;

	private volatile int pollingTimer;
	private PollingThread thread;
	protected Map<String, Object> writeTasks;
	private Map<String, Object> diffMap;

	private IConfigSource source;

	public AbstractVariableService(IConfigSource source) {
		this.source = source;
		diffMap = new HashMap<String, Object>();
		writeTasks = new HashMap<String, Object>();
	}

	public void init() {
		initInstances();
		Map<String, Object> initalValues = source.readValues();
		if (setPolling(initalValues.get(POLLINGTIMER_KEY))) {
		} else if (getPollingTimerUser() > 0)
			this.pollingTimer = getPollingTimerUser();
		else if (source.getPollingTimer() > 0)
			this.pollingTimer = source.getPollingTimer();
		else
			this.pollingTimer = defaultPollingTimer;
		setAttributeValues(initalValues);
		Map<String, Object> initValues = getAttributeValues();
		initValues.put(POLLINGTIMER_KEY, pollingTimer);
		source.createConfig(initValues);

		start();
	}

	public synchronized void start() {
		if (thread != null)
			return;

		thread = new PollingThread();
		thread.start();
	}

	public synchronized void stop() {
		if (thread == null)
			return;

		thread.stop = true;
		thread.interrupt();
	}

	public void write(String id, Object object) {
		if (PRIMITIVE_TYPES.contains(object.getClass()))
			addWriteTask(id, object);
		else if (object.getClass().isArray()) {
			if (object instanceof boolean[])
				addWriteTask(id, toObjectArray((boolean[]) object));
			else if (object instanceof byte[])
				addWriteTask(id, toObjectArray((byte[]) object));
			else if (object instanceof short[])
				addWriteTask(id, toObjectArray((short[]) object));
			else if (object instanceof int[])
				addWriteTask(id, toObjectArray((int[]) object));
			else if (object instanceof long[])
				addWriteTask(id, toObjectArray((long[]) object));
			else if (object instanceof float[])
				addWriteTask(id, toObjectArray((float[]) object));
			else if (object instanceof double[])
				addWriteTask(id, toObjectArray((double[]) object));
			else if (object instanceof char[])
				addWriteTask(id, toObjectArray((char[]) object));
			else if (object instanceof String[])
				addWriteTask(id, object);
		} else {
			Map<String, Object> writeTasks = new HashMap<String, Object>();
			writeDataClass(id, object, writeTasks);
			addWriteTasks(writeTasks);
		}
	}

	private class PollingThread extends Thread {

		private boolean stop = false;

		@Override
		public void run() {

			int diff;
			long time;
			while (!stop) {
				Map<String, Object> values = source.readValues();
				time = System.currentTimeMillis();
				if (values != null) {
					apply(values);
				}
				write();
				diff = (int) (System.currentTimeMillis() - time);

				try {
					sleep(pollingTimer - ((diff > pollingTimer) ? 0 : diff));
				} catch (InterruptedException e) {
				}
			}
			write();
			source.close();
		}

		private void apply(Map<String, Object> values) {
			Object pollingValue = values.remove(POLLINGTIMER_KEY);
			setPolling(pollingValue);
			if (values.isEmpty())
				return;
			setAttributeValues(values);
		}

		private void write() {
			HashMap<String, Object> writeMap = new HashMap<String, Object>();
			synchronized (writeTasks) {
				writeMap.putAll(writeTasks);
				writeTasks.clear();
			}
			if (!writeMap.isEmpty())
				source.writeValues(writeMap);
		}
	}

	private boolean setPolling(Object value) {
		if (value != null) {
			try {
				int result = ensureInt(value);
				if (result > 0) {
					pollingTimer = result;
					return true;
				}
			} catch (IllegalArgumentException e) {
				error("polling rate", e);
			}
		}

		return false;
	}

	protected Map<String, Object> getDiffMap() {
		return diffMap;
	}

	protected void addWriteTask(String id, Object value) {
		synchronized (writeTasks) {
			writeTasks.put(id, value);
		}
	}

	protected void addWriteTasks(Map<String, Object> values) {
		synchronized (writeTasks) {
			writeTasks.putAll(values);
		}
	}

	protected abstract void initInstances();

	protected abstract void setAttributeValues(Map<String, Object> values);

	protected abstract int getPollingTimerUser();

	/**
	 * called on init
	 */
	protected abstract Map<String, Object> getAttributeValues();

	protected abstract void writeDataClass(String id, Object dcObject,
			Map<String, Object> writeMap);

	protected void checkMinMax(int value, Integer min, Integer max)
			throws IllegalArgumentException {
		if (min != null && value < min)
			throw new NumberFormatException("Value out of range. Value="
					+ value + " < " + min);
		if (max != null && value > max)
			throw new NumberFormatException("Value out of range. Value="
					+ value + " > " + max);
	}

	protected void checkMinMax(long value, Long min, Long max)
			throws IllegalArgumentException {
		if (min != null && value < min)
			throw new NumberFormatException("Value out of range. Value="
					+ value + " < " + min);
		if (max != null && value > max)
			throw new NumberFormatException("Value out of range. Value="
					+ value + " > " + max);
	}

	protected void checkMinMax(double value, Double min, Double max)
			throws IllegalArgumentException {
		if (min != null && value < min)
			throw new NumberFormatException("Value out of range. Value="
					+ value + " < " + min);
		if (max != null && value > max)
			throw new NumberFormatException("Value out of range. Value="
					+ value + " > " + max);
	}

	protected boolean ensureBoolean(Object value) {
		if (value instanceof Boolean)
			return (Boolean) value;

		return Boolean.parseBoolean(value.toString());
	}

	protected boolean[] ensureBooleanArray(Object value, int length)
			throws IllegalArgumentException {
		boolean[] result = null;
		if (value instanceof boolean[])
			result = (boolean[]) value;
		else if (value instanceof Boolean[]) {
			Boolean[] values = (Boolean[]) value;
			result = new boolean[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = values[i];
		} else if (value instanceof String[]) {
			String[] values = (String[]) value;
			result = new boolean[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = ensureBoolean(values[i]);
		} else
			throw new IllegalArgumentException("value of unknown type: "
					+ value.getClass());

		if (result.length == length)
			return result;
		throw new IllegalArgumentException("array length must be " + length
				+ ", but was " + result.length);
	}

	protected Boolean[] toObjectArray(boolean[] array) {
		Boolean[] objects = new Boolean[array.length];
		for (int i = 0; i < array.length; i++)
			objects[i] = array[i];

		return objects;
	}

	protected byte ensureByte(Object value) throws IllegalArgumentException {
		if (value instanceof Byte)
			return (Byte) value;

		return Byte.parseByte(value.toString());
	}

	protected byte[] ensureByteArray(Object value, int length)
			throws IllegalArgumentException {
		byte[] result = null;
		if (value instanceof byte[])
			result = (byte[]) value;
		else if (value instanceof Byte[]) {
			Byte[] values = (Byte[]) value;
			result = new byte[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = values[i];
		} else if (value instanceof String[]) {
			String[] values = (String[]) value;
			result = new byte[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = ensureByte(values[i]);
		} else
			throw new IllegalArgumentException("value of unknown type: "
					+ value.getClass());

		if (result.length == length)
			return result;
		throw new IllegalArgumentException("array length must be " + length
				+ ", but was " + result.length);
	}

	protected Byte[] toObjectArray(byte[] array) {
		Byte[] objects = new Byte[array.length];
		for (int i = 0; i < array.length; i++)
			objects[i] = array[i];

		return objects;
	}

	protected short ensureShort(Object value) throws IllegalArgumentException {
		if (value instanceof Short)
			return (Short) value;

		return Short.parseShort(value.toString());
	}

	protected short[] ensureShortArray(Object value, int length)
			throws IllegalArgumentException {
		short[] result = null;
		if (value instanceof short[])
			result = (short[]) value;
		else if (value instanceof Short[]) {
			Short[] values = (Short[]) value;
			result = new short[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = values[i];
		} else if (value instanceof String[]) {
			String[] values = (String[]) value;
			result = new short[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = ensureShort(values[i]);
		} else
			throw new IllegalArgumentException("value of unknown type: "
					+ value.getClass());

		if (result.length == length)
			return result;
		throw new IllegalArgumentException("array length must be " + length
				+ ", but was " + result.length);
	}

	protected Short[] toObjectArray(short[] array) {
		Short[] objects = new Short[array.length];
		for (int i = 0; i < array.length; i++)
			objects[i] = array[i];

		return objects;
	}

	protected int ensureInt(Object value) throws IllegalArgumentException {
		if (value instanceof Integer)
			return (Integer) value;

		return Integer.parseInt(value.toString());
	}

	protected int[] ensureIntArray(Object value, int length)
			throws IllegalArgumentException {
		int[] result = null;
		if (value instanceof int[])
			result = (int[]) value;
		else if (value instanceof Integer[]) {
			Integer[] values = (Integer[]) value;
			result = new int[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = values[i];
		} else if (value instanceof String[]) {
			String[] values = (String[]) value;
			result = new int[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = ensureInt(values[i]);
		} else
			throw new IllegalArgumentException("value of unknown type: "
					+ value.getClass());

		if (result.length == length)
			return result;
		throw new IllegalArgumentException("array length must be " + length
				+ ", but was " + result.length);
	}

	protected Integer[] toObjectArray(int[] array) {
		Integer[] objects = new Integer[array.length];
		for (int i = 0; i < array.length; i++)
			objects[i] = array[i];

		return objects;
	}

	protected long ensureLong(Object value) throws IllegalArgumentException {
		if (value instanceof Long)
			return (Long) value;

		return Long.parseLong(value.toString());
	}

	protected long[] ensureLongArray(Object value, int length)
			throws IllegalArgumentException {
		long[] result = null;
		if (value instanceof long[])
			result = (long[]) value;
		else if (value instanceof Long[]) {
			Long[] values = (Long[]) value;
			result = new long[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = values[i];
		} else if (value instanceof String[]) {
			String[] values = (String[]) value;
			result = new long[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = ensureLong(values[i]);
		} else
			throw new IllegalArgumentException("value of unknown type: "
					+ value.getClass());

		if (result.length == length)
			return result;
		throw new IllegalArgumentException("array length must be " + length
				+ ", but was " + result.length);
	}

	protected Long[] toObjectArray(long[] array) {
		Long[] objects = new Long[array.length];
		for (int i = 0; i < array.length; i++)
			objects[i] = array[i];

		return objects;
	}

	protected float ensureFloat(Object value) throws IllegalArgumentException {
		if (value instanceof Float)
			return (Float) value;

		return Float.parseFloat(value.toString());
	}

	protected float[] ensureFloatArray(Object value, int length)
			throws IllegalArgumentException {
		float[] result = null;
		if (value instanceof float[])
			result = (float[]) value;
		else if (value instanceof Float[]) {
			Float[] values = (Float[]) value;
			result = new float[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = values[i];
		} else if (value instanceof String[]) {
			String[] values = (String[]) value;
			result = new float[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = ensureFloat(values[i]);
		} else
			throw new IllegalArgumentException("value of unknown type: "
					+ value.getClass());

		if (result.length == length)
			return result;
		throw new IllegalArgumentException("array length must be " + length
				+ ", but was " + result.length);
	}

	protected Float[] toObjectArray(float[] array) {
		Float[] objects = new Float[array.length];
		for (int i = 0; i < array.length; i++)
			objects[i] = array[i];

		return objects;
	}

	protected double ensureDouble(Object value) throws IllegalArgumentException {
		if (value instanceof Double)
			return (Double) value;

		return Double.parseDouble(value.toString());
	}

	protected double[] ensureDoubleArray(Object value, int length)
			throws IllegalArgumentException {
		double[] result = null;
		if (value instanceof double[])
			result = (double[]) value;
		else if (value instanceof Double[]) {
			Double[] values = (Double[]) value;
			result = new double[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = values[i];
		} else if (value instanceof String[]) {
			String[] values = (String[]) value;
			result = new double[values.length];
			for (int i = 0; i < values.length; i++)
				result[i] = ensureDouble(values[i]);
		} else
			throw new IllegalArgumentException("value of unknown type: "
					+ value.getClass());

		if (result.length == length)
			return result;
		throw new IllegalArgumentException("array length must be " + length
				+ ", but was " + result.length);
	}

	protected Double[] toObjectArray(double[] array) {
		Double[] objects = new Double[array.length];
		for (int i = 0; i < array.length; i++)
			objects[i] = array[i];

		return objects;
	}

	protected char ensureChar(Object value) throws IllegalArgumentException {
		if (value instanceof Character)
			return (Character) value;

		if (value instanceof String) {
			String s = value.toString();
			if (s.length() == 1)
				return s.charAt(0);
		}

		throw new IllegalArgumentException();
	}

	protected char[] ensureCharArray(Object value, int length)
			throws IllegalArgumentException {
		char[] result = null;
		if (value instanceof char[])
			result = (char[]) value;
		else if (value instanceof String)
			result = ((String) value).toCharArray();
		else
			throw new IllegalArgumentException("value of unknown type: "
					+ value.getClass());

		if (result.length <= length)
			return result;
		throw new IllegalArgumentException("array length must be <=" + length
				+ ", but was " + result.length);
	}

	protected String toObjectArray(char[] array) {
		return String.valueOf(array);
	}

	protected String ensureString(Object value) throws IllegalArgumentException {
		if (value instanceof String)
			return (String) value;

		throw new IllegalArgumentException("value of unknown type: "
				+ value.getClass());
	}

	protected void error(String id, Exception e) {
		if (logging)
			System.out.println("[" + this.getClass().getSimpleName()
					+ "] ERROR: " + e.getMessage()
					+ ((id != null) ? "   (" + id + ")" : ""));
	}

	protected void warning(String id, String message) {
		if (logging)
			System.out.println("[" + this.getClass().getSimpleName()
					+ "] Warning: " + message
					+ ((id != null) ? "   (" + id + ")" : ""));
	}

	@SuppressWarnings({ "rawtypes" })
	private static final Set<Class> PRIMITIVE_TYPES = new HashSet<Class>(
			Arrays.asList(Boolean.class, Character.class, String.class,
					Byte.class, Short.class, Integer.class, Long.class,
					Float.class, Double.class));

}

Back to the top