From do-not-reply at jboss.org Wed Sep 7 13:30:33 2011
Content-Type: multipart/mixed; boundary="===============0965117229852855484=="
MIME-Version: 1.0
From: do-not-reply at jboss.org
To: gatein-commits at lists.jboss.org
Subject: [gatein-commits] gatein SVN: r7332 - in
portal/trunk/component/common/src: test/java/org/exoplatform/commons/utils
and 1 other directory.
Date: Wed, 07 Sep 2011 13:30:33 -0400
Message-ID: <201109071730.p87HUXtg031929@svn01.web.mwc.hst.phx2.redhat.com>
--===============0965117229852855484==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Author: julien_viet
Date: 2011-09-07 13:30:31 -0400 (Wed, 07 Sep 2011)
New Revision: 7332
Added:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/seri=
alization/MarshalledObject.java
portal/trunk/component/common/src/test/java/org/exoplatform/commons/util=
s/TestMarshalledObject.java
Log:
GTNPORTAL-2087 : Simple MarshalledObject to contain an marshalled object in=
to bytes
Added: portal/trunk/component/common/src/main/java/org/exoplatform/commons/=
serialization/MarshalledObject.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/trunk/component/common/src/main/java/org/exoplatform/commons/ser=
ialization/MarshalledObject.java (rev 0)
+++ portal/trunk/component/common/src/main/java/org/exoplatform/commons/ser=
ialization/MarshalledObject.java 2011-09-07 17:30:31 UTC (rev 7332)
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.commons.serialization;
+
+import org.gatein.common.io.IOTools;
+import org.gatein.common.io.UndeclaredIOException;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.lang.reflect.UndeclaredThrowableException;
+import java.util.Arrays;
+
+/**
+ * A simple marshalled object that retain the state of an object as a byte=
s.
+ *
+ * @author Julien Viet
+ */
+public class MarshalledObject
+{
+
+ public static MarshalledObject marshall(S s=
erializable) throws NullPointerException
+ {
+ if (serializable =3D=3D null)
+ {
+ throw new NullPointerException("Cannot marshall null");
+ }
+ try
+ {
+ byte[] bytes =3D IOTools.serialize(serializable);
+ return new MarshalledObject(serializable.getClass().getClassLo=
ader(), bytes);
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ }
+
+ /** . */
+ private final ClassLoader loader;
+
+ /** . */
+ private final byte[] state;
+
+ private MarshalledObject(ClassLoader loader, byte[] state)
+ {
+ this.loader =3D loader;
+ this.state =3D state;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj =3D=3D this)
+ {
+ return true;
+ }
+ if (obj instanceof MarshalledObject)
+ {
+ MarshalledObject> that =3D (MarshalledObject>)obj;
+ return Arrays.equals(state, that.state);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return Arrays.hashCode(state);
+ }
+
+ public S unmarshall() throws UndeclaredThrowableException
+ {
+ try
+ {
+ return (S)IOTools.unserialize(state, loader);
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new UndeclaredThrowableException(e);
+ }
+ }
+}
Added: portal/trunk/component/common/src/test/java/org/exoplatform/commons/=
utils/TestMarshalledObject.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/trunk/component/common/src/test/java/org/exoplatform/commons/uti=
ls/TestMarshalledObject.java (rev 0)
+++ portal/trunk/component/common/src/test/java/org/exoplatform/commons/uti=
ls/TestMarshalledObject.java 2011-09-07 17:30:31 UTC (rev 7332)
@@ -0,0 +1,72 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.commons.utils;
+
+import org.exoplatform.commons.serialization.MarshalledObject;
+import org.exoplatform.component.test.AbstractGateInTest;
+
+/** @author Julien Viet=
a> */
+public class TestMarshalledObject extends AbstractGateInTest
+{
+
+ public void testSerialization()
+ {
+ String from =3D "foo";
+ MarshalledObject marshalled =3D MarshalledObject.marshall(fr=
om);
+ String to =3D marshalled.unmarshall();
+ assertEquals(to, from);
+ }
+
+ public void testNPE()
+ {
+ try
+ {
+ MarshalledObject.marshall(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ }
+ }
+
+ public void testHashCode()
+ {
+ MarshalledObject marshalled1 =3D MarshalledObject.marshall("=
foo");
+ assertEquals(marshalled1.hashCode(), marshalled1.hashCode());
+ MarshalledObject marshalled2 =3D MarshalledObject.marshall("=
foo");
+ assertEquals(marshalled1.hashCode(), marshalled2.hashCode());
+ assertEquals(marshalled2.hashCode(), marshalled1.hashCode());
+ MarshalledObject marshalled3 =3D MarshalledObject.marshall("=
bar");
+ assertNotSame(marshalled1.hashCode(), marshalled3.hashCode());
+ assertNotSame(marshalled3.hashCode(), marshalled1.hashCode());
+ }
+
+ public void testEquals()
+ {
+ MarshalledObject marshalled1 =3D MarshalledObject.marshall("=
foo");
+ assertTrue(marshalled1.equals(marshalled1));
+ MarshalledObject marshalled2 =3D MarshalledObject.marshall("=
foo");
+ assertTrue(marshalled1.equals(marshalled2));
+ assertTrue(marshalled2.equals(marshalled1));
+ MarshalledObject marshalled3 =3D MarshalledObject.marshall("=
bar");
+ assertFalse(marshalled1.equals(marshalled3));
+ assertFalse(marshalled3.equals(marshalled1));
+ }
+}
--===============0965117229852855484==--