You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by do...@apache.org on 2022/01/22 22:27:02 UTC
[empire-db] branch version3 updated: EMPIREDB-362 serialization
This is an automated email from the ASF dual-hosted git repository.
doebele pushed a commit to branch version3
in repository https://gitbox.apache.org/repos/asf/empire-db.git
The following commit(s) were added to refs/heads/version3 by this push:
new 1992d31 EMPIREDB-362 serialization
1992d31 is described below
commit 1992d318b06e636c1fd1d17c2d97036df616c80e
Author: Rainer Döbele <do...@apache.org>
AuthorDate: Sat Jan 22 23:27:01 2022 +0100
EMPIREDB-362 serialization
---
.../java/org/apache/empire/commons/ClassUtils.java | 2 +-
.../main/java/org/apache/empire/db/DBColumn.java | 57 ++++++++--------------
.../main/java/org/apache/empire/db/DBCommand.java | 44 +++++------------
.../main/java/org/apache/empire/db/DBRecord.java | 42 +++++++++++++++-
.../main/java/org/apache/empire/db/DBRowSet.java | 16 +++---
5 files changed, 83 insertions(+), 78 deletions(-)
diff --git a/empire-db/src/main/java/org/apache/empire/commons/ClassUtils.java b/empire-db/src/main/java/org/apache/empire/commons/ClassUtils.java
index 34778c2..18178ba 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/ClassUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/ClassUtils.java
@@ -53,7 +53,7 @@ public final class ClassUtils
* @return
*/
@SuppressWarnings("unchecked")
- public static <T> T testSerialization(Class<T> clazz, Object objToSerialize)
+ public static <T> T testSerialization(Class<T> clazz, T objToSerialize)
{
try
{ ByteArrayOutputStream baos = new ByteArrayOutputStream();
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBColumn.java b/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
index 27f08b9..8311c68 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
@@ -22,9 +22,9 @@ package org.apache.empire.db;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
-import java.lang.reflect.Field;
import java.util.Set;
+import org.apache.empire.commons.ClassUtils;
import org.apache.empire.commons.Options;
import org.apache.empire.commons.StringUtils;
import org.apache.empire.data.Column;
@@ -69,8 +69,8 @@ public abstract class DBColumn extends DBColumnExpr
// basic data
protected final transient DBRowSet rowset;
- protected final String name;
- protected String comment;
+ protected final String name;
+ protected String comment;
private Boolean quoteName = null;
@@ -121,49 +121,30 @@ public abstract class DBColumn extends DBColumnExpr
* Custom serialization for transient rowset.
*/
private void writeObject(ObjectOutputStream strm) throws IOException
- {
- if (rowset==null)
- { // No rowset
- strm.writeObject("");
- strm.defaultWriteObject();
- return;
- }
- // write dbid and rowset-name
- String dbid = rowset.getDatabase().getIdentifier();
- String rsname = rowset.getName();
- strm.writeObject(dbid);
- strm.writeObject(rsname);
- if (log.isDebugEnabled())
- log.debug("Serialization: writing DBColumn "+dbid+"."+rsname);
+ { // RowSet
+ strm.writeObject(rowset.getDatabase().getIdentifier());
+ strm.writeObject(rowset.getName());
+ // write default
strm.defaultWriteObject();
}
/**
* Custom serialization for transient rowset.
*/
- private void readObject(ObjectInputStream strm) throws IOException, ClassNotFoundException,
- SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
+ private void readObject(ObjectInputStream strm) throws IOException, ClassNotFoundException
{
String dbid = String.valueOf(strm.readObject());
- if (StringUtils.isNotEmpty(dbid))
- { // Find Rowset
- String rsname = String.valueOf(strm.readObject());
- if (log.isDebugEnabled())
- log.debug("Serialization: reading DBColumn "+dbid+"."+rsname);
- // find database
- DBDatabase db = DBDatabase.findById(dbid);
- if (db==null)
- throw new ClassNotFoundException(dbid);
- // find database
- DBRowSet srs = db.getRowSet(rsname);
- if (srs==null)
- throw new ClassNotFoundException(dbid+"."+rsname);
- // set final field
- Field f = DBColumn.class.getDeclaredField("rowset");
- f.setAccessible(true);
- f.set(this, srs);
- f.setAccessible(false);
- }
+ String rsid = String.valueOf(strm.readObject());
+ // find database
+ DBDatabase dbo = DBDatabase.findById(dbid);
+ if (dbo==null)
+ throw new ClassNotFoundException(dbid);
+ // find rowset
+ DBRowSet rso = dbo.getRowSet(rsid);
+ if (rso==null)
+ throw new ClassNotFoundException(dbid+"."+rsid);
+ // set final field
+ ClassUtils.setPrivateFieldValue(DBColumn.class, this, "rowset", rso);
// read the rest
strm.defaultReadObject();
}
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
index 5c1fffd..e95b932 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
@@ -21,7 +21,6 @@ package org.apache.empire.db;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
-import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -31,6 +30,7 @@ import java.util.List;
import java.util.Set;
import java.util.Vector;
+import org.apache.empire.commons.ClassUtils;
import org.apache.empire.commons.StringUtils;
import org.apache.empire.data.DataType;
import org.apache.empire.db.expr.compare.DBCompareColExpr;
@@ -61,6 +61,9 @@ public abstract class DBCommand extends DBCommandExpr
// Logger
protected static final Logger log = LoggerFactory.getLogger(DBCommand.class);
+
+ // Database
+ private final transient DBDatabase db;
// Distinct Select
protected boolean selectDistinct = false;
// Lists
@@ -73,8 +76,6 @@ public abstract class DBCommand extends DBCommandExpr
// Parameters for prepared Statements
protected Vector<DBCmdParam> cmdParams = null;
private int paramUsageCount= 0;
- // Database
- private transient DBDatabase db;
/**
* Constructs a new DBCommand object and set the specified DBDatabase object.
@@ -90,17 +91,8 @@ public abstract class DBCommand extends DBCommandExpr
* Custom serialization for transient database.
*/
private void writeObject(ObjectOutputStream strm) throws IOException
- {
- if (db==null)
- { // No database
- strm.writeObject("");
- strm.defaultWriteObject();
- return;
- }
- String dbid = db.getIdentifier();
- strm.writeObject(dbid);
- if (log.isDebugEnabled())
- log.debug("Serialization: writing DBCommand "+dbid);
+ { // Database
+ strm.writeObject(db.getIdentifier());
// write the rest
strm.defaultWriteObject();
}
@@ -108,24 +100,15 @@ public abstract class DBCommand extends DBCommandExpr
/**
* Custom deserialization for transient database.
*/
- private void readObject(ObjectInputStream strm) throws IOException, ClassNotFoundException,
- SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
+ private void readObject(ObjectInputStream strm) throws IOException, ClassNotFoundException
{
String dbid = String.valueOf(strm.readObject());
- if (StringUtils.isNotEmpty(dbid))
- { // Find database
- if (log.isDebugEnabled())
- log.debug("Serialization: reading DBCommand "+dbid);
- // find database
- DBDatabase sdb = DBDatabase.findById(dbid);
- if (sdb==null)
- throw new ClassNotFoundException(dbid);
- // set final field
- Field f = DBCommand.class.getDeclaredField("db");
- f.setAccessible(true);
- f.set(this, sdb);
- f.setAccessible(false);
- }
+ // find database
+ DBDatabase dbo = DBDatabase.findById(dbid);
+ if (dbo==null)
+ throw new ItemNotFoundException(dbid);
+ // set final field
+ ClassUtils.setPrivateFieldValue(DBCommand.class, this, "db", dbo);
// read the rest
strm.defaultReadObject();
}
@@ -192,7 +175,6 @@ public abstract class DBCommand extends DBCommandExpr
try
{
DBCommand clone = (DBCommand)super.clone();
- clone.db = db;
// Clone lists
if (select!=null)
clone.select = new ArrayList<DBColumnExpr>(select);
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRecord.java b/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
index 4153caa..cb81175 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
@@ -18,6 +18,9 @@
*/
package org.apache.empire.db;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.util.Collection;
@@ -25,6 +28,7 @@ import java.util.List;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.PropertyUtilsBean;
+import org.apache.empire.commons.ClassUtils;
import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.commons.Options;
import org.apache.empire.commons.StringUtils;
@@ -38,6 +42,7 @@ import org.apache.empire.db.exceptions.FieldValueNotFetchedException;
import org.apache.empire.db.expr.compare.DBCompareExpr;
import org.apache.empire.exceptions.BeanPropertyGetException;
import org.apache.empire.exceptions.InvalidArgumentException;
+import org.apache.empire.exceptions.ItemNotFoundException;
import org.apache.empire.exceptions.NotSupportedException;
import org.apache.empire.exceptions.ObjectNotValidException;
import org.apache.empire.xml.XMLUtil;
@@ -203,7 +208,7 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
// the context
protected final DBContext context;
- protected final DBRowSet rowset;
+ protected final transient DBRowSet rowset;
// This is the record data
private State state;
@@ -215,6 +220,41 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
protected boolean enableRollbackHandling;
protected boolean validateFieldValues;
+
+ /**
+ * Custom serialization for transient rowset.
+ */
+ private void writeObject(ObjectOutputStream strm) throws IOException
+ { // RowSet
+ String dbid = rowset.getDatabase().getIdentifier();
+ String rsid = rowset.getName();
+ strm.writeObject(dbid);
+ strm.writeObject(rsid);
+ // write object
+ strm.defaultWriteObject();
+ }
+
+ /**
+ * Custom serialization for transient rowset.
+ */
+ private void readObject(ObjectInputStream strm) throws IOException, ClassNotFoundException
+ {
+ String dbid = String.valueOf(strm.readObject());
+ String rsid = String.valueOf(strm.readObject());
+ // find database
+ DBDatabase dbo = DBDatabase.findById(dbid);
+ if (dbo==null)
+ throw new ItemNotFoundException(dbid);
+ // find rowset
+ DBRowSet rso = dbo.getRowSet(rsid);
+ if (rso==null)
+ throw new ItemNotFoundException(dbid+"."+rsid);
+ // set final field
+ ClassUtils.setPrivateFieldValue(DBRecord.class, this, "rowset", rso);
+ // read the rest
+ strm.defaultReadObject();
+ }
+
/**
* Constructs a new DBRecord.<BR>
* @param context the DBContext for this record
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
index 86a6386..16fd778 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
@@ -160,9 +160,8 @@ public abstract class DBRowSet extends DBExpr
* Custom serialization for transient database.
*/
private void writeObject(ObjectOutputStream strm) throws IOException
- {
- String dbid = (db!=null ? db.getIdentifier() : "");
- strm.writeObject(dbid);
+ { // Database
+ strm.writeObject(db.getIdentifier());
// write the rest
strm.defaultWriteObject();
}
@@ -171,11 +170,14 @@ public abstract class DBRowSet extends DBExpr
* Custom deserialization for transient database.
*/
private void readObject(ObjectInputStream strm) throws IOException, ClassNotFoundException
- {
- // Database
+ { // Database
String dbid = String.valueOf(strm.readObject());
- DBDatabase database = DBDatabase.findById(dbid);
- ClassUtils.setPrivateFieldValue(DBRowSet.class, this, "db", database);
+ // find database
+ DBDatabase dbo = DBDatabase.findById(dbid);
+ if (dbo==null)
+ throw new ItemNotFoundException(dbid);
+ // set final field
+ ClassUtils.setPrivateFieldValue(DBRowSet.class, this, "db", dbo);
// read the rest
strm.defaultReadObject();
}