Java Code Examples for org.alfresco.error.AlfrescoRuntimeException#create()

The following examples show how to use org.alfresco.error.AlfrescoRuntimeException#create() . 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: AliasableAspect.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Set the email alias for the specified node. 
 * 
 * If the rule is broken, AlfrescoRuntimeException will be thrown.
 * 
 * @param nodeRef Reference to target node
 * @param alias Alias that we want to set to the target node
 * @exception AlfrescoRuntimeException if the <b>alias</b> property is duplicated by another node.
 */
public void addAlias(NodeRef nodeRef, String alias)
{
    if(logger.isDebugEnabled())
    {
        logger.debug("add email alias nodeRef:" + nodeRef + ", alias:" + alias);
    }
    // first try to see if the new alias is in use elsewhere?   
    try
    {
        attributeService.createAttribute(nodeRef, ALIASABLE_ATTRIBUTE_KEY_1, ALIASABLE_ATTRIBUTE_KEY_2, normaliseAlias(alias));
    }
    catch (DuplicateAttributeException de)
    {
        throw AlfrescoRuntimeException.create(ERROR_MSG_DUPLICATE_ALIAS, normaliseAlias(alias));
    }
    
}
 
Example 2
Source File: ModuleComponentHelper.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Add a managed module component to the registry of components.  These will be controlled
 * by the {@link #startModules()} method.
 * 
 * @param component a module component to be executed
 */
public synchronized void registerComponent(ModuleComponent component)
{
    String moduleId = component.getModuleId();
    String name = component.getName();
    // Get the map of components for the module
    Map<String, ModuleComponent> componentsByName = componentsByNameByModule.get(moduleId);
    if (componentsByName == null)
    {
        componentsByName = new HashMap<String, ModuleComponent>(11);
        componentsByNameByModule.put(moduleId, componentsByName);
    }
    // Check if the component has already been registered
    if (componentsByName.containsKey(name))
    {
        throw AlfrescoRuntimeException.create(ERR_COMPONENT_ALREADY_REGISTERED, name, moduleId);
    }
    // Add it
    componentsByName.put(name, component);
    // Done
    if (logger.isDebugEnabled())
    {
        logger.debug("Registered component: " + component);
    }
}
 
Example 3
Source File: RepoUsageComponentImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Checks that the 'System' user is active in a read-write txn.
 */
private final void checkTxnState(TxnReadState txnStateNeeded)
{
    switch (txnStateNeeded)
    {
        case TXN_READ_WRITE:
            if (AlfrescoTransactionSupport.getTransactionReadState() != TxnReadState.TXN_READ_WRITE)
            {
                throw AlfrescoRuntimeException.create("system.usage.err.no_txn_readwrite");
            }
            break;
        case TXN_READ_ONLY:
            if (AlfrescoTransactionSupport.getTransactionReadState() == TxnReadState.TXN_NONE)
            {
                throw AlfrescoRuntimeException.create("system.usage.err.no_txn");
            }
            break;
    }
}
 
Example 4
Source File: ModuleComponentHelper.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Checks that all components have been executed or considered for execution.
 * @param executedComponents Set<ModuleComponent>
 */
private void checkForOrphanComponents(Set<ModuleComponent> executedComponents)
{
    Set<ModuleComponent> missedComponents = new HashSet<ModuleComponent>(executedComponents);
    
    // Iterate over each module registered by components
    for (Map.Entry<String, Map<String, ModuleComponent>> entry : componentsByNameByModule.entrySet())
    {
        String moduleId = entry.getKey();
        Map<String, ModuleComponent> componentsByName = entry.getValue();
        // Iterate over each component registered against the module ID
        for (Map.Entry<String, ModuleComponent> entryInner : componentsByName.entrySet())
        {
            String componentName = entryInner.getKey();
            ModuleComponent component = entryInner.getValue();
            // Check if it has been executed
            if (executedComponents.contains(component))
            {
                // It was executed, so remove it from the missed components set
                missedComponents.remove(component);
            }
            else
            {
                String msg = I18NUtil.getMessage(
                        ERR_COMPONENT_IN_MISSING_MODULE,
                        componentName, moduleId);
                logger.error(msg);
            }
        }
    }
    // Dump if there were orphans
    if (missedComponents.size() > 0)
    {
        throw AlfrescoRuntimeException.create(ERR_ORPHANED_COMPONENTS, missedComponents.size());
    }
}
 
Example 5
Source File: AbstractModuleComponent.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @see #executeInternal() the abstract method to be implemented by subclasses
 */
public final synchronized void execute()
{
    // ensure that this has not been executed already
	String tenantDomain = tenantAdminService.getCurrentUserDomain();
	if (! executed.containsKey(tenantDomain))
	{
		executed.put(tenantDomain, false);
	}
	
    if (executed.get(tenantDomain))
    {
        throw AlfrescoRuntimeException.create(ERR_ALREADY_EXECUTED, moduleId, name);
    }
    // Ensure properties have been set
    checkProperties();
    // Execute
    try
    {
        executeInternal();
    }
    catch (Throwable e)
    {
        throw AlfrescoRuntimeException.create(e, ERR_EXECUTION_FAILED, name, e.getMessage());
    }
    finally
    {
        // There are no second chances
    	executed.put(tenantDomain, true);
    }
}
 
Example 6
Source File: ScriptExecutorImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void executeScriptUrl(Connection connection, String scriptUrl) throws Exception
{
    Dialect dialect = this.dialect;
    String dialectStr = dialect.getClass().getSimpleName();
    InputStream scriptInputStream = getScriptInputStream(dialect.getClass(), scriptUrl);
    // check that it exists
    if (scriptInputStream == null)
    {
        throw AlfrescoRuntimeException.create(ERR_SCRIPT_NOT_FOUND, scriptUrl);
    }
    // write the script to a temp location for future and failure reference
    File tempFile = null;
    try
    {
        tempFile = TempFileProvider.createTempFile("AlfrescoSchema-" + dialectStr + "-Update-", ".sql");
        ContentWriter writer = new FileContentWriter(tempFile);
        writer.putContent(scriptInputStream);
    }
    finally
    {
        try { scriptInputStream.close(); } catch (Throwable e) {}  // usually a duplicate close
    }
    // now execute it
    String dialectScriptUrl = scriptUrl.replaceAll(DialectUtil.PLACEHOLDER_DIALECT, dialect.getClass().getName());
    // Replace the script placeholders
    executeScriptFile(connection, tempFile, dialectScriptUrl);
}
 
Example 7
Source File: SchemaBootstrap.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void executeScriptUrl(Connection connection, String scriptUrl) throws Exception
{
    Dialect dialect = this.dialect;
    String dialectStr = dialect.getClass().getSimpleName();
    InputStream scriptInputStream = getScriptInputStream(dialect.getClass(), scriptUrl);
    // check that it exists
    if (scriptInputStream == null)
    {
        throw AlfrescoRuntimeException.create(ERR_SCRIPT_NOT_FOUND, scriptUrl);
    }
    // write the script to a temp location for future and failure reference
    File tempFile = null;
    try
    {
        tempFile = TempFileProvider.createTempFile("AlfrescoSchema-" + dialectStr + "-Update-", ".sql");
        ContentWriter writer = new FileContentWriter(tempFile);
        writer.putContent(scriptInputStream);
    }
    finally
    {
        try { scriptInputStream.close(); } catch (Throwable e) {}  // usually a duplicate close
    }
    // now execute it
    String dialectScriptUrl = scriptUrl.replaceAll(DialectUtil.PLACEHOLDER_DIALECT, dialect.getClass().getName());
    // Replace the script placeholders
    executeScriptFile(connection, tempFile, dialectScriptUrl);
}
 
Example 8
Source File: DictionaryModelType.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void beforeDeleteNode(NodeRef nodeRef)
{
    boolean workingCopy = nodeService.hasAspect(nodeRef, ContentModel.ASPECT_WORKING_COPY);
    NodeRef wcNodeRef = (NodeRef)AlfrescoTransactionSupport.getResource(KEY_WORKING_COPY);
    if ((wcNodeRef != null) && (wcNodeRef.equals(nodeRef)))
    {
        workingCopy = true;
    }
    
    boolean isVersionNode = nodeRef.getStoreRef().getIdentifier().equals(Version2Model.STORE_ID);
    
    // Ignore if the node is a working copy or version node
    if (! (workingCopy || isVersionNode))
    {
        QName modelName = (QName)this.nodeService.getProperty(nodeRef, ContentModel.PROP_MODEL_NAME);
        
        if (logger.isTraceEnabled())
        {
            logger.trace("beforeDeleteNode: nodeRef="+nodeRef+" validate model delete (modelName="+modelName+")");
        }
        
        if (modelName != null)
        {
            // Validate model delete against usages - content and/or workflows
            if (!modelValidator.canDeleteModel(modelName))
            {
                throw AlfrescoRuntimeException.create(MODEL_IN_USE, modelName);
            }

            Set<NodeRef> pendingModelDeletes = (Set<NodeRef>)AlfrescoTransactionSupport.getResource(KEY_PENDING_DELETE_MODELS);
            if (pendingModelDeletes == null)
            {
                //pendingModelDeletes = Collections.newSetFromMap(new ConcurrentHashMap()); // Java 6
                pendingModelDeletes = new CopyOnWriteArraySet<NodeRef>();
                AlfrescoTransactionSupport.bindResource(KEY_PENDING_DELETE_MODELS, pendingModelDeletes);
            }
            pendingModelDeletes.add(tenantService.getName(nodeRef));
            
            AlfrescoTransactionSupport.bindListener(this.transactionListener);
        }
    }
    else
    {
        if (logger.isTraceEnabled())
        {
            logger.trace("beforeDeleteNode: nodeRef="+nodeRef+ " ignored ("+(workingCopy ? " workingCopy " : "")+(isVersionNode ? " isVersionNode " : "")+") ["+AlfrescoTransactionSupport.getTransactionId()+"]");
        }
    }
}
 
Example 9
Source File: SchemaBootstrap.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Check that the necessary scripts have been executed against the database
 */
private void checkSchemaPatchScripts(
        Connection connection,
        List<SchemaUpgradeScriptPatch> scriptPatches,
        boolean apply) throws Exception
{
    // first check if there have been any applied patches
    int appliedPatchCount = countAppliedPatches(connection);
    if (appliedPatchCount == 0)
    {
        // This is a new schema, so upgrade scripts are irrelevant
        // and patches will not have been applied yet
        return;
    }

    ensureCurrentClusterMemberIsBootstrapping(connection);

    // Retrieve the first installed schema number
    int installedSchema = getInstalledSchemaNumber(connection);

    nextPatch:
    for (SchemaUpgradeScriptPatch patch : scriptPatches)
    {
        final String patchId = patch.getId();
        final String scriptUrl = patch.getScriptUrl();
        
        // Check if any of the alternative patches were executed
        List<Patch> alternatives = patch.getAlternatives();
        for (Patch alternativePatch : alternatives)
        {
            String alternativePatchId = alternativePatch.getId();
            boolean alternativeSucceeded = didPatchSucceed(connection, alternativePatchId, true);
            if (alternativeSucceeded)
            {
                continue nextPatch;
            }
        }

        // check if the script was successfully executed
        boolean wasSuccessfullyApplied = didPatchSucceed(connection, patchId, false);
        if (wasSuccessfullyApplied)
        {
            // Either the patch was executed before or the system was bootstrapped
            // with the patch bean present.
            continue;
        }
        else if (!patch.applies(installedSchema))
        {
            // Patch does not apply to the installed schema number
            continue;
        }
        else if (!apply)
        {
            // the script was not run and may not be run automatically
            throw AlfrescoRuntimeException.create(ERR_SCRIPT_NOT_RUN, scriptUrl);
        }
        // it wasn't run and it can be run now
        executeScriptUrl(connection, scriptUrl);
    }
}