I am trying to write some standalone code and not an OSGi bundle for learning and trying out things in AEM. In OSGi bundle I could get the ResourceResolverFactory
with the @Reference
annotation.
I am wondering how do I get that in my standalone code. I need this factory to get the ResourceResolver
. I am not sure if this is even a possibility in the standalone code.
Actually same question goes for another factory, PageManagerFactory
!
Thanks - Atul
That is simply not possible. You need the OSGi runtime to work with OSGi services, period.
In a standalone Java applicaiton, you could technically start the framework in the main
method of a Java program. Check out the Apache Felix docs to see how. But to be honest, I don't see the point of doing so if you're working with AEM. That just makes things more complicated for you. You'd need bundles anyway and you'd be reinventing what AEM already does for you in the first place.
Now, if what you want to do is quickly experiment with some code without the overhead of creating an OSGi bundle, here's a couple of easy ways I know:
A couple of ways to quicly write code and test it out in AEM without creating a full-blown project.
This is an Open Source tool that allows you to execute Groovy code on your AEM instance. It gives you access to the Sling and JCR APIs and also allows you to interact with OSGi services.
It's easy to use, just install it, open the console page, type your script and execute it.
It provides Out Of the Box bindings for a number of commonly used objects, such as the resourceResolver
, session
, pageManager
, queryBuilder
, slingRequest
and such.
It also provides a number of convenience methods that make the Sling and JCR APIs easier to use, as well as a very simple utility that allows you to obtain references to OSGi services.
You asked about the ResourceResolverFactory
, here's how you can get one in a script:
def resourceResolverFactory = getService("org.apache.sling.api.resource.ResourceResolverFactory")
The console can even generate this code for you, it has a nice autocomplete feature:
Check out the project on GitHub to learn more.
Mind you, the console itself uses a bunch of OSGi bundles so the code is not actually standalone in that it doesn't exist in isolation from the OSGi environment. However, the user does not neeed to create bundles and deploy them just to experiment with the code.
Another project you might want to look at is the AEM HTL REPL. I haven't used it but I know it allows you to use the WCM Use APIs by writing server-side JavaScript.
This is essentionally what @VAr's answer suggests. Create a new component or modify an existing one.
Use a JSP scriptlet or a JavaScript utilising the WCM Use API. You can do it in CRXDE or use an actual IDE to write the code and the Vault tool to upload it to AEM. There are nice plugins for IntelliJ and Eclipse that make it somewhat easier.