Search in sources :

Example 71 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class JwtSignerVerifier method keysToKeyPair.

public static KeyPair keysToKeyPair(String publicKeyStr, String privateKeyStr) {
    Decoder base64Decoder = Base64.getDecoder();
    byte[] publicBase64 = base64Decoder.decode(publicKeyStr);
    byte[] privateBase64 = base64Decoder.decode(privateKeyStr);
    try {
        KeyFactory kf = KeyFactory.getInstance("ECDSA", BouncyCastleProvider.PROVIDER_NAME);
        PublicKey publicKey = kf.generatePublic(new X509EncodedKeySpec(publicBase64));
        PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(privateBase64));
        return new KeyPair(publicKey, privateKey);
    } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Decoder(java.util.Base64.Decoder) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException) KeyFactory(java.security.KeyFactory)

Example 72 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class SerialServerSocketBridge method connect.

private void connect() {
    if (this.socket == null)
        throw new ShouldNeverHappenException("No socket to connect to.");
    try {
        serialOutputStream.connect(getSocketOutputStream());
        serialInputStream.connect(getSocketInputStream());
    } catch (Exception e) {
        throw new ShouldNeverHappenException("Failed to connect streams.");
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) SocketException(java.net.SocketException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) SerialPortException(com.infiniteautomation.mango.io.serial.SerialPortException)

Example 73 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class SetPointHandlerRT method eventRaised.

@Override
public void eventRaised(EventInstance evt) {
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE)
        return;
    // Validate that the target point is available.
    DataPointRT targetPoint = Common.runtimeManager.getDataPoint(vo.getTargetPointId());
    if (targetPoint == null) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetPointMissing"), evt.getEventType());
        return;
    }
    if (!targetPoint.getPointLocator().isSettable()) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetNotSettable"), evt.getEventType());
        return;
    }
    int targetDataType = targetPoint.getVO().getPointLocator().getDataTypeId();
    DataValue value;
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
        // Get the source data point.
        DataPointRT sourcePoint = Common.runtimeManager.getDataPoint(vo.getActivePointId());
        if (sourcePoint == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointMissing"), evt.getEventType());
            return;
        }
        PointValueTime valueTime = sourcePoint.getPointValue();
        if (valueTime == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointValue"), evt.getEventType());
            return;
        }
        if (DataTypes.getDataType(valueTime.getValue()) != targetDataType) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointDataType"), evt.getEventType());
            return;
        }
        value = valueTime.getValue();
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE) {
        value = DataValue.stringToValue(vo.getActiveValueToSet(), targetDataType);
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
        if (activeScript == null) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScript"), evt.getEventType());
            return;
        }
        Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
        context.put(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, targetPoint);
        Map<String, Object> additionalContext = new HashMap<String, Object>();
        additionalContext.put(SetPointEventHandlerVO.EVENT_CONTEXT_KEY, new EventInstanceWrapper(evt));
        try {
            for (IntStringPair cxt : vo.getAdditionalContext()) {
                DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
                if (dprt != null)
                    context.put(cxt.getValue(), dprt);
            }
            PointValueTime pvt = CompiledScriptExecutor.execute(activeScript, context, additionalContext, evt.getActiveTimestamp(), targetPoint.getDataTypeId(), evt.getActiveTimestamp(), vo.getScriptPermissions(), NULL_WRITER, new ScriptLog(NULL_WRITER, LogLevel.FATAL), setCallback, importExclusions, false);
            value = pvt.getValue();
        } catch (ScriptPermissionsException e) {
            raiseFailureEvent(e.getTranslatableMessage(), evt.getEventType());
            return;
        } catch (ScriptException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getCause().getMessage()), evt.getEventType());
            return;
        } catch (ResultTypeException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getMessage()), evt.getEventType());
            return;
        }
    } else
        throw new ShouldNeverHappenException("Unknown active action: " + vo.getActiveAction());
    // Queue a work item to perform the set point.
    if (CompiledScriptExecutor.UNCHANGED != value)
        Common.backgroundProcessing.addWorkItem(new SetPointWorkItem(vo.getTargetPointId(), new PointValueTime(value, evt.getActiveTimestamp()), this));
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) HashMap(java.util.HashMap) IntStringPair(com.serotonin.db.pair.IntStringPair) SetPointWorkItem(com.serotonin.m2m2.rt.maint.work.SetPointWorkItem) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EventInstanceWrapper(com.serotonin.m2m2.rt.script.EventInstanceWrapper)

Example 74 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class BackgroundProcessingImpl method initialize.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.rt.maint.BackroundProcessing#initialize(boolean)
     */
@Override
public void initialize(boolean safe) {
    if (state != PRE_INITIALIZE)
        return;
    // Set the started indicator to true.
    state = INITIALIZE;
    try {
        this.timer = Providers.get(TimerProvider.class).getTimer();
        this.highPriorityService = (OrderedThreadPoolExecutor) timer.getExecutorService();
        this.highPriorityRejectionHandler = new TaskRejectionHandler();
        this.mediumPriorityRejectionHandler = new TaskRejectionHandler();
    } catch (ProviderNotFoundException e) {
        throw new ShouldNeverHappenException(e);
    }
    this.highPriorityService.setRejectedExecutionHandler(this.highPriorityRejectionHandler);
    // Adjust the high priority pool sizes now
    int corePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.HIGH_PRI_CORE_POOL_SIZE);
    int maxPoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.HIGH_PRI_MAX_POOL_SIZE);
    this.highPriorityService.setCorePoolSize(corePoolSize);
    this.highPriorityService.setMaximumPoolSize(maxPoolSize);
    // TODO Quick Fix for Setting default size somewhere other than in Lifecycle or Main
    Common.defaultTaskQueueSize = Common.envProps.getInt("runtime.realTimeTimer.defaultTaskQueueSize", 1);
    // Pull our settings from the System Settings
    corePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.MED_PRI_CORE_POOL_SIZE);
    // Sanity check to ensure the pool sizes are appropriate
    if (corePoolSize < MED_PRI_MAX_POOL_SIZE_MIN)
        corePoolSize = MED_PRI_MAX_POOL_SIZE_MIN;
    mediumPriorityService = new OrderedThreadPoolExecutor(corePoolSize, corePoolSize, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new MangoThreadFactory("medium", Thread.MAX_PRIORITY - 2), mediumPriorityRejectionHandler, Common.envProps.getBoolean("runtime.realTimeTimer.flushTaskQueueOnReject", false), Common.timer.getTimeSource());
    corePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.LOW_PRI_CORE_POOL_SIZE);
    // Sanity check to ensure the pool sizes are appropriate
    if (corePoolSize < LOW_PRI_MAX_POOL_SIZE_MIN)
        corePoolSize = LOW_PRI_MAX_POOL_SIZE_MIN;
    lowPriorityService = new ThreadPoolExecutor(corePoolSize, corePoolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new MangoThreadFactory("low", Thread.NORM_PRIORITY));
    this.state = RUNNING;
}
Also used : TaskRejectionHandler(com.serotonin.m2m2.util.timeout.TaskRejectionHandler) ProviderNotFoundException(com.serotonin.provider.ProviderNotFoundException) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 75 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class BackupWorkItem method schedule.

/**
 * Statically Schedule a Timer Task that will run this work item.
 *
 * TODO For Startup Make this an RTM and add to the RuntimeManager's list of startup RTMS
 * currently only the Module RTMs get started there.  For now this call is
 * used within the RuntimeManager
 */
public static void schedule() {
    try {
        // Test trigger for running every 25 seconds.
        // String cronTrigger = "0/25 * * * * ?";
        // Trigger to run at Set Hour/Minute "s m h * * ?";
        int hour = SystemSettingsDao.getIntValue(SystemSettingsDao.BACKUP_HOUR);
        int minute = SystemSettingsDao.getIntValue(SystemSettingsDao.BACKUP_MINUTE);
        String cronTrigger = "0 " + minute + " " + hour + " * * ?";
        task = new BackupSettingsTask(cronTrigger);
        Common.backgroundProcessing.schedule(task);
    } catch (ParseException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ParseException(java.text.ParseException)

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