Search in sources :

Example 1 with MetaTypeService

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);
}
Also used : Dictionary(java.util.Dictionary) MetaTypeService(org.osgi.service.metatype.MetaTypeService) Hashtable(java.util.Hashtable) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ServiceRegistration(org.osgi.framework.ServiceRegistration) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory)

Example 2 with MetaTypeService

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;
}
Also used : MetaTypeService(org.osgi.service.metatype.MetaTypeService) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) HashMap(java.util.HashMap) Map(java.util.Map) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 3 with MetaTypeService

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;
}
Also used : Dictionary(java.util.Dictionary) MetaTypeService(org.osgi.service.metatype.MetaTypeService) Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) Mockito.anyString(org.mockito.Mockito.anyString) ServiceReference(org.osgi.framework.ServiceReference) TYPE(org.codice.ddf.ui.admin.api.ConfigurationAdmin.TYPE) BundleContext(org.osgi.framework.BundleContext) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 4 with MetaTypeService

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();
}
Also used : MetaTypeService(org.osgi.service.metatype.MetaTypeService) BundleContext(org.osgi.framework.BundleContext) Nullable(javax.annotation.Nullable)

Example 5 with MetaTypeService

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");
    }
}
Also used : Arrays(java.util.Arrays) ConfigurationAdmin(org.codice.ddf.admin.core.api.ConfigurationAdmin) Constants(org.osgi.framework.Constants) LoggerFactory(org.slf4j.LoggerFactory) Application(org.codice.ddf.admin.application.service.Application) FeaturesService(org.apache.karaf.features.FeaturesService) SynchronizedInstaller(org.codice.ddf.sync.installer.api.SynchronizedInstaller) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) Map(java.util.Map) Bundle(org.osgi.framework.Bundle) EnumSet(java.util.EnumSet) ReflectionException(javax.management.ReflectionException) BundleInfo(org.apache.karaf.features.BundleInfo) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) SystemService(org.apache.karaf.system.SystemService) Service(org.codice.ddf.admin.core.api.Service) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Feature(org.apache.karaf.features.Feature) FeatureDetails(org.codice.ddf.admin.application.rest.model.FeatureDetails) Set(java.util.Set) ObjectName(javax.management.ObjectName) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) Collectors(java.util.stream.Collectors) BundleContext(org.osgi.framework.BundleContext) Objects(java.util.Objects) MalformedObjectNameException(javax.management.MalformedObjectNameException) List(java.util.List) Stream(java.util.stream.Stream) AccessController(java.security.AccessController) ApplicationPlugin(org.codice.ddf.admin.application.plugin.ApplicationPlugin) HashMap(java.util.HashMap) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ArrayList(java.util.ArrayList) SERVICE_FACTORYPID(org.osgi.service.cm.ConfigurationAdmin.SERVICE_FACTORYPID) CollectionUtils(org.apache.commons.collections.CollectionUtils) MBeanServer(javax.management.MBeanServer) MBeanRegistrationException(javax.management.MBeanRegistrationException) ManagementFactory(java.lang.management.ManagementFactory) InstanceNotFoundException(javax.management.InstanceNotFoundException) Nullable(javax.annotation.Nullable) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) PrivilegedActionException(java.security.PrivilegedActionException) Logger(org.slf4j.Logger) SecurityLogger(ddf.security.audit.SecurityLogger) FileUtils(org.apache.commons.io.FileUtils) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) MetaTypeService(org.osgi.service.metatype.MetaTypeService) MBeanException(javax.management.MBeanException) Paths(java.nio.file.Paths) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) MetaTypeService(org.osgi.service.metatype.MetaTypeService) FeaturesService(org.apache.karaf.features.FeaturesService) SystemService(org.apache.karaf.system.SystemService) Service(org.codice.ddf.admin.core.api.Service) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) MetaTypeService(org.osgi.service.metatype.MetaTypeService) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) BundleInfo(org.apache.karaf.features.BundleInfo)

Aggregations

MetaTypeService (org.osgi.service.metatype.MetaTypeService)15 MetaTypeInformation (org.osgi.service.metatype.MetaTypeInformation)12 Bundle (org.osgi.framework.Bundle)8 Hashtable (java.util.Hashtable)6 ObjectClassDefinition (org.osgi.service.metatype.ObjectClassDefinition)6 ArrayList (java.util.ArrayList)5 Dictionary (java.util.Dictionary)5 HashMap (java.util.HashMap)5 BundleContext (org.osgi.framework.BundleContext)5 Map (java.util.Map)4 AttributeDefinition (org.osgi.service.metatype.AttributeDefinition)3 ServiceTracker (org.osgi.util.tracker.ServiceTracker)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Nullable (javax.annotation.Nullable)2 BundleInfo (org.apache.karaf.features.BundleInfo)2 SystemService (org.apache.karaf.system.SystemService)2 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)2 Service (org.codice.ddf.admin.core.api.Service)2 Mockito.anyString (org.mockito.Mockito.anyString)2 ServiceReference (org.osgi.framework.ServiceReference)2