Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

service injection with parameter

Avatar

Level 2

I am trying to call AWS S3 bucket from our AEM. I have created an S3Manager class/service to to wrap it. which will be injected/referenced from wherever needed. This is working fine, but requires to create AmazonS3Client instance each time when I need to upload or download a file.

I am trying to initialise the amazonS3Client in the constructor instead, so I don't need to create so many instance.

private AmazonS3Client amazonS3Client;

public S3Manager(){

  String externalId = sitemapService.getAssumeRoleId();
   String roleArn = sitemapService.getRoleArn();


   amazonS3Client = getAmazonS3Client(externalId, roleArn);
}

SitemapService is another service I created to read the OSGI config (from xml or the console). The way I am doing seems not working, I am getting following exception, when initialisation try to run  sitemapService.getAssumeRoleId();

Error during instantiation of the implementation object (java.lang.NullPointerException)

I assume at the point of running, sitemapservice has not been injected yet. Is there a way, I can assure that sitemapservice injection is completed before initialise S3Manager? Or there is better way to achieve what I am looking for. Thanks

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

When you use the standard injection (just @Reference) it has the ReferencePolicy static. That means, that this reference must be satisfied in order to acticate the component.

But I am quite sure, that your issue is different. When you implement that in the constructor, the OSGI runtime has not yet injected the references. You need to implement a service activator, see Apache Felix - Apache Felix Service Component Runtime (SCR)  for a way how to do it (note: today you are encouraged to use OSGI annotations and no longer SCR, but the activate stuff remains the same ...)

Jörg

View solution in original post

3 Replies

Avatar

Correct answer by
Employee Advisor

When you use the standard injection (just @Reference) it has the ReferencePolicy static. That means, that this reference must be satisfied in order to acticate the component.

But I am quite sure, that your issue is different. When you implement that in the constructor, the OSGI runtime has not yet injected the references. You need to implement a service activator, see Apache Felix - Apache Felix Service Component Runtime (SCR)  for a way how to do it (note: today you are encouraged to use OSGI annotations and no longer SCR, but the activate stuff remains the same ...)

Jörg

Avatar

Level 2

Thanks Jörg. How would I able to make sure that the injection happens before the initialisation? I have already tried to set attribute immediate to true for the Component, but that doesn't make any difference.  Is there something I can configure the order or so ? Thanks

Avatar

Employee Advisor

Make sure you understand the OSGI component livecycle. Initialization should not happen in the constructor, but rather in the activate() method (or the method annotated with @Activate).

Besides the OSGI specification (which is sometimes hard to read) I found org.osgi.service.component | osgi.enroute.site  to explain it nicely.

Jörg