Hi,

First of all: I've read the JavaDoc and know
SyntheticResource#adaptTo() returns null "by design". But I'd still
like to know if there are any deeper meanings behind this.

I'm building a more complex component using an API relying on the
ability to adapt resources to other classes/interfaces, which I think
provides a very convenient and elegant way to retrieve instances of
classes without actually having to publicly export them in the bundle.
So for example, I built an interface MyStuff with a private
MyStuffImpl, and a MyStuffAdapterFactory implementing AdapterFactory
which handles the actual adapting.

It all works fine as long as I have an underlying node associated with
the resource path (which I normally do). But when I statically
included a component without an existing underlying node, e.g. using
<sling:include path="fake" resourceType="my/resource/type" /> in the
JSP of another component, I ran into the obvious problem.

Although <sling:defineObjects/> is giving me a resource of type
SyntheticResource with the correct path and resourceType (which made
me think that was the purpose of having such a 'virtual' resource
implementation), the fun ends with resource.adaptTo(MyStuff.class)
always returning null.

That leaves me with several options, none of which are exactly delightful:

1) Make sure the fake node is always in the repository: This extends
my creation routine, bloats the repository and leaves me with a
backward compatibility issue; I'd have to provide a way to upgrade
existing content to ensure the fake nodes everywhere.

2) Check for null and duplicate code in the JSP: Sure I can add all
these checks,but I still have to copy code from MyStuffImpl where I
actually need something computed - which practically renders my API
useless. Unless I make those methods static, which would be
suboptimal.

3) Publicly export the implementation in the bundle: That enables me
to create an instance of MyStuff even if resource.adaptTo() returns
null. Yuck! :)

If there are no system-critical reasons behind returning null in
SyntheticResource#adaptTo(), i.e. breaking core code that relies on
the fact that it always returns null, I'd suggest to make it adaptable
by default, and leave it to the AdapterFactory implementation whether
or not to return null.

Of course, I'd have to be warned that some stuff isn't available due
to the missing data in the repository, but my component could still do
something useful with a synthetic resource, and I'd rather confine all
the required checks to the implementation of MyStuff rather than the
JSP code.

WDYT?

/rofe

Reply via email to