Last active
February 10, 2021 08:35
-
-
Save sustacek/5502c19cfa757367b518aead660682ab to your computer and use it in GitHub Desktop.
OSGi - list all instances of a factory config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aQute.bnd.annotation.metatype.Meta; | |
import com.liferay.portal.configuration.metatype.annotations.ExtendedObjectClassDefinition; | |
import com.liferay.portal.configuration.metatype.bnd.util.ConfigurableUtil; | |
import org.osgi.framework.InvalidSyntaxException; | |
import org.osgi.service.cm.Configuration; | |
import org.osgi.service.cm.ConfigurationAdmin; | |
import org.osgi.service.component.annotations.Component; | |
import org.osgi.service.component.annotations.Reference; | |
import java.io.IOException; | |
@Component | |
class ConfigsLister { | |
public void getConfigs() throws IOException, InvalidSyntaxException { | |
String filterString = String.format( | |
"(service.factoryPid=%s)", MyConfiguration.ID); | |
// You can even filter even on individual properties of the config, like 'myKey', | |
// to retrieve a single config (if you know its property you configured) | |
// String filterString = String.format( | |
// "(&(service.factoryPid=%s)(myKey=%s))", | |
// MyConfiguration.ID, "XYZ"); | |
// retrieves all matching configs -- created for given factory from OSGi container | |
Configuration[] configurations = | |
_configurationAdmin.listConfigurations(filterString); | |
// _configurationAdmin::listConfigurations might return <null> (not empty array) | |
if (configurations == null) { | |
System.out.println("Sorry, no configs found with filter " + filterString); | |
return; | |
} | |
for(Configuration configuration: configurations) { | |
System.out.println( | |
"Found config: pid: " + configuration.getPid() + "), properties: " + | |
configuration.getProperties()); | |
// Gives you an instance of MyConfiguration, to retrieve all your fields nicely typed | |
MyConfiguration typedConfig = ConfigurableUtil.createConfigurable( | |
MyConfiguration.class, configuration.getProperties()); | |
String myKey = typedConfig.myKey(); | |
// ... | |
} | |
} | |
@Reference | |
private ConfigurationAdmin _configurationAdmin; | |
// A sample OSGi config type | |
@ExtendedObjectClassDefinition( | |
category = "todo-my-category", | |
factoryInstanceLabelAttribute = "myKey", | |
scope = ExtendedObjectClassDefinition.Scope.SYSTEM | |
) | |
@Meta.OCD( | |
factory = true, id = MyConfiguration.ID, | |
localization = "content/Language", name = "todo-my-config.config-name" | |
) | |
public interface MyConfiguration { | |
String ID = "com.acme.MyConfiguration"; | |
@Meta.AD( | |
name = "my-key", description = "my-key.description") | |
String myKey(); | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment