Author: rbaxter85
Date: Mon Dec 23 21:08:39 2013
New Revision: 1553211
URL: http://svn.apache.org/r1553211
Log:
SHINDIG-1935
Delivered For Yun Zhi Lin
Fix sizeof warnings when parsing templates with large amounts of text
Modified:
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateLibraryFactory.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/TemplateLibraryFactoryTest.java
Modified:
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml?rev=1553211&r1=1553210&r2=1553211&view=diff
==============================================================================
---
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml
(original)
+++
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml
Mon Dec 23 21:08:39 2013
@@ -152,4 +152,13 @@ under the License.
-->
<sizeOfPolicy maxDepth="5000" maxDepthExceededBehavior="abort"/>
</cache>
+
+ <!-- Used to cache Gadget template library -->
+ <cache name="parsedXml">
+ <!--
+ The elements stored in this cache are complex and the default
sizeOfPolicy
+ is insufficient.
+ -->
+ <sizeOfPolicy maxDepth="5000" maxDepthExceededBehavior="continue"/>
+ </cache>
</ehcache>
Modified:
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateLibraryFactory.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateLibraryFactory.java?rev=1553211&r1=1553210&r2=1553211&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateLibraryFactory.java
(original)
+++
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/templates/TemplateLibraryFactory.java
Mon Dec 23 21:08:39 2013
@@ -18,6 +18,10 @@
*/
package org.apache.shindig.gadgets.templates;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+
import org.apache.shindig.auth.AnonymousSecurityToken;
import org.apache.shindig.common.cache.Cache;
import org.apache.shindig.common.cache.CacheProvider;
@@ -82,6 +86,27 @@ public class TemplateLibraryFactory {
}
if (element == null) {
+ // JIRA 1935
+ // rewrite the template content to reduce the object number counted by
ehcache
+ if (!context.getDebug()) {
+ BufferedReader reader = new BufferedReader(new
StringReader(content));
+ StringBuilder sb = new StringBuilder();
+ String s;
+ try {
+ while ((s = reader.readLine()) != null) {
+ sb.append(s);
+ }
+ content = sb.toString();
+ } catch (IOException e) {
+ // not re-throw exception here
+ // If it fails to rewrite the string, just uses the original
string for xml parsing
+ } finally {
+ try {
+ reader.close();
+ } catch (IOException e) {}
+ }
+ }
+
element = XmlUtil.parse(content);
if (key != null) {
parsedXmlCache.addElement(key, element);
Modified:
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/TemplateLibraryFactoryTest.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/TemplateLibraryFactoryTest.java?rev=1553211&r1=1553210&r2=1553211&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/TemplateLibraryFactoryTest.java
(original)
+++
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/templates/TemplateLibraryFactoryTest.java
Mon Dec 23 21:08:39 2013
@@ -24,28 +24,37 @@ import static org.junit.Assert.assertTru
import org.apache.shindig.auth.SecurityToken;
import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.common.xml.XmlException;
+import org.apache.shindig.common.xml.XmlUtil;
+import org.apache.shindig.expressions.Expressions;
import org.apache.shindig.gadgets.GadgetContext;
import org.apache.shindig.gadgets.GadgetException;
import org.apache.shindig.gadgets.http.HttpRequest;
import org.apache.shindig.gadgets.http.HttpResponse;
import org.apache.shindig.gadgets.http.HttpResponseBuilder;
import org.apache.shindig.gadgets.http.RequestPipeline;
+import org.apache.shindig.gadgets.templates.tags.TagHandler;
import org.junit.Test;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import com.google.common.collect.ImmutableMap;
public class TemplateLibraryFactoryTest {
public static final Uri SPEC_URL =
Uri.parse("http://www.example.org/dir/g.xml");
public static final Uri TEMPLATE_URL =
Uri.parse("http://www.example.org/dir/template.xml");
+ public static final String NAMESPACE_URI = "#my";
private static final String TEMPLATE_LIBRARY =
- "<Templates xmlns:my='#my'>" +
- " <Namespace prefix='my' url='#my'/>" +
- " <JavaScript>script</JavaScript>" +
- " <Style>style</Style>" +
- " <Template tag='my:Tag1'>external1</Template>" +
- " <Template tag='my:Tag2'>external2</Template>" +
- " <Template tag='my:Tag3'>external3</Template>" +
- " <Template tag='my:Tag4'>external4</Template>" +
+ "<Templates xmlns:my='#my'>" + '\n' +
+ " <Namespace prefix='my' url='#my'/>" + '\n' +
+ " <JavaScript>script;\nscript2</JavaScript>" + '\n' +
+ " <Style>style\nstyle2</Style>" + '\n' +
+ " <Template tag='my:Tag1'>external1</Template>" + '\n' +
+ " <Template tag='my:Tag2'>external2</Template>" + '\n' +
+ " <Template tag='my:Tag3'>external3</Template>" + '\n' +
+ " <Template tag='my:Tag4'>external4</Template>" + '\n' +
"</Templates>";
@Test
@@ -83,6 +92,66 @@ public class TemplateLibraryFactoryTest
assertTrue(pipeline.request.getIgnoreCache());
}
+ @Test
+ public void testTemplateLibraryRewrite() throws GadgetException,
XmlException {
+ CapturingPipeline pipeline = new CapturingPipeline();
+ TemplateLibraryFactory factory = new TemplateLibraryFactory( pipeline,
null );
+ GadgetContext context = new GadgetContext() {
+ @Override
+ public Uri getUrl() {
+ return SPEC_URL;
+ }
+
+ @Override
+ public String getContainer() {
+ return "default";
+ }
+
+ @Override
+ public boolean getDebug() {
+ return false;
+ }
+
+ @Override
+ public boolean getIgnoreCache() {
+ return true;
+ }
+ };
+
+ TemplateLibrary library = factory.loadTemplateLibrary(context,
TEMPLATE_URL);
+ TagRegistry registry = library.getTagRegistry();
+
+ assertNotNull(registry.getHandlerFor(new TagRegistry.NSName(NAMESPACE_URI,
"Tag1")));
+ assertNotNull(registry.getHandlerFor(new TagRegistry.NSName(NAMESPACE_URI,
"Tag2")));
+ assertNotNull(registry.getHandlerFor(new TagRegistry.NSName(NAMESPACE_URI,
"Tag3")));
+ assertNotNull(registry.getHandlerFor(new TagRegistry.NSName(NAMESPACE_URI,
"Tag4")));
+
+ TagHandler handler = registry.getHandlerFor(new
TagRegistry.NSName(NAMESPACE_URI, "Tag1"));
+ Element doc = XmlUtil.parse(TEMPLATE_LIBRARY);
+ Node result = doc.getOwnerDocument().createDocumentFragment();
+ Element tag = doc.getOwnerDocument().createElement("test");
+ final TemplateContext templateContext = new TemplateContext(null,
ImmutableMap.<String, Object>of());
+ TemplateProcessor processor = new
DefaultTemplateProcessor(Expressions.forTesting()) {
+ @Override
+ public TemplateContext getTemplateContext() {
+ return templateContext;
+ }
+ };
+ handler.process(result, tag, processor);
+
+ assertEquals("<STYLE>stylestyle2</STYLE>" +
+ "<JAVASCRIPT>script;script2</JAVASCRIPT>",
serializeResources(templateContext));
+ }
+
+ private String serializeResources(TemplateContext context) {
+ StringBuilder builder = new StringBuilder();
+ for (TemplateResource resource : context.getResources()) {
+ builder.append(resource);
+ }
+
+ return builder.toString();
+ }
+
private static class CapturingPipeline implements RequestPipeline {
HttpRequest request;