Search in sources :

Example 21 with CloudRuntimeException

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);
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 22 with CloudRuntimeException

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);
        }
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 23 with CloudRuntimeException

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);
    }
}
Also used : Script(com.cloud.utils.script.Script) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputInterpreter(com.cloud.utils.script.OutputInterpreter) File(java.io.File)

Example 24 with CloudRuntimeException

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);
    }
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PreparedStatement(java.sql.PreparedStatement) AttributeOverride(javax.persistence.AttributeOverride)

Example 25 with CloudRuntimeException

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;
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(java.sql.Connection) Savepoint(java.sql.Savepoint) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1279 PreparedStatement (java.sql.PreparedStatement)320 SQLException (java.sql.SQLException)320 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)236 ResultSet (java.sql.ResultSet)217 ArrayList (java.util.ArrayList)217 ConfigurationException (javax.naming.ConfigurationException)171 HashMap (java.util.HashMap)129 DB (com.cloud.utils.db.DB)118 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)115 IOException (java.io.IOException)95 HostVO (com.cloud.host.HostVO)94 Answer (com.cloud.agent.api.Answer)84 Account (com.cloud.user.Account)84 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)82 URISyntaxException (java.net.URISyntaxException)82 ActionEvent (com.cloud.event.ActionEvent)70 TransactionStatus (com.cloud.utils.db.TransactionStatus)65 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)63 InternalErrorException (com.cloud.exception.InternalErrorException)57