Search in sources :

Example 1 with SkinReference

use of org.xwiki.lesscss.internal.skin.SkinReference 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 SkinReference

use of org.xwiki.lesscss.internal.skin.SkinReference in project xwiki-platform by xwiki.

the class LessCompilerScriptService method clearCacheFromSkin.

/**
 * Remove every generated files corresponding to a filesystem skin.
 * The script calling this method needs the programming rights.
 * @param skin name of the filesystem skin
 * @return true if the operation succeed
 *
 * @since 6.4M2
 */
public boolean clearCacheFromSkin(String skin) {
    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 {
        SkinReference skinReference = skinReferenceFactory.createReference(skin);
        lessCache.clearFromSkin(skinReference);
        colorThemeCache.clearFromSkin(skinReference);
        return true;
    } catch (LESSCompilerException e) {
        return false;
    }
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) SkinReference(org.xwiki.lesscss.internal.skin.SkinReference) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException)

Example 3 with SkinReference

use of org.xwiki.lesscss.internal.skin.SkinReference in project xwiki-platform by xwiki.

the class LessCompilerScriptServiceTest method clearCacheFromSkinWithRights.

@Test
public void clearCacheFromSkinWithRights() 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);
    SkinReference skinReference = mock(SkinReference.class);
    when(skinReferenceFactory.createReference("skin")).thenReturn(skinReference);
    // Tests
    assertTrue(mocker.getComponentUnderTest().clearCacheFromSkin("skin"));
    // Verify
    verify(lessCache).clearFromSkin(skinReference);
    verify(colorThemeCache).clearFromSkin(skinReference);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) SkinReference(org.xwiki.lesscss.internal.skin.SkinReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with SkinReference

use of org.xwiki.lesscss.internal.skin.SkinReference in project xwiki-platform by xwiki.

the class CacheKeyFactoryTest method getCacheKeyWithContext.

@Test
public void getCacheKeyWithContext() throws Exception {
    // Mocks
    LESSResourceReference lessResource = mock(LESSResourceReference.class);
    SkinReference skin = mock(SkinReference.class);
    ColorThemeReference colorTheme = mock(ColorThemeReference.class);
    when(lessResource.serialize()).thenReturn("lessResource");
    when(skin.serialize()).thenReturn("skin");
    when(colorTheme.serialize()).thenReturn("colorTheme");
    when(xcontextCacheKeyFactory.getCacheKey()).thenReturn("XWikiContext[Mock]");
    // Test
    assertEquals("12_lessResource_4_skin_10_colorTheme_18_XWikiContext[Mock]", mocker.getComponentUnderTest().getCacheKey(lessResource, skin, colorTheme, true));
}
Also used : ColorThemeReference(org.xwiki.lesscss.internal.colortheme.ColorThemeReference) SkinReference(org.xwiki.lesscss.internal.skin.SkinReference) LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference) Test(org.junit.Test)

Example 5 with SkinReference

use of org.xwiki.lesscss.internal.skin.SkinReference in project xwiki-platform by xwiki.

the class CacheKeyFactoryTest method getCacheKeyWithoutContext.

@Test
public void getCacheKeyWithoutContext() throws Exception {
    // Mocks
    LESSResourceReference lessResource = mock(LESSResourceReference.class);
    SkinReference skin = mock(SkinReference.class);
    ColorThemeReference colorTheme = mock(ColorThemeReference.class);
    when(lessResource.serialize()).thenReturn("lessResource");
    when(skin.serialize()).thenReturn("skin");
    when(colorTheme.serialize()).thenReturn("colorTheme");
    // Test
    assertEquals("12_lessResource_4_skin_10_colorTheme", mocker.getComponentUnderTest().getCacheKey(lessResource, skin, colorTheme, false));
}
Also used : ColorThemeReference(org.xwiki.lesscss.internal.colortheme.ColorThemeReference) SkinReference(org.xwiki.lesscss.internal.skin.SkinReference) LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference) Test(org.junit.Test)

Aggregations

SkinReference (org.xwiki.lesscss.internal.skin.SkinReference)6 Test (org.junit.Test)3 ColorThemeReference (org.xwiki.lesscss.internal.colortheme.ColorThemeReference)3 LESSCompilerException (org.xwiki.lesscss.compiler.LESSCompilerException)2 LESSResourceReference (org.xwiki.lesscss.resources.LESSResourceReference)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 DefaultLESSCompiler (org.xwiki.lesscss.internal.compiler.DefaultLESSCompiler)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1