[jboss-cvs] JBoss Messaging SVN: r5486 - in trunk: src/main/org/jboss/messaging/core/transaction/impl and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Dec 9 08:28:48 EST 2008
Author: jmesnil
Date: 2008-12-09 08:28:48 -0500 (Tue, 09 Dec 2008)
New Revision: 5486
Modified:
trunk/src/main/org/jboss/messaging/core/management/impl/MessagingServerControl.java
trunk/src/main/org/jboss/messaging/core/transaction/impl/XidImpl.java
trunk/tests/src/org/jboss/messaging/tests/unit/core/management/impl/MessagingServerControlTest.java
Log:
DRYing the code
moved Base64 encoding of a Xid in XidImpl class
Modified: trunk/src/main/org/jboss/messaging/core/management/impl/MessagingServerControl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/management/impl/MessagingServerControl.java 2008-12-09 12:29:10 UTC (rev 5485)
+++ trunk/src/main/org/jboss/messaging/core/management/impl/MessagingServerControl.java 2008-12-09 13:28:48 UTC (rev 5486)
@@ -63,7 +63,7 @@
import org.jboss.messaging.core.settings.impl.QueueSettings;
import org.jboss.messaging.core.transaction.ResourceManager;
import org.jboss.messaging.core.transaction.Transaction;
-import org.jboss.messaging.util.Base64;
+import org.jboss.messaging.core.transaction.impl.XidImpl;
import org.jboss.messaging.util.SimpleString;
/**
@@ -101,28 +101,6 @@
private boolean messageCounterEnabled;
// Static --------------------------------------------------------
-
- public static String toBase64String(final Xid xid)
- {
- //TODO - is this really necessary?
- //In JBM we know the XId instance will *always* be an instance of XidImpl
- //and XidImpl
-
- byte[] branchQualifier = xid.getBranchQualifier();
- byte[] globalTransactionId = xid.getGlobalTransactionId();
- int formatId = xid.getFormatId();
-
- 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);
- return Base64.encodeBytes(hashBytes);
- }
// Constructors --------------------------------------------------
@@ -461,7 +439,7 @@
{
Date creation = new Date(entry.getValue());
Xid xid = entry.getKey();
- s[i++] = DATE_FORMAT.format(creation) + " base64: " + toBase64String(xid) + " "+ xid.toString();
+ s[i++] = DATE_FORMAT.format(creation) + " base64: " + XidImpl.toBase64String(xid) + " "+ xid.toString();
}
return s;
}
@@ -472,7 +450,7 @@
for (Xid xid : xids)
{
- if (toBase64String(xid).equals(transactionAsBase64))
+ if (XidImpl.toBase64String(xid).equals(transactionAsBase64))
{
Transaction transaction = resourceManager.removeTransaction(xid);
transaction.commit();
@@ -488,7 +466,7 @@
for (Xid xid : xids)
{
- if (toBase64String(xid).equals(transactionAsBase64))
+ if (XidImpl.toBase64String(xid).equals(transactionAsBase64))
{
Transaction transaction = resourceManager.removeTransaction(xid);
List<MessageReference> rolledBack = transaction.rollback(queueSettingsRepository);
Modified: trunk/src/main/org/jboss/messaging/core/transaction/impl/XidImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/transaction/impl/XidImpl.java 2008-12-09 12:29:10 UTC (rev 5485)
+++ trunk/src/main/org/jboss/messaging/core/transaction/impl/XidImpl.java 2008-12-09 13:28:48 UTC (rev 5486)
@@ -26,6 +26,8 @@
import javax.transaction.xa.Xid;
+import org.jboss.messaging.util.Base64;
+
/**
*
* Xid implementation
@@ -50,6 +52,33 @@
private boolean hashCalculated;
+ // Static --------------------------------------------------------
+
+ public static String toBase64String(final Xid xid)
+ {
+ return Base64.encodeBytes(toByteArray(xid));
+ }
+
+ private static byte[] toByteArray(final Xid xid)
+ {
+ byte[] branchQualifier = xid.getBranchQualifier();
+ byte[] globalTransactionId = xid.getGlobalTransactionId();
+ int formatId = xid.getFormatId();
+
+ 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);
+ return hashBytes;
+ }
+
+ // Constructors --------------------------------------------------
+
/**
* Standard constructor
* @param branchQualifier
@@ -172,15 +201,7 @@
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);
+ byte[] hashBytes = toByteArray(this);
String s = new String(hashBytes);
hash = s.hashCode();
hashCalculated = true;
@@ -194,5 +215,4 @@
return bytes;
}
-
}
Modified: trunk/tests/src/org/jboss/messaging/tests/unit/core/management/impl/MessagingServerControlTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/management/impl/MessagingServerControlTest.java 2008-12-09 12:29:10 UTC (rev 5485)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/management/impl/MessagingServerControlTest.java 2008-12-09 13:28:48 UTC (rev 5486)
@@ -64,6 +64,7 @@
import org.jboss.messaging.core.settings.impl.QueueSettings;
import org.jboss.messaging.core.transaction.ResourceManager;
import org.jboss.messaging.core.transaction.Transaction;
+import org.jboss.messaging.core.transaction.impl.XidImpl;
import org.jboss.messaging.core.version.Version;
import org.jboss.messaging.util.SimpleString;
@@ -534,7 +535,7 @@
public void testCommitPreparedTransactionWithKnownPreparedTransaction() throws Exception
{
Xid xid = randomXid();
- String transactionAsBase64 = MessagingServerControl.toBase64String(xid);
+ String transactionAsBase64 = XidImpl.toBase64String(xid);
Transaction tx = createMock(Transaction.class);
expect(resourceManager.getPreparedTransactions()).andReturn(Arrays.asList(xid));
@@ -555,7 +556,7 @@
public void testCommitPreparedTransactionWithUnknownPreparedTransaction() throws Exception
{
Xid xid = randomXid();
- String transactionAsBase64 = MessagingServerControl.toBase64String(xid);
+ String transactionAsBase64 = XidImpl.toBase64String(xid);
expect(resourceManager.getPreparedTransactions()).andStubReturn(Collections.emptyList());
@@ -571,7 +572,7 @@
public void testRollbackPreparedTransactionWithKnownPreparedTransaction() throws Exception
{
Xid xid = randomXid();
- String transactionAsBase64 = MessagingServerControl.toBase64String(xid);
+ String transactionAsBase64 = XidImpl.toBase64String(xid);
Transaction tx = createMock(Transaction.class);
expect(resourceManager.getPreparedTransactions()).andReturn(Arrays.asList(xid));
@@ -592,7 +593,7 @@
public void testRollbackPreparedTransactionWithUnknownPreparedTransaction() throws Exception
{
Xid xid = randomXid();
- String transactionAsBase64 = MessagingServerControl.toBase64String(xid);
+ String transactionAsBase64 = XidImpl.toBase64String(xid);
expect(resourceManager.getPreparedTransactions()).andStubReturn(Collections.emptyList());
More information about the jboss-cvs-commits
mailing list