Search in sources :

Example 21 with ShouldNeverHappenException

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()));
            }
        }
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException)

Example 22 with ShouldNeverHappenException

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;
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 23 with ShouldNeverHappenException

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);
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 24 with ShouldNeverHappenException

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);
    }
}
Also used : Field(java.lang.reflect.Field) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) JspException(javax.servlet.jsp.JspException) IOException(java.io.IOException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 25 with ShouldNeverHappenException

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);
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) StatisticsGenerator(com.serotonin.m2m2.view.stats.StatisticsGenerator) ValueChangeCounter(com.serotonin.m2m2.view.stats.ValueChangeCounter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) AnalogStatistics(com.serotonin.m2m2.view.stats.AnalogStatistics) IOException(java.io.IOException)

Aggregations

ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)83 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)10 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)9 SQLException (java.sql.SQLException)9 ParseException (java.text.ParseException)8 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 FileNotFoundException (java.io.FileNotFoundException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)5 JsonException (com.serotonin.json.JsonException)4 JsonWriter (com.serotonin.json.JsonWriter)4 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)4 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)4 CronTimerTrigger (com.serotonin.timer.CronTimerTrigger)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 StringWriter (java.io.StringWriter)4 HashMap (java.util.HashMap)4