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;
}
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());
}
}
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;
}
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;
}
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;
}
Aggregations