[jboss-cvs] JBoss Messaging SVN: r3584 - in trunk: src/main/org/jboss/messaging/core/tx and 4 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Jan 17 09:51:51 EST 2008


Author: timfox
Date: 2008-01-17 09:51:51 -0500 (Thu, 17 Jan 2008)
New Revision: 3584

Added:
   trunk/src/main/org/jboss/messaging/core/tx/
   trunk/src/main/org/jboss/messaging/core/tx/MessagingXid.java
   trunk/tests/src/org/jboss/messaging/core/tx/
   trunk/tests/src/org/jboss/messaging/core/tx/test/
   trunk/tests/src/org/jboss/messaging/core/tx/test/unit/
   trunk/tests/src/org/jboss/messaging/core/tx/test/unit/MessagingXidTest.java
Log:
Added missing files


Added: trunk/src/main/org/jboss/messaging/core/tx/MessagingXid.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/tx/MessagingXid.java	                        (rev 0)
+++ trunk/src/main/org/jboss/messaging/core/tx/MessagingXid.java	2008-01-17 14:51:51 UTC (rev 3584)
@@ -0,0 +1,231 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * 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.jboss.messaging.core.tx;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.Serializable;
+
+import javax.transaction.xa.Xid;
+
+import org.jboss.messaging.util.Streamable;
+
+/**
+ * 
+ * Xid implementation
+ * 
+ * @author <a href="mailto:adrian at jboss.org>Adrian Brock</a>
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ * @author <a href="mailto:juha at jboss.org">Juha Lindfors</a>
+ * 
+ * @version $Revision 1.1 $
+ */
+public class MessagingXid implements Xid, Serializable, Streamable
+{
+   private static final long serialVersionUID = -1893120702576869245L;
+
+   private byte[] branchQualifier;
+   
+   private int formatId;
+   
+   private byte[] globalTransactionId;
+   
+   private int hash;
+   
+   private boolean hashCalculated;
+   
+   /**
+    * For serialization only
+    */
+   public MessagingXid()
+   {      
+   }
+   
+   /**
+    * Standard constructor
+    * @param branchQualifier
+    * @param formatId
+    * @param globalTransactionId
+    */
+   public MessagingXid(byte[] branchQualifier, int formatId, byte[] globalTransactionId)
+   {
+      this.branchQualifier = branchQualifier;
+      this.formatId = formatId;
+      this.globalTransactionId = globalTransactionId;          
+   }
+   
+   /**
+    * Copy constructor
+    * @param other
+    */
+   public MessagingXid(Xid other)
+   {
+      this.branchQualifier = copyBytes(other.getBranchQualifier());
+      this.formatId = other.getFormatId();
+      this.globalTransactionId = copyBytes(other.getGlobalTransactionId());
+   }
+
+   // Xid implementation ------------------------------------------------------------------
+   
+   public byte[] getBranchQualifier()
+   {
+      return branchQualifier;
+   }
+
+   public int getFormatId()
+   {
+      return formatId;
+   }
+
+   public byte[] getGlobalTransactionId()
+   {
+      return globalTransactionId;
+   }
+   
+   // Public -------------------------------------------------------------------------------
+            
+   public int hashCode()
+   {
+      if (!hashCalculated)
+      {
+         calcHash();
+      }
+      return hash;
+   }
+   
+   public boolean equals(Object other)
+   {
+      if (!(other instanceof Xid))
+      {
+         return false;
+      }
+      Xid xother = (Xid)other;
+      if (xother.getFormatId() != formatId)
+      {
+         return false;
+      }
+      if (xother.getBranchQualifier().length != this.branchQualifier.length)
+      {
+         return false;
+      }
+      if (xother.getGlobalTransactionId().length != this.globalTransactionId.length)
+      {
+         return false;
+      }
+      for (int i = 0; i < this.branchQualifier.length; i++)
+      {
+         byte[] otherBQ = xother.getBranchQualifier();
+         if (this.branchQualifier[i] != otherBQ[i])
+         {
+            return false;
+         }         
+      }
+      for (int i = 0; i < this.globalTransactionId.length; i++)
+      {
+         byte[] otherGtx = xother.getGlobalTransactionId();
+         if (this.globalTransactionId[i] != otherGtx[i])
+         {
+            return false;
+         }
+      }
+      return true;
+   }
+
+   public String toString()
+   {
+     	return "MessagingXid (" + System.identityHashCode(this) + " bq:" + stringRep(branchQualifier) +
+     	" formatID:" + formatId + " gtxid:" + stringRep(globalTransactionId);
+   }
+   
+      
+   // Streamable implementation ------------------------------------------------------------
+
+   public void read(DataInputStream in) throws Exception
+   {
+      int len = in.readInt();      
+      branchQualifier = new byte[len];      
+      in.readFully(branchQualifier);
+      
+      formatId = in.readInt();
+      
+      len = in.readInt();      
+      globalTransactionId = new byte[len];      
+      in.readFully(globalTransactionId);            
+   }
+
+   public void write(DataOutputStream out) throws Exception
+   {
+      out.writeInt(branchQualifier.length);
+      out.write(branchQualifier);
+      
+      out.writeInt(formatId);
+      
+      out.writeInt(globalTransactionId.length);
+      out.write(globalTransactionId);
+   }
+   
+   // Private -------------------------------------------------------------------------------
+   
+   private String stringRep(byte[] bytes)
+   {
+      StringBuffer buff = new StringBuffer();
+      for (int i = 0; i < bytes.length; i++)
+      {
+         byte b = bytes[i];
+         
+         buff.append(b);
+         
+         if (i != bytes.length - 1)
+         {
+            buff.append('.');
+         }
+      }
+      
+      return buff.toString();
+   }
+   
+   private void calcHash()
+   {
+      byte[] hashBytes = new byte[branchQualifier.length + globalTransactionId.length + 4];
+      System.arraycopy(branchQualifier, 0, hashBytes, 0, branchQualifier.length);
+      System.arraycopy(globalTransactionId, 0, hashBytes, branchQualifier.length, globalTransactionId.length);
+      byte[] intBytes = new byte[4];
+      for (int i = 0; i < 4; i++)
+      {
+         intBytes[i] = (byte)((formatId >> (i * 8)) % 0xFF);
+      }
+      System.arraycopy(intBytes, 0, hashBytes, branchQualifier.length + globalTransactionId.length, 4);
+      String s = new String(hashBytes);
+      hash = s.hashCode();
+      hashCalculated = true;
+   }
+   
+   private byte[] copyBytes(byte[] other)
+   {
+      byte[] bytes = new byte[other.length];
+      
+      System.arraycopy(other, 0, bytes, 0, other.length);
+      
+      return bytes;
+   }
+
+}

Added: trunk/tests/src/org/jboss/messaging/core/tx/test/unit/MessagingXidTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/core/tx/test/unit/MessagingXidTest.java	                        (rev 0)
+++ trunk/tests/src/org/jboss/messaging/core/tx/test/unit/MessagingXidTest.java	2008-01-17 14:51:51 UTC (rev 3584)
@@ -0,0 +1,172 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * 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.jboss.messaging.core.tx.test.unit;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import javax.transaction.xa.Xid;
+
+import org.jboss.messaging.core.tx.MessagingXid;
+import org.jboss.messaging.test.unit.RandomUtil;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ * 
+ * A MessagingXidTest
+ * 
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class MessagingXidTest extends UnitTestCase
+{
+   public void testSerialize() throws Exception
+   {
+      MessagingXid xid = new MessagingXid(RandomUtil.randomBytes(), RandomUtil.randomInt(),
+                                          RandomUtil.randomBytes());
+      
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      
+      ObjectOutputStream oos = new ObjectOutputStream(baos);
+      
+      oos.writeObject(xid);
+      
+      oos.flush();
+      
+      ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
+      
+      Object obj = ois.readObject();
+      
+      assertTrue(obj instanceof MessagingXid);
+      
+      MessagingXid xid2 = (MessagingXid)obj;
+      
+      assertXidsEquivalent(xid, xid2);
+      
+      assertEquals(xid, xid2);     
+   }
+   
+   public void testStandardConstructor()
+   {
+      byte[] bq = RandomUtil.randomBytes();
+      
+      byte[] globalTXID = RandomUtil.randomBytes();
+      
+      int formatID = RandomUtil.randomInt();
+      
+      MessagingXid xid1 = new MessagingXid(bq, formatID, globalTXID);
+      
+      assertByteArraysEquivalent(bq, xid1.getBranchQualifier());
+      
+      assertByteArraysEquivalent(globalTXID, xid1.getGlobalTransactionId());
+      
+      assertEquals(formatID, xid1.getFormatId());
+   }
+   
+   public void testCopyConstructor()
+   {
+      MessagingXid xid1 = new MessagingXid(RandomUtil.randomBytes(), RandomUtil.randomInt(),
+                                          RandomUtil.randomBytes());
+      
+      MessagingXid xid2 = new MessagingXid(xid1);
+      
+      assertXidsEquivalent(xid1, xid2);
+      
+      assertEquals(xid2, xid2);
+   }
+   
+   public void testDefaultConstructor()
+   {
+      MessagingXid xid1 = new MessagingXid();
+      
+      assertNull(xid1.getBranchQualifier());
+      
+      assertNull(xid1.getGlobalTransactionId());
+      
+      assertEquals(0, xid1.getFormatId());
+   }
+   
+   public void testEqualsWithForeign()
+   {
+      MessagingXid xid1 = new MessagingXid(RandomUtil.randomBytes(), RandomUtil.randomInt(),
+            RandomUtil.randomBytes());
+
+      Xid foreign = new ForeignXid(xid1.getBranchQualifier(), xid1.getFormatId(), xid1.getGlobalTransactionId());
+      
+      assertTrue(xid1.equals(foreign));
+      
+      foreign = new ForeignXid(RandomUtil.randomBytes(), RandomUtil.randomInt(),
+                               RandomUtil.randomBytes());
+      
+      assertFalse(xid1.equals(foreign));
+      
+   }
+   
+   // Private ---------------------------------------------------------------------------------
+   
+   private void assertXidsEquivalent(Xid xid1, Xid xid2)
+   {
+      assertByteArraysEquivalent(xid1.getBranchQualifier(), xid2.getBranchQualifier());
+      
+      assertEquals(xid1.getFormatId(), xid2.getFormatId());
+      
+      assertByteArraysEquivalent(xid1.getGlobalTransactionId(), xid2.getGlobalTransactionId());
+   }
+   
+   // Inner classes ---------------------------------------------------------------------------
+   
+   class ForeignXid implements Xid
+   {
+      private byte[] branchQualifier;
+      
+      private int formatId;
+      
+      private byte[] globalTransactionId;
+      
+      public ForeignXid(byte[] branchQualifier, int formatId, byte[] globalTransactionId)
+      {
+         this.branchQualifier = branchQualifier;
+         this.formatId = formatId;
+         this.globalTransactionId = globalTransactionId;          
+      }
+           
+      public byte[] getBranchQualifier()
+      {
+         return this.branchQualifier;
+      }
+
+      public int getFormatId()
+      {
+         return this.formatId;
+      }
+
+      public byte[] getGlobalTransactionId()
+      {
+         return this.globalTransactionId;
+      }
+      
+   }
+   
+   
+}




More information about the jboss-cvs-commits mailing list