Expand my Community achievements bar.

SOLVED

Unable to access the value from OSGI configuration

Avatar

Level 2

Hi, I wanted to have an OSGi configuration with one field (type=String) to provide the folder path. So, I have written 2 classes for that - Interface and Implementation class file. Below is the code for your references.
Interface file:-

package devry.edu.core.services;
public interface OSGIConfigMisspelling {
public String getFolderPath();
}

 Implementation file:-

package devry.edu.core.services.impl;
import devry.edu.core.services.OSGIConfigMisspelling;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.*;

@Component(service = OSGIConfigMisspellingImpl.class,immediate = true)
@Designate(ocd = OSGIConfigMisspellingImpl.ServiceConfig.class )
public class OSGIConfigMisspellingImpl implements OSGIConfigMisspelling {

@ObjectClassDefinition(name="DidYouMean Servlet Content Fragment Folder - OSGi Configuration",
description = "OSGi Configuration for CF Folder")
public @interface ServiceConfig {

@AttributeDefinition(
name = "Folder Path",
description = "Enter folder path",
type = AttributeType.STRING)
public String folderPath() default "/content/dam/devry/misspelled-words";
}
private String folderPath;

@Activate
protected void activate(ServiceConfig serviceConfig){
folderPath=serviceConfig.folderPath();

}

@Override
public String getFolderPath() {
return folderPath;
}

}

I am getting this configurations tab in the Felix console with the provided name after building the project. But, from here on - "I want to use this attribute value coming from the config field in a path based servlet so how can I use that", can anyone please help?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@vikas101 Probably you will need to create an object of the class on the servlet using @reference and call the respective get method to get the value.

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

@vikas101 Probably you will need to create an object of the class on the servlet using @reference and call the respective get method to get the value.

Avatar

Level 2

@Manu_Mathew_ Yes, it's working using "Reference" annotation. The issue was also with the Impl class as in '@Component(service=.....)', I have provided the implementation class but it should be the interface. Thanks!

Avatar

Community Advisor

Use the annotation @reference to refer the configuration OSGi class and call the getter method available in the OSGi to get the folder path property 

 

Avatar

Level 2

Hi @Mani_kumar_ It worked using "Reference" annotation. Also, I had another issue with the Impl class as in '@Component(service=.....)', I have provided the implementation class but it should be the interface. Anyway Thanks a lot!

Avatar

Community Advisor

Make @Reference of OSGI class in your servlet and call respective method. Refer

https://gist.github.com/nateyolles/d0a547d814376e66f7aa