Expand my Community achievements bar.

SOLVED

Extending CQ native commerce provider API

Avatar

Level 3

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;

 

 

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 1

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;

View solution in original post

16 Replies

Avatar

Level 10
Seems like You are using deprecated api can you use new api AbstractJcrCommerceService

Avatar

Level 10

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 

Avatar

Level 3

Hi  ,

   Even with the code from GIT hub i'm facing the same problem . I'm on AEM 5.6.1 and using CQ commerce 5.18 . Also In the code i'm making sure i'm not using any deprecated API methods . Like , with CQ commerce 5.8 it uses AbstractJcrCommerceServiceFactory.services where as with 5.18 it is AbstractJcrCommerceServiceFactory.getServiceContext .

I have further debugged and found that due to some reasons  , only in AbstractJcrCommerceServiceFactory and its subclasses with using @reference annotation , services (like slingRepository )  are not getting injected . However I'm able to get the same services using bundle context with below code 

ServiceReference serviceReference = bundleContext.getServiceReference(serviceName); return   bundleContext.getService(serviceReference);

To proceed , i'm using the above code to get the services  in TrainingJcrCommerceFactory class (which extends AbstractJcrCommerceServiceFactory ) and returning them to TrainingServiceContext class (which Extends ServiceContext) . The overridden getServiceContext method in TrainingJcrCommerceFactory  would return instance of TrainingServiceContext  . 

This way it works , but i'm still clueless why the same services are not getting injected  in AbstractJcrCommerceServiceFactory and its subclasses  when using with @reference annotation . The same services with using @reference are getting injected properly in other component classes . Did any one face such behavior before ? Am i missing any thing?

regards

Krishna

Avatar

Level 1

Hi,

I seem to have the same problem. 

I was trying to build my first e-commerce bundle and I followed the tutorial provided at http://dev.day.com/content/ddc/en/gems/commerce-framework.html, and even using the code from GitHub as it is, it doesn't work. The product page is left blank and exceptions are thrown.

Any suggestion on how to solve this (provided that I am using the GitHub code AS IT IS and I am not very familiar with the Commerce API yet)?

Thanks in advance,

Sandro

Avatar

Level 3

Hi Sandro ,

  I was getting null pointer exceptions because dependency services were not getting injected properly by felix . In my case felix was not able to inject dependencies  due to cq-hybris-impl reference implementation bundle  . With this bundle when ever I tried to start my felix console it started with lot of exception trace .  Since I was working with native implementation , I stopped this bundle restarted my cq instance and from then it started working . 

Avatar

Level 1

Hi again,
I think I am facing a different problem. I followed the tutorial above and built the package using Maven in my local fresh CQ instance (with no other package installed than the ones included in the native installation), and when trying to use the example implementation for the e-commerce (changing the value in cq:commerceProvider and opening a single product page from the Geometrixx-outdoor site) I am getting an exception and the page cannot load. Here is a part of the stacktrace as it is in the error.log file:

Caused by: java.lang.AbstractMethodError: com.adobe.training.TrainingCommerceServiceImpl.isAvailable(Ljava/lang/String;)Z
    at org.apache.jsp.apps.geometrixx_002doutdoors.components.page.body_jsp._jspService(body_jsp.java:165)
    at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502)
    ... 129 more

As you can see in the last part, it seems the exception is caused by the abstract method TrainingCommerceServiceImpl.isAvailable (which is not included in the implementation downloaded from GitHub). When I try to add this method to my class just returning "true", the page gets loaded, but it is empty, and the stacktrace in the log this time is:


Caused by: java.lang.NullPointerException
    at com.adobe.cq.commerce.common.AbstractJcrCommerceSession.<init>(AbstractJcrCommerceSession.java:140)
    at com.adobe.training.TrainingCommerceSessionImpl.<init>(TrainingCommerceSessionImpl.java:17)
    at com.adobe.training.TrainingCommerceServiceImpl.login(TrainingCommerceServiceImpl.java:33)
    at org.apache.jsp.libs.cq.personalization.components.contextstores.cart.init_js_jsp._jspService(init_js_jsp.java:175)
    at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502)
    ... 116 more

So seemingly I am getting a NullPointerException in AbstractJcrCommerceSession.<init> and I don't know why..
As I said I am just getting started on the e-commerce module of CQ, and this is my first attempt , so I don't know the API very well (I assumed that the code provided in the tutorial would work straight out of the box)...Any help on this issue?

Thanks in advance,
Sandro

Avatar

Level 10

To sandro - what version of CQ are you using?  This code is for 5.6 or higher. When the article was written last Spring - it worked as shown in the video.

Avatar

Correct answer by
Level 1

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;

Avatar

Former Community Member

I tried the same. but still the exception remains..

any more suggestions?

Avatar

Level 10

Here is the output you should see:

[img]product.png[/img]

Avatar

Level 3

Hi ,

   Did you get a chance to debug the code with CQ commerce API added as src .  That was how I was able to dig out the problem ,  I faced with 'AbstractJcrCommerceServiceFactory.getServiceContext ' . I was on AEM 5.6.1 and the code from GIT hub didn't work out straight for me . 

Avatar

Former Community Member

Hi krisgumm,

 

you have mentioned that 

I was on AEM 5.6.1 and the code from GIT hub didn't work out straight for me . 

can you specify me what are the changes you have done to make the code work?

Thanks,

vksptg

Avatar

Level 2

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.

Avatar

Level 4

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

Avatar

Level 1

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);
  }

}