diff --git a/pom.xml b/pom.xml index e80e57d..54fdd53 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.dasein dasein-persist - x2013.02.2 + x2013.02.3 jar dasein-persist @@ -77,7 +77,7 @@ mysql mysql-connector-java - 5.1.10 + 5.1.29 test diff --git a/src/main/java/org/dasein/persist/Execution.java b/src/main/java/org/dasein/persist/Execution.java index f745c14..cbe1af6 100644 --- a/src/main/java/org/dasein/persist/Execution.java +++ b/src/main/java/org/dasein/persist/Execution.java @@ -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 { diff --git a/src/main/java/org/dasein/persist/PersistenceException.java b/src/main/java/org/dasein/persist/PersistenceException.java index 6d8b2ff..d612db2 100644 --- a/src/main/java/org/dasein/persist/PersistenceException.java +++ b/src/main/java/org/dasein/persist/PersistenceException.java @@ -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; } diff --git a/src/main/java/org/dasein/persist/jdbc/AutomatedSql.java b/src/main/java/org/dasein/persist/jdbc/AutomatedSql.java index 9922c89..ca8bbba 100644 --- a/src/main/java/org/dasein/persist/jdbc/AutomatedSql.java +++ b/src/main/java/org/dasein/persist/jdbc/AutomatedSql.java @@ -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; @@ -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 { @@ -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); diff --git a/src/main/java/org/dasein/persist/jdbc/Loader.java b/src/main/java/org/dasein/persist/jdbc/Loader.java index 2a0d4de..b0dcd33 100644 --- a/src/main/java/org/dasein/persist/jdbc/Loader.java +++ b/src/main/java/org/dasein/persist/jdbc/Loader.java @@ -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; @@ -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); @@ -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); @@ -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); } } }