annotation property from another component | Community
Skip to main content
New Participant
October 30, 2015
Solved

annotation property from another component

  • October 30, 2015
  • 5 replies
  • 848 views

Hi,

now my component reads its annotation properties with the activation function.

I need to have a property shared by two components.

Can i read the property of another component inside the activation function?

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by samuel786431

This is the solution:

 

BundleContext bundleContext = FrameworkUtil.getBundle(PublishPagesService.class).getBundleContext(); ServiceReference osgiRef = bundleContext.getServiceReference(PublishPagesService.class.getName()); PublishPagesService service = (PublishPagesService) bundleContext.getService(osgiRef);

5 replies

Lokesh_Shivalingaiah
New Participant
November 2, 2015

You can also use @reference and get the instance of your service in another service.

samuel786431AuthorAccepted solution
New Participant
November 2, 2015

This is the solution:

 

BundleContext bundleContext = FrameworkUtil.getBundle(PublishPagesService.class).getBundleContext(); ServiceReference osgiRef = bundleContext.getServiceReference(PublishPagesService.class.getName()); PublishPagesService service = (PublishPagesService) bundleContext.getService(osgiRef);
smacdonald2008
New Participant
November 2, 2015

The activate function that is located within an OSGi service is invoked when you activate the Bundle. 

From within your Java service - you can use the Sling API to read a node property. 

New Participant
November 2, 2015

I have a component that is also a service. It initialize the variables inside the activate function and return them with another function.

Inside the jsp i have  MyService pageService = sling.MyService(MyService.class) that launch the activate method. This part of code work fine

 

Now inside the java code if i create an object of MyServiceImplementation and i try to retrieve the variables, they are all empty because the activate function isn't launched.

Can i force the launch pageService.activate(ctx)? What can i put as ctx?

Another way is to use the service directly inside the java but i don't have the sling variable and i don't know how to retrieve that

smacdonald2008
New Participant
October 30, 2015

You can code a public method that returns that property in a given service. So if ServiceA has prop Foo and you want the Foo prop in ServiceB, create a method in ServiceA that returns Foo. Then in Service B, create an instance of ServiceA and invoke the method that returns Foo. You can use dependency injection to get a ServiceA instance.