Added helper to get transactionally-bound List, Map and Set resources

- Generics allow the following, for instance:
   Map<NodeRef,List<QName>> 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
This commit is contained in:
Derek Hulley
2009-04-01 13:17:52 +00:00
parent b83115a62c
commit e7d9ce8fc6
3 changed files with 122 additions and 2 deletions

View File

@@ -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 extends Object> 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;
}
/**

View File

@@ -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<Object> testWork = new RetryingTransactionCallback<Object>()
{
public Object execute() throws Exception
{
Map<String, String> map = TransactionalResourceHelper.getMap("abc");
assertNotNull("Map not created", map);
map.put("1", "ONE");
Map<String, String> mapCheck = TransactionalResourceHelper.getMap("abc");
assertTrue("Same map not retrieved", map == mapCheck);
return null;
}
};
// kick it all off
transactionService.getRetryingTransactionHelper().doInTransaction(testWork);
}
}

View File

@@ -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 "<i>if not existing, then create</i>" code.
*
* @author Derek Hulley
* @since 3.2
*/
public abstract class TransactionalResourceHelper
{
/**
* Support method to retrieve or create and bind a <tt>HashMap</tt> to the current transaction.
*
* @param <K> the map key type
* @param <V> the map value type
* @param resourceKey the key under which the resource will be stored
* @return Returns an previously-bound <tt>Map</tt> or else a newly-bound <tt>HashMap</tt>
*/
public static final <K,V> Map<K,V> getMap(Object resourceKey)
{
Map<K,V> map = AlfrescoTransactionSupport.<Map<K,V>>getResource(resourceKey);
if (map == null)
{
map = new HashMap<K, V>(29);
AlfrescoTransactionSupport.bindResource(resourceKey, map);
}
return map;
}
/**
* Support method to retrieve or create and bind a <tt>HashSet</tt> to the current transaction.
*
* @param <V> the set value type
* @param resourceKey the key under which the resource will be stored
* @return Returns an previously-bound <tt>Set</tt> or else a newly-bound <tt>HashSet</tt>
*/
public static final <V> Set<V> getSet(Object resourceKey)
{
Set<V> set = AlfrescoTransactionSupport.<Set<V>>getResource(resourceKey);
if (set == null)
{
set = new HashSet<V>(29);
AlfrescoTransactionSupport.bindResource(resourceKey, set);
}
return set;
}
/**
* Support method to retrieve or create and bind a <tt>ArrayList</tt> to the current transaction.
*
* @param <V> the list value type
* @param resourceKey the key under which the resource will be stored
* @return Returns an previously-bound <tt>List</tt> or else a newly-bound <tt>ArrayList</tt>
*/
public static final <V> List<V> getList(Object resourceKey)
{
List<V> list = AlfrescoTransactionSupport.<List<V>>getResource(resourceKey);
if (list == null)
{
list = new ArrayList<V>(29);
AlfrescoTransactionSupport.bindResource(resourceKey, list);
}
return list;
}
}