use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.
the class Common method encrypt.
public static String encrypt(String plaintext, String alg) {
try {
if ("NONE".equals(alg))
return plaintext;
if ("BCRYPT".equals(alg)) {
return BCrypt.hashpw(plaintext, BCrypt.gensalt());
}
MessageDigest md = MessageDigest.getInstance(alg);
if (md == null)
throw new ShouldNeverHappenException("MessageDigest algorithm " + alg + " not found. Set the 'security.hashAlgorithm' property in env.properties appropriately. " + "Use 'NONE' for no hashing.");
md.update(plaintext.getBytes(UTF8_CS));
byte[] raw = md.digest();
String hash = new String(Base64.encodeBase64(raw));
return hash;
} 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 Common method getCronTrigger.
public static CronTimerTrigger getCronTrigger(int periodType, int delaySeconds) {
int delayMinutes = 0;
if (delaySeconds >= 60) {
delayMinutes = delaySeconds / 60;
delaySeconds %= 60;
if (delayMinutes >= 60)
delayMinutes = 59;
}
try {
switch(periodType) {
case TimePeriods.MILLISECONDS:
throw new ShouldNeverHappenException("Can't create a cron trigger for milliseconds");
case TimePeriods.SECONDS:
return new CronTimerTrigger("* * * * * ?");
case TimePeriods.MINUTES:
return new CronTimerTrigger(delaySeconds + " * * * * ?");
case TimePeriods.HOURS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " * * * ?");
case TimePeriods.DAYS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 * * ?");
case TimePeriods.WEEKS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 ? * MON");
case TimePeriods.MONTHS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 * ?");
case TimePeriods.YEARS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 JAN ?");
default:
throw new ShouldNeverHappenException("Invalid cron period type: " + periodType);
}
} catch (ParseException e) {
throw new ShouldNeverHappenException(e);
}
}
use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.
the class DeltamationCommon method getCronTrigger.
/**
* Delta implementation of com.serotonin.m2m2.Common.getCronTrigger()
* Adds an "every" parameter and overloaded methods
*
* @param every
* @param periodType
* @param delaySeconds
* @return
*/
public static CronTimerTrigger getCronTrigger(int every, int periodType, int delaySeconds) {
// could use constraint
if (every <= 0) {
every = 1;
}
int delayMinutes = 0;
if (delaySeconds >= 60) {
delayMinutes = delaySeconds / 60;
delaySeconds %= 60;
if (delayMinutes >= 60)
delayMinutes = 59;
}
try {
switch(periodType) {
case TimePeriods.MILLISECONDS:
throw new ShouldNeverHappenException("Can't create a cron trigger for milliseconds");
case TimePeriods.SECONDS:
return new CronTimerTrigger("*/" + Integer.toString(every) + " * * * * ?");
case TimePeriods.MINUTES:
return new CronTimerTrigger(delaySeconds + " */" + Integer.toString(every) + " * * * ?");
case TimePeriods.HOURS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " */" + Integer.toString(every) + " * * ?");
case TimePeriods.DAYS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 */" + Integer.toString(every) + " * ?");
case TimePeriods.WEEKS:
// TODO cant do every on weeks
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 ? * MON");
case TimePeriods.MONTHS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 */" + Integer.toString(every) + " ?");
case TimePeriods.YEARS:
return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 JAN ? */" + Integer.toString(every));
default:
throw new ShouldNeverHappenException("Invalid cron period type: " + periodType);
}
} catch (ParseException e) {
throw new ShouldNeverHappenException(e);
}
}
Aggregations