Search in sources :

Example 11 with MetaTypeInformation

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

the class AdminConsoleServiceTest method getConfigAdmin.

private AdminConsoleService getConfigAdmin() throws IOException, InvalidSyntaxException, NotCompliantMBeanException {
    final BundleContext testBundleContext = mock(BundleContext.class);
    final MetaTypeService testMTS = mock(MetaTypeService.class);
    ConfigurationAdminImpl configurationAdminImpl = new ConfigurationAdminImpl(CONFIGURATION_ADMIN, new ArrayList<>()) {

        @Override
        BundleContext getBundleContext() {
            return testBundleContext;
        }

        @Override
        MetaTypeService getMetaTypeService() {
            return testMTS;
        }

        @Override
        public boolean isPermittedToViewService(String servicePid) {
            return true;
        }

        @Override
        public boolean isPermittedToViewService(String servicePid, Subject subject) {
            return true;
        }
    };
    AdminConsoleService configurationAdmin = new AdminConsoleService(CONFIGURATION_ADMIN, configurationAdminImpl) {

        @Override
        public boolean isPermittedToViewService(String servicePid) {
            return true;
        }
    };
    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 (AdminConsoleService.TYPE type : AdminConsoleService.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.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(), any())).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) Subject(org.apache.shiro.subject.Subject) ServiceReference(org.osgi.framework.ServiceReference) BundleContext(org.osgi.framework.BundleContext) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 12 with MetaTypeInformation

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

the class MetaServiceCaller method getPidsWithMetaInfo.

public static List<String> getPidsWithMetaInfo(BundleContext context) {
    return withMetaTypeService(context, metatypeService -> {
        List<String> pids1 = new ArrayList<>();
        Bundle[] bundles = context.getBundles();
        if (metatypeService != null) {
            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;
    });
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation)

Example 13 with MetaTypeInformation

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

the class MetaServiceCaller method getMetatype.

private static MetaInfo getMetatype(BundleContext context, MetaTypeService metaTypeService, String pid) {
    if (metaTypeService != null) {
        for (Bundle bundle : context.getBundles()) {
            MetaTypeInformation info = metaTypeService.getMetaTypeInformation(bundle);
            if (info == null) {
                continue;
            }
            String[] pids = info.getPids();
            for (String cPid : pids) {
                if (cPid.equals(pid)) {
                    return new MetaInfo(info.getObjectClassDefinition(cPid, null), false);
                }
            }
            pids = info.getFactoryPids();
            for (String cPid : pids) {
                if (cPid.equals(pid)) {
                    return new MetaInfo(info.getObjectClassDefinition(cPid, null), true);
                }
            }
        }
    }
    return null;
}
Also used : Bundle(org.osgi.framework.Bundle) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation)

Example 14 with MetaTypeInformation

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

the class ConfigurationAdminExt 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.
     * @return Map of <code>ObjectClassDefinition</code> objects indexed by the PID (or factory PID)
     * to which they pertain
     */
private Map getObjectClassDefinitions(final IdGetter idGetter) {
    Locale locale = Locale.getDefault();
    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.toString());
                    } catch (IllegalArgumentException ignore) {
                    // ignore - just don't show this configuration
                    }
                    if (ocd != null) {
                        objectClassesDefinitions.put(idList[j], ocd);
                    }
                }
            }
        }
    }
    return objectClassesDefinitions;
}
Also used : Locale(java.util.Locale) 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 15 with MetaTypeInformation

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

Aggregations

MetaTypeInformation (org.osgi.service.metatype.MetaTypeInformation)21 Bundle (org.osgi.framework.Bundle)14 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