Posts

Context Aware Configuration : Context Path Strategy

Image
I was working on a solution which involves introduction of sling context aware configuration to the application. When I explain the design to my manager, he agreed on all implementation steps except the default mapping of configuration with content using sling:configRef property. He pointed me out some of the challenges that may come while adding the property to the already running multi-locale site with experience fragments. Since the design was so impressive, I was keen to implement it somehow with very less interference with  content. So I started searching for a way more suitable to our requirement and I found something called "Context Path Strategy". This is something not very new but availability of resources specially for new developer are very limited so thought of walking through the implementation via this post. So what this Context Path Strategy is ? To implement context aware configuration, you will have to set sling:configRef property to every parent nodes of y

@Inject : A discouraged sling-model annotation

Image
"I just copy and paste it from somewhere and since it was working so I keep it like this. 😎"           @ Model (adaptables = Resource . class )      public class ExampleModel { @ Inject private MyService myService ; @ Inject private String title ; // ... rest of the class } I got this reply from the developer while reviewing his code above so thought of writing one extension post of my previous blog  Common Myths Around Sling Model .  So lets dive into the working mechanism of @Inject annotation but before that I would like to explain the injector first.                                                                      An OOB injector      @ Component (property= Constants . SERVICE_RANKING + ":Integer=3000" ,                service={ Injector . class , InjectAnnotationProcessorFactory2 . class }) public class ChildResourceInjector extends AbstractInjector implements Injector ,      Inject

[AEM Package installation issue] : Newer package version com.myapp:myapp.ui.apps:x.x.x-SNAPSHOT was installed externally. Marking as ignored.

Image
  Few days back I was looking into a package installation issue in one of the environment.  Issue was related to deployment. Even after deployment was successful some packages were not getting installed automatically. I need to manually install those packages to have the update changes.  Error log was saying something: 08.12.2022 08:42:54.382 *INFO* [OsgiInstallerImpl] com.adobe.granite.installer.factory.packages.impl.PackageTransformer Newer package version com.myapp:myapp.ui.content:2.10.0-SNAPSHOT was installed externally. Marking as ignored. Reason: In that particular instance, once some packages with version 3.0.0-SNAPSHOT was deployed and now we were trying to deploy package with version 2.10.0-SNAPSHOT, so AEM was not accepting those packages by saying that you already have higher version package installed. Means you already have updated code installed. Solution: In the above scenario, to make your latest code deployed automatically, you need to somehow increase the version of y

Common cause for NullPionterException or Null value in Junit test case for sling model in aem

Image
  Below are the common cause for NullPointerException and Null value in Junit test for Sling model: Problem with the adaptables Let say your sling model is adaptables to sling Resource and in your test class you are trying to get the instance of your respective Object by adapting request of aemContext. In this case you will end up with NullPointerException.  For example: Sling Model : @Model ( adaptables = { Resource .class}, adapters = { SolutionHeroComponent .class}, resourceType = { SolutionHeroComponentImpl .RESOURCE_TYPE}, defaultInjectionStrategy = DefaultInjectionStrategy .OPTIONAL ) @Exporter ( name = "jackson" , selector = "solutionhero" , extensions = "json" ) public class SolutionHeroComponentImpl implements SolutionHeroComponent { protected static final String RESOURCE_TYPE = "myproject/components/solutionhero" ; @Inject @Named (SOLUTION_HERO_TITLE)

"java.lang.NoSuchFieldError: FACTORY" while running Junit test in AEM codebase

 I was implementing Unit testing for a already running AEM project. After adding all the dependencies and after creation of basic structure for Sling model test class my test gets executed as expected. @ ExtendWith ({ AemContextExtension . class , MockitoExtension . class }) class DummyComponentImplTest { @ BeforeEach void setUp () { } @ Test public void testGetHeading () { } } But soon as I add below line at line #25 in the above running test case private final AemContext aemContext = new AemContext (); and try to run the test in terminal getting below error in the terminal's maven console. Running com . myproject . aem . models . impl . DummyComponentImplTest [ERROR] Tests run : 1 , Failures : 0 , Errors : 1 , Skipped : 0 , Time elapsed : 2.258 s <<< FAILURE! - in com . myproject . aem . models . impl . DummyComponentImplTest [ERROR] testGetHeading Time elapsed : 0.007 s <<< ERROR! java . lang . NoSuchFieldError

Common Myths Around Sling Model

Image
Fortunately I got a chance to talk to several people from AEM fraternity to understand their take about sling model and below are the summery of those discussions: Sling model is only back-end business logic provider to AEM components. Adaptable are just a way to get respective sling object reference. For example if you need slingRequest object in sling model then make your sling model adaptable to HttpSlingServletRequest. You can not get a value from sling model in a servlet or in any other class. When you need a reference of an OSGI service, you have only one way, that is @ OSGiService annotation. You can export only JSON data thru sling model exporter. You can not extend one sling model into another sling model. Sling model can to be created outside of the designated package in AEM. One Interface can not be a Sling Model, whenever you need to create a Sling Model you will have to write a pure POJO concrete implemented java class. You can only read properties of a node thru sling mod