use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.
the class DateUtil method parseDateString.
public static Date parseDateString(TimeZone tz, String dateString, String formatString) {
DateFormat df = new SimpleDateFormat(formatString);
df.setTimeZone(tz);
try {
return df.parse(dateString);
} catch (ParseException e) {
throw new CloudRuntimeException("why why ", e);
}
}
use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.
the class NumbersUtil method parseInterval.
/**
* Converts a string of the format 'yy-MM-dd'T'HH:mm:ss.SSS" into ms.
*
* @param str containing the interval.
* @param defaultValue value to return if str doesn't parse. If -1, throws VmopsRuntimeException
* @return interval in ms
*/
public static long parseInterval(String str, long defaultValue) {
SimpleDateFormat sdf = null;
if (str.contains("D")) {
sdf = new SimpleDateFormat("dd'D'HH'h'mm'M'ss'S'SSS'ms'");
} else if (str.contains("h")) {
sdf = new SimpleDateFormat("HH'h'mm'M'ss'S'SSS'ms'");
} else if (str.contains("M")) {
sdf = new SimpleDateFormat("mm'M'ss'S'SSS'ms'");
} else if (str.contains("S")) {
sdf = new SimpleDateFormat("ss'S'SSS'ms'");
} else if (str.contains("ms")) {
sdf = new SimpleDateFormat("SSS'ms'");
}
Date date;
try {
if (str == null || sdf == null) {
throw new ParseException("String is wrong", 0);
}
date = sdf.parse(str);
return date.getTime();
} catch (ParseException e) {
if (defaultValue != -1) {
return defaultValue;
} else {
throw new CloudRuntimeException("Unable to parse: " + str, e);
}
}
}
use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.
the class ProcessUtil method pidCheck.
// paths cannot be hardcoded
public static void pidCheck(String pidDir, String run) throws ConfigurationException {
String dir = pidDir == null ? "/var/run" : pidDir;
try {
final File propsFile = PropertiesUtil.findConfigFile("environment.properties");
if (propsFile == null) {
s_logger.debug("environment.properties could not be opened");
} else {
final FileInputStream finputstream = new FileInputStream(propsFile);
final Properties props = new Properties();
props.load(finputstream);
finputstream.close();
dir = props.getProperty("paths.pid");
if (dir == null) {
dir = "/var/run";
}
}
} catch (IOException e) {
s_logger.debug("environment.properties could not be opened");
}
final File pidFile = new File(dir + File.separator + run);
try {
if (!pidFile.createNewFile()) {
if (!pidFile.exists()) {
throw new ConfigurationException("Unable to write to " + pidFile.getAbsolutePath() + ". Are you sure you're running as root?");
}
final FileInputStream is = new FileInputStream(pidFile);
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
final String pidLine = reader.readLine();
if (pidLine == null) {
throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath());
}
try {
final long pid = Long.parseLong(pidLine);
final Script script = new Script("bash", 120000, s_logger);
script.add("-c", "ps -p " + pid);
final String result = script.execute();
if (result == null) {
throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath());
}
if (!pidFile.delete()) {
throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath());
}
if (!pidFile.createNewFile()) {
throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath());
}
} catch (final NumberFormatException e) {
throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath());
}
}
pidFile.deleteOnExit();
final Script script = new Script("bash", 120000, s_logger);
script.add("-c", "echo $PPID");
final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
script.execute(parser);
final String pid = parser.getLine();
final FileOutputStream strm = new FileOutputStream(pidFile);
strm.write((pid + "\n").getBytes());
strm.close();
} catch (final IOException e) {
throw new CloudRuntimeException("Unable to create the " + pidFile.getAbsolutePath() + ". Are you running as root?", e);
}
}
use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.
the class GenericDaoBase method remove.
@Override
public boolean remove(final ID id) {
if (_removeSql == null) {
return expunge(id);
}
final Transaction txn = Transaction.currentTxn();
PreparedStatement pstmt = null;
try {
txn.start();
pstmt = txn.prepareAutoCloseStatement(_removeSql.first());
final Attribute[] attrs = _removeSql.second();
prepareAttribute(1, pstmt, attrs[attrs.length - 1], null);
for (int i = 0; i < attrs.length - 1; i++) {
prepareAttribute(i + 2, pstmt, attrs[i], id);
}
final int result = pstmt.executeUpdate();
txn.commit();
if (_cache != null) {
_cache.remove(id);
}
return result > 0;
} catch (final SQLException e) {
throw new CloudRuntimeException("DB Exception on: " + pstmt, e);
}
}
use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.
the class Merovingian method release.
public boolean release(String key) {
boolean validLock = false;
try {
assert _locks.size() > 0 : "There are no locks here. Why are you trying to release " + key;
Ternary<Savepoint, Integer, Long> lock = _locks.get(key);
if (lock != null) {
validLock = true;
if (lock.second() > 1) {
lock.second(lock.second() - 1);
if (s_logger.isTraceEnabled()) {
s_logger.trace("Lock: Releasing " + key + " but not in DB " + lock.second());
}
return false;
}
if (s_logger.isDebugEnabled() && !_locks.keySet().iterator().next().equals(key)) {
s_logger.trace("Lock: Releasing out of order for " + key);
}
_locks.remove(key);
if (s_logger.isTraceEnabled()) {
s_logger.trace("Lock: Releasing " + key + " after " + (InaccurateClock.getTime() - lock.third()));
}
Connection conn = getConnection(key, true);
conn.rollback(lock.first());
} else {
s_logger.warn("Merovingian.release() is called against key " + key + " but the lock of this key does not exist!");
}
if (_locks.size() == 0) {
closeConnection();
}
} catch (SQLException e) {
s_logger.warn("unable to rollback for " + key);
} finally {
synchronized (s_memLocks) {
Pair<Lock, Integer> memLock = s_memLocks.get(key);
if (memLock != null) {
memLock.second(memLock.second() - 1);
if (memLock.second() <= 0) {
s_memLocks.remove(key);
}
if (validLock)
memLock.first().unlock();
} else {
throw new CloudRuntimeException("Merovingian.release() is called for key " + key + ", but its memory lock no longer exist! This is not good, guys");
}
}
}
return true;
}
Aggregations