use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.
the class PollingDataSource method joinTermination.
@Override
public void joinTermination() {
super.joinTermination();
if (jobThread == null)
return;
terminationLock = new Object();
int tries = 10;
while (true) {
synchronized (terminationLock) {
Thread localThread = jobThread;
if (localThread == null)
break;
try {
terminationLock.wait(30000);
} catch (InterruptedException e) {
// no op
}
if (jobThread != null) {
if (tries-- > 0)
LOG.warn("Waiting for data source to stop: id=" + getId() + ", type=" + getClass());
else
throw new ShouldNeverHappenException("Timeout waiting for data source to stop: id=" + getId() + ", type=" + getClass() + ", stackTrace=" + Arrays.toString(localThread.getStackTrace()));
}
}
}
}
use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.
the class ImageValue method getDigest.
public byte[] getDigest() {
if (digest == null) {
try {
MessageDigest digester = MessageDigest.getInstance("MD5");
byte[] data;
if (this.data != null)
data = this.data;
else
data = getImageData();
digester.update(data);
digester.update((byte) (data.length >> 24));
digester.update((byte) (data.length >> 16));
digester.update((byte) (data.length >> 8));
digester.update((byte) data.length);
digest = digester.digest();
} catch (NoSuchAlgorithmException e) {
LOG.error(e.getMessage(), e);
throw new ShouldNeverHappenException("Digest type MD5 not supported");
} catch (IOException e) {
LOG.error("Failed to load image data for computing image value digest: " + e.getMessage(), e);
return new byte[0];
}
}
return digest;
}
use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.
the class StringEncrypter method hashEncrypt.
public static synchronized byte[] hashEncrypt(byte[] bytes, String algorithm) {
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(bytes);
return md.digest();
} catch (NoSuchAlgorithmException e) {
// Should never happen, so just wrap in a runtime exception and rethrow
throw new ShouldNeverHappenException(e);
}
}
use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.
the class Functions method getConstant.
public static Object getConstant(String className, String fieldName) {
try {
Class<?> clazz = Class.forName(className);
Field field = clazz.getField(fieldName);
return field.get(clazz);
} catch (Exception e) {
throw new ShouldNeverHappenException(e);
}
}
use of com.serotonin.ShouldNeverHappenException in project ma-modules-public by infiniteautomation.
the class IdPointValueStatisticsQuantizerJsonCallback method closePeriod.
/*
* (non-Javadoc)
*
* @see
* com.serotonin.m2m2.web.mvc.rest.v1.statistics.ParentStatisticsQuantizerCallback#closePeriod(
* java.util.Map, long)
*/
@Override
public void closePeriod(Map<Integer, StatisticsGenerator> periodStatsMap, long periodStartTime) {
if (limiter.limited())
return;
try {
// Write out the period start time
this.jgen.writeStartObject();
writeTimestamp(periodStartTime);
this.jgen.writeStringField(ROLLUP, this.rollup.name());
Iterator<Integer> it = periodStatsMap.keySet().iterator();
while (it.hasNext()) {
Integer id = it.next();
StatisticsGenerator stats = periodStatsMap.get(id);
DataPointVO vo = this.voMap.get(id);
if (stats instanceof ValueChangeCounter) {
ValueChangeCounter statisticsGenerator = (ValueChangeCounter) stats;
switch(rollup) {
case ALL:
this.jgen.writeObjectFieldStart(vo.getXid());
if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
this.writeImageValue(statisticsGenerator.getFirstValue(), statisticsGenerator.getFirstTime(), periodStartTime, vo, RollupEnum.FIRST.name());
else
this.writeDataValue(periodStartTime, statisticsGenerator.getFirstValue(), vo, RollupEnum.FIRST.name());
if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
this.writeImageValue(statisticsGenerator.getLastValue(), statisticsGenerator.getLastTime(), periodStartTime, vo, RollupEnum.LAST.name());
else
this.writeDataValue(periodStartTime, statisticsGenerator.getLastValue(), vo, RollupEnum.LAST.name());
this.jgen.writeNumberField(RollupEnum.COUNT.name(), statisticsGenerator.getCount());
this.jgen.writeEndObject();
break;
case START:
if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
this.writeImageValue(statisticsGenerator.getStartValue(), periodStartTime, periodStartTime, vo, vo.getXid());
else
this.writeDataValue(periodStartTime, statisticsGenerator.getStartValue(), vo, vo.getXid());
break;
case FIRST:
if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
this.writeImageValue(statisticsGenerator.getFirstValue(), statisticsGenerator.getFirstTime(), periodStartTime, vo, vo.getXid());
else
this.writeDataValue(periodStartTime, statisticsGenerator.getFirstValue(), vo, vo.getXid());
break;
case LAST:
if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
this.writeImageValue(statisticsGenerator.getLastValue(), statisticsGenerator.getLastTime(), periodStartTime, vo, vo.getXid());
else
this.writeDataValue(periodStartTime, statisticsGenerator.getLastValue(), vo, vo.getXid());
break;
case COUNT:
this.jgen.writeNumberField(vo.getXid(), statisticsGenerator.getCount());
break;
default:
// Default to first
this.writeDataValue(periodStartTime, statisticsGenerator.getFirstValue(), vo, vo.getXid());
}
} else if (stats instanceof AnalogStatistics) {
AnalogStatistics statisticsGenerator = (AnalogStatistics) stats;
switch(rollup) {
case ALL:
this.jgen.writeObjectFieldStart(vo.getXid());
this.writeDouble(statisticsGenerator.getAverage(), vo, RollupEnum.AVERAGE.name());
this.writeDouble(statisticsGenerator.getDelta(), vo, RollupEnum.DELTA.name());
this.writeDouble(statisticsGenerator.getMinimumValue(), vo, RollupEnum.MINIMUM.name());
this.writeDouble(statisticsGenerator.getMaximumValue(), vo, RollupEnum.MAXIMUM.name());
Double acc = statisticsGenerator.getLastValue();
if (acc == null) {
acc = statisticsGenerator.getMaximumValue();
}
this.writeDouble(acc, vo, RollupEnum.ACCUMULATOR.name());
this.writeDouble(statisticsGenerator.getSum(), vo, RollupEnum.SUM.name());
this.writeDouble(statisticsGenerator.getFirstValue(), vo, RollupEnum.FIRST.name());
this.writeDouble(statisticsGenerator.getLastValue(), vo, RollupEnum.LAST.name());
this.writeIntegral(statisticsGenerator.getIntegral(), vo, RollupEnum.INTEGRAL.name());
this.jgen.writeNumberField(RollupEnum.COUNT.name(), statisticsGenerator.getCount());
this.jgen.writeEndObject();
break;
case AVERAGE:
this.writeDouble(statisticsGenerator.getAverage(), vo, vo.getXid());
break;
case DELTA:
this.writeDouble(statisticsGenerator.getDelta(), vo, vo.getXid());
break;
case MINIMUM:
this.writeDouble(statisticsGenerator.getMinimumValue(), vo, vo.getXid());
break;
case MAXIMUM:
this.writeDouble(statisticsGenerator.getMaximumValue(), vo, vo.getXid());
break;
// TODO This should be removed after discussion with Jared
case ACCUMULATOR:
Double accumulatorValue = statisticsGenerator.getLastValue();
if (accumulatorValue == null) {
accumulatorValue = statisticsGenerator.getMaximumValue();
}
this.writeDouble(accumulatorValue, vo, vo.getXid());
break;
case SUM:
this.writeDouble(statisticsGenerator.getSum(), vo, vo.getXid());
break;
case START:
this.writeDouble(statisticsGenerator.getStartValue(), vo, vo.getXid());
break;
case FIRST:
this.writeDouble(statisticsGenerator.getFirstValue(), vo, vo.getXid());
break;
case LAST:
this.writeDouble(statisticsGenerator.getLastValue(), vo, vo.getXid());
break;
case COUNT:
this.jgen.writeNumberField(vo.getXid(), statisticsGenerator.getCount());
break;
case INTEGRAL:
this.writeIntegral(statisticsGenerator.getIntegral(), vo, vo.getXid());
break;
default:
// Default to first
this.writeDouble(statisticsGenerator.getFirstValue(), vo, vo.getXid());
}
} else {
throw new ShouldNeverHappenException("Unsuported statistics type: " + stats.getClass().getName());
}
}
this.jgen.writeEndObject();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
Aggregations