use of org.osgi.service.metatype.MetaTypeService in project felix by apache.
the class MetaTypeServiceImplTest method testAfterCretionManagedServiceFactory.
public void testAfterCretionManagedServiceFactory() {
MetaTypeService mts = new MetaTypeServiceImpl(bundleContext);
MetaTypeInformation mti = mts.getMetaTypeInformation(bundleContext.getBundle());
// assert still empty
checkEmpty(mti);
// register a service with PID
String factoryPid = "testAfterCreation_factory";
MockManagedServiceFactory service = new MockManagedServiceFactory();
Dictionary props = new Hashtable();
props.put(Constants.SERVICE_PID, factoryPid);
ServiceRegistration sr = bundleContext.registerService(ManagedServiceFactory.class.getName(), service, props);
// locales should contain MockMetaTypeProvider.LOCALES
assertNotNull(mti.getLocales());
assertTrue(mti.getLocales().length == 1);
assertEquals(MockMetaTypeProvider.LOCALES[0], mti.getLocales()[0]);
// pids must be empty
assertTrue(mti.getPids() == null || mti.getPids().length == 0);
// pids must contain pid
assertNotNull(mti.getFactoryPids());
assertTrue(mti.getFactoryPids().length == 1);
assertEquals(factoryPid, mti.getFactoryPids()[0]);
// change the service PID
String factoryPid2 = "testAfterCreation2";
Dictionary props2 = new Hashtable();
props2.put(Constants.SERVICE_PID, factoryPid2);
sr.setProperties(props2);
// pids must contain pid2
assertNotNull(mti.getFactoryPids());
assertTrue(mti.getFactoryPids().length == 1);
assertEquals(factoryPid2, mti.getFactoryPids()[0]);
// unregister the service
sr.unregister();
// ensure everything is clear now again
checkEmpty(mti);
}
use of org.osgi.service.metatype.MetaTypeService in project felix by apache.
the class MetaTypeServiceSupport method getObjectClassDefinitions.
/**
* Returns the <code>ObjectClassDefinition</code> objects for the IDs
* returned by the <code>idGetter</code>. Depending on the
* <code>idGetter</code> implementation this will be for factory PIDs or
* plain PIDs.
*
* @param idGetter The {@link IdGetter} used to get the list of factory PIDs
* or PIDs from <code>MetaTypeInformation</code> objects.
* @param locale The name of the locale to get the object class definitions
* for.
* @return Map of <code>ObjectClassDefinition</code> objects indexed by the
* PID (or factory PID) to which they pertain
*/
private Map getObjectClassDefinitions(final IdGetter idGetter, final String locale) {
final Map objectClassesDefinitions = new HashMap();
final MetaTypeService mts = this.getMetaTypeService();
if (mts != null) {
final Bundle[] bundles = this.getBundleContext().getBundles();
for (int i = 0; i < bundles.length; i++) {
final MetaTypeInformation mti = mts.getMetaTypeInformation(bundles[i]);
if (mti != null) {
final String[] idList = idGetter.getIds(mti);
for (int j = 0; idList != null && j < idList.length; j++) {
// After getting the list of PIDs, a configuration might be
// removed. So the getObjectClassDefinition will throw
// an exception, and this will prevent ALL configuration from
// being displayed. By catching it, the configurations will be
// visible
ObjectClassDefinition ocd = null;
try {
ocd = mti.getObjectClassDefinition(idList[j], locale);
} catch (IllegalArgumentException ignore) {
// ignore - just don't show this configuration
}
if (ocd != null) {
objectClassesDefinitions.put(idList[j], ocd);
}
}
}
}
}
return objectClassesDefinitions;
}
use of org.osgi.service.metatype.MetaTypeService in project ddf by codice.
the class ConfigurationAdminTest method getConfigAdmin.
private ConfigurationAdmin getConfigAdmin() throws IOException, InvalidSyntaxException {
final BundleContext testBundleContext = mock(BundleContext.class);
final MetaTypeService testMTS = mock(MetaTypeService.class);
ConfigurationAdminExt configurationAdminExt = new ConfigurationAdminExt(CONFIGURATION_ADMIN) {
@Override
BundleContext getBundleContext() {
return testBundleContext;
}
@Override
MetaTypeService getMetaTypeService() {
return testMTS;
}
@Override
public boolean isPermittedToViewService(String servicePid) {
return true;
}
};
ConfigurationAdmin configurationAdmin = new ConfigurationAdmin(CONFIGURATION_ADMIN, configurationAdminExt);
configurationAdmin.setGuestClaimsHandlerExt(mockGuestClaimsHandlerExt);
Dictionary<String, Object> testProp = new Hashtable<>();
testProp.put(TEST_KEY, TEST_VALUE);
when(testConfig.getPid()).thenReturn(TEST_PID);
when(testConfig.getFactoryPid()).thenReturn(TEST_FACTORY_PID);
when(testConfig.getBundleLocation()).thenReturn(TEST_LOCATION);
when(testConfig.getProperties()).thenReturn(testProp);
Bundle testBundle = mock(Bundle.class);
Dictionary bundleHeaders = mock(Dictionary.class);
MetaTypeInformation testMTI = mock(MetaTypeInformation.class);
ObjectClassDefinition testOCD = mock(ObjectClassDefinition.class);
ServiceReference testRef1 = mock(ServiceReference.class);
ServiceReference[] testServRefs = { testRef1 };
ArrayList<AttributeDefinition> attDefs = new ArrayList<>();
for (int cardinality : CARDINALITIES) {
for (TYPE type : TYPE.values()) {
AttributeDefinition testAttDef = mock(AttributeDefinition.class);
when(testAttDef.getCardinality()).thenReturn(cardinality);
when(testAttDef.getType()).thenReturn(type.getType());
when(testAttDef.getID()).thenReturn(getKey(cardinality, type));
attDefs.add(testAttDef);
}
}
when(testRef1.getProperty(Constants.SERVICE_PID)).thenReturn(TEST_PID);
when(testRef1.getBundle()).thenReturn(testBundle);
when(testBundle.getLocation()).thenReturn(TEST_LOCATION);
when(testBundle.getHeaders(anyString())).thenReturn(bundleHeaders);
when(bundleHeaders.get(Constants.BUNDLE_NAME)).thenReturn(TEST_BUNDLE_NAME);
when(testOCD.getName()).thenReturn(TEST_OCD);
when(testOCD.getAttributeDefinitions(ObjectClassDefinition.ALL)).thenReturn(attDefs.toArray(new AttributeDefinition[attDefs.size()]));
when(testMTI.getBundle()).thenReturn(testBundle);
when(testMTI.getFactoryPids()).thenReturn(new String[] { TEST_FACTORY_PID });
when(testMTI.getPids()).thenReturn(new String[] { TEST_PID });
when(testMTI.getObjectClassDefinition(anyString(), anyString())).thenReturn(testOCD);
when(testMTS.getMetaTypeInformation(testBundle)).thenReturn(testMTI);
when(testBundleContext.getBundles()).thenReturn(new Bundle[] { testBundle });
when(CONFIGURATION_ADMIN.listConfigurations(anyString())).thenReturn(new Configuration[] { testConfig });
when(CONFIGURATION_ADMIN.getConfiguration(anyString(), anyString())).thenReturn(testConfig);
when(testBundleContext.getAllServiceReferences(anyString(), anyString())).thenReturn(testServRefs);
when(testBundleContext.getAllServiceReferences(anyString(), anyString())).thenReturn(testServRefs);
return configurationAdmin;
}
use of org.osgi.service.metatype.MetaTypeService in project ddf by codice.
the class ApplicationServiceBean method getMetaTypeService.
/**
* Gets the service with the specified class name. Will create a new {@link ServiceTracker} if the
* service is not already retrieved.
*
* @return the service or <code>null</code> if missing.
*/
@Nullable
final MetaTypeService getMetaTypeService() {
BundleContext context = getContext();
if (serviceTracker == null) {
serviceTracker = new ServiceTracker<>(context, META_TYPE_NAME, null);
serviceTracker.open();
}
return (MetaTypeService) serviceTracker.getService();
}
use of org.osgi.service.metatype.MetaTypeService in project ddf by codice.
the class ApplicationServiceBean method getServices.
/**
* {@inheritDoc}.
*/
@Override
public List<Map<String, Object>> getServices(String applicationID) {
try {
List<Service> services = configAdmin.listServices(getDefaultFactoryLdapFilter(), getDefaultLdapFilter());
if (services.isEmpty()) {
return Collections.emptyList();
}
Set<BundleInfo> bundleInfos = getBundleInfosForApplication(applicationID);
if (bundleInfos == null) {
return Collections.emptyList();
}
Set<String> bundleLocations = bundleInfos.stream().map(BundleInfo::getLocation).collect(Collectors.toSet());
MetaTypeService metatypeService = getMetaTypeService();
Set<MetaTypeInformation> metatypeInformation = (metatypeService == null) ? Collections.emptySet() : Arrays.stream(getContext().getBundles()).filter(b -> bundleLocations.contains(computeLocation(b))).map(metatypeService::getMetaTypeInformation).collect(Collectors.toSet());
return services.stream().filter(service -> hasBundleLocation(service, bundleLocations) || hasMetatypesForService(service, metatypeInformation)).collect(Collectors.toList());
} catch (VirtualMachineError e) {
throw e;
} catch (Throwable e) {
LOGGER.info("Could not retrieve services", e);
throw new ApplicationServiceBeanException("Could not retrieve services");
}
}
Aggregations