Skip to content

Instantly share code, notes, and snippets.

@yupadhyay
Created February 14, 2016 18:13
Show Gist options
  • Save yupadhyay/9eea9f1818416620edf9 to your computer and use it in GitHub Desktop.
Save yupadhyay/9eea9f1818416620edf9 to your computer and use it in GitHub Desktop.
Create Custom ConfigFactory AEM
Here is how you can use configuration factory,
public interface MyCustomService{
}
--------------------------------------------
@Component (ConfigurationFactory=true, metatype = true, policy=ConfigurationPolicy.REQUIRE)
@Service(value = MyCustomService.class)
//This can be anything as explained above
@Properties({
@Property(name=SOMETHING, unbounded=PropertyUnbounded.ARRAY),
@Property(name=SOMETHING, unbounded=PropertyUnbounded.ARRAY),
@Property(name="key",value="Something random")
})
public class MyCustomServiceFactory implements MyCustomService {
}
-----------------------------------------------------
public interface MyCustomServiceFacade {
public MyCustomService getMyCustomService (String key);
}
-----------------------------------------------------
@Component
@Service
@References({
@Reference (cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC, referenceInterface = MyCustomService.class, name="MyCustomService")
})
public class MyCustomServiceFacadeImpl implements MyCustomServiceFacade {
private static String KEY="key";
private final Map<String, MyCustomService > myCustomServices = new HashMap<String, MyCustomService>();
private ComponentContext;
// works only for exact matches
public MyCustomService get MyCustomService(String key) {
return myCustomServices.get (key);
}
protected void bindMyCustomService(ServiceReference ref) {
synchronized (this. myCustomServices) {
String customKey = ref.getProperty(KEY);
MyCustomServices operation = this.componentContext.locateService("MyCustomService",ref);
//Or you can use
//MyCustomServices operation = ref.getProperty("service.pid");
if (operation != null) {
myCustomServices.put(customKey,ref);
}
}
}
protected void unbindMyCustomService(ServiceReference ref) {
synchronized (this.myCustomServices) {
String customKey = ref.getProperty(KEY);
myCustomServices.remove(customKey);
}
}
@Activate
protected void activate (ComponentContext ctx) {
this.componentContext = ctx;
}
}
-----------------------------------------
POM Dependencies:
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.configadmin</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.6.0</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment