Search in sources :

Example 1 with MetaTypeInformation

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

the class MetaCompleter method updateMeta.

private synchronized void updateMeta() {
    List<String> pids = MetaServiceCaller.withMetaTypeService(context, metatypeService -> {
        List<String> pids1 = new ArrayList<>();
        Bundle[] bundles = context.getBundles();
        for (Bundle bundle : bundles) {
            MetaTypeInformation info = metatypeService.getMetaTypeInformation(bundle);
            if (info == null) {
                continue;
            }
            if (info.getFactoryPids() != null) {
                pids1.addAll(Arrays.asList(info.getFactoryPids()));
            }
            if (info.getPids() != null) {
                pids1.addAll(Arrays.asList(info.getPids()));
            }
        }
        return pids1;
    });
    if (pids != null) {
        delegate.getStrings().clear();
        delegate.getStrings().addAll(pids);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation)

Example 2 with MetaTypeInformation

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

the class StoreResourceTask method getMetaTypeOCD.

/**
 * Determines the object class definition matching the specified designate.
 *
 * @param data The meta data containing 'local' object class definitions.
 * @param designate The designate whose object class definition should be determined.
 * @return
 * @throws ResourceProcessorException
 */
private ObjectClassDefinition getMetaTypeOCD(MetaData data, Designate designate) throws ResourceProcessorException {
    boolean isFactoryConfig = isFactoryConfig(designate);
    Bundle bundle = getBundle(designate.getBundleLocation(), isFactoryConfig);
    if (bundle == null) {
        return null;
    }
    MetaTypeInformation mti = m_metaService.getMetaTypeInformation(bundle);
    if (mti == null) {
        return null;
    }
    String pid = isFactoryConfig ? pid = designate.getFactoryPid() : designate.getPid();
    try {
        ObjectClassDefinition tempOcd = mti.getObjectClassDefinition(pid, null);
        // tempOcd will always have a value, if pid was not known IAE will be thrown
        String ocdRef = designate.getObject().getOcdRef();
        if (ocdRef.equals(tempOcd.getID())) {
            return tempOcd;
        }
    } catch (IllegalArgumentException iae) {
    // let null be returned
    }
    return null;
}
Also used : Bundle(org.osgi.framework.Bundle) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 3 with MetaTypeInformation

use of org.osgi.service.metatype.MetaTypeInformation 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 4 with MetaTypeInformation

use of org.osgi.service.metatype.MetaTypeInformation 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 5 with MetaTypeInformation

use of org.osgi.service.metatype.MetaTypeInformation in project ddf by codice.

the class ApplicationServiceBean method checkForMetaTypesForService.

/**
     * Checks to see if there are any metatypes out there for a particular service.
     *
     * @param metatypeInformations - Where we'll look for metatypes that match our service from.
     * @param service              - our service we want metatypes for.
     * @return true if there is, and the service should be added, or false if it shouldn't be.
     */
private boolean checkForMetaTypesForService(Set<MetaTypeInformation> metatypeInformations, Map<String, Object> service) {
    String id = (String) service.get("id");
    boolean ifFactory = (Boolean) service.get("factory");
    if (ifFactory) {
        for (MetaTypeInformation information : metatypeInformations) {
            if (information != null) {
                for (String pid : information.getFactoryPids()) {
                    if (pid.equals(id)) {
                        return true;
                    }
                }
            }
        }
    } else {
        for (MetaTypeInformation information : metatypeInformations) {
            if (information != null) {
                for (String pid : information.getPids()) {
                    if (pid.equals(id)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation)

Aggregations

MetaTypeInformation (org.osgi.service.metatype.MetaTypeInformation)20 Bundle (org.osgi.framework.Bundle)13 MetaTypeService (org.osgi.service.metatype.MetaTypeService)12 ObjectClassDefinition (org.osgi.service.metatype.ObjectClassDefinition)9 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 Dictionary (java.util.Dictionary)5 Hashtable (java.util.Hashtable)5 Map (java.util.Map)5 BundleContext (org.osgi.framework.BundleContext)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Service (org.codice.ddf.admin.core.api.Service)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Locale (java.util.Locale)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 BundleInfo (org.apache.karaf.features.BundleInfo)2 SystemService (org.apache.karaf.system.SystemService)2