Search in sources :

Example 31 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class CacheMacro method getContentCache.

/**
 * Get a cache matching the passed time to live and max entries.
 * <p>
 * Note that whenever a new cache is created it currently means a new thread is used too (since the JBoss cache used
 * underneath uses a thread for evicting entries from the cache). We need to modify our xwiki-cache module to allow
 * setting time to live on cache items, see https://jira.xwiki.org/browse/XWIKI-5907
 * </p>
 *
 * @param lifespan the number of seconds to cache the content
 * @param maxEntries the maximum number of entries in the cache (Least Recently Used entries are ejected)
 * @return the matching cache (a new cache is created if no existing one is found)
 * @throws MacroExecutionException in case we fail to create the new cache
 */
Cache<List<Block>> getContentCache(int lifespan, int maxEntries) throws MacroExecutionException {
    CacheKey cacheKey = new CacheKey(lifespan, maxEntries);
    Cache<List<Block>> contentCache = this.contentCacheMap.get(cacheKey);
    if (contentCache == null) {
        // Create Cache
        LRUCacheConfiguration configuration = new LRUCacheConfiguration(String.format("cacheMacro.%s", cacheKey.toString()), maxEntries);
        configuration.getLRUEvictionConfiguration().setLifespan(lifespan);
        try {
            contentCache = this.cacheManager.createNewLocalCache(configuration);
        } catch (CacheException e) {
            throw new MacroExecutionException("Failed to create content cache", e);
        }
        this.contentCacheMap.put(cacheKey, contentCache);
    }
    return contentCache;
}
Also used : CacheException(org.xwiki.cache.CacheException) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) List(java.util.List) LRUCacheConfiguration(org.xwiki.cache.config.LRUCacheConfiguration)

Example 32 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class ContextMacroTest method executeWhenNoDocumentSpecified.

@Test
public void executeWhenNoDocumentSpecified() throws Exception {
    ContextMacroParameters parameters = new ContextMacroParameters();
    try {
        this.mocker.getComponentUnderTest().execute(parameters, "", new MacroTransformationContext());
        fail("Should have thrown an exception");
    } catch (MacroExecutionException expected) {
        Assert.assertEquals("You must specify a 'document' parameter pointing to the document to set in the " + "context as the current document.", expected.getMessage());
    }
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) Test(org.junit.Test)

Example 33 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class VelocityMacro method evaluateString.

@Override
protected String evaluateString(VelocityMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    String result = "";
    try {
        VelocityContext velocityContext = this.velocityManager.getCurrentVelocityContext();
        VelocityMacroFilter filter = getFilter(parameters);
        String cleanedContent = content;
        // Execute pre filter
        if (filter != null) {
            cleanedContent = filter.before(cleanedContent, velocityContext);
        }
        StringWriter writer = new StringWriter();
        // Use the Transformation id as the name passed to the Velocity Engine. This name is used internally
        // by Velocity as a cache index key for caching macros.
        String key = context.getTransformationContext().getId();
        if (key == null) {
            key = "unknown namespace";
        }
        // Execute Velocity context
        this.velocityManager.evaluate(writer, key, new StringReader(cleanedContent));
        result = writer.toString();
        // Execute post filter
        if (filter != null) {
            result = filter.after(result, velocityContext);
        }
    } catch (XWikiVelocityException e) {
        throw new MacroExecutionException("Failed to evaluate Velocity Macro for content [" + content + "]", e);
    }
    return result;
}
Also used : VelocityMacroFilter(org.xwiki.rendering.macro.velocity.filter.VelocityMacroFilter) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) StringReader(java.io.StringReader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 34 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class ScriptClassLoaderHandlerListener method createOrExtendClassLoader.

/**
 * @param createNewClassLoader if true create a new classloader and if false extend an existing one with the passed
 *            additional jars
 * @param jarsParameterValue the value of the macro parameters used to pass extra URLs that should be in the
 *            execution class loader
 * @param classLoader the parent classloader for the classloader to create or the classloader to extend, depending
 *            on the value of the createNewClassLoader parameter
 * @return the new classloader or the extended one
 * @throws Exception in case of an error in building or extending the class loader
 */
private ExtendedURLClassLoader createOrExtendClassLoader(boolean createNewClassLoader, String jarsParameterValue, ClassLoader classLoader) throws Exception {
    ExtendedURLClassLoader cl;
    if (canHaveJarsParameters()) {
        if (createNewClassLoader) {
            cl = this.attachmentClassLoaderFactory.createAttachmentClassLoader(jarsParameterValue, classLoader);
        } else {
            cl = (ExtendedURLClassLoader) classLoader;
            this.attachmentClassLoaderFactory.extendAttachmentClassLoader(jarsParameterValue, cl);
        }
        this.execution.getContext().setProperty(EXECUTION_CONTEXT_JARPARAMS_KEY, jarsParameterValue);
    } else {
        throw new MacroExecutionException("You cannot pass additional jars since you don't have programming rights");
    }
    return cl;
}
Also used : ExtendedURLClassLoader(org.xwiki.classloader.ExtendedURLClassLoader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 35 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class AbstractScriptMacro method execute.

@Override
public List<Block> execute(P parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    List<Block> result = Collections.emptyList();
    if (StringUtils.isNotEmpty(content)) {
        try {
            // send evaluation starts event
            ScriptEvaluatingEvent event = new ScriptEvaluatingEvent(getDescriptor().getId().getId());
            this.observation.notify(event, context, parameters);
            if (event.isCanceled()) {
                throw new MacroExecutionException(event.getReason());
            }
            // 2) Run script engine on macro block content
            List<Block> blocks = evaluateBlock(parameters, content, context);
            if (parameters.isOutput()) {
                result = blocks;
            }
        } finally {
            // send evaluation finished event
            this.observation.notify(new ScriptEvaluatedEvent(getDescriptor().getId().getId()), context, parameters);
        }
    }
    return result;
}
Also used : ScriptEvaluatedEvent(org.xwiki.script.event.ScriptEvaluatedEvent) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block)

Aggregations

MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)48 Test (org.junit.Test)12 Block (org.xwiki.rendering.block.Block)12 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)10 DocumentReference (org.xwiki.model.reference.DocumentReference)9 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)7 MacroBlock (org.xwiki.rendering.block.MacroBlock)7 XDOM (org.xwiki.rendering.block.XDOM)7 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)6 Expectations (org.jmock.Expectations)5 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)5 StringReader (java.io.StringReader)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)4 IncludeMacroParameters (org.xwiki.rendering.macro.include.IncludeMacroParameters)4 HashMap (java.util.HashMap)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 GroupBlock (org.xwiki.rendering.block.GroupBlock)3 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)3 TableBlock (org.xwiki.rendering.block.TableBlock)3