use of org.xwiki.lesscss.internal.colortheme.ColorThemeReference in project xwiki-platform by xwiki.
the class AbstractCachedCompiler method getResult.
/**
* Get the result of the compilation.
* @param lessResourceReference reference to the LESS content
* @param includeSkinStyle include the main LESS file of the skin in order to have variables and mix-ins
* defined there
* @param useVelocity either or not the resource be parsed by Velocity before compiling it
* @param force force the computation, even if the output is already in the cache (not recommended)
* @param skin name of the skin used for the context
* @return the desired object
* @throws LESSCompilerException if problems occur
*/
public T getResult(LESSResourceReference lessResourceReference, boolean includeSkinStyle, boolean useVelocity, String skin, boolean force) throws LESSCompilerException {
// If the cache is disabled, we just compile
if (lessContext.isCacheDisabled()) {
return compiler.compute(lessResourceReference, includeSkinStyle, useVelocity, true, skin);
}
T result = null;
SkinReference skinReference = skinReferenceFactory.createReference(skin);
ColorThemeReference colorThemeReference = colorThemeReferenceFactory.createReference(currentColorThemeGetter.getCurrentColorTheme(true, "default"));
// Only one computation is allowed in the same time per color theme, then the waiting threads will be able to
// use the last result stored in the cache.
Object mutex = cache.getMutex(lessResourceReference, skinReference, colorThemeReference);
synchronized (mutex) {
// Check if the result is in the cache
if (!force) {
result = cache.get(lessResourceReference, skinReference, colorThemeReference);
if (result != null) {
// we only do the Velocity Execution step.
if (lessContext.isHtmlExport() && useVelocity && this instanceof DefaultLESSCompiler) {
compiler.compute(lessResourceReference, includeSkinStyle, true, false, skin);
}
return cloneResult(result);
}
}
// Either the result was in the cache or the force flag is set to true, we need to compile
try {
result = compiler.compute(lessResourceReference, includeSkinStyle, useVelocity, true, skin);
} catch (LESSCompilerException e) {
logger.error("Error during the compilation of the resource [{}].", lessResourceReference, e);
// We must cache the result, even if the compilation have failed, to prevent re-compiling again and
// again (the compilation will still fail until the LESS resource is updated so it useless to retry).
result = exceptionAsResult(e);
} finally {
// Put the result in the cache
cache.set(lessResourceReference, skinReference, colorThemeReference, result);
}
}
return cloneResult(result);
}
use of org.xwiki.lesscss.internal.colortheme.ColorThemeReference in project xwiki-platform by xwiki.
the class LessCompilerScriptServiceTest method clearCacheFromColorThemeWithRights.
@Test
public void clearCacheFromColorThemeWithRights() throws Exception {
// Mocks
XWikiDocument doc = mock(XWikiDocument.class);
DocumentReference authorReference = new DocumentReference("wiki", "Space", "User");
when(xcontext.getDoc()).thenReturn(doc);
when(doc.getAuthorReference()).thenReturn(authorReference);
DocumentReference currentDocReference = new DocumentReference("wiki", "Space", "Page");
when(doc.getDocumentReference()).thenReturn(currentDocReference);
when(authorizationManager.hasAccess(Right.PROGRAM, authorReference, currentDocReference)).thenReturn(true);
ColorThemeReference colorThemeReference = mock(ColorThemeReference.class);
when(colorThemeReferenceFactory.createReference("colorTheme")).thenReturn(colorThemeReference);
// Tests
assertTrue(mocker.getComponentUnderTest().clearCacheFromColorTheme("colorTheme"));
// Verify
verify(lessCache).clearFromColorTheme(colorThemeReference);
verify(colorThemeCache).clearFromColorTheme(colorThemeReference);
}
use of org.xwiki.lesscss.internal.colortheme.ColorThemeReference in project xwiki-platform by xwiki.
the class ColorThemeListenerTest method onEventWhenColorThemeChanged.
@Test
public void onEventWhenColorThemeChanged() throws Exception {
// Mocks
Event event = mock(Event.class);
XWikiDocument doc = mock(XWikiDocument.class);
Object data = new Object();
EntityReference classReference = new LocalDocumentReference("ColorThemes", "ColorThemeClass");
List<BaseObject> objects = new ArrayList<>();
BaseObject object = mock(BaseObject.class);
objects.add(object);
when(doc.getXObjects(classReference)).thenReturn(objects);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
when(doc.getDocumentReference()).thenReturn(documentReference);
ColorThemeReference colorThemeReference = new DocumentColorThemeReference(documentReference, null);
when(colorThemeReferenceFactory.createReference(eq(documentReference))).thenReturn(colorThemeReference);
// Test
mocker.getComponentUnderTest().onEvent(event, doc, data);
// Verify
verify(lessResourcesCache).clearFromColorTheme(colorThemeReference);
verify(colorThemeCache).clearFromColorTheme(colorThemeReference);
}
use of org.xwiki.lesscss.internal.colortheme.ColorThemeReference in project xwiki-platform by xwiki.
the class ColorThemeListenerTest method onEventWhenFlamingoThemeChanged.
@Test
public void onEventWhenFlamingoThemeChanged() throws Exception {
// Mocks
Event event = mock(Event.class);
XWikiDocument doc = mock(XWikiDocument.class);
Object data = new Object();
EntityReference classReference = new LocalDocumentReference("FlamingoThemesCode", "ThemeClass");
List<BaseObject> objects = new ArrayList<>();
BaseObject object = mock(BaseObject.class);
objects.add(object);
when(doc.getXObjects(classReference)).thenReturn(objects);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
when(doc.getDocumentReference()).thenReturn(documentReference);
ColorThemeReference colorThemeReference = new DocumentColorThemeReference(documentReference, null);
when(colorThemeReferenceFactory.createReference(eq(documentReference))).thenReturn(colorThemeReference);
// Test
mocker.getComponentUnderTest().onEvent(event, doc, data);
// Verify
verify(lessResourcesCache).clearFromColorTheme(colorThemeReference);
verify(colorThemeCache).clearFromColorTheme(colorThemeReference);
}
use of org.xwiki.lesscss.internal.colortheme.ColorThemeReference in project xwiki-platform by xwiki.
the class LessCompilerScriptService method clearCacheFromColorTheme.
/**
* Remove every generated files corresponding to a color theme.
* The script calling this method needs the programming rights.
* @param colorTheme fullname of the color theme
* @return true if the operation succeed
*/
public boolean clearCacheFromColorTheme(String colorTheme) {
XWikiContext xcontext = xcontextProvider.get();
// Check if the current script has the programing rights
if (!authorizationManager.hasAccess(Right.PROGRAM, xcontext.getDoc().getAuthorReference(), xcontext.getDoc().getDocumentReference())) {
return false;
}
try {
ColorThemeReference colorThemeReference = colorThemeReferenceFactory.createReference(colorTheme);
lessCache.clearFromColorTheme(colorThemeReference);
colorThemeCache.clearFromColorTheme(colorThemeReference);
return true;
} catch (LESSCompilerException e) {
return false;
}
}
Aggregations