You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Jason Bailey <Ja...@sas.com> on 2014/07/16 21:06:41 UTC

adding java classes into the jcr

We're using CQ5 pretty extensively for our external web presence and I'm in the process of evaluating Sling for a wider range of use cases internally.

One of the questions that comes up on a regular basis is whether we are able to define a java object alongside the JSP when we are creating a widget. So that we can potentially implement a very specific business logic and have it co-located with the presentation logic.   There seems to be support in Sling for compiling java classes located in the JCR, but there's a lack of clarity on when/how that occurs.

Any information would be appreciated.

-Jason

RE: adding java classes into the jcr

Posted by Jason Bailey <Ja...@sas.com>.
Hi Olaf

I appreciate the feedback. You're correct that this is a desire to modularize on the component level, as I stated in my email, when I'm dealing with people ramping up on the framework this question of co-location is the most common I get. 

I had half expected that someone had implemented something where the default java ScriptEngine was removed and an Event based compiler added to handle java files. Not that I'm bothered too much, I'm doing more due diligence then anything else.

I will check out slice and neba.

-Jason
________________________________________
From: Olaf Otto <ol...@x100.de>
Sent: Thursday, July 17, 2014 6:06 PM
To: users@sling.apache.org
Subject: RE: adding java classes into the jcr

Hi Jason

I take it the idea is to strictly modularize on a CQ component level? While
there is some support for placing java files in the JCR and use them as
servlets for specific resources, this approach is somewhat exotic and you're
better of providing application code  - such as models - in OSGi bundles.
This is much better in terms of separation of layers and concerns, and
allows you to use all the OSGi goodness Felix provides, such as SCR
(http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-pl
ugin/scr-annotations.html) as well as any of the following solutions:

1.) The #1 way to provide specific models for resource in Sling is to use an
AdapterFactory so you can .adaptTo(SpecificModel.class) from a resource,
e.g. in a view, taglib, servlet and the like.
(http://sling.apache.org/documentation/the-sling-engine/adapters.html)

2.) Nowadays, you can do so declaratively using Sling Models
(http://sling.apache.org/documentation/bundles/models.html). This also gives
you Resource - to - model mapping, i.e. mapping properties and resources
onto a POJO java model.

3.) If you'd like to leverage google guice, you can use Slice from cognifide
(http://www.cognifide.com/blogs/cq/make-your-cq-life-easier-with-slice/).
This will also support .adaptTo and provides resource to model mapping.

4.) Finally, there is NEBA (http://neba.io), which gives you .adaptTo +
resource to model mapping and Spring MVC plus some more features. I'm a bit
biased towards that one - since I am NEBA's lead developer ;-)


Kind regards,
Olaf



-----Original Message-----
From: Jason Bailey [mailto:Jason.Bailey@sas.com]
Sent: Mittwoch, 16. Juli 2014 21:07
To: users@sling.apache.org
Subject: adding java classes into the jcr

We're using CQ5 pretty extensively for our external web presence and I'm in
the process of evaluating Sling for a wider range of use cases internally.

One of the questions that comes up on a regular basis is whether we are able
to define a java object alongside the JSP when we are creating a widget. So
that we can potentially implement a very specific business logic and have it
co-located with the presentation logic.   There seems to be support in Sling
for compiling java classes located in the JCR, but there's a lack of clarity
on when/how that occurs.

Any information would be appreciated.

-Jason


Re: adding java classes into the jcr

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

On Fri, Jul 18, 2014 at 12:06 AM, Olaf Otto <ol...@x100.de> wrote:
> ...While
> there is some support for placing java files in the JCR and use them as
> servlets for specific resources, this approach is somewhat exotic and you're
> better of providing application code  - such as models - in OSGi bundles...

Absolutely agree with that, having business logic in bundles makes it
easier to test, version and manage that critical code than having code
scattered around the content repository.

I recommend Sling Models [1] for business logic today, as they allow
you to have your business logic in OSGi bundles, while keeping the
boilerplate code to a minimum. And they run on a bare Sling instance
with no extra dependencies.

I'm not a fan of using additional component management systems like
Spring or Guice on top of Sling . OSGi with declarative services
provides everything you need IMO, and you get that out of the box with
Sling, without having to mix several java component models.

I suppose people are using other tools because they are used to them,
but if you're starting new projects I wouldn't do that.

-Bertrand

[1] http://sling.apache.org/documentation/bundles/models.html

RE: adding java classes into the jcr

Posted by Olaf Otto <ol...@x100.de>.
Hi Jason

I take it the idea is to strictly modularize on a CQ component level? While
there is some support for placing java files in the JCR and use them as
servlets for specific resources, this approach is somewhat exotic and you're
better of providing application code  - such as models - in OSGi bundles.
This is much better in terms of separation of layers and concerns, and
allows you to use all the OSGi goodness Felix provides, such as SCR
(http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-pl
ugin/scr-annotations.html) as well as any of the following solutions:

1.) The #1 way to provide specific models for resource in Sling is to use an
AdapterFactory so you can .adaptTo(SpecificModel.class) from a resource,
e.g. in a view, taglib, servlet and the like.
(http://sling.apache.org/documentation/the-sling-engine/adapters.html)

2.) Nowadays, you can do so declaratively using Sling Models
(http://sling.apache.org/documentation/bundles/models.html). This also gives
you Resource - to - model mapping, i.e. mapping properties and resources
onto a POJO java model.

3.) If you'd like to leverage google guice, you can use Slice from cognifide
(http://www.cognifide.com/blogs/cq/make-your-cq-life-easier-with-slice/).
This will also support .adaptTo and provides resource to model mapping.

4.) Finally, there is NEBA (http://neba.io), which gives you .adaptTo +
resource to model mapping and Spring MVC plus some more features. I'm a bit
biased towards that one - since I am NEBA's lead developer ;-)


Kind regards,
Olaf



-----Original Message-----
From: Jason Bailey [mailto:Jason.Bailey@sas.com] 
Sent: Mittwoch, 16. Juli 2014 21:07
To: users@sling.apache.org
Subject: adding java classes into the jcr

We're using CQ5 pretty extensively for our external web presence and I'm in
the process of evaluating Sling for a wider range of use cases internally.

One of the questions that comes up on a regular basis is whether we are able
to define a java object alongside the JSP when we are creating a widget. So
that we can potentially implement a very specific business logic and have it
co-located with the presentation logic.   There seems to be support in Sling
for compiling java classes located in the JCR, but there's a lack of clarity
on when/how that occurs.

Any information would be appreciated.

-Jason