You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Макаров Роман <ro...@kupivip.ru> on 2014/09/15 07:32:03 UTC

other components inside Dynamic component?

Hi all,

I have a question regarding Dynamic component. I use it for keeping parts of tml code in the database. It renders insertions used for functions calling such us ${someFunc('someParam')} , but it does not render any tapestry tags correctly. It just omits "t" prefix instead and adds xmlns attribute. For example, the following code

<t:if test="true">asdf</t:if>

Rendered to:

<if test="true" xmlns="http://tapestry.apache.org/schema/tapestry_5_3.xsd">asdf</if>

What are the suggestions? Are there any good examples of code in the net?

Here is My code:

public class TrackerDynamic extends Tracker {

    @Property
    private DynamicTemplate dynamicTemplate;

    @InjectService("DynamicTemplateParser")
    private DynamicTemplateParser dynamicTemplateParser;

    private String getTemplateString() {
        StringBuilder sb = new StringBuilder(
                "<div id=\"trackerDynamic\" xmlns:t=\"http://tapestry.apache.org/schema/tapestry_5_3.xsd\" xmlns:p=\"tapestry:parameter\">\n");
        sb.append(...//some code for inserting custom data from database
        sb.append("</div>");
        return sb.toString();
    }

    @Override
    void beginRender() {
        try {
            dynamicTemplate = dynamicTemplateParser.parseTemplate(
                    new StringResource(getTemplateString())
            );
        } catch (Exception ex) {
        }
    }
}


TrackerDynamic.tml:

<t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">

<t:Dynamic template="prop:dynamicTemplate"></t:Dynamic>

</t:container>


StringResource.java:

public class StringResource implements Resource {

    private String tml;

    public StringResource(String tml) {
        this.tml = tml;
    }

    @Override
    public boolean exists() {
        return false;
    }

    @Override
    public InputStream openStream() throws IOException {
        return new ByteArrayInputStream(tml.getBytes("UTF-8"));
    }

    @Override
    public URL toURL() {
        return null;
    }

    @Override
    public Resource forLocale(Locale locale) {
        return null;
    }

    @Override
    public Resource forFile(String relativePath) {
        return null;
    }

    @Override
    public Resource withExtension(String extension) {
        return null;
    }

    @Override
    public String getFolder() {
        return null;
    }

    @Override
    public String getFile() {
        return null;
    }

    @Override
    public String getPath() {
        return null;
    }
}


RE: other components inside Dynamic component?

Posted by Lance Java <la...@googlemail.com>.
I doubt that'll do anything since the field is a per thread value.

I meant to add an appropriate hashCode() an equals() method to
StringResource. Or perhaps override the DynamicTemplateParser service with
a non caching version.

RE: other components inside Dynamic component?

Posted by Lance Java <la...@googlemail.com>.
No, the field is still a Per-Thread value. It will be null for the next
request. You've just made it so that the field will be calculated at most
once per request.
 On 15 Sep 2014 14:42, "Макаров Роман" <ro...@kupivip.ru> wrote:

> Changed to this. Is it ok now?
>
> ...
>    private DynamicTemplate dynamicTemplate;
>
>     public String getLastModificationDate() {
>         return partnerCodeService.getLastModificationDate().toString();
>     }
>
>     @Cached(watch = "LastModificationDate")
>     public DynamicTemplate getDynamicTemplate() {
>         if (dynamicTemplate == null) {
>             dynamicTemplate = dynamicTemplateParser.parseTemplate(
>                     new StringResource(getTemplateString()));
>         }
>         return dynamicTemplate;
>     }
>
>
> -----Original Message-----
> From: Макаров Роман [mailto:roman.makarov@kupivip.ru]
> Sent: Monday, September 15, 2014 5:26 PM
> To: Tapestry users
> Subject: RE: other components inside Dynamic component?
>
> So, if DynamicTemplateParser has a cache by Resource, how is it
> identifying the resource? Or, just how to avoid the problem?
>
> By the way, now my code looks as following, but not sure it changes the
> cache behavior:
>
>
> public class TrackerDynamic extends Tracker {
>
>     private DynamicTemplate dynamicTemplate;
>
>     public DynamicTemplate getDynamicTemplate() {
>         if (dynamicTemplate == null) {
>             dynamicTemplate = dynamicTemplateParser.parseTemplate(
>                     new StringResource(getTemplateString()));
>         }
>         return dynamicTemplate;
>     }
>
> ...
>
> }
>
>
> -----Original Message-----
> From: Lance Java [mailto:lance.java@googlemail.com]
> Sent: Monday, September 15, 2014 4:37 PM
> To: Tapestry users
> Subject: Re: other components inside Dynamic component?
>
> Not sure how to fix your issue but I spot another issue.
> DynamicTemplateParser has a cache by Resource. It looks like you add to
> the cache for every render. It's only a question of time before you hit out
> of memory errors.
>  On 15 Sep 2014 06:32, "Макаров Роман" <ro...@kupivip.ru> wrote:
>
> > Hi all,
> >
> > I have a question regarding Dynamic component. I use it for keeping
> > parts of tml code in the database. It renders insertions used for
> > functions calling such us ${someFunc('someParam')} , but it does not
> > render any tapestry tags correctly. It just omits "t" prefix instead
> > and adds xmlns attribute. For example, the following code
> >
> > <t:if test="true">asdf</t:if>
> >
> > Rendered to:
> >
> > <if test="true"
> > xmlns="http://tapestry.apache.org/schema/tapestry_5_3.xsd
> > ">asdf</if>
> >
> > What are the suggestions? Are there any good examples of code in the net?
> >
> > Here is My code:
> >
> > public class TrackerDynamic extends Tracker {
> >
> >     @Property
> >     private DynamicTemplate dynamicTemplate;
> >
> >     @InjectService("DynamicTemplateParser")
> >     private DynamicTemplateParser dynamicTemplateParser;
> >
> >     private String getTemplateString() {
> >         StringBuilder sb = new StringBuilder(
> >                 "<div id=\"trackerDynamic\" xmlns:t=\"
> > http://tapestry.apache.org/schema/tapestry_5_3.xsd\"
> > xmlns:p=\"tapestry:parameter\">\n");
> >         sb.append(...//some code for inserting custom data from database
> >         sb.append("</div>");
> >         return sb.toString();
> >     }
> >
> >     @Override
> >     void beginRender() {
> >         try {
> >             dynamicTemplate = dynamicTemplateParser.parseTemplate(
> >                     new StringResource(getTemplateString())
> >             );
> >         } catch (Exception ex) {
> >         }
> >     }
> > }
> >
> >
> > TrackerDynamic.tml:
> >
> > <t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd
> "
> > xmlns:p="tapestry:parameter">
> >
> > <t:Dynamic template="prop:dynamicTemplate"></t:Dynamic>
> >
> > </t:container>
> >
> >
> > StringResource.java:
> >
> > public class StringResource implements Resource {
> >
> >     private String tml;
> >
> >     public StringResource(String tml) {
> >         this.tml = tml;
> >     }
> >
> >     @Override
> >     public boolean exists() {
> >         return false;
> >     }
> >
> >     @Override
> >     public InputStream openStream() throws IOException {
> >         return new ByteArrayInputStream(tml.getBytes("UTF-8"));
> >     }
> >
> >     @Override
> >     public URL toURL() {
> >         return null;
> >     }
> >
> >     @Override
> >     public Resource forLocale(Locale locale) {
> >         return null;
> >     }
> >
> >     @Override
> >     public Resource forFile(String relativePath) {
> >         return null;
> >     }
> >
> >     @Override
> >     public Resource withExtension(String extension) {
> >         return null;
> >     }
> >
> >     @Override
> >     public String getFolder() {
> >         return null;
> >     }
> >
> >     @Override
> >     public String getFile() {
> >         return null;
> >     }
> >
> >     @Override
> >     public String getPath() {
> >         return null;
> >     }
> > }
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>

RE: other components inside Dynamic component?

Posted by Макаров Роман <ro...@kupivip.ru>.
Changed to this. Is it ok now?

...
   private DynamicTemplate dynamicTemplate;

    public String getLastModificationDate() {
        return partnerCodeService.getLastModificationDate().toString();
    }

    @Cached(watch = "LastModificationDate")
    public DynamicTemplate getDynamicTemplate() {
        if (dynamicTemplate == null) {
            dynamicTemplate = dynamicTemplateParser.parseTemplate(
                    new StringResource(getTemplateString()));
        }
        return dynamicTemplate;
    }


-----Original Message-----
From: Макаров Роман [mailto:roman.makarov@kupivip.ru] 
Sent: Monday, September 15, 2014 5:26 PM
To: Tapestry users
Subject: RE: other components inside Dynamic component?

So, if DynamicTemplateParser has a cache by Resource, how is it identifying the resource? Or, just how to avoid the problem?

By the way, now my code looks as following, but not sure it changes the cache behavior:


public class TrackerDynamic extends Tracker {

    private DynamicTemplate dynamicTemplate;

    public DynamicTemplate getDynamicTemplate() {
        if (dynamicTemplate == null) {
            dynamicTemplate = dynamicTemplateParser.parseTemplate(
                    new StringResource(getTemplateString()));
        }
        return dynamicTemplate;
    }

...

}


-----Original Message-----
From: Lance Java [mailto:lance.java@googlemail.com]
Sent: Monday, September 15, 2014 4:37 PM
To: Tapestry users
Subject: Re: other components inside Dynamic component?

Not sure how to fix your issue but I spot another issue.
DynamicTemplateParser has a cache by Resource. It looks like you add to the cache for every render. It's only a question of time before you hit out of memory errors.
 On 15 Sep 2014 06:32, "Макаров Роман" <ro...@kupivip.ru> wrote:

> Hi all,
>
> I have a question regarding Dynamic component. I use it for keeping 
> parts of tml code in the database. It renders insertions used for 
> functions calling such us ${someFunc('someParam')} , but it does not 
> render any tapestry tags correctly. It just omits "t" prefix instead 
> and adds xmlns attribute. For example, the following code
>
> <t:if test="true">asdf</t:if>
>
> Rendered to:
>
> <if test="true" 
> xmlns="http://tapestry.apache.org/schema/tapestry_5_3.xsd
> ">asdf</if>
>
> What are the suggestions? Are there any good examples of code in the net?
>
> Here is My code:
>
> public class TrackerDynamic extends Tracker {
>
>     @Property
>     private DynamicTemplate dynamicTemplate;
>
>     @InjectService("DynamicTemplateParser")
>     private DynamicTemplateParser dynamicTemplateParser;
>
>     private String getTemplateString() {
>         StringBuilder sb = new StringBuilder(
>                 "<div id=\"trackerDynamic\" xmlns:t=\"
> http://tapestry.apache.org/schema/tapestry_5_3.xsd\"
> xmlns:p=\"tapestry:parameter\">\n");
>         sb.append(...//some code for inserting custom data from database
>         sb.append("</div>");
>         return sb.toString();
>     }
>
>     @Override
>     void beginRender() {
>         try {
>             dynamicTemplate = dynamicTemplateParser.parseTemplate(
>                     new StringResource(getTemplateString())
>             );
>         } catch (Exception ex) {
>         }
>     }
> }
>
>
> TrackerDynamic.tml:
>
> <t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
> xmlns:p="tapestry:parameter">
>
> <t:Dynamic template="prop:dynamicTemplate"></t:Dynamic>
>
> </t:container>
>
>
> StringResource.java:
>
> public class StringResource implements Resource {
>
>     private String tml;
>
>     public StringResource(String tml) {
>         this.tml = tml;
>     }
>
>     @Override
>     public boolean exists() {
>         return false;
>     }
>
>     @Override
>     public InputStream openStream() throws IOException {
>         return new ByteArrayInputStream(tml.getBytes("UTF-8"));
>     }
>
>     @Override
>     public URL toURL() {
>         return null;
>     }
>
>     @Override
>     public Resource forLocale(Locale locale) {
>         return null;
>     }
>
>     @Override
>     public Resource forFile(String relativePath) {
>         return null;
>     }
>
>     @Override
>     public Resource withExtension(String extension) {
>         return null;
>     }
>
>     @Override
>     public String getFolder() {
>         return null;
>     }
>
>     @Override
>     public String getFile() {
>         return null;
>     }
>
>     @Override
>     public String getPath() {
>         return null;
>     }
> }
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org

RE: other components inside Dynamic component?

Posted by Макаров Роман <ro...@kupivip.ru>.
So, if DynamicTemplateParser has a cache by Resource, how is it identifying the resource? Or, just how to avoid the problem?

By the way, now my code looks as following, but not sure it changes the cache behavior:


public class TrackerDynamic extends Tracker {

    private DynamicTemplate dynamicTemplate;

    public DynamicTemplate getDynamicTemplate() {
        if (dynamicTemplate == null) {
            dynamicTemplate = dynamicTemplateParser.parseTemplate(
                    new StringResource(getTemplateString()));
        }
        return dynamicTemplate;
    }

...

}


-----Original Message-----
From: Lance Java [mailto:lance.java@googlemail.com] 
Sent: Monday, September 15, 2014 4:37 PM
To: Tapestry users
Subject: Re: other components inside Dynamic component?

Not sure how to fix your issue but I spot another issue.
DynamicTemplateParser has a cache by Resource. It looks like you add to the cache for every render. It's only a question of time before you hit out of memory errors.
 On 15 Sep 2014 06:32, "Макаров Роман" <ro...@kupivip.ru> wrote:

> Hi all,
>
> I have a question regarding Dynamic component. I use it for keeping 
> parts of tml code in the database. It renders insertions used for 
> functions calling such us ${someFunc('someParam')} , but it does not 
> render any tapestry tags correctly. It just omits "t" prefix instead 
> and adds xmlns attribute. For example, the following code
>
> <t:if test="true">asdf</t:if>
>
> Rendered to:
>
> <if test="true" 
> xmlns="http://tapestry.apache.org/schema/tapestry_5_3.xsd
> ">asdf</if>
>
> What are the suggestions? Are there any good examples of code in the net?
>
> Here is My code:
>
> public class TrackerDynamic extends Tracker {
>
>     @Property
>     private DynamicTemplate dynamicTemplate;
>
>     @InjectService("DynamicTemplateParser")
>     private DynamicTemplateParser dynamicTemplateParser;
>
>     private String getTemplateString() {
>         StringBuilder sb = new StringBuilder(
>                 "<div id=\"trackerDynamic\" xmlns:t=\"
> http://tapestry.apache.org/schema/tapestry_5_3.xsd\"
> xmlns:p=\"tapestry:parameter\">\n");
>         sb.append(...//some code for inserting custom data from database
>         sb.append("</div>");
>         return sb.toString();
>     }
>
>     @Override
>     void beginRender() {
>         try {
>             dynamicTemplate = dynamicTemplateParser.parseTemplate(
>                     new StringResource(getTemplateString())
>             );
>         } catch (Exception ex) {
>         }
>     }
> }
>
>
> TrackerDynamic.tml:
>
> <t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
> xmlns:p="tapestry:parameter">
>
> <t:Dynamic template="prop:dynamicTemplate"></t:Dynamic>
>
> </t:container>
>
>
> StringResource.java:
>
> public class StringResource implements Resource {
>
>     private String tml;
>
>     public StringResource(String tml) {
>         this.tml = tml;
>     }
>
>     @Override
>     public boolean exists() {
>         return false;
>     }
>
>     @Override
>     public InputStream openStream() throws IOException {
>         return new ByteArrayInputStream(tml.getBytes("UTF-8"));
>     }
>
>     @Override
>     public URL toURL() {
>         return null;
>     }
>
>     @Override
>     public Resource forLocale(Locale locale) {
>         return null;
>     }
>
>     @Override
>     public Resource forFile(String relativePath) {
>         return null;
>     }
>
>     @Override
>     public Resource withExtension(String extension) {
>         return null;
>     }
>
>     @Override
>     public String getFolder() {
>         return null;
>     }
>
>     @Override
>     public String getFile() {
>         return null;
>     }
>
>     @Override
>     public String getPath() {
>         return null;
>     }
> }
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org

Re: other components inside Dynamic component?

Posted by Lance Java <la...@googlemail.com>.
Not sure how to fix your issue but I spot another issue.
DynamicTemplateParser has a cache by Resource. It looks like you add to the
cache for every render. It's only a question of time before you hit out of
memory errors.
 On 15 Sep 2014 06:32, "Макаров Роман" <ro...@kupivip.ru> wrote:

> Hi all,
>
> I have a question regarding Dynamic component. I use it for keeping parts
> of tml code in the database. It renders insertions used for functions
> calling such us ${someFunc('someParam')} , but it does not render any
> tapestry tags correctly. It just omits "t" prefix instead and adds xmlns
> attribute. For example, the following code
>
> <t:if test="true">asdf</t:if>
>
> Rendered to:
>
> <if test="true" xmlns="http://tapestry.apache.org/schema/tapestry_5_3.xsd
> ">asdf</if>
>
> What are the suggestions? Are there any good examples of code in the net?
>
> Here is My code:
>
> public class TrackerDynamic extends Tracker {
>
>     @Property
>     private DynamicTemplate dynamicTemplate;
>
>     @InjectService("DynamicTemplateParser")
>     private DynamicTemplateParser dynamicTemplateParser;
>
>     private String getTemplateString() {
>         StringBuilder sb = new StringBuilder(
>                 "<div id=\"trackerDynamic\" xmlns:t=\"
> http://tapestry.apache.org/schema/tapestry_5_3.xsd\"
> xmlns:p=\"tapestry:parameter\">\n");
>         sb.append(...//some code for inserting custom data from database
>         sb.append("</div>");
>         return sb.toString();
>     }
>
>     @Override
>     void beginRender() {
>         try {
>             dynamicTemplate = dynamicTemplateParser.parseTemplate(
>                     new StringResource(getTemplateString())
>             );
>         } catch (Exception ex) {
>         }
>     }
> }
>
>
> TrackerDynamic.tml:
>
> <t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
> xmlns:p="tapestry:parameter">
>
> <t:Dynamic template="prop:dynamicTemplate"></t:Dynamic>
>
> </t:container>
>
>
> StringResource.java:
>
> public class StringResource implements Resource {
>
>     private String tml;
>
>     public StringResource(String tml) {
>         this.tml = tml;
>     }
>
>     @Override
>     public boolean exists() {
>         return false;
>     }
>
>     @Override
>     public InputStream openStream() throws IOException {
>         return new ByteArrayInputStream(tml.getBytes("UTF-8"));
>     }
>
>     @Override
>     public URL toURL() {
>         return null;
>     }
>
>     @Override
>     public Resource forLocale(Locale locale) {
>         return null;
>     }
>
>     @Override
>     public Resource forFile(String relativePath) {
>         return null;
>     }
>
>     @Override
>     public Resource withExtension(String extension) {
>         return null;
>     }
>
>     @Override
>     public String getFolder() {
>         return null;
>     }
>
>     @Override
>     public String getFile() {
>         return null;
>     }
>
>     @Override
>     public String getPath() {
>         return null;
>     }
> }
>
>