Java Code Examples for org.alfresco.repo.transaction.RetryingTransactionHelper#setForceWritable()

The following examples show how to use org.alfresco.repo.transaction.RetryingTransactionHelper#setForceWritable() . 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: WorkflowDeployer.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
  protected void onBootstrap(ApplicationEvent event)
  {
  	RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
  	txnHelper.setForceWritable(true);
  	txnHelper.doInTransaction(new RetryingTransactionCallback<Object>() {

	@Override
	public Object execute() throws Throwable {
		// run as System on bootstrap
        return AuthenticationUtil.runAs(new RunAsWork<Object>()
        {
            public Object doWork()
            {
                init();
                return null;
            }
        }, AuthenticationUtil.getSystemUserName());
	}
  		
}, false, true);
      
      tenantAdminService.register(this);
  }
 
Example 2
Source File: NodeStringLengthWorker.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @param progress          the thread-safe progress
 */
private synchronized void doWork(NodeStringLengthWorkResult progress) throws Exception
{
    // Build batch processor
    BatchProcessWorkProvider<NodePropertyEntity> workProvider = new NodeStringLengthWorkProvider(progress);
    BatchProcessWorker<NodePropertyEntity> worker = new NodeStringLengthBatch(progress);
    RetryingTransactionHelper retryingTransactionHelper = transactionService.getRetryingTransactionHelper();
    retryingTransactionHelper.setForceWritable(true);
    
    BatchProcessor<NodePropertyEntity> batchProcessor = new BatchProcessor<NodePropertyEntity>(
            "NodeStringLengthWorker",
            retryingTransactionHelper,
            workProvider,
            threadCount,
            batchSize,
            ctx,
            logger,
            1000);
    batchProcessor.process(worker, true);
}
 
Example 3
Source File: EncryptionChecker.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void onBootstrap(ApplicationEvent event)
{
    RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
    txnHelper.setForceWritable(true);      // Force write in case server is read-only
    
    txnHelper.doInTransaction(new RetryingTransactionCallback<Void>()
    {
        public Void execute() throws Throwable
        {
            try
            {
                keyStoreChecker.validateKeyStores();
            }
            catch(Throwable e)
            {
                // Just throw as a runtime exception
                throw new AlfrescoRuntimeException("Keystores are invalid", e);
            }

            return null;
        }
    });
}
 
Example 4
Source File: HashPasswordTransactionListener.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void afterCommit()
{
    // get transaction helper and force it to be writable in case system is in read only mode
    RetryingTransactionHelper txHelper = transactionService.getRetryingTransactionHelper();
    txHelper.setForceWritable(true);
    txHelper.doInTransaction(new RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {
            AuthenticationUtil.pushAuthentication();
            AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
            try
            {
                if (logger.isDebugEnabled())
                {
                    logger.debug("Re-hashing password for user: " + username);
                }
                
                // update the users password to force a new hash to be generated
                authenticationDao.updateUser(username, password);
                
                if (logger.isDebugEnabled())
                {
                    logger.debug("Password for user '" + username + "' has been re-hashed following login");
                }
                
                return null;
            }
            finally
            {
                AuthenticationUtil.popAuthentication();
            }
        }
    }, false, true);
}
 
Example 5
Source File: UpgradePasswordHashWorker.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param progress          the thread-safe progress
 */
private synchronized void doWork(UpgradePasswordHashWorkResult progress) throws Exception
{
    // Build batch processor
    BatchProcessWorkProvider<Long> workProvider = new UpgradePasswordHashWorkProvider(progress);
    BatchProcessWorker<Long> worker = new UpgradePasswordHashBatch(progress);
    RetryingTransactionHelper retryingTransactionHelper = transactionService.getRetryingTransactionHelper();
    retryingTransactionHelper.setForceWritable(true);

    //Create the QNames if they don't exist
    retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {
            qnameDAO.getOrCreateQName(ContentModel.PROP_PASSWORD_HASH);
            qnameDAO.getOrCreateQName(ContentModel.PROP_HASH_INDICATOR);
            return null;
        }
    }, false, true);

    BatchProcessor<Long> batchProcessor = new BatchProcessor<Long>(
            "UpgradePasswordHashWorker",
            retryingTransactionHelper,
            workProvider,
            threadCount,
            batchSize,
            ctx,
            logger,
            1000);
    batchProcessor.process(worker, true);
}
 
Example 6
Source File: PersonServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void makeHomeFolderIfRequired(NodeRef person)
{
    if ((person != null) && (homeFolderCreationDisabled == false))
    {
        NodeRef homeFolder = DefaultTypeConverter.INSTANCE.convert(NodeRef.class, nodeService.getProperty(person, ContentModel.PROP_HOMEFOLDER));
        if (homeFolder == null)
        {
            final ChildAssociationRef ref = nodeService.getPrimaryParent(person);
            RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
            txnHelper.setForceWritable(true);
            boolean requiresNew = false;
            if (AlfrescoTransactionSupport.getTransactionReadState() != TxnReadState.TXN_READ_WRITE)
            {
                // We can be in a read-only transaction, so force a new transaction
                // Note that the transaction will *always* be in read-only mode if the server read-only veto is there 
                requiresNew = true;
            }
            txnHelper.doInTransaction(new RetryingTransactionCallback<Object>()
            {
                public Object execute() throws Throwable
                {
                    makeHomeFolderAsSystem(ref);
                    return null;
                }
            }, false, requiresNew);
        }
    }
}
 
Example 7
Source File: DescriptorServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String loadLicense()
{
    // Ensure that we force a writable txn for this operation
    final RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
    txnHelper.setForceWritable(true);
    
    final RetryingTransactionCallback<String> loadCallback = new RetryingTransactionCallback<String>()
    {
        @Override
        public String execute() throws Throwable
        {
            return licenseService.loadLicense();      
        }
    };
    // ... and we have to be 'system' for this, too
    String result = AuthenticationUtil.runAs(new RunAsWork<String>()
    {
        public String doWork() throws Exception
        {
            return txnHelper.doInTransaction(loadCallback, false, true);
        }
    }, AuthenticationUtil.getSystemUserName());

    if (logger.isDebugEnabled())
    {
        logger.debug("Load license call returning: " + result);
    }
    return result;
}
 
Example 8
Source File: DescriptorServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String loadLicense(final InputStream licenseStream)
{
    // Ensure that we force a writable txn for this operation
    final RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
    txnHelper.setForceWritable(true);
    
    final RetryingTransactionCallback<String> loadCallback = new RetryingTransactionCallback<String>()
    {
        @Override
        public String execute() throws Throwable
        {
            return licenseService.loadLicense(licenseStream);      
        }
    };
    // ... and we have to be 'system' for this, too
    String result = AuthenticationUtil.runAs(new RunAsWork<String>()
    {
        public String doWork() throws Exception
        {
            return txnHelper.doInTransaction(loadCallback, false, true);
        }
    }, AuthenticationUtil.getSystemUserName());

    if (logger.isDebugEnabled())
    {
        logger.debug("Load license call returning: " + result);
    }
    return result;
}
 
Example 9
Source File: DescriptorServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p/>
 * Restrictions are not changed; previous restrictions remain in place.
 */
@Override
public void onLicenseFail()
{
    synchronized (licenseLock)
    {
        // Current restrictions remain in place
        // Make sure that the repo descriptor is updated the first time
        if (isCurrentRepoDescriptorIsNull())
        {
            final RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
            RetryingTransactionCallback<Void> nopLoadLicense = new RetryingTransactionCallback<Void>()
            {
                @Override
                public Void execute()
                {
                    Descriptor newRepoDescriptor = currentRepoDescriptorDAO.updateDescriptor(serverDescriptor, LicenseMode.UNKNOWN);
                    setCurrentRepoDescriptor(newRepoDescriptor);
                    return null;
                }
            };
            txnHelper.setForceWritable(true);
            txnHelper.doInTransaction(nopLoadLicense, false, false);

        }
    }
}
 
Example 10
Source File: AbstractResourceWebScript.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected RetryingTransactionHelper getTransactionHelper(String api)
{
    RetryingTransactionHelper transHelper = transactionService.getRetryingTransactionHelper();
    if (api.equals("authentication"))
    {
        transHelper.setForceWritable(true);
    }
    return transHelper;
}
 
Example 11
Source File: ThumbnailServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see org.alfresco.service.cmr.thumbnail.ThumbnailService#createThumbnail(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.lang.String, org.alfresco.service.cmr.repository.TransformationOptions, java.lang.String, org.alfresco.service.cmr.thumbnail.ThumbnailParentAssociationDetails)
 */
public NodeRef createThumbnail(final NodeRef node, final QName contentProperty, final String mimetype,
        final TransformationOptions transformationOptions, final String thumbnailName, final ThumbnailParentAssociationDetails assocDetails)
{
    // Parameter check
    ParameterCheck.mandatory("node", node);
    ParameterCheck.mandatory("contentProperty", contentProperty);
    ParameterCheck.mandatoryString("mimetype", mimetype);
    ParameterCheck.mandatory("transformationOptions", transformationOptions);

    if (logger.isDebugEnabled() == true)
    {
        logger.debug("Creating thumbnail (node=" + node.toString() + "; contentProperty="
                    + contentProperty.toString() + "; mimetype=" + mimetype);
    }
    
    if (thumbnailName != null)
    {
        NodeRef existingThumbnail = getThumbnailByName(node, contentProperty, thumbnailName);
        if (existingThumbnail != null)
        {
            if (logger.isDebugEnabled() == true)
            {
                logger.debug("Creating thumbnail: There is already a thumbnail with the name '" + thumbnailName + "' (node=" + node.toString() + "; contentProperty=" + contentProperty.toString() + "; mimetype=" + mimetype);
            }
            
            // Return the thumbnail that has already been created
            return existingThumbnail;
        }
    }
    
    RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
    txnHelper.setForceWritable(true);
    boolean requiresNew = false;
    if (AlfrescoTransactionSupport.getTransactionReadState() != TxnReadState.TXN_READ_WRITE)
    {
        //We can be in a read-only transaction, so force a new transaction 
        requiresNew = true;
    }
    return txnHelper.doInTransaction(new RetryingTransactionCallback<NodeRef>()
    {

        @Override
        public NodeRef execute() throws Throwable
        {
            return AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<NodeRef>()
            {
                public NodeRef doWork() throws Exception
                {
                   return createThumbnailNode( node, 
                                               contentProperty,
                                                mimetype, 
                                                transformationOptions, 
                                                thumbnailName, 
                                                assocDetails);
                }
            }, AuthenticationUtil.getSystemUserName());
        }

    }, false, requiresNew);
    
}
 
Example 12
Source File: DescriptorServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void bootstrap()
{
    logger.debug("onBootstrap");

    // We force write mode
    RetryingTransactionHelper helper = transactionService.getRetryingTransactionHelper();
    helper.setForceWritable(true);
    
    // create the initial installed descriptor
    RetryingTransactionCallback<Descriptor> getDescriptorCallback = new RetryingTransactionCallback<Descriptor>()
    {
        public Descriptor execute() 
        {
            return installedRepoDescriptorDAO.getDescriptor();
        }
    };
    Descriptor installed =  helper.doInTransaction(getDescriptorCallback, false, false);
    if (installed != null)
    {
        installedRepoDescriptor = installed;
    }
    else
    {
        installedRepoDescriptor = new UnknownDescriptor();
    }
    
    /*
     *  Initialize license service if on classpath.  
     *  If no class exists a dummy license service is used.
     *  Make the LicenseService available in the context.
     */
    licenseService = (LicenseService) constructSpecialService("org.alfresco.enterprise.license.LicenseComponent");
    if (licenseService == null)
    {
        // No license server code - install a dummy license service instead
        licenseService = new NOOPLicenseService();
    } 
    ApplicationContext applicationContext = getApplicationContext();
    if (applicationContext instanceof ConfigurableApplicationContext)
    {
        ((ConfigurableApplicationContext) applicationContext).getBeanFactory().registerSingleton(
                "licenseService", licenseService);
    }

    // Register HeartBeat with LicenseService
    licenseService.registerOnLicenseChange((LicenseChangeHandler) hbDataCollectorService);


    // Now listen for future license changes
    licenseService.registerOnLicenseChange(this);
    
    try
    {   
        // Verify license has side effect of loading any new licenses
        licenseService.verifyLicense();
    }
    catch (LicenseException e)
    {
        // Swallow Licence Exception Here
        // Don't log error: It'll be reported by other means
    };
}
 
Example 13
Source File: DescriptorServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void onLicenseChange(final LicenseDescriptor licenseDescriptor)
{
    synchronized (licenseLock)
    {
        logger.debug("Received changed license descriptor: " + licenseDescriptor);
                
        RetryingTransactionCallback<RepoUsage> updateLicenseCallback = new RetryingTransactionCallback<RepoUsage>()
        {
            public RepoUsage execute()
            {
                // Configure the license restrictions
                RepoUsage.LicenseMode newMode = licenseDescriptor.getLicenseMode();
                Long expiryTime = licenseDescriptor.getValidUntil() == null ? null : licenseDescriptor.getValidUntil().getTime();
                RepoUsage restrictions = new RepoUsage(
                        System.currentTimeMillis(), 
                        licenseDescriptor.getMaxUsers(), 
                        licenseDescriptor.getMaxDocs(), 
                        newMode,
                        expiryTime,
                        false);
                repoUsageComponent.setRestrictions(restrictions);
                
                // Reset usage upon loading the unlimited license
                if (restrictions.getUsers() == null)
                {
                    repoUsageComponent.resetUsage(UsageType.USAGE_USERS);
                }
                if (restrictions.getDocuments() == null)
                {
                    repoUsageComponent.resetUsage(UsageType.USAGE_DOCUMENTS);
                }

                // persist the server descriptor values in the current repository descriptor
                if (currentRepoDescriptorNullOrModeHasChanged(newMode))
                {
                    if (logger.isDebugEnabled())
                    {
                        logger.debug("Changing license mode in current repo descriptor to: " + newMode);
                    }
                    Descriptor newRepoDescriptor = currentRepoDescriptorDAO.updateDescriptor(
                            serverDescriptor,
                            newMode);
                    setCurrentRepoDescriptor(newRepoDescriptor);
                }
                if (logger.isDebugEnabled())
                {
                    logger.debug("License restrictions updated: " + restrictions);
                }
                return null;
            }
        };
        RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
        txnHelper.setForceWritable(true);
        txnHelper.doInTransaction(updateLicenseCallback);
    }
}