Skip to main content
Level 3
Oct 16, 2015 2:27 AM UTC
Solved

Extending CQ native commerce provider API

Hi there ,

 I have started exploring cq commerce and as part of it was trying to extend the CQ native API .  Referring the article from here    http://helpx.adobe.com/adobe-cq/using/creating-custom-cq-commerce-providers.html ,   i have deployed my extended CQ commerce classes in felix console as a OSGI bundle . My custom CQ commerce provider is 'training' .

  However in geometrixx / en pages when I change 'cq:commerceprovider' property to 'training' and start using the pages it throws Null pointer exceptions in the back end with no products getting displayed on the category pages .

 My debugger pointed out to getServiceContext   method   from   OOTB class 'AbstractJcrCommerceServiceFactory'  . It seems that   AbstractJcrCommerceServiceFactory has service dependencies via @ reference  to set of services like languagemanager , slingRepository etc which should normally gets injected  . The getServiceContext method basically gives handles to these set of injected references . 

These services are injecting as expected when I'm using the OOTB reference commerce provider implementation('geometrixx')  which by default ships in with CQ installation . However when I change the provider to 'training' these services are not getting  injected  and getserviceContext returns null,which eventually is throwing null pointer exception .  

I see the same behaviour when I deploy my bundle from 'crxde' or 'eclipse ' .  I was on java 1.7 and also tested it in java 1.6  . From felix console I can see the bundle properly registered . Any pointers why these services not getting injected ? 

 

@Component(componentAbstract = true, metatype = false) public abstract class AbstractJcrCommerceServiceFactory implements CommerceServiceFactory { @Reference protected LanguageManager languageManager; @Reference protected UserPropertiesService userPropertiesService; @Reference protected SlingSettingsService slingSettingsService; @Reference protected SlingRepository slingRepository; @Reference protected CommerceSearchProviderManager searchProviderManager;

 

 

 

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
ramm18660403Accepted solution
Oct 16, 2015 2:27 AM UTC

To Sandro - The issue that you have faced is because in the AbstractJcrCommerceSession, the languageManager is null. You have to put following code in you custom CommerceServiceFactory class. That will resolve your issue.

public class TrainingCommerceServiceFactory extends AbstractJcrCommerceServiceFactory implements CommerceServiceFactory{

      @Reference
      protected LanguageManager languageManager;
      @Reference
      protected UserPropertiesService userPropertiesService;
      @Reference
      protected SlingSettingsService slingSettingsService;
      @Reference
      protected SlingRepository slingRepository;
      @Reference
      protected CommerceSearchProviderManager searchProviderManager;

16 replies

smacdonald2008
Level 10
Oct 16, 2015 2:27 AM UTC

Watch the video here and there is also a link to GitHub that has the sample code -- see if this helps:

http://dev.day.com/content/ddc/en/gems/commerce-framework.html


Thxs 

Translate
Sham_HC
Level 10
Oct 16, 2015 2:27 AM UTC
Seems like You are using deprecated api can you use new api AbstractJcrCommerceService
Translate
Sep 23, 2016 7:28 AM UTC

This worked for me :) Thanks!

Translate
Level 2
Mar 5, 2018 11:15 AM UTC

Hi Sham,

I am facing similar issue where the ServiceContext seems to be empty. It returns languageManager , slingSettingService etc as null.

Caused by: java.lang.NullPointerException: null at com.adobe.cq.commerce.common.AbstractJcrCommerceSession.<init>(AbstractJcrCommerceSession.java:158) at com.gtsp.aem.core.commerce.GTSPCommerceSessionImpl.<init>(GTSPCommerceSessionImpl.java:45) 

Can you point out the exact version to be used? We are using AEM 6.3 and the commerce API showing as 5.15.48 on Depfinder.

Although we are not adding any dependency for commerce API in our POM, seems to be resolved from the Uber jar dependency.

mariad52599385​ What you changed to make it work?

Any pointer will be helpful. We are trying to use We-Retail implementation example.

Rohan.

Translate
vijays80591732
Level 3
Sep 24, 2018 6:48 AM UTC

Hi Madireddy,

I'm struckup with the same issue and I put your fix in my code. It started working. But when I restarted the server it is again not working. after that I have restarted my AEM server multiple times but no luck. do you have any idea on this?

Thanks,

Vijay

Translate
Oct 24, 2018 2:27 PM UTC

I ran into this issue as well. It was due to my project using OSGI R6 Annotations, while the AbstractJcrCommerceServiceFactory and the WeRetailCommerceServiceFactory both use the older Felix SCR annotations to populate the Referenced Services.

I was able to get around this by using OSGI R6 Annotations to reference the LanguageManager, UserPropertiesService, SlingSettingsService, SlingRepository, and CommerceSearchProviderManager within my CommerceServiceFactory.

Then within my CommerceServiceFactory.getCommerceService() method, call the bind methods that belong to AbstractJcrCommerceServiceFactory

public class MyCommerceServiceFactory extends AbstractJcrCommerceServiceFactory implements CommerceServiceFactory {

   @Reference
   protected LanguageManager languageManager;
   @Reference
   protected UserPropertiesService userPropertiesService;
   @Reference
   protected SlingSettingsService slingSettingsService;
   @Reference
   protected SlingRepository slingRepository;
   @Reference
   protected CommerceSearchProviderManager searchProviderManager;

  public CommerceService getCommerceService(Resource res) {

   bindLanguageManager(languageManager);
   bindUserPropertiesService(userPropertiesService);
   bindSlingSettingsService(slingSettingsService);
   bindSlingRepository(slingRepository);
   bindSearchProviderManager(searchProviderManager);
   return new MyCommerceServiceImpl(getServiceContext(), res);
  }

}

Translate