Search in sources :

Example 1 with ColorThemeReference

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);
}
Also used : ColorThemeReference(org.xwiki.lesscss.internal.colortheme.ColorThemeReference) DefaultLESSCompiler(org.xwiki.lesscss.internal.compiler.DefaultLESSCompiler) SkinReference(org.xwiki.lesscss.internal.skin.SkinReference) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException)

Example 2 with ColorThemeReference

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);
}
Also used : ColorThemeReference(org.xwiki.lesscss.internal.colortheme.ColorThemeReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 3 with 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);
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentColorThemeReference(org.xwiki.lesscss.internal.colortheme.DocumentColorThemeReference) ColorThemeReference(org.xwiki.lesscss.internal.colortheme.ColorThemeReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentColorThemeReference(org.xwiki.lesscss.internal.colortheme.DocumentColorThemeReference) EntityReference(org.xwiki.model.reference.EntityReference) ArrayList(java.util.ArrayList) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) Event(org.xwiki.observation.event.Event) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) BaseObject(com.xpn.xwiki.objects.BaseObject) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 4 with 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);
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentColorThemeReference(org.xwiki.lesscss.internal.colortheme.DocumentColorThemeReference) ColorThemeReference(org.xwiki.lesscss.internal.colortheme.ColorThemeReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentColorThemeReference(org.xwiki.lesscss.internal.colortheme.DocumentColorThemeReference) EntityReference(org.xwiki.model.reference.EntityReference) ArrayList(java.util.ArrayList) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) Event(org.xwiki.observation.event.Event) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) BaseObject(com.xpn.xwiki.objects.BaseObject) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 5 with 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;
    }
}
Also used : ColorThemeReference(org.xwiki.lesscss.internal.colortheme.ColorThemeReference) XWikiContext(com.xpn.xwiki.XWikiContext) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException)

Aggregations

ColorThemeReference (org.xwiki.lesscss.internal.colortheme.ColorThemeReference)8 Test (org.junit.Test)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 SkinReference (org.xwiki.lesscss.internal.skin.SkinReference)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 BaseObject (com.xpn.xwiki.objects.BaseObject)2 ArrayList (java.util.ArrayList)2 DocumentCreatedEvent (org.xwiki.bridge.event.DocumentCreatedEvent)2 DocumentDeletedEvent (org.xwiki.bridge.event.DocumentDeletedEvent)2 DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)2 LESSCompilerException (org.xwiki.lesscss.compiler.LESSCompilerException)2 DocumentColorThemeReference (org.xwiki.lesscss.internal.colortheme.DocumentColorThemeReference)2 LESSResourceReference (org.xwiki.lesscss.resources.LESSResourceReference)2 EntityReference (org.xwiki.model.reference.EntityReference)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 Event (org.xwiki.observation.event.Event)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 DefaultLESSCompiler (org.xwiki.lesscss.internal.compiler.DefaultLESSCompiler)1