Expand my Community achievements bar.

SOLVED

annotation property from another component

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Level 4

This is the solution:

 

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

View solution in original post

5 Replies

Avatar

Level 10

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.

Avatar

Level 4

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

Avatar

Level 10

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. 

Avatar

Correct answer by
Level 4

This is the solution:

 

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

Avatar

Level 10

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