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?
Solved! Go to Solution.
Views
Replies
Total Likes
@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.
Views
Replies
Total Likes
@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.
Views
Replies
Total Likes
@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!
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
Views
Replies
Total Likes
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!
Views
Replies
Total Likes
Make @Reference of OSGI class in your servlet and call respective method. Refer
https://gist.github.com/nateyolles/d0a547d814376e66f7aa
Views
Replies
Total Likes
Views
Likes
Replies