You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Pablo Ruggia <pr...@gmail.com> on 2006/10/26 18:48:57 UTC

ITemplateSourceDelegate

Has someone a working example of an ITemplateSourceDelegate or or a
PageSpecificationResolverImpl ? I can't find any.
I only want to have a .class for each page and in the same package the html
template. All .page configuration will be set using annotations.
Thanks !!

Re: ITemplateSourceDelegate

Posted by andyhot <an...@di.uoa.gr>.
I recently did the exact same thing and just saw your implementation...

Actually, I got a slightly better solution ;)

After finding the page class, you have to create a 'virtual' page spec.

The trick is to set its specificationLocation to be in the classpath,
right in
the package of the class you've found... Something like this:

String fullPath = pageClass.getName().replace('.', '/');
ClasspathResource virtualPage = new ClasspathResource(new
DefaultClassResolver(), fullPath + ".page");

and then

spec.setSpecificationLocation(virtualPage);

No need to create that asset




Pablo Ruggia wrote:
> Thank you very much, you save me many hours of work.
> I've modified your class so it try to find templates in the packages you
> define in "org.apache.tapestry.page-class-packages" configuration, and
> look
> it into classpath, so it has not have to be only in WEB-INF/classes.
> Here is the code and the hivemind configuration i've used to make it
> work:
>
> <?xml version="1.0"?>
> <module id="ar.com.example" version="4.0.0">
>  <implementation
> service-id="tapestry.page.SpecificationResolverDelegate">
>      <invoke-factory model="threaded">
>          <construct class="ar.com.example.SpecificationResolverDelegate">
>              <set-object property="clazzFinder"
> value="infrastructure:classFinder"/>
>          </construct>
>      </invoke-factory>
>  </implementation>
> </module>
>
> package ar.com.example;
>
> import org.apache.hivemind.Location;
> import org.apache.hivemind.Resource;
> import org.apache.hivemind.impl.LocationImpl;
> import org.apache.tapestry.INamespace;
> import org.apache.tapestry.IRequestCycle;
> import org.apache.tapestry.resolver.ISpecificationResolverDelegate;
> import org.apache.tapestry.services.ClassFinder;
> import org.apache.tapestry.spec.AssetSpecification;
> import org.apache.tapestry.spec.ComponentSpecification;
> import org.apache.tapestry.spec.IComponentSpecification;
> import org.apache.tapestry.util.DescribedLocation;
>
> public class SpecificationResolverDelegate implements
>        ISpecificationResolverDelegate {
>
>    private ClassFinder clazzFinder;
>
>    public ClassFinder getClazzFinder() {
>        return clazzFinder;
>    }
>
>    public void setClazzFinder(ClassFinder clazzFinder) {
>        this.clazzFinder = clazzFinder;
>    }
>
>    public IComponentSpecification findPageSpecification(IRequestCycle
> cycle,
>            INamespace namespace, String simplePageName) {
>
>        //first I try to find the page class
>        String packages = namespace
>                .getPropertyValue("org.apache.tapestry.page-class-packages
> ");
>
>        String className = simplePageName.replace('/', '.');
>        Class pageClass = getClazzFinder().findClass(packages, className);
>
>        if (pageClass == null)
>            return null;
>
>        //very good, I've found the page class, so I have to create the
> specification
>        IComponentSpecification spec = new ComponentSpecification();
>        Resource namespaceResource = namespace.getSpecificationLocation();
>        Resource componentResource = namespaceResource
>                .getRelativeResource(simplePageName + ".page");
>        Location location = new LocationImpl(componentResource);
>        spec.setLocation(location);
>        spec.setSpecificationLocation(componentResource);
>        spec.setComponentClassName(pageClass.getName());
>
>        //add the $template asset telling the framework where to look for
> the template
>        AssetSpecification aspec = new AssetSpecification();
>        aspec.setPath("classpath:/"
>                + pageClass.getPackage().getName().replace(".", "/") + "/"
>                + className + ".html");
>        aspec.setLocation(new DescribedLocation(
>                spec.getSpecificationLocation(), ""));
>        spec.addAsset("$template", aspec);
>
>        //return the specification
>        return spec;
>
>    }
>
>    public IComponentSpecification findComponentSpecification(
>            IRequestCycle cycle, INamespace namespace, String type) {
>        return null;
>    }
>
> }
>
>
>
>
> On 10/27/06, spamsucks@spamsucks.com <sp...@spamsucks.com> wrote:
>>
>> > Has someone a working example of an ITemplateSourceDelegate
>> > or or a PageSpecificationResolverImpl ? I can't find any.
>> > I only want to have a .class for each page and in the same
>> > package the html template. All .page configuration will be
>> > set using annotations.
>> > Thanks !!
>> >
>>
>> Here is ISpecificationResolverDelegate which I use in my project. It
>> allows
>> me to
>> 1. get rid of *.page files. Ionly need MyPage.html and MyPage.class
>> 2. store both MyPage.html and MyPage.class in the same location ( in my
>> case
>> it's WEB_INF\classes\com\Mycompany\page\ )
>> 3. use getPage( "com.mycompany.MyPage" )
>> 4. get rid of <meta key="org.apache.tapestry.page-class-packages" >
>> in my
>> .application file
>>
>> Hope this helps.
>>
>> import org.apache.hivemind.Resource;
>> import org.apache.hivemind.impl.LocationImpl;
>> import org.apache.tapestry.INamespace;
>> import org.apache.tapestry.IRequestCycle;
>> import org.apache.tapestry.resolver.ISpecificationResolverDelegate;
>> import org.apache.tapestry.spec.ComponentSpecification;
>> import org.apache.tapestry.spec.IComponentSpecification;
>>
>> public class SpecificationResolverDelegate implements
>> ISpecificationResolverDelegate {
>>
>>         public IComponentSpecification
>> findPageSpecification(IRequestCycle
>> cycle, INamespace namespace, String simplePageName){
>>
>>                 final Resource namespaceLocation =
>> namespace.getSpecificationLocation();
>>                 final Resource templateResource =
>> namespaceLocation.getRelativeResource( "classes/" +
>> simplePageName.replace
>> (
>> '.', '/' ) + ".html" );
>>
>>                 if ( templateResource.getResourceURL() != null ){
>>                         final Resource pageResource =
>> namespaceLocation.getRelativeResource( "classes/" +
>> simplePageName.replace
>> (
>> '.', '/' ) + ".page" );
>>
>>                         final IComponentSpecification specification =
>> new
>> ComponentSpecification();
>>                         specification.setComponentClassName(
>> simplePageName
>> );
>>                         specification.setPageSpecification( false );
>>                         specification.setSpecificationLocation(
>> pageResource
>> );
>>                         specification.setLocation( new LocationImpl(
>> templateResource ) );
>>                         return specification;
>>                 }
>>                 return null;
>>         }
>>
>>         public IComponentSpecification findComponentSpecification(
>> IRequestCycle cycle, INamespace namespace, String type){
>>                 return null;
>>         }
>> }
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>


-- 
Andreas Andreou - andyhot@apache.org - http://andyhot.di.uoa.gr
Tapestry / Tacos developer
Open Source / J2EE Consulting 


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


Re: ITemplateSourceDelegate

Posted by Pablo Ruggia <pr...@gmail.com>.
Thank you very much, you save me many hours of work.
I've modified your class so it try to find templates in the packages you
define in "org.apache.tapestry.page-class-packages" configuration, and look
it into classpath, so it has not have to be only in WEB-INF/classes.
Here is the code and the hivemind configuration i've used to make it work:

<?xml version="1.0"?>
<module id="ar.com.example" version="4.0.0">
  <implementation service-id="tapestry.page.SpecificationResolverDelegate">
      <invoke-factory model="threaded">
          <construct class="ar.com.example.SpecificationResolverDelegate">
              <set-object property="clazzFinder"
value="infrastructure:classFinder"/>
          </construct>
      </invoke-factory>
  </implementation>
</module>

package ar.com.example;

import org.apache.hivemind.Location;
import org.apache.hivemind.Resource;
import org.apache.hivemind.impl.LocationImpl;
import org.apache.tapestry.INamespace;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.resolver.ISpecificationResolverDelegate;
import org.apache.tapestry.services.ClassFinder;
import org.apache.tapestry.spec.AssetSpecification;
import org.apache.tapestry.spec.ComponentSpecification;
import org.apache.tapestry.spec.IComponentSpecification;
import org.apache.tapestry.util.DescribedLocation;

public class SpecificationResolverDelegate implements
        ISpecificationResolverDelegate {

    private ClassFinder clazzFinder;

    public ClassFinder getClazzFinder() {
        return clazzFinder;
    }

    public void setClazzFinder(ClassFinder clazzFinder) {
        this.clazzFinder = clazzFinder;
    }

    public IComponentSpecification findPageSpecification(IRequestCycle
cycle,
            INamespace namespace, String simplePageName) {

        //first I try to find the page class
        String packages = namespace
                .getPropertyValue("org.apache.tapestry.page-class-packages
");

        String className = simplePageName.replace('/', '.');
        Class pageClass = getClazzFinder().findClass(packages, className);

        if (pageClass == null)
            return null;

        //very good, I've found the page class, so I have to create the
specification
        IComponentSpecification spec = new ComponentSpecification();
        Resource namespaceResource = namespace.getSpecificationLocation();
        Resource componentResource = namespaceResource
                .getRelativeResource(simplePageName + ".page");
        Location location = new LocationImpl(componentResource);
        spec.setLocation(location);
        spec.setSpecificationLocation(componentResource);
        spec.setComponentClassName(pageClass.getName());

        //add the $template asset telling the framework where to look for
the template
        AssetSpecification aspec = new AssetSpecification();
        aspec.setPath("classpath:/"
                + pageClass.getPackage().getName().replace(".", "/") + "/"
                + className + ".html");
        aspec.setLocation(new DescribedLocation(
                spec.getSpecificationLocation(), ""));
        spec.addAsset("$template", aspec);

        //return the specification
        return spec;

    }

    public IComponentSpecification findComponentSpecification(
            IRequestCycle cycle, INamespace namespace, String type) {
        return null;
    }

}




On 10/27/06, spamsucks@spamsucks.com <sp...@spamsucks.com> wrote:
>
> > Has someone a working example of an ITemplateSourceDelegate
> > or or a PageSpecificationResolverImpl ? I can't find any.
> > I only want to have a .class for each page and in the same
> > package the html template. All .page configuration will be
> > set using annotations.
> > Thanks !!
> >
>
> Here is ISpecificationResolverDelegate which I use in my project. It
> allows
> me to
> 1. get rid of *.page files. Ionly need MyPage.html and MyPage.class
> 2. store both MyPage.html and MyPage.class in the same location ( in my
> case
> it's WEB_INF\classes\com\Mycompany\page\ )
> 3. use getPage( "com.mycompany.MyPage" )
> 4. get rid of <meta key="org.apache.tapestry.page-class-packages" > in my
> .application file
>
> Hope this helps.
>
> import org.apache.hivemind.Resource;
> import org.apache.hivemind.impl.LocationImpl;
> import org.apache.tapestry.INamespace;
> import org.apache.tapestry.IRequestCycle;
> import org.apache.tapestry.resolver.ISpecificationResolverDelegate;
> import org.apache.tapestry.spec.ComponentSpecification;
> import org.apache.tapestry.spec.IComponentSpecification;
>
> public class SpecificationResolverDelegate implements
> ISpecificationResolverDelegate {
>
>         public IComponentSpecification findPageSpecification(IRequestCycle
> cycle, INamespace namespace, String simplePageName){
>
>                 final Resource namespaceLocation =
> namespace.getSpecificationLocation();
>                 final Resource templateResource =
> namespaceLocation.getRelativeResource( "classes/" + simplePageName.replace
> (
> '.', '/' ) + ".html" );
>
>                 if ( templateResource.getResourceURL() != null ){
>                         final Resource pageResource =
> namespaceLocation.getRelativeResource( "classes/" + simplePageName.replace
> (
> '.', '/' ) + ".page" );
>
>                         final IComponentSpecification specification = new
> ComponentSpecification();
>                         specification.setComponentClassName(
> simplePageName
> );
>                         specification.setPageSpecification( false );
>                         specification.setSpecificationLocation(
> pageResource
> );
>                         specification.setLocation( new LocationImpl(
> templateResource ) );
>                         return specification;
>                 }
>                 return null;
>         }
>
>         public IComponentSpecification findComponentSpecification(
> IRequestCycle cycle, INamespace namespace, String type){
>                 return null;
>         }
> }
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

RE: ITemplateSourceDelegate

Posted by sp...@spamsucks.com.
> Has someone a working example of an ITemplateSourceDelegate 
> or or a PageSpecificationResolverImpl ? I can't find any.
> I only want to have a .class for each page and in the same 
> package the html template. All .page configuration will be 
> set using annotations.
> Thanks !!
> 

Here is ISpecificationResolverDelegate which I use in my project. It allows
me to
1. get rid of *.page files. Ionly need MyPage.html and MyPage.class
2. store both MyPage.html and MyPage.class in the same location ( in my case
it's WEB_INF\classes\com\Mycompany\page\ )
3. use getPage( "com.mycompany.MyPage" ) 
4. get rid of <meta key="org.apache.tapestry.page-class-packages" > in my
.application file

Hope this helps.

import org.apache.hivemind.Resource;
import org.apache.hivemind.impl.LocationImpl;
import org.apache.tapestry.INamespace;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.resolver.ISpecificationResolverDelegate;
import org.apache.tapestry.spec.ComponentSpecification;
import org.apache.tapestry.spec.IComponentSpecification;

public class SpecificationResolverDelegate implements
ISpecificationResolverDelegate {

	public IComponentSpecification findPageSpecification(IRequestCycle
cycle, INamespace namespace, String simplePageName){

		final Resource namespaceLocation =
namespace.getSpecificationLocation();
		final Resource templateResource =
namespaceLocation.getRelativeResource( "classes/" + simplePageName.replace(
'.', '/' ) + ".html" );

		if ( templateResource.getResourceURL() != null ){
			final Resource pageResource =
namespaceLocation.getRelativeResource( "classes/" + simplePageName.replace(
'.', '/' ) + ".page" );

			final IComponentSpecification specification = new
ComponentSpecification();
			specification.setComponentClassName( simplePageName
);
			specification.setPageSpecification( false );
			specification.setSpecificationLocation( pageResource
);
			specification.setLocation( new LocationImpl(
templateResource ) );
			return specification;
		}
		return null;
	}

	public IComponentSpecification findComponentSpecification(
IRequestCycle cycle, INamespace namespace, String type){
		return null;
	}
}




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