Juan Hernandez has uploaded a new change for review.

Change subject: core: Introduce ExternalId
......................................................................

core: Introduce ExternalId

This patch introduces a new ExternalId class intended to manage
identifiers generated by external systems and attached to internal
entities. This external identifiers are basically opaque objects stored
as arrays of bytes in the engine.

The patch also introduces a custom GWT serializer for this class because
GWT RPC doesn't correctly handle arrays of bytes.

Change-Id: Ib93020ddc6fe6249f0bf6dd4b24db4d2d0e0b045
Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com>
---
A 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ExternalId.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSource.java
M 
frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
A 
frontend/webadmin/modules/gwt-extension/src/main/java/org/ovirt/engine/core/common/utils/ExternalId_CustomFieldSerializer.java
4 files changed, 133 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/75/19475/1

diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ExternalId.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ExternalId.java
new file mode 100644
index 0000000..2c74e17
--- /dev/null
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ExternalId.java
@@ -0,0 +1,93 @@
+package org.ovirt.engine.core.common.utils;
+
+import java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * This class represents an identifier used by an external system. We can't 
make
+ * any assumption about those identifiers, so we store them as plain arrays of
+ * bytes.
+ */
+public class ExternalId implements Serializable {
+    // Serialization identifier:
+    private static final long serialVersionUID = 7859034308053227906L;
+
+    // The plain bytes of the identifier:
+    private byte[] bytes;
+
+    // The hash code is computed when the identifier is created in order to
+    // avoid having to compute it repeatedly when the identifier is used as
+    // a key in a hash map:
+    private int hash;
+
+    public ExternalId(byte[] values) {
+        this.bytes = values;
+        this.hash = Arrays.hashCode(values);
+    }
+
+    /**
+     * This constructor is handy for tests, where one wants to create an
+     * external id without having to write the cumbersome syntax of creation
+     * of byte arrays, so instead of this:
+     *
+     * <code>
+     * ExternalId externalId = new ExternalId(
+     *     new byte[] {
+     *         (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x03,
+     *         (byte) 0x03, (byte) 0x04, (byte) 0x05, (byte) 0x06,
+     *     }
+     * );
+     * </code>
+     *
+     * One can write this:
+     *
+     * <code>
+     * ExternalId externalId = new ExternalId(
+     *     0x01, 0x02, 0x03, 0x04,
+     *     0x05, 0x06, 0x07, 0x08
+     * );
+     * </code>
+     *
+     * The parameters are integers in order to avoid the cast to {@code byte} 
as
+     * well.
+     */
+    public ExternalId(int... values) {
+        bytes = new byte[values.length];
+        for (int i = 0; i < values.length; i++) {
+            bytes[i] = (byte) (values[i] & 0xff);
+        }
+    }
+
+    public byte[] getBytes() {
+        return bytes;
+    }
+
+    public String toString() {
+        StringBuilder buffer = new StringBuilder(2 * bytes.length);
+        for (int i = 0; i < bytes.length; i++) {
+            String text = Integer.toHexString(bytes[i] & 0xff);
+            if (text.length() == 1) {
+                buffer.append('0');
+            }
+            buffer.append(text);
+        }
+        return buffer.toString();
+    }
+
+    @Override
+    public int hashCode() {
+        return hash;
+    }
+
+    @Override
+    public boolean equals(final Object object) {
+        if (object == null) {
+            return false;
+        }
+        if (object instanceof ExternalId) {
+            final ExternalId that = (ExternalId) object;
+            return Arrays.equals(this.bytes, that.bytes);
+        }
+        return false;
+    }
+}
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSource.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSource.java
index 5118c8b..0e74b1d 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSource.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSource.java
@@ -2,6 +2,7 @@
 
 import java.lang.reflect.Method;
 
+import org.ovirt.engine.core.common.utils.ExternalId;
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.compat.Version;
 import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
@@ -30,6 +31,8 @@
                 tmpValue = ((Guid) tmpValue).getUuid();
             } else if (tmpValue instanceof Version) {
                 tmpValue = value.toString();
+            } else if (tmpValue instanceof ExternalId) {
+                tmpValue = ((ExternalId) value).getBytes();
             }
         }
 
diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
 
b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
index 38268ca..7e2406b 100644
--- 
a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
@@ -190,6 +190,7 @@
         <include name="common/osinfo/OsRepository.java"/>
         <include name="common/utils/SimpleDependecyInjector.java"/>
         <include name="common/utils/VersionStorageFormatUtil.java"/>
+        <include name="common/utils/ExternalId.java"/>
 
         <!-- Required by frontend -->
                <include name="common/interfaces/SearchType.java" />
diff --git 
a/frontend/webadmin/modules/gwt-extension/src/main/java/org/ovirt/engine/core/common/utils/ExternalId_CustomFieldSerializer.java
 
b/frontend/webadmin/modules/gwt-extension/src/main/java/org/ovirt/engine/core/common/utils/ExternalId_CustomFieldSerializer.java
new file mode 100644
index 0000000..f77b8f2
--- /dev/null
+++ 
b/frontend/webadmin/modules/gwt-extension/src/main/java/org/ovirt/engine/core/common/utils/ExternalId_CustomFieldSerializer.java
@@ -0,0 +1,36 @@
+package org.ovirt.engine.core.common.utils;
+
+import com.google.gwt.user.client.rpc.SerializationException;
+import com.google.gwt.user.client.rpc.SerializationStreamReader;
+import com.google.gwt.user.client.rpc.SerializationStreamWriter;
+
+/**
+ * The RPC mechanism used by GWT doesn't work well with arrays of bytes so we
+ * need to serialize them manually.
+ */
+@SuppressWarnings("unused")
+public class ExternalId_CustomFieldSerializer {
+
+    public static ExternalId instantiate(SerializationStreamReader reader) 
throws SerializationException {
+        int length = reader.readInt();
+        byte[] bytes = new byte[length];
+        for (int i = 0; i < length; i++) {
+            bytes[i] = reader.readByte();
+        }
+        return new ExternalId(bytes);
+    }
+
+    public static void serialize(SerializationStreamWriter writer, ExternalId 
instance) throws SerializationException {
+        byte[] bytes = instance.getBytes();
+        writer.writeInt(bytes.length);
+        for (int i = 0; i < bytes.length; i++) {
+            writer.writeByte(bytes[i]);
+        }
+    }
+
+    public static void deserialize(SerializationStreamReader reader, 
ExternalId instance) throws SerializationException {
+        // Nothing to do, the deserialization has already been performed during
+        // instantiation.
+    }
+
+}


-- 
To view, visit http://gerrit.ovirt.org/19475
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib93020ddc6fe6249f0bf6dd4b24db4d2d0e0b045
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to