Search in sources :

Example 11 with MetaTypeService

use of org.osgi.service.metatype.MetaTypeService in project felix by apache.

the class MetatypeSupport method check.

public boolean check(final Object obj, final Bundle providingBundle, final String pid) {
    final MetaTypeService mts = (MetaTypeService) obj;
    final MetaTypeInformation mti = mts.getMetaTypeInformation(providingBundle);
    if (mti != null) {
        try {
            return mti.getObjectClassDefinition(pid, null) != null;
        } catch (final IllegalArgumentException e) {
            return false;
        }
    }
    return false;
}
Also used : MetaTypeService(org.osgi.service.metatype.MetaTypeService) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation)

Example 12 with MetaTypeService

use of org.osgi.service.metatype.MetaTypeService in project felix by apache.

the class Activator method start.

/**
 * Starts this bundle doing the following:
 * <ol>
 * <li>Register as listener for service events concerning the
 *      <code>LogService</code>
 * <li>Try to get the <code>LogService</code>
 * <li>Registers the <code>MetaTypeService</code> implementation provided
 *      by this bundle.
 * </ol>
 *
 * @param context The <code>BundleContext</code> of this activator's bundle
 */
public void start(BundleContext context) {
    // register for log service events
    logService = new ServiceTracker(context, NAME_LOG_SERVICE, null);
    logService.open();
    // register the MetaTypeService now, that we are ready
    Dictionary props = new Hashtable();
    props.put(Constants.SERVICE_PID, "org.apache.felix.metatype.MetaTypeService");
    props.put(Constants.SERVICE_DESCRIPTION, "MetaTypeService Specification 1.2 Implementation");
    props.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
    MetaTypeService metaTypeService = new MetaTypeServiceImpl(context);
    context.registerService(MetaTypeService.class.getName(), metaTypeService, props);
}
Also used : Dictionary(java.util.Dictionary) MetaTypeService(org.osgi.service.metatype.MetaTypeService) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Hashtable(java.util.Hashtable)

Example 13 with MetaTypeService

use of org.osgi.service.metatype.MetaTypeService in project felix by apache.

the class MetaTypeServiceImplTest method testAfterCretionManagedService.

public void testAfterCretionManagedService() {
    MetaTypeService mts = new MetaTypeServiceImpl(bundleContext);
    MetaTypeInformation mti = mts.getMetaTypeInformation(bundleContext.getBundle());
    // assert still empty
    checkEmpty(mti);
    // register a service with PID
    String pid = "testAfterCreation";
    MockManagedService service = new MockManagedService();
    Dictionary props = new Hashtable();
    props.put(Constants.SERVICE_PID, pid);
    ServiceRegistration sr = bundleContext.registerService(ManagedService.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 contain pid
    assertNotNull(mti.getPids());
    assertTrue(mti.getPids().length == 1);
    assertEquals(pid, mti.getPids()[0]);
    // change the service PID
    String pid2 = "testAfterCreation2";
    Dictionary props2 = new Hashtable();
    props2.put(Constants.SERVICE_PID, pid2);
    sr.setProperties(props2);
    // pids must contain pid2
    assertNotNull(mti.getPids());
    assertTrue(mti.getPids().length == 1);
    assertEquals(pid2, mti.getPids()[0]);
    // factoryPids must be empty
    assertTrue(mti.getFactoryPids() == null || mti.getFactoryPids().length == 0);
    // unregister the service
    sr.unregister();
    // ensure everything is clear now again
    checkEmpty(mti);
}
Also used : ManagedService(org.osgi.service.cm.ManagedService) 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)

Example 14 with MetaTypeService

use of org.osgi.service.metatype.MetaTypeService in project felix by apache.

the class MetaTypeServiceImplTest method testEmpty.

public void testEmpty() {
    MetaTypeService mts = new MetaTypeServiceImpl(bundleContext);
    MetaTypeInformation mti = mts.getMetaTypeInformation(bundleContext.getBundle());
    checkEmpty(mti);
}
Also used : MetaTypeService(org.osgi.service.metatype.MetaTypeService) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation)

Example 15 with MetaTypeService

use of org.osgi.service.metatype.MetaTypeService in project karaf by apache.

the class MetaServiceCaller method doWithMetaType.

/**
 * Attempts to find MetaType information for the specified PID and
 * invokes the supplied callback with the result of the search
 */
public static <T> T doWithMetaType(BundleContext context, String pid, Function<Optional<MetaInfo>, T> function) {
    ServiceReference<MetaTypeService> ref = context.getServiceReference(MetaTypeService.class);
    if (ref != null) {
        try {
            MetaTypeService metaService = context.getService(ref);
            MetaInfo metaInfo = getMetatype(context, metaService, pid);
            return function.apply(Optional.ofNullable(metaInfo));
        } finally {
            context.ungetService(ref);
        }
    }
    return null;
}
Also used : MetaTypeService(org.osgi.service.metatype.MetaTypeService)

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