Java Code Examples for org.alfresco.repo.transaction.RetryingTransactionHelper#RetryingTransactionCallback

The following examples show how to use org.alfresco.repo.transaction.RetryingTransactionHelper#RetryingTransactionCallback . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiTServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Format of a valid domain string is "@domain@"
 */
@Test
public void testIsTenantName()
{
    RetryingTransactionHelper.RetryingTransactionCallback<Void> work = new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {

            boolean result = tenantService.isTenantName(STRING);
            assertFalse("The string was reported as domain, but it is not", result);

            result = tenantService.isTenantName(STRING_WITH_EXISTENT_DOMAIN);
            assertTrue("The string was not reported as domain.", result);
            return null;
        }
    };
    transactionService.getRetryingTransactionHelper().doInTransaction(work);
}
 
Example 2
Source File: MultiTServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testGetPrimaryDomain()
{
    RetryingTransactionHelper.RetryingTransactionCallback<Void> work = new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {
            String result = tenantService.getPrimaryDomain(USER1);
            assertNull("The primary domain should be null for a non tenant user without a tenant in name.", result);

            result = tenantService.getPrimaryDomain(USER2_WITH_DOMAIN);
            assertNull("The primary domain should be null for a tenant user if multi tenancy is not enabled.", result);

            createTenant(DOMAIN);
            result = tenantService.getPrimaryDomain(USER2_WITH_DOMAIN);
            assertEquals("The primary domain of the USER2 should be " + DOMAIN + ", but was reported as " + result, DOMAIN, result);

            result = tenantService.getPrimaryDomain(USER1);
            assertTrue("The primary domain should be the default one (empty string) for a non tenant user without a tenant in name.", result.equals(TenantService.DEFAULT_DOMAIN));
            return null;
        }
    };
    transactionService.getRetryingTransactionHelper().doInTransaction(work);
}
 
Example 3
Source File: MultiTServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testGetTenant()
{
    RetryingTransactionHelper.RetryingTransactionCallback<Void> work = new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {
            Tenant tenant = tenantService.getTenant(DOMAIN);
            assertNull("The tenant should not exist.", tenant);

            createTenant(DOMAIN);
            tenant = tenantService.getTenant(DOMAIN);
            assertNotNull("The tenant should exist.", tenant);
            assertTrue("The tenant should have the correct domain.", DOMAIN.equals(tenant.getTenantDomain()));
            assertTrue("The tenant should be enabled.", tenant.isEnabled());
            return null;
        }
    };
    transactionService.getRetryingTransactionHelper().doInTransaction(work);
}
 
Example 4
Source File: MultiTServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testGetUserDomain()
{
    RetryingTransactionHelper.RetryingTransactionCallback<Void> work = new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {

            String result = tenantService.getUserDomain(USER1);
            assertEquals("The user domain should be the default one for a non tenant user without a tenant in name.", TenantService.DEFAULT_DOMAIN, result);

            result = tenantService.getUserDomain(USER2_WITH_DOMAIN);
            assertEquals("The user domain should be the default one for a user with email like name if multi tenancy is not enabled.", TenantService.DEFAULT_DOMAIN, result);

            createTenant(DOMAIN);
            result = tenantService.getUserDomain(USER2_WITH_DOMAIN);
            assertEquals("The user domain should be of the USER2 is " + DOMAIN + ", but was reported as " + result, DOMAIN, result);

            result = tenantService.getUserDomain(USER1);
            assertTrue("The user domain should be the default one (empty string) for a non tenant user without a tenant in name.", result.equals(TenantService.DEFAULT_DOMAIN));
            return null;
        }
    };
    transactionService.getRetryingTransactionHelper().doInTransaction(work);
}
 
Example 5
Source File: AlfrescoRpcAuthenticator.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Does work in a transaction. This will be a writeable transaction unless the system is in read-only mode.
 * 
 * @param callback
 *            a callback that does the work
 * @return the result, or <code>null</code> if not applicable
 */
protected <T> T doInTransaction(RetryingTransactionHelper.RetryingTransactionCallback<T> callback)
{
    // Get the transaction service
    
    TransactionService txService = getTransactionService(); 
    
    // DEBUG
    
    if ( logger.isDebugEnabled())
        logger.debug("Using " + (txService.isReadOnly() ? "ReadOnly" : "Write") + " transaction");
    
    return txService.getRetryingTransactionHelper().doInTransaction(callback, txService.isReadOnly());
}
 
Example 6
Source File: AdminUiTransformerDebug.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
String run(String sourceExtension, String targetExtension, String renditionName)
{
    RetryingTransactionHelper.RetryingTransactionCallback<String> makeNodeCallback = new RetryingTransactionHelper.RetryingTransactionCallback<String>()
    {
        public String execute() throws Throwable
        {
            return runWithinTransaction(sourceExtension, targetExtension);
        }
    };
    return getTransactionService().getRetryingTransactionHelper().doInTransaction(makeNodeCallback, false, true);
}
 
Example 7
Source File: AdminUiTransformerDebug.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public NodeRef createSourceNode(String extension, String sourceMimetype)
{
    // Create a content node which will serve as test data for our transformations.
    RetryingTransactionHelper.RetryingTransactionCallback<NodeRef> makeNodeCallback = new RetryingTransactionHelper.RetryingTransactionCallback<NodeRef>()
    {
        public NodeRef execute() throws Throwable
        {
            // Create a source node loaded with a quick file.
            URL url = loadQuickTestFile(extension);
            URI uri = url.toURI();
            File sourceFile = new File(uri);

            final NodeRef companyHome = getRepositoryHelper().getCompanyHome();

            Map<QName, Serializable> props = new HashMap<QName, Serializable>();
            String localName = "TestTransform." + extension;
            props.put(ContentModel.PROP_NAME, localName);
            NodeRef node = nodeService.createNode(
                    companyHome,
                    ContentModel.ASSOC_CONTAINS,
                    QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, localName),
                    ContentModel.TYPE_CONTENT,
                    props).getChildRef();

            ContentWriter writer = getContentService().getWriter(node, ContentModel.PROP_CONTENT, true);
            writer.setMimetype(sourceMimetype);
            writer.setEncoding("UTF-8");
            writer.putContent(sourceFile);

            return node;
        }
    };
    NodeRef contentNodeRef = getTransactionService().getRetryingTransactionHelper().doInTransaction(makeNodeCallback);
    this.nodesToDeleteAfterTest.add(contentNodeRef);
    return contentNodeRef;
}
 
Example 8
Source File: DifferrentMimeTypeTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@After
public void deleteTemporaryNodeRefsAndTempFiles()
{
    // Tidy up the test nodes we created
    RetryingTransactionHelper.RetryingTransactionCallback<Void> deleteNodeCallback = new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
    {
        public Void execute() throws Throwable
        {
            // Delete them in reverse order.
            for (Iterator<NodeRef> iter = nodesToDeleteAfterTest.descendingIterator(); iter.hasNext(); )
            {
                NodeRef nextNodeToDelete = iter.next();

                if (nodeService.exists(nextNodeToDelete))
                {
                    if (log.isDebugEnabled())
                    {
                        log.debug("Deleting temporary node " + nextNodeToDelete);
                    }
                    nodeService.deleteNode(nextNodeToDelete);
                }
            }
            return null;
        }
    };
    transactionService.getRetryingTransactionHelper().doInTransaction(deleteNodeCallback);
}
 
Example 9
Source File: MultiTServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testIsTenantUser()
{
    RetryingTransactionHelper.RetryingTransactionCallback<Void> work = new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {


            // Create a user with a plain user name without a domain
            NodeRef userNodeRef = createUser(USER1, TenantService.DEFAULT_DOMAIN, PASS);
            assertNotNull("The user was not created.", userNodeRef);
            assertFalse("The user is not from a tenant, but was reported otherwise.", multiTServiceImpl.isTenantUser(USER1));

            // Create a user with a name as an email, but not from tenant
            userNodeRef =  createUser(USER2_WITH_DOMAIN, TenantService.DEFAULT_DOMAIN, PASS);
            assertNotNull("The user was not created.", userNodeRef);
            assertFalse("The user is not from a tenant, but was reported otherwise.", multiTServiceImpl.isTenantUser(USER2_WITH_DOMAIN));

            // Create a tenant and a user in it
            createTenant(DOMAIN);
            userNodeRef = createUser(USER3, DOMAIN, PASS);
            assertNotNull("The user was not created.", userNodeRef);
            assertTrue("The user is from a tenant, but was reported otherwise.", multiTServiceImpl.isTenantUser(USER3 + MultiTServiceImpl.SEPARATOR + DOMAIN));
            return null;
        }
    };
    transactionService.getRetryingTransactionHelper().doInTransaction(work);
}
 
Example 10
Source File: AbstractRenditionIntegrationTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
void createUser(final String username,
                        final String firstName,
                        final String lastName,
                        final String jobTitle,
                        final long quota)
{
    RetryingTransactionHelper.RetryingTransactionCallback<Void> createUserCallback = () ->
    {
        authenticationService.createAuthentication(username, PASSWORD.toCharArray());

        PropertyMap personProperties = new PropertyMap();
        personProperties.put(ContentModel.PROP_USERNAME, username);
        personProperties.put(ContentModel.PROP_AUTHORITY_DISPLAY_NAME, "title" + username);
        personProperties.put(ContentModel.PROP_FIRSTNAME, firstName);
        personProperties.put(ContentModel.PROP_LASTNAME, lastName);
        personProperties.put(ContentModel.PROP_EMAIL, username+"@example.com");
        personProperties.put(ContentModel.PROP_JOBTITLE, jobTitle);
        if (quota > 0)
        {
            personProperties.put(ContentModel.PROP_SIZE_QUOTA, quota);
        }
        personService.createPerson(personProperties);
        return null;
    };

    RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
    txnHelper.doInTransaction(createUserCallback);
}
 
Example 11
Source File: RenditionService2Test.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void scheduleRendition(RetryingTransactionHelper.RetryingTransactionCallback callback, String uniqueId)
{
    try
    {
        callback.execute();
    }
    catch (Throwable throwable)
    {
        throwable.printStackTrace();
        fail("The rendition callback failed: " + throwable);
    }
}
 
Example 12
Source File: TransactionCleanupTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
  @Before
  public void before()
  {
  	ServiceRegistry serviceRegistry = (ServiceRegistry)ctx.getBean("ServiceRegistry");
      NamespaceService namespaceService = serviceRegistry.getNamespaceService();
this.transactionService = serviceRegistry.getTransactionService();
this.authenticationService = (MutableAuthenticationService)ctx.getBean("authenticationService");
this.nodeService = serviceRegistry.getNodeService();
this.searchService = serviceRegistry.getSearchService();
this.nodeDAO = (NodeDAO)ctx.getBean("nodeDAO");
this.nodesCache = (SimpleCache<Serializable, Serializable>) ctx.getBean("node.nodesSharedCache");
      this.worker = (DeletedNodeCleanupWorker)ctx.getBean("nodeCleanup.deletedNodeCleanup");
      this.worker.setMinPurgeAgeDays(0);

  	this.helper = transactionService.getRetryingTransactionHelper();
      authenticationService.authenticate("admin", "admin".toCharArray());
      
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
NodeRef storeRoot = nodeService.getRootNode(storeRef);
List<NodeRef> nodeRefs = searchService.selectNodes(
        storeRoot, "/app:company_home", null, namespaceService, false);
final NodeRef companyHome = nodeRefs.get(0);

  	RetryingTransactionHelper.RetryingTransactionCallback<NodeRef> createNode = new RetryingTransactionHelper.RetryingTransactionCallback<NodeRef>()
  	{
	@Override
	public NodeRef execute() throws Throwable
	{
		return nodeService.createNode(companyHome, ContentModel.ASSOC_CONTAINS, QName.createQName("test", GUID.generate()), ContentModel.TYPE_CONTENT).getChildRef();
	}
  	};
  	this.nodeRef1 = helper.doInTransaction(createNode, false, true);
  	this.nodeRef2 = helper.doInTransaction(createNode, false, true);
  	this.nodeRef3 = helper.doInTransaction(createNode, false, true);
  	this.nodeRef4 = helper.doInTransaction(createNode, false, true);
  	this.nodeRef5 = helper.doInTransaction(createNode, false, true);
  }
 
Example 13
Source File: BaseAuthenticationFilter.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Executes a callback in a transaction as the system user
 * 
 * @param callback
 *            the callback
 * @return the return value from the callback
 */
protected <T> T doInSystemTransaction(final RetryingTransactionHelper.RetryingTransactionCallback<T> callback)
{
    return AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<T>()
    {
        public T doWork() throws Exception
        {
            return transactionService.getRetryingTransactionHelper().doInTransaction(callback, transactionService.isReadOnly());
        }
    }, AuthenticationUtil.SYSTEM_USER_NAME);
}
 
Example 14
Source File: PersonServiceTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void addUserUsageContent(final String userName, final int stringDataLength)
{
    RetryingTransactionHelper.RetryingTransactionCallback<Void> usageCallback = new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {
            try
            {
                AuthenticationUtil.pushAuthentication();
                AuthenticationUtil.setFullyAuthenticatedUser(userName);
                String textData = "This is default text added. Add more: ";
                for (int i = 0; i < stringDataLength; i++)
                {
                    textData += "abc";
                }
                NodeRef homeFolder = getHomeSpaceFolderNode(userName);
                NodeRef folder = nodeService.createNode(
                        homeFolder,
                        ContentModel.ASSOC_CONTAINS,
                        QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testFolder"),
                        ContentModel.TYPE_FOLDER).getChildRef();
                addTextContent(folder, "text1.txt", textData);
            }
            finally
            {
                AuthenticationUtil.popAuthentication();
            }
            return null;
        }
    };

    RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
    txnHelper.doInTransaction(usageCallback);
}
 
Example 15
Source File: PostTxnCallbackScheduler.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
PostTxTransactionListener(RetryingTransactionHelper.RetryingTransactionCallback callback, String uniqueId)
{
    this.callback = callback;
    this.id = uniqueId;
    logger.debug("Created lister with id = " + id);
}
 
Example 16
Source File: PostTxnCallbackScheduler.java    From alfresco-repository with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * @param callback The callback to be scheduled in a post-commit phase
 * @param uniqueId The unique id of the callback. Consecutive requests to schedule the callback with the same id
 *                will overwrite the previously scheduled one.
 */
public void scheduleRendition(RetryingTransactionHelper.RetryingTransactionCallback callback, String uniqueId)
{
    AlfrescoTransactionSupport.bindListener(new PostTxTransactionListener(callback, uniqueId));
}