From e7d9ce8fc694dbcedde7cfae3a58a9156c770eb2 Mon Sep 17 00:00:00 2001 From: Derek Hulley Date: Wed, 1 Apr 2009 13:17:52 +0000 Subject: [PATCH] Added helper to get transactionally-bound List, Map and Set resources - Generics allow the following, for instance: Map> filters = TransactionalResourceHelper.getMap(KEY_NODEREF_FILTER); git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@13794 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../AlfrescoTransactionSupport.java | 7 +- .../AlfrescoTransactionSupportTest.java | 20 ++++ .../TransactionalResourceHelper.java | 97 +++++++++++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 source/java/org/alfresco/repo/transaction/TransactionalResourceHelper.java diff --git a/source/java/org/alfresco/repo/transaction/AlfrescoTransactionSupport.java b/source/java/org/alfresco/repo/transaction/AlfrescoTransactionSupport.java index 0fc1d2bfe9..a2b710a8c2 100644 --- a/source/java/org/alfresco/repo/transaction/AlfrescoTransactionSupport.java +++ b/source/java/org/alfresco/repo/transaction/AlfrescoTransactionSupport.java @@ -202,8 +202,11 @@ public abstract class AlfrescoTransactionSupport * * @param key the thread resource map key * @return Returns a thread resource of null if not present + * + * @see TransactionalResourceHelper for helper methods to create and bind common collection types */ - public static Object getResource(Object key) + @SuppressWarnings("unchecked") + public static R getResource(Object key) { // get the synchronization TransactionSynchronizationImpl txnSynch = getSynchronization(); @@ -216,7 +219,7 @@ public abstract class AlfrescoTransactionSupport " key: " + key + "\n" + " resource: " + resource); } - return resource; + return (R) resource; } /** diff --git a/source/java/org/alfresco/repo/transaction/AlfrescoTransactionSupportTest.java b/source/java/org/alfresco/repo/transaction/AlfrescoTransactionSupportTest.java index 68ca4c6074..d682cd270a 100644 --- a/source/java/org/alfresco/repo/transaction/AlfrescoTransactionSupportTest.java +++ b/source/java/org/alfresco/repo/transaction/AlfrescoTransactionSupportTest.java @@ -26,6 +26,7 @@ package org.alfresco.repo.transaction; import java.util.ArrayList; import java.util.List; +import java.util.Map; import javax.transaction.UserTransaction; @@ -258,4 +259,23 @@ public class AlfrescoTransactionSupportTest extends TestCase checkTxnReadState = transactionService.getRetryingTransactionHelper().doInTransaction(getReadStateWork, false); assertEquals("Expected 'read-write transaction'", TxnReadState.TXN_READ_WRITE, checkTxnReadState); } + + public void testResourceHelper() throws Exception + { + // start a transaction + RetryingTransactionCallback testWork = new RetryingTransactionCallback() + { + public Object execute() throws Exception + { + Map map = TransactionalResourceHelper.getMap("abc"); + assertNotNull("Map not created", map); + map.put("1", "ONE"); + Map mapCheck = TransactionalResourceHelper.getMap("abc"); + assertTrue("Same map not retrieved", map == mapCheck); + return null; + } + }; + // kick it all off + transactionService.getRetryingTransactionHelper().doInTransaction(testWork); + } } diff --git a/source/java/org/alfresco/repo/transaction/TransactionalResourceHelper.java b/source/java/org/alfresco/repo/transaction/TransactionalResourceHelper.java new file mode 100644 index 0000000000..cca5c9ce4d --- /dev/null +++ b/source/java/org/alfresco/repo/transaction/TransactionalResourceHelper.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program 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 General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.transaction; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Helper class that will look up or create transactional resources. + * This shortcuts some of the "if not existing, then create" code. + * + * @author Derek Hulley + * @since 3.2 + */ +public abstract class TransactionalResourceHelper +{ + /** + * Support method to retrieve or create and bind a HashMap to the current transaction. + * + * @param the map key type + * @param the map value type + * @param resourceKey the key under which the resource will be stored + * @return Returns an previously-bound Map or else a newly-bound HashMap + */ + public static final Map getMap(Object resourceKey) + { + Map map = AlfrescoTransactionSupport.>getResource(resourceKey); + if (map == null) + { + map = new HashMap(29); + AlfrescoTransactionSupport.bindResource(resourceKey, map); + } + return map; + } + + /** + * Support method to retrieve or create and bind a HashSet to the current transaction. + * + * @param the set value type + * @param resourceKey the key under which the resource will be stored + * @return Returns an previously-bound Set or else a newly-bound HashSet + */ + public static final Set getSet(Object resourceKey) + { + Set set = AlfrescoTransactionSupport.>getResource(resourceKey); + if (set == null) + { + set = new HashSet(29); + AlfrescoTransactionSupport.bindResource(resourceKey, set); + } + return set; + } + + /** + * Support method to retrieve or create and bind a ArrayList to the current transaction. + * + * @param the list value type + * @param resourceKey the key under which the resource will be stored + * @return Returns an previously-bound List or else a newly-bound ArrayList + */ + public static final List getList(Object resourceKey) + { + List list = AlfrescoTransactionSupport.>getResource(resourceKey); + if (list == null) + { + list = new ArrayList(29); + AlfrescoTransactionSupport.bindResource(resourceKey, list); + } + return list; + } +}