use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.
the class SimulatorManagerImpl method configure.
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
try {
Connection conn = Transaction.getStandaloneConnectionWithException();
conn.setAutoCommit(true);
_concierge = new ConnectionConcierge("SimulatorConnection", conn, true);
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to get a db connection", e);
}
return true;
}
use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.
the class GenericDaoBase method executeList.
protected List<T> executeList(final String sql, final Object... params) {
final Transaction txn = Transaction.currentTxn();
PreparedStatement pstmt = null;
final List<T> result = new ArrayList<T>();
try {
pstmt = txn.prepareAutoCloseStatement(sql);
int i = 0;
for (final Object param : params) {
pstmt.setObject(++i, param);
}
final ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
result.add(toEntityBean(rs, true));
}
return result;
} catch (final SQLException e) {
throw new CloudRuntimeException("DB Exception on: " + pstmt, e);
} catch (final Throwable e) {
throw new CloudRuntimeException("Caught: " + pstmt, e);
}
}
use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.
the class GenericDaoBase method expunge.
@Override
public boolean expunge(final ID id) {
final Transaction txn = Transaction.currentTxn();
PreparedStatement pstmt = null;
String sql = null;
try {
txn.start();
for (final Pair<String, Attribute[]> deletSql : _deleteSqls) {
sql = deletSql.first();
final Attribute[] attrs = deletSql.second();
pstmt = txn.prepareAutoCloseStatement(sql);
for (int i = 0; i < attrs.length; i++) {
prepareAttribute(i + 1, pstmt, attrs[i], id);
}
pstmt.executeUpdate();
}
txn.commit();
if (_cache != null) {
_cache.remove(id);
}
return true;
} 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 GenericDaoBase method searchIncludingRemoved.
@Override
public List<T> searchIncludingRemoved(SearchCriteria<T> sc, final Filter filter, final Boolean lock, final boolean cache, final boolean enable_query_cache) {
String clause = sc != null ? sc.getWhereClause() : null;
if (clause != null && clause.length() == 0) {
clause = null;
}
final StringBuilder str = createPartialSelectSql(sc, clause != null, enable_query_cache);
if (clause != null) {
str.append(clause);
}
Collection<JoinBuilder<SearchCriteria<?>>> joins = null;
if (sc != null) {
joins = sc.getJoins();
if (joins != null) {
addJoins(str, joins);
}
}
List<Object> groupByValues = addGroupBy(str, sc);
addFilter(str, filter);
final Transaction txn = Transaction.currentTxn();
if (lock != null) {
assert (txn.dbTxnStarted() == true) : "As nice as I can here now....how do you lock when there's no DB transaction? Review your db 101 course from college.";
str.append(lock ? FOR_UPDATE_CLAUSE : SHARE_MODE_CLAUSE);
}
final String sql = str.toString();
PreparedStatement pstmt = null;
final List<T> result = new ArrayList<T>();
try {
pstmt = txn.prepareAutoCloseStatement(sql);
int i = 0;
if (clause != null) {
for (final Pair<Attribute, Object> value : sc.getValues()) {
prepareAttribute(++i, pstmt, value.first(), value.second());
}
}
if (joins != null) {
i = addJoinAttributes(i, pstmt, joins);
}
if (groupByValues != null) {
for (Object value : groupByValues) {
pstmt.setObject(i++, value);
}
}
if (s_logger.isDebugEnabled() && lock != null) {
txn.registerLock(pstmt.toString());
}
final ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
result.add(toEntityBean(rs, cache));
}
return result;
} catch (final SQLException e) {
throw new CloudRuntimeException("DB Exception on: " + pstmt, e);
} catch (final Throwable e) {
throw new CloudRuntimeException("Caught: " + pstmt, e);
}
}
use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.
the class GenericDaoBase method setField.
@DB(txn = false)
protected void setField(Object entity, Field field, ResultSet rs, int index) throws SQLException {
try {
final Class<?> type = field.getType();
if (type == String.class) {
byte[] bytes = rs.getBytes(index);
if (bytes != null) {
try {
if (field.getAnnotation(Column.class).encryptable()) {
field.set(entity, DBEncryptionUtil.decrypt(new String(bytes, "UTF-8")));
} else {
field.set(entity, new String(bytes, "UTF-8"));
}
} catch (IllegalArgumentException e) {
assert (false);
throw new CloudRuntimeException("IllegalArgumentException when converting UTF-8 data");
} catch (UnsupportedEncodingException e) {
assert (false);
throw new CloudRuntimeException("UnsupportedEncodingException when converting UTF-8 data");
}
} else {
field.set(entity, null);
}
} else if (type == long.class) {
field.setLong(entity, rs.getLong(index));
} else if (type == Long.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getLong(index));
}
} else if (type.isEnum()) {
final Enumerated enumerated = field.getAnnotation(Enumerated.class);
final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
final Enum<?>[] enums = (Enum<?>[]) field.getType().getEnumConstants();
for (final Enum<?> e : enums) {
if ((enumType == EnumType.STRING && e.name().equalsIgnoreCase(rs.getString(index))) || (enumType == EnumType.ORDINAL && e.ordinal() == rs.getInt(index))) {
field.set(entity, e);
return;
}
}
} else if (type == int.class) {
field.set(entity, rs.getInt(index));
} else if (type == Integer.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getInt(index));
}
} else if (type == Date.class) {
final Object data = rs.getDate(index);
if (data == null) {
field.set(entity, null);
return;
}
field.set(entity, DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
} else if (type == Calendar.class) {
final Object data = rs.getDate(index);
if (data == null) {
field.set(entity, null);
return;
}
final Calendar cal = Calendar.getInstance();
cal.setTime(DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
field.set(entity, cal);
} else if (type == boolean.class) {
field.setBoolean(entity, rs.getBoolean(index));
} else if (type == Boolean.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getBoolean(index));
}
} else if (type == URI.class) {
try {
String str = rs.getString(index);
field.set(entity, str == null ? null : new URI(str));
} catch (URISyntaxException e) {
throw new CloudRuntimeException("Invalid URI: " + rs.getString(index), e);
}
} else if (type == URL.class) {
try {
String str = rs.getString(index);
field.set(entity, str != null ? new URL(str) : null);
} catch (MalformedURLException e) {
throw new CloudRuntimeException("Invalid URL: " + rs.getString(index), e);
}
} else if (type == Ip.class) {
final Enumerated enumerated = field.getAnnotation(Enumerated.class);
final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
Ip ip = null;
if (enumType == EnumType.STRING) {
String s = rs.getString(index);
ip = s == null ? null : new Ip(NetUtils.ip2Long(s));
} else {
ip = new Ip(rs.getLong(index));
}
field.set(entity, ip);
} else if (type == short.class) {
field.setShort(entity, rs.getShort(index));
} else if (type == Short.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getShort(index));
}
} else if (type == float.class) {
field.setFloat(entity, rs.getFloat(index));
} else if (type == Float.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getFloat(index));
}
} else if (type == double.class) {
field.setDouble(entity, rs.getDouble(index));
} else if (type == Double.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getDouble(index));
}
} else if (type == byte.class) {
field.setByte(entity, rs.getByte(index));
} else if (type == Byte.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getByte(index));
}
} else if (type == byte[].class) {
field.set(entity, rs.getBytes(index));
} else {
field.set(entity, rs.getObject(index));
}
} catch (final IllegalAccessException e) {
throw new CloudRuntimeException("Yikes! ", e);
}
}
Aggregations