You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Justin Edelson <ju...@justinedelson.com> on 2011/03/15 02:51:14 UTC

add support for @Inject to scripting.java

I was looking at how much code is involved in referencing an OSGi
service from a script built using the scripting.java bundle and think
it's a bit verbose:

SlingBindings bindings = (SlingBindings)
request.getAttribute(SlingBindings.class.getName());
SlingScriptHelper sling = bindings.getSling();
Interface service = sling.getService(Interface.class);

I was thinking about adding some kind of annotation-based injection
support so that you could replace these lines with just:


@Inject
private Interface service;

public void service(request, response) {
  // some code using service

}

@Inject seems like the obvious candidate to support for this. WDYT?

Justin

Re: add support for @Inject to scripting.java

Posted by chetan mehrotra <ch...@gmail.com>.
It might be tricky to implement this in scripting based on other
languages also. As typically the compile and instantiation phase are
done in same call and one does not get chance to inspect the field
variable.

For example n Groovy the script string is internally converted to
Script class and then its run method is invoked. So to do any field
processing one would need to get a handle to the script object before
it is evaluated. One way is that for dynamic language like Javascript,
groovy etc we can have object which hide this lookup logic by
intercepting the method call and the executing them

Chetan Mehrotra



On Tue, Mar 15, 2011 at 8:07 PM, Felix Meschberger <fm...@adobe.com> wrote:
> Hi,
>
> Sounds like an excellent idea !
>
> Can this be generalized to also support JSP and other scripting
> languages compiling to Java Bytecode ?
>
> Regards
> Felix
>
> Am Dienstag, den 15.03.2011, 01:51 +0000 schrieb Justin Edelson:
>> I was looking at how much code is involved in referencing an OSGi
>> service from a script built using the scripting.java bundle and think
>> it's a bit verbose:
>>
>> SlingBindings bindings = (SlingBindings)
>> request.getAttribute(SlingBindings.class.getName());
>> SlingScriptHelper sling = bindings.getSling();
>> Interface service = sling.getService(Interface.class);
>>
>> I was thinking about adding some kind of annotation-based injection
>> support so that you could replace these lines with just:
>>
>>
>> @Inject
>> private Interface service;
>>
>> public void service(request, response) {
>>   // some code using service
>>
>> }
>>
>> @Inject seems like the obvious candidate to support for this. WDYT?
>>
>> Justin
>
>
>

Re: add support for @Inject to scripting.java

Posted by Felix Meschberger <fm...@adobe.com>.
Hi,

Sounds like an excellent idea !

Can this be generalized to also support JSP and other scripting
languages compiling to Java Bytecode ?

Regards
Felix

Am Dienstag, den 15.03.2011, 01:51 +0000 schrieb Justin Edelson: 
> I was looking at how much code is involved in referencing an OSGi
> service from a script built using the scripting.java bundle and think
> it's a bit verbose:
> 
> SlingBindings bindings = (SlingBindings)
> request.getAttribute(SlingBindings.class.getName());
> SlingScriptHelper sling = bindings.getSling();
> Interface service = sling.getService(Interface.class);
> 
> I was thinking about adding some kind of annotation-based injection
> support so that you could replace these lines with just:
> 
> 
> @Inject
> private Interface service;
> 
> public void service(request, response) {
>   // some code using service
> 
> }
> 
> @Inject seems like the obvious candidate to support for this. WDYT?
> 
> Justin



Re: add support for @Inject to scripting.java

Posted by Justin Edelson <ju...@justinedelson.com>.
Yes, that's what I was referring to.

On Mar 15, 2011, at 8:18 AM, chetan mehrotra <ch...@gmail.com> wrote:

> Also just to confirm with Inject we are referring to
> javax.inject.Inject [1] annotation which is part of JSR 300 and not a
> new custom one
> 
> Chetan Mehrotra
> 
> [1] http://download.oracle.com/javaee/6/api/javax/inject/Inject.html
> 
> On Tue, Mar 15, 2011 at 3:49 PM, Bertrand Delacretaz
> <bd...@apache.org> wrote:
>> Hi,
>> 
>> On Tue, Mar 15, 2011 at 10:16 AM, chetan mehrotra
>> <ch...@gmail.com> wrote:
>>> One reason for using Inject would be that @Reference is a compile time
>>> annotation (i.e. RetentionPolicy.SOURCE). And due do that a new
>>> annotation TestReference is being used in Sling Testing component [1].
>> 
>> I agree with Chetan here, as the existing felix @Reference annotation
>> cannot be used, better use a different name to avoid confusion.
>> 
>> -Bertrand
>> 
>>> [1] http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/TestReference.java?view=markup
>> 

Re: add support for @Inject to scripting.java

Posted by chetan mehrotra <ch...@gmail.com>.
Also just to confirm with Inject we are referring to
javax.inject.Inject [1] annotation which is part of JSR 300 and not a
new custom one

Chetan Mehrotra

[1] http://download.oracle.com/javaee/6/api/javax/inject/Inject.html

On Tue, Mar 15, 2011 at 3:49 PM, Bertrand Delacretaz
<bd...@apache.org> wrote:
> Hi,
>
> On Tue, Mar 15, 2011 at 10:16 AM, chetan mehrotra
> <ch...@gmail.com> wrote:
>> One reason for using Inject would be that @Reference is a compile time
>> annotation (i.e. RetentionPolicy.SOURCE). And due do that a new
>> annotation TestReference is being used in Sling Testing component [1].
>
> I agree with Chetan here, as the existing felix @Reference annotation
> cannot be used, better use a different name to avoid confusion.
>
> -Bertrand
>
>> [1] http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/TestReference.java?view=markup
>

Re: add support for @Inject to scripting.java

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Tue, Mar 15, 2011 at 10:16 AM, chetan mehrotra
<ch...@gmail.com> wrote:
> One reason for using Inject would be that @Reference is a compile time
> annotation (i.e. RetentionPolicy.SOURCE). And due do that a new
> annotation TestReference is being used in Sling Testing component [1].

I agree with Chetan here, as the existing felix @Reference annotation
cannot be used, better use a different name to avoid confusion.

-Bertrand

> [1] http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/TestReference.java?view=markup

Re: add support for @Inject to scripting.java

Posted by Felix Meschberger <fm...@adobe.com>.
Hi,

Am Dienstag, den 15.03.2011, 14:28 +0000 schrieb Felix Meschberger: 
> Hi,
> 
> Am Dienstag, den 15.03.2011, 09:16 +0000 schrieb chetan mehrotra: 
> > One reason for using Inject would be that @Reference is a compile time
> > annotation (i.e. RetentionPolicy.SOURCE). And due do that a new
> > annotation TestReference is being used in Sling Testing component [1].
> > 
> > And as Inject is part of JSR 300 we can possibly have reference to
> > such a class in the code.
> 
> Good catch ...
> 
> Do we want to support more of JSR 300 ?

Self-answering: @Named might make sense to provide the actual name of
the bound variable ...

Regards
Felix

> 
> 
> Regards
> Felix
> 
> > 
> > Chetan Mehrotra
> > 
> > [1] http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/TestReference.java?view=markup
> > 
> > On Tue, Mar 15, 2011 at 1:45 PM, Markus Joschko
> > <ma...@gmail.com> wrote:
> > >
> > > I like the idea.
> > > I just wonder if it might be a bit inconsistent to use @Inject when
> > > most of the other code in a sling project (OSGI components) uses
> > > @Reference.
> > > As a developer I would appreciate consistency in how dependency
> > > injection is done.
> > >
> > > Regards,
> > >  Markus
> > >
> > > On Tue, Mar 15, 2011 at 2:51 AM, Justin Edelson
> > > <ju...@justinedelson.com> wrote:
> > > > I was looking at how much code is involved in referencing an OSGi
> > > > service from a script built using the scripting.java bundle and think
> > > > it's a bit verbose:
> > > >
> > > > SlingBindings bindings = (SlingBindings)
> > > > request.getAttribute(SlingBindings.class.getName());
> > > > SlingScriptHelper sling = bindings.getSling();
> > > > Interface service = sling.getService(Interface.class);
> > > >
> > > > I was thinking about adding some kind of annotation-based injection
> > > > support so that you could replace these lines with just:
> > > >
> > > >
> > > > @Inject
> > > > private Interface service;
> > > >
> > > > public void service(request, response) {
> > > >  // some code using service
> > > >
> > > > }
> > > >
> > > > @Inject seems like the obvious candidate to support for this. WDYT?
> > > >
> > > > Justin
> > > >
> 
> 



Re: add support for @Inject to scripting.java

Posted by Felix Meschberger <fm...@adobe.com>.
Hi,

Am Dienstag, den 15.03.2011, 09:16 +0000 schrieb chetan mehrotra: 
> One reason for using Inject would be that @Reference is a compile time
> annotation (i.e. RetentionPolicy.SOURCE). And due do that a new
> annotation TestReference is being used in Sling Testing component [1].
> 
> And as Inject is part of JSR 300 we can possibly have reference to
> such a class in the code.

Good catch ...

Do we want to support more of JSR 300 ?


Regards
Felix

> 
> Chetan Mehrotra
> 
> [1] http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/TestReference.java?view=markup
> 
> On Tue, Mar 15, 2011 at 1:45 PM, Markus Joschko
> <ma...@gmail.com> wrote:
> >
> > I like the idea.
> > I just wonder if it might be a bit inconsistent to use @Inject when
> > most of the other code in a sling project (OSGI components) uses
> > @Reference.
> > As a developer I would appreciate consistency in how dependency
> > injection is done.
> >
> > Regards,
> >  Markus
> >
> > On Tue, Mar 15, 2011 at 2:51 AM, Justin Edelson
> > <ju...@justinedelson.com> wrote:
> > > I was looking at how much code is involved in referencing an OSGi
> > > service from a script built using the scripting.java bundle and think
> > > it's a bit verbose:
> > >
> > > SlingBindings bindings = (SlingBindings)
> > > request.getAttribute(SlingBindings.class.getName());
> > > SlingScriptHelper sling = bindings.getSling();
> > > Interface service = sling.getService(Interface.class);
> > >
> > > I was thinking about adding some kind of annotation-based injection
> > > support so that you could replace these lines with just:
> > >
> > >
> > > @Inject
> > > private Interface service;
> > >
> > > public void service(request, response) {
> > >  // some code using service
> > >
> > > }
> > >
> > > @Inject seems like the obvious candidate to support for this. WDYT?
> > >
> > > Justin
> > >



Re: add support for @Inject to scripting.java

Posted by Carsten Ziegeler <cz...@apache.org>.
Justin Edelson  wrote
> My real reason for not wanting to use @Reference is that this isn't an
> SCR context and it seems confusing to use SCR annotations in a non-SCR
> context
Agreed.

> I think this could be confusing. scripting.java follows the path
> conventions of other scripts, just with a .java extensions. This opens
> up the path to have a scripting.java-compiled servlet apply to a
> different resource type than it "should" based on the path location.
Right...so yes, let's quickly forget about this idea...

> Crazy counter-proposal.... maven-scr-plugin (and friends) should support
> @Inject as a replacement for @Reference.
Interesting idea - not sure though as this mixes annotation spaces and I
guess there is a lot of tooling processing @Inject.

Regards
Carsten
-- 
Carsten Ziegeler
cziegeler@apache.org

Re: add support for @Inject to scripting.java

Posted by Justin Edelson <ju...@gmail.com>.
On 3/15/11 8:28 AM, Carsten Ziegeler wrote:
> chetan mehrotra  wrote
>> One reason for using Inject would be that @Reference is a compile time
>> annotation (i.e. RetentionPolicy.SOURCE). And due do that a new
>> annotation TestReference is being used in Sling Testing component [1].
>>
>> And as Inject is part of JSR 300 we can possibly have reference to
>> such a class in the code.
>>
> As the scripting.java bundle is compiling the class, we could use the
> @Reference annotation.
> However, maybe this is not a good idea to use it here as well.
My real reason for not wanting to use @Reference is that this isn't an
SCR context and it seems confusing to use SCR annotations in a non-SCR
context, which brings us to...

> 
> What about this crazy idea? We support the scr annotations and the
> scripting.java registers a "generated" bundle which contains the scr
> descriptors. This would make these services managed by scr.
> But maybe that's really too crazy...

I think this could be confusing. scripting.java follows the path
conventions of other scripts, just with a .java extensions. This opens
up the path to have a scripting.java-compiled servlet apply to a
different resource type than it "should" based on the path location.

So, yeah, I think too crazy...

Crazy counter-proposal.... maven-scr-plugin (and friends) should support
@Inject as a replacement for @Reference.

Justin

> 
> Regards
> Carsten


Re: add support for @Inject to scripting.java

Posted by Carsten Ziegeler <cz...@apache.org>.
chetan mehrotra  wrote
> One reason for using Inject would be that @Reference is a compile time
> annotation (i.e. RetentionPolicy.SOURCE). And due do that a new
> annotation TestReference is being used in Sling Testing component [1].
> 
> And as Inject is part of JSR 300 we can possibly have reference to
> such a class in the code.
> 
As the scripting.java bundle is compiling the class, we could use the
@Reference annotation.
However, maybe this is not a good idea to use it here as well.

What about this crazy idea? We support the scr annotations and the
scripting.java registers a "generated" bundle which contains the scr
descriptors. This would make these services managed by scr.
But maybe that's really too crazy...

Regards
Carsten
-- 
Carsten Ziegeler
cziegeler@apache.org

Re: add support for @Inject to scripting.java

Posted by chetan mehrotra <ch...@gmail.com>.
One reason for using Inject would be that @Reference is a compile time
annotation (i.e. RetentionPolicy.SOURCE). And due do that a new
annotation TestReference is being used in Sling Testing component [1].

And as Inject is part of JSR 300 we can possibly have reference to
such a class in the code.

Chetan Mehrotra

[1] http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/TestReference.java?view=markup

On Tue, Mar 15, 2011 at 1:45 PM, Markus Joschko
<ma...@gmail.com> wrote:
>
> I like the idea.
> I just wonder if it might be a bit inconsistent to use @Inject when
> most of the other code in a sling project (OSGI components) uses
> @Reference.
> As a developer I would appreciate consistency in how dependency
> injection is done.
>
> Regards,
>  Markus
>
> On Tue, Mar 15, 2011 at 2:51 AM, Justin Edelson
> <ju...@justinedelson.com> wrote:
> > I was looking at how much code is involved in referencing an OSGi
> > service from a script built using the scripting.java bundle and think
> > it's a bit verbose:
> >
> > SlingBindings bindings = (SlingBindings)
> > request.getAttribute(SlingBindings.class.getName());
> > SlingScriptHelper sling = bindings.getSling();
> > Interface service = sling.getService(Interface.class);
> >
> > I was thinking about adding some kind of annotation-based injection
> > support so that you could replace these lines with just:
> >
> >
> > @Inject
> > private Interface service;
> >
> > public void service(request, response) {
> >  // some code using service
> >
> > }
> >
> > @Inject seems like the obvious candidate to support for this. WDYT?
> >
> > Justin
> >

Re: add support for @Inject to scripting.java

Posted by Markus Joschko <ma...@gmail.com>.
I like the idea.
I just wonder if it might be a bit inconsistent to use @Inject when
most of the other code in a sling project (OSGI components) uses
@Reference.
As a developer I would appreciate consistency in how dependency
injection is done.

Regards,
 Markus

On Tue, Mar 15, 2011 at 2:51 AM, Justin Edelson
<ju...@justinedelson.com> wrote:
> I was looking at how much code is involved in referencing an OSGi
> service from a script built using the scripting.java bundle and think
> it's a bit verbose:
>
> SlingBindings bindings = (SlingBindings)
> request.getAttribute(SlingBindings.class.getName());
> SlingScriptHelper sling = bindings.getSling();
> Interface service = sling.getService(Interface.class);
>
> I was thinking about adding some kind of annotation-based injection
> support so that you could replace these lines with just:
>
>
> @Inject
> private Interface service;
>
> public void service(request, response) {
>  // some code using service
>
> }
>
> @Inject seems like the obvious candidate to support for this. WDYT?
>
> Justin
>