Skip to content

Commit

Permalink
don't swallow exceptions and convert arrays to<->from json (if asJSON…
Browse files Browse the repository at this point in the history
… is present)
  • Loading branch information
turtlebender committed Mar 26, 2014
1 parent f821e7f commit 6a416b8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -3,7 +3,7 @@

<groupId>org.dasein</groupId>
<artifactId>dasein-persist</artifactId>
<version>x2013.02.2</version>
<version>x2013.02.3</version>
<packaging>jar</packaging>

<name>dasein-persist</name>
Expand Down Expand Up @@ -77,7 +77,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
<version>5.1.29</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dasein/persist/Execution.java
Expand Up @@ -376,7 +376,7 @@ else if( res instanceof HashMap ) {
}
catch( SQLException e ) {
logger.debug("Error executing event: " + e.getMessage(), e);
throw new PersistenceException(e.getMessage());
throw new PersistenceException(e);
}
}
finally {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/dasein/persist/PersistenceException.java
Expand Up @@ -60,13 +60,14 @@ public PersistenceException(String msg) {
super(msg);
}


/**
* Constructs a new persistence exception that results from the
* specified data store exception.
* @param cse the cause for this persistence exception
*/
public PersistenceException(Exception cse) {
super(cse.getMessage());
super(cse.getMessage(), cse);
cause = cse;
}

Expand Down
31 changes: 28 additions & 3 deletions src/main/java/org/dasein/persist/jdbc/AutomatedSql.java
Expand Up @@ -20,9 +20,10 @@
/* Copyright (c) 2006 Valtira Corporation, All Rights Reserved */
package org.dasein.persist.jdbc;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
Expand All @@ -45,6 +46,8 @@
import org.dasein.util.CachedItem;
import org.dasein.util.Translator;
import org.dasein.util.uom.Measured;
import org.json.JSONArray;
import org.json.JSONException;

public class AutomatedSql extends Execution {
static public enum Operator {
Expand Down Expand Up @@ -420,6 +423,28 @@ else if( t.equals(UUID.class) ) {
statement.setString(i, ob.toString());
}
}
else if( t.isArray() ){
try {
JSONArray serialized = new JSONArray();
Class<?> componentType = t.getComponentType();
Method toJson = componentType.getDeclaredMethod("toJSON");
int length = Array.getLength(ob);
for(int counter = 0 ; counter < length ; counter++){
serialized.put(toJson.invoke(Array.get(ob, counter)));
}
StringWriter writer = new StringWriter();
serialized.write(writer);
statement.setString(i, writer.toString());
} catch (NoSuchMethodException e) {
throw new SQLException("No toJSON method: " + t.getName(), e);
} catch (InvocationTargetException e) {
throw new SQLException("Error invoking toJSON method: " + t.getName(), e);
} catch (IllegalAccessException e) {
throw new SQLException("Error invoking toJSON method: " + t.getName(), e);
} catch (JSONException e) {
throw new SQLException("Error serializing to JSON: " + t.getName(), e);
}
}
else {
if( ob == null ) {
statement.setNull(i, Types.VARCHAR);
Expand Down
39 changes: 34 additions & 5 deletions src/main/java/org/dasein/persist/jdbc/Loader.java
Expand Up @@ -20,9 +20,7 @@
/* Copyright (c) 2006 Valtira Corporation, All Rights Reserved */
package org.dasein.persist.jdbc;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
Expand All @@ -40,6 +38,9 @@
import org.dasein.util.CachedItem;
import org.dasein.util.uom.Measured;
import org.dasein.util.uom.UnitOfMeasure;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Loader extends AutomatedSql {
static public final Logger logger = Logger.getLogger(Loader.class);
Expand Down Expand Up @@ -349,6 +350,34 @@ else if( type.getName().startsWith("java.") ){
if( rs.wasNull() ) {
ob = null;
}
}else if(type.isArray()){
try {
JSONArray object = new JSONArray(rs.getString(i));
Class<?> componentType = type.getComponentType();
Method m = componentType.getDeclaredMethod("valueOf", JSONObject.class);
Object newArray = Array.newInstance(componentType, object.length());
for(int counter = 0 ; counter < object.length() ; counter++){
Array.set(newArray, counter, m.invoke(null, object.get(counter)));
}
ob = newArray;
} catch (JSONException e) {
throw new SQLException("Error parsing json: " + type.getName(), e);
} catch (NoSuchMethodException e) {
throw new SQLException(
"No valueOf function for loading JSON: " + type.getName(),
e
);
} catch (InvocationTargetException e) {
throw new SQLException(
"Failed to invoke valueOf function for loading JSON: " + type.getName(),
e
);
} catch (IllegalAccessException e) {
throw new SQLException(
"Failed to invoke valueOf function for loading JSON: " + type.getName(),
e
);
}
}
else {
String str = rs.getString(i);
Expand All @@ -359,11 +388,11 @@ else if( type.getName().startsWith("java.") ){
else {
try {
Method m = type.getDeclaredMethod("valueOf", String.class);

ob = m.invoke(null, str);
}
catch( Exception e ) {
throw new SQLException("I have no idea how to map to " + type.getName());
throw new SQLException("I have no idea how to map to " + type.getName(), e);
}
}
}
Expand Down

0 comments on commit 6a416b8

Please sign in to comment.