Commit 2d3ef9cb authored by Riegel's avatar Riegel
Browse files

Refactor: Use SerializationUtils for DatabaseHandler

parent 99385663
...@@ -4,17 +4,15 @@ import de.hft.stuttgart.citydoctor2.datastructure.BoundingBox; ...@@ -4,17 +4,15 @@ import de.hft.stuttgart.citydoctor2.datastructure.BoundingBox;
import de.hft.stuttgart.citydoctor2.datastructure.CityObject; import de.hft.stuttgart.citydoctor2.datastructure.CityObject;
import de.hft.stuttgart.citydoctor2.datastructure.GmlId; import de.hft.stuttgart.citydoctor2.datastructure.GmlId;
import de.hft.stuttgart.citydoctor2.utils.Pair; import de.hft.stuttgart.citydoctor2.utils.Pair;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.h2gis.functions.factory.H2GISDBFactory; import org.h2gis.functions.factory.H2GISDBFactory;
import org.h2gis.utilities.wrapper.ConnectionWrapper; import org.h2gis.utilities.wrapper.ConnectionWrapper;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
...@@ -82,11 +80,7 @@ public class DatabaseHandler { ...@@ -82,11 +80,7 @@ public class DatabaseHandler {
logger.debug("Attempting to serialize city object {} ", co); logger.debug("Attempting to serialize city object {} ", co);
} }
ps.setString(4, co.getClass().getName()); ps.setString(4, co.getClass().getName());
ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bytes = SerializationUtils.serialize(co);
try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
out.writeObject(co);
}
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ps.setBinaryStream(3, bis, bytes.length); ps.setBinaryStream(3, bis, bytes.length);
ps.executeUpdate(); ps.executeUpdate();
...@@ -95,9 +89,6 @@ public class DatabaseHandler { ...@@ -95,9 +89,6 @@ public class DatabaseHandler {
} catch (SQLException e) { } catch (SQLException e) {
logger.error("Saving of city object to embedded database failed!"); logger.error("Saving of city object to embedded database failed!");
logger.debug(e.getMessage()); logger.debug(e.getMessage());
} catch (IOException e) {
logger.error("Encountered unknown writing error for embedded database!");
logger.debug(e.getMessage());
} }
} }
...@@ -108,13 +99,11 @@ public class DatabaseHandler { ...@@ -108,13 +99,11 @@ public class DatabaseHandler {
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
if (rs.next()) { if (rs.next()) {
try (InputStream is = rs.getBinaryStream("data")) { try (InputStream is = rs.getBinaryStream("data")) {
return deserializeCityObject(new ObjectInputStream(is), rs.getString("type")); return SerializationUtils.deserialize(is);
} }
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} }
} catch (SQLException e) { } catch (SQLException e) {
logger.error("Loading of city object from embedded database failed!"); logger.error("Loading of city object from embedded database failed!");
...@@ -197,23 +186,16 @@ public class DatabaseHandler { ...@@ -197,23 +186,16 @@ public class DatabaseHandler {
return count; return count;
} }
private static List<? extends CityObject> retrieveCityObjects(ResultSet rs) throws SQLException { private static List<CityObject> retrieveCityObjects(ResultSet rs) throws SQLException {
List<CityObject> cos = new ArrayList<>(); List<CityObject> cos = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
try (InputStream is = rs.getBinaryStream("data")) { try (InputStream is = rs.getBinaryStream("data")) {
cos.add(deserializeCityObject(new ObjectInputStream(is), rs.getString("type"))); cos.add(SerializationUtils.deserialize(is));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} }
} }
return cos; return cos;
} }
private static CityObject deserializeCityObject(ObjectInputStream ois, String className) throws IOException, ClassNotFoundException {
Class<?> type = Class.forName(className);
CityObject castCo = (CityObject) type.cast(ois.readObject());
return castCo.getTopLevelCityObject();
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment