Author: julien_viet
Date: 2010-03-07 04:53:34 -0500 (Sun, 07 Mar 2010)
New Revision: 2021
Modified:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/BinaryOutput.java
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/OutputStreamPrinter.java
portal/trunk/component/portal/src/main/java/org/exoplatform/portal/resource/ResourceRenderer.java
portal/trunk/component/portal/src/main/java/org/exoplatform/portal/resource/SkinService.java
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/OutputStreamWriterGroovyPrinter.java
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/ResourceRequestFilter.java
Log:
- get rid of the resource filter cache as it is possible to cache directly the binary
encoded array in the css service itself (actually the cache was very badly used (my fault
btw))
Modified:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/BinaryOutput.java
===================================================================
---
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/BinaryOutput.java 2010-03-06
22:24:53 UTC (rev 2020)
+++
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/BinaryOutput.java 2010-03-07
09:53:34 UTC (rev 2021)
@@ -34,6 +34,6 @@
void write(byte[] bytes) throws IOException;
- void write(byte[] b, int off, int len) throws IOException;
+ void write(byte[] bytes, int off, int len) throws IOException;
}
Modified:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/OutputStreamPrinter.java
===================================================================
---
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/OutputStreamPrinter.java 2010-03-06
22:24:53 UTC (rev 2020)
+++
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/OutputStreamPrinter.java 2010-03-07
09:53:34 UTC (rev 2021)
@@ -257,13 +257,13 @@
}
}
- public final void write(byte[] b, int off, int len) throws IOException
+ public final void write(byte[] bytes, int off, int len) throws IOException
{
if (!failed)
{
try
{
- out.write(b, off, len);
+ out.write(bytes, off, len);
}
catch (IOException e)
{
Modified:
portal/trunk/component/portal/src/main/java/org/exoplatform/portal/resource/ResourceRenderer.java
===================================================================
---
portal/trunk/component/portal/src/main/java/org/exoplatform/portal/resource/ResourceRenderer.java 2010-03-06
22:24:53 UTC (rev 2020)
+++
portal/trunk/component/portal/src/main/java/org/exoplatform/portal/resource/ResourceRenderer.java 2010-03-07
09:53:34 UTC (rev 2021)
@@ -19,6 +19,10 @@
package org.exoplatform.portal.resource;
+import org.exoplatform.commons.utils.BinaryOutput;
+
+import java.io.IOException;
+
/**
* An interface defining the renderer contract for a resource.
*
@@ -29,11 +33,12 @@
{
/**
- * Returns an appendable for the performing the rendering of the resource.
+ * Returns an output stream for performing resource rendering.
*
- * @return the appendable
+ * @return a stream
+ * @throws IOException any io exception
*/
- Appendable getAppendable();
+ BinaryOutput getOutput() throws IOException;
/**
* Instruct the renderer about the expiration time in seconds. A non positive value
Modified:
portal/trunk/component/portal/src/main/java/org/exoplatform/portal/resource/SkinService.java
===================================================================
---
portal/trunk/component/portal/src/main/java/org/exoplatform/portal/resource/SkinService.java 2010-03-06
22:24:53 UTC (rev 2020)
+++
portal/trunk/component/portal/src/main/java/org/exoplatform/portal/resource/SkinService.java 2010-03-07
09:53:34 UTC (rev 2021)
@@ -19,6 +19,8 @@
package org.exoplatform.portal.resource;
+import org.exoplatform.commons.utils.BinaryOutput;
+import org.exoplatform.commons.utils.ByteArrayOutput;
import org.exoplatform.commons.utils.PropertyManager;
import org.exoplatform.commons.utils.Safe;
import org.exoplatform.container.ExoContainerContext;
@@ -111,9 +113,9 @@
private final HashSet<String> availableSkins_;
- private final Map<String, String> ltCache;
+ private final Map<String, CachedStylesheet> ltCache;
- private final Map<String, String> rtCache;
+ private final Map<String, CachedStylesheet> rtCache;
private final Map<String, Set<String>> portletThemes_;
@@ -137,8 +139,8 @@
portalSkins_ = new LinkedHashMap<SkinKey, SkinConfig>();
skinConfigs_ = new LinkedHashMap<SkinKey, SkinConfig>(20);
availableSkins_ = new HashSet<String>(5);
- ltCache = new ConcurrentHashMap<String, String>();
- rtCache = new ConcurrentHashMap<String, String>();
+ ltCache = new ConcurrentHashMap<String, CachedStylesheet>();
+ rtCache = new ConcurrentHashMap<String, CachedStylesheet>();
portletThemes_ = new HashMap<String, Set<String>>();
portalContainerName = context.getPortalContainerName();
mainResolver = new MainResourceResolver(portalContainerName, skinConfigs_);
@@ -174,10 +176,6 @@
*/
public void addPortalSkin(String module, String skinName, String cssPath,
ServletContext scontext, boolean overwrite)
{
-
- // // Triggers a put if absent
- // mainResolver.registerContext(scontext);
-
availableSkins_.add(skinName);
SkinKey key = new SkinKey(module, skinName);
SkinConfig skinConfig = portalSkins_.get(key);
@@ -196,8 +194,8 @@
{
portalSkins_.put(key, new SimpleSkin(this, module, skinName, cssPath));
}
- ltCache.put(cssPath, cssData);
- rtCache.put(cssPath, cssData);
+ ltCache.put(cssPath, new CachedStylesheet(cssData));
+ rtCache.put(cssPath, new CachedStylesheet(cssData));
}
public void addSkin(String module, String skinName, String cssPath, ServletContext
scontext)
@@ -230,9 +228,6 @@
public void addSkin(String module, String skinName, String cssPath, ServletContext
scontext, boolean overwrite)
{
- // // Triggers a put if absent
- // mainResolver.registerContext(scontext);
-
availableSkins_.add(skinName);
SkinKey key = new SkinKey(module, skinName);
SkinConfig skinConfig = skinConfigs_.get(key);
@@ -245,7 +240,6 @@
public void addSkin(String module, String skinName, String cssPath, String cssData)
{
- //
availableSkins_.add(skinName);
SkinKey key = new SkinKey(module, skinName);
SkinConfig skinConfig = skinConfigs_.get(key);
@@ -253,8 +247,8 @@
{
skinConfigs_.put(key, new SimpleSkin(this, module, skinName, cssPath));
}
- ltCache.put(cssPath, cssData);
- rtCache.put(cssPath, cssData);
+ ltCache.put(cssPath, new CachedStylesheet(cssData));
+ rtCache.put(cssPath, new CachedStylesheet(cssData));
}
public void addTheme(String categoryName, List<String> themesName)
@@ -287,20 +281,18 @@
{
try
{
- final StringBuilder sb = new StringBuilder();
+ final ByteArrayOutput output = new ByteArrayOutput();
renderCSS(new ResourceRenderer()
{
- public Appendable getAppendable()
+ public BinaryOutput getOutput() throws IOException
{
- return sb;
+ return output;
}
-
public void setExpiration(long seconds)
{
-
}
}, cssPath);
- return sb.toString();
+ return output.toString();
}
catch (IOException e)
{
@@ -341,26 +333,30 @@
}
//
- Map<String, String> cache = orientation == Orientation.LT ? ltCache :
rtCache;
- String css = cache.get(path);
+ Map<String, CachedStylesheet> cache = orientation == Orientation.LT ?
ltCache : rtCache;
+ CachedStylesheet css = cache.get(path);
if (css == null)
{
StringBuilder sb = new StringBuilder();
processCSS(sb, path, orientation, true);
- css = sb.toString();
+ css = new CachedStylesheet(sb.toString());
cache.put(path, css);
}
- renderer.getAppendable().append(css);
+ css.writeTo(renderer.getOutput());
}
else
{
- processCSS(renderer.getAppendable(), path, orientation, false);
+ StringBuffer sb = new StringBuffer();
+ processCSS(sb, path, orientation, false);
+ byte[] bytes = sb.toString().getBytes("UTF-8");
+ renderer.getOutput().write(bytes);
}
}
public String getMergedCSS(String cssPath)
{
- return ltCache.get(cssPath);
+ CachedStylesheet stylesheet = ltCache.get(cssPath);
+ return stylesheet != null ? stylesheet.getText() : null;
}
public Collection<SkinConfig> getPortalSkins(String skinName)
Modified:
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/OutputStreamWriterGroovyPrinter.java
===================================================================
---
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/OutputStreamWriterGroovyPrinter.java 2010-03-06
22:24:53 UTC (rev 2020)
+++
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/OutputStreamWriterGroovyPrinter.java 2010-03-07
09:53:34 UTC (rev 2021)
@@ -91,9 +91,9 @@
out.write(bytes);
}
- public void write(byte[] b, int off, int len) throws IOException
+ public void write(byte[] bytes, int off, int len) throws IOException
{
- out.write(b, off, len);
+ out.write(bytes, off, len);
}
public void write(byte b) throws IOException
Modified:
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/ResourceRequestFilter.java
===================================================================
---
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/ResourceRequestFilter.java 2010-03-06
22:24:53 UTC (rev 2020)
+++
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/ResourceRequestFilter.java 2010-03-07
09:53:34 UTC (rev 2021)
@@ -19,17 +19,11 @@
package org.exoplatform.portal.application;
-import org.exoplatform.commons.utils.CharsetCharEncoder;
-import org.exoplatform.commons.utils.CharsetTextEncoder;
-import org.exoplatform.commons.utils.PropertyManager;
-import org.exoplatform.commons.utils.TableCharEncoder;
-import org.exoplatform.commons.utils.TextEncoder;
+import org.exoplatform.commons.utils.*;
import org.exoplatform.container.ExoContainer;
import org.exoplatform.container.web.AbstractFilter;
import org.exoplatform.portal.resource.ResourceRenderer;
import org.exoplatform.portal.resource.SkinService;
-import org.exoplatform.services.cache.ExoCache;
-import org.exoplatform.services.cache.concurrent.ConcurrentFIFOExoCache;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
@@ -41,6 +35,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
+import java.nio.charset.Charset;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -59,27 +54,22 @@
public class ResourceRequestFilter extends AbstractFilter
{
- protected static Log log =
ExoLogger.getLogger("portal:ResourceRequestFilter");
+ protected static Log log = ExoLogger.getLogger(ResourceRequestFilter.class);
+ private static final Charset UTF_8 = Charset.forName("UTF-8");
+
private FilterConfig cfg;
private ImageType[] imageTypes = ImageType.values();
- private ConcurrentMap<String, FutureTask<Image>> mirroredImageCache =
- new ConcurrentHashMap<String, FutureTask<Image>>();
+ private ConcurrentMap<String, FutureTask<Image>> mirroredImageCache = new
ConcurrentHashMap<String, FutureTask<Image>>();
- private ExoCache cssCache = new ConcurrentFIFOExoCache(50);
-
public void afterInit(FilterConfig filterConfig)
{
cfg = filterConfig;
log.info("Cache eXo Resource at client: " +
!PropertyManager.isDevelopping());
}
- /** The optimized encoder. */
- private static final TextEncoder encoder =
- new CharsetTextEncoder(new TableCharEncoder(CharsetCharEncoder.getUTF8()));
-
public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain) throws IOException,
ServletException
{
@@ -93,75 +83,31 @@
if (uri.endsWith(".css"))
{
final OutputStream out = response.getOutputStream();
- final Appendable app = new Appendable()
+ final BinaryOutput output = new BinaryOutput()
{
- public Appendable append(CharSequence csq) throws IOException
+ public Charset getCharset()
{
- // julien : yeah there is a nasty cast but for now it is ok as we know it
is a string
- // need to work on an optimized appender for
- String s = (String)csq;
-
- // Get existing bytes
- byte[] bytes = null;
- try
- {
- bytes = (byte[])cssCache.get(s);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
-
- // Get bytes if needed
- if (bytes == null)
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream(s.length() *
2);
- encoder.encode(s, 0, s.length(), baos);
- baos.flush();
- bytes = baos.toByteArray();
- try
- {
- cssCache.put(s, bytes);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
-
- //
- try
- {
- out.write(bytes);
- }
- catch (IOException ignore)
- {
- }
-
- //
- return this;
+ return UTF_8;
}
-
- public Appendable append(CharSequence csq, int start, int end) throws
IOException
+ public void write(byte b) throws IOException
{
- throw new UnsupportedOperationException("Should no be called");
+ out.write(b);
}
-
- public Appendable append(char c) throws IOException
+ public void write(byte[] bytes) throws IOException
{
- encoder.encode(c, out);
- return this;
+ out.write(bytes);
}
+ public void write(byte[] bytes, int off, int len) throws IOException
+ {
+ out.write(bytes, off, len);
+ }
};
-
ResourceRenderer renderer = new ResourceRenderer()
{
- public Appendable getAppendable()
+ public BinaryOutput getOutput() throws IOException
{
- //
- return app;
+ return output;
}
-
public void setExpiration(long seconds)
{
if (seconds > 0)