1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.lesscss.internal.cache

File AbstractCachedCompiler.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
22
2
1
177
72
9
0.41
11
2
4.5

Classes

Class Line # Actions
AbstractCachedCompiler 46 22 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.lesscss.internal.cache;
21   
22    import javax.inject.Inject;
23    import javax.inject.Provider;
24   
25    import org.slf4j.Logger;
26    import org.xwiki.lesscss.compiler.LESSCompilerException;
27    import org.xwiki.lesscss.internal.LESSContext;
28    import org.xwiki.lesscss.internal.colortheme.ColorThemeReference;
29    import org.xwiki.lesscss.internal.colortheme.ColorThemeReferenceFactory;
30    import org.xwiki.lesscss.internal.colortheme.CurrentColorThemeGetter;
31    import org.xwiki.lesscss.internal.compiler.DefaultLESSCompiler;
32    import org.xwiki.lesscss.internal.skin.SkinReference;
33    import org.xwiki.lesscss.internal.skin.SkinReferenceFactory;
34    import org.xwiki.lesscss.resources.LESSResourceReference;
35   
36    import com.xpn.xwiki.XWikiContext;
37   
38    /**
39    * Implements a cache system to prevent the compiler to be called too often.
40    *
41    * @param <T> class of the expected results
42    *
43    * @since 6.4M2
44    * @version $Id: 5e13c50c3449e96d7861ce1c8891e756c5f2705e $
45    */
 
46    public abstract class AbstractCachedCompiler<T>
47    {
48    protected LESSCache<T> cache;
49   
50    protected CachedCompilerInterface<T> compiler;
51   
52    @Inject
53    protected Provider<XWikiContext> xcontextProvider;
54   
55    @Inject
56    protected CurrentColorThemeGetter currentColorThemeGetter;
57   
58    @Inject
59    protected SkinReferenceFactory skinReferenceFactory;
60   
61    @Inject
62    protected ColorThemeReferenceFactory colorThemeReferenceFactory;
63   
64    @Inject
65    protected LESSContext lessContext;
66   
67    @Inject
68    protected Logger logger;
69   
70    /**
71    * Get the result of the compilation.
72    * @param lessResourceReference reference to the LESS content
73    * @param includeSkinStyle include the main LESS file of the skin in order to have variables and mix-ins
74    * defined there
75    * @param useVelocity either or not the resource be parsed by Velocity before compiling it
76    * @param force force the computation, even if the output is already in the cache (not recommended)
77    * @return the desired object
78    * @throws LESSCompilerException if problems occur
79    */
 
80  2133 toggle public T getResult(LESSResourceReference lessResourceReference, boolean includeSkinStyle, boolean useVelocity,
81    boolean force) throws LESSCompilerException
82    {
83  2133 XWikiContext context = xcontextProvider.get();
84  2133 String skin = context.getWiki().getSkin(context);
85  2132 return getResult(lessResourceReference, includeSkinStyle, useVelocity, skin, force);
86    }
87   
88    /**
89    * Get the result of the compilation.
90    * @param lessResourceReference reference to the LESS content
91    * @param includeSkinStyle include the main LESS file of the skin in order to have variables and mix-ins
92    * defined there
93    * @param useVelocity either or not the resource be parsed by Velocity before compiling it
94    * @param force force the computation, even if the output is already in the cache (not recommended)
95    * @param skin name of the skin used for the context
96    * @return the desired object
97    * @throws LESSCompilerException if problems occur
98    */
 
99  2166 toggle public T getResult(LESSResourceReference lessResourceReference, boolean includeSkinStyle, boolean useVelocity,
100    String skin, boolean force) throws LESSCompilerException
101    {
102    // If the cache is disabled, we just compile
103  2168 if (lessContext.isCacheDisabled()) {
104  1 return compiler.compute(lessResourceReference, includeSkinStyle, useVelocity, true, skin);
105    }
106   
107  2168 T result = null;
108   
109  2168 SkinReference skinReference = skinReferenceFactory.createReference(skin);
110  2169 ColorThemeReference colorThemeReference = colorThemeReferenceFactory.createReference(
111    currentColorThemeGetter.getCurrentColorTheme(true, "default"));
112   
113    // Only one computation is allowed in the same time per color theme, then the waiting threads will be able to
114    // use the last result stored in the cache.
115  2169 Object mutex = cache.getMutex(lessResourceReference, skinReference, colorThemeReference);
116  2169 synchronized (mutex) {
117   
118    // Check if the result is in the cache
119  2169 if (!force) {
120  2168 result = cache.get(lessResourceReference, skinReference, colorThemeReference);
121  2168 if (result != null) {
122    // The LESS file contains Velocity code that call resources (ie: $xwiki.getSkinFile), and the HTML
123    // exporter listens these calls to know which resources must be exported.
124    // If we only use the cache, we would have a correct CSS file but some resources will be missing.
125    // So we need to execute the velocity again, even if the LESS file is cached.
126    // To perform this quickly, we do not recompile the LESS code (which would be useless anyway), but
127    // we only do the Velocity Execution step.
128  2097 if (lessContext.isHtmlExport() && useVelocity && this instanceof DefaultLESSCompiler) {
129  1 compiler.compute(lessResourceReference, includeSkinStyle, true, false, skin);
130    }
131  2097 return cloneResult(result);
132    }
133    }
134   
135    // Either the result was in the cache or the force flag is set to true, we need to compile
136  72 try {
137  72 result = compiler.compute(lessResourceReference, includeSkinStyle, useVelocity, true, skin);
138    } catch (LESSCompilerException e) {
139  1 logger.error("Error during the compilation of the resource [{}].", lessResourceReference, e);
140    // We must cache the result, even if the compilation have failed, to prevent re-compiling again and
141    // again (the compilation will still fail until the LESS resource is updated so it useless to retry).
142  1 result = exceptionAsResult(e);
143    } finally {
144    // Put the result in the cache
145  72 cache.set(lessResourceReference, skinReference, colorThemeReference, result);
146    }
147    }
148   
149  72 return cloneResult(result);
150    }
151   
152    /**
153    * Returns a clone of the result to avoid returning the instance stored in the cache. Need to be implemented by
154    * subclasses.
155    * @param toClone result to clone
156    * @return a clone of the result
157    *
158    * @since 6.4M3
159    */
160    protected abstract T cloneResult(T toClone);
161   
162    /**
163    * Convert an exception to a result object that we can store in the cache. Thanks to this, the compilation will not
164    * be restarted until the cache is cleared. It is needed because the cache cannot store null values.
165    *
166    * This method must be overrided. For example, it could return the serialized exception or an empty object.
167    *
168    * @param exception exception to store in the cache
169    * @return an object that can be stored in the cache and returned to the user next time
170    *
171    * @since 7.3M1
172    * @since 6.4.6
173    * @since 7.1.2
174    * @since 7.2.1
175    */
176    protected abstract T exceptionAsResult(LESSCompilerException exception);
177    }