You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Andreas Kollegger <an...@kollegger.name> on 2009/06/18 03:03:54 UTC

resource objects in ESP

I have a class MyResource which implements Resource and can be adapted  
to a ValueMap. A custom MyResourceProvider serves up the MyResources  
as requested. In my esp scripts, I had just been adapting the resource  
to a Map, then accessing the properties through that. But, I'd like to  
use MyResource.

If I try to adaptTo(MyResource), the adapt succeeds (I see debug from  
MyResource.adaptTo() and I have a non-null javascript object upon  
return), but it has no methods or properties.

So, I started writing some debug code to investigate what was going on.

This is the debugging pre-amble in my html.esp...

<%
	log.debug("Original Resource: " + resource); // resource displayed as  
the resource path
	// this loop produces no output
	for (i in resource) {
		log.debug("\t" + i);
	}
	log.debug("resource.getPath: " + resource.getPath()); // method call  
succeeds, resource path displayed
	
	var resourceObject = resource.getObject();
	log.debug("resourceObject: " + resourceObject); // resourceObject  
displays as the resource path
	// this loop produces no output
	for (i in resourceObject) {
		log.debug("\t" + i);
	}
	
	var valueMap = resource.adaptTo(Packages.java.util.Map);
	log.debug("ValueMap: " + valueMap); // valueMap displays as a java  
object instance
	// this loop shows all the expected methods and fields
	for (i in valueMap) {
		log.debug("\t" + i);
	}
	log.debug("valueMap.name: " + valueMap.get("name")); // this  
succeeds, displaying the correct name value

	var asString = resource.adaptTo(Packages.java.lang.String); //  
adapting to String produces MyResource's toString()
	log.debug("asString: " + asString); // produces the expected  
MyResource.toString() value
	// this loop produces no output
	for (i in asString) {
		log.debug("\t" + i);
	}

	var myResource = resource.adaptTo(Packages.example.MyResource);
	log.debug("MyResource: " + myResource); // myResource displayed as  
resource path
	// this loop produces no output
	for (i in myResource) {
		log.debug("\t" + i);
	}
	
%>

It looks like MyResource is getting wrapped in a ScriptableResource  
instead of just being converted into a javascript object. Is that  
right? How can I get a javascript object equivalent of MyResource?

Confused,
Andreas


Re: resource objects in ESP

Posted by Andreas Kollegger <ak...@tembopublic.org>.
Answering my own question: apparently it is because of special  
handling for Resource subclasses. If I create a new class MyInfo which  
has the same properties as MyResource, then enable  
MyResource.adaptTo(MyInfo.class) to return a comparable instance of  
MyInfo, I get a javascript object with all the expected methods. I  
guess that makes sense, since typically a Resource is a wrapper for  
some underlying domain object.

-Andreas


On Jun 17, 2009, at 9:03 PM, Andreas Kollegger wrote:

> I have a class MyResource which implements Resource and can be  
> adapted to a ValueMap. A custom MyResourceProvider serves up the  
> MyResources as requested. In my esp scripts, I had just been  
> adapting the resource to a Map, then accessing the properties  
> through that. But, I'd like to use MyResource.
>
> If I try to adaptTo(MyResource), the adapt succeeds (I see debug  
> from MyResource.adaptTo() and I have a non-null javascript object  
> upon return), but it has no methods or properties.
>
> So, I started writing some debug code to investigate what was going  
> on.
>
> This is the debugging pre-amble in my html.esp...
>
> <%
> 	log.debug("Original Resource: " + resource); // resource displayed  
> as the resource path
> 	// this loop produces no output
> 	for (i in resource) {
> 		log.debug("\t" + i);
> 	}
> 	log.debug("resource.getPath: " + resource.getPath()); // method  
> call succeeds, resource path displayed
> 	
> 	var resourceObject = resource.getObject();
> 	log.debug("resourceObject: " + resourceObject); // resourceObject  
> displays as the resource path
> 	// this loop produces no output
> 	for (i in resourceObject) {
> 		log.debug("\t" + i);
> 	}
> 	
> 	var valueMap = resource.adaptTo(Packages.java.util.Map);
> 	log.debug("ValueMap: " + valueMap); // valueMap displays as a java  
> object instance
> 	// this loop shows all the expected methods and fields
> 	for (i in valueMap) {
> 		log.debug("\t" + i);
> 	}
> 	log.debug("valueMap.name: " + valueMap.get("name")); // this  
> succeeds, displaying the correct name value
>
> 	var asString = resource.adaptTo(Packages.java.lang.String); //  
> adapting to String produces MyResource's toString()
> 	log.debug("asString: " + asString); // produces the expected  
> MyResource.toString() value
> 	// this loop produces no output
> 	for (i in asString) {
> 		log.debug("\t" + i);
> 	}
>
> 	var myResource = resource.adaptTo(Packages.example.MyResource);
> 	log.debug("MyResource: " + myResource); // myResource displayed as  
> resource path
> 	// this loop produces no output
> 	for (i in myResource) {
> 		log.debug("\t" + i);
> 	}
> 	
> %>
>
> It looks like MyResource is getting wrapped in a ScriptableResource  
> instead of just being converted into a javascript object. Is that  
> right? How can I get a javascript object equivalent of MyResource?
>
> Confused,
> Andreas
>