Search in sources :

Example 6 with MetaTypeService

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

the class ApplicationServiceBeanTest method testGetServicesMetatypeInfo.

/**
 * Tests the {@link ApplicationServiceBean#getServices(String)} method for the case where the
 * services do not have the "configurations" key and there is MetatypeInformation present for each
 * service.
 *
 * <p>This test mostly just checks that
 *
 * @throws Exception
 */
@Test
public void testGetServicesMetatypeInfo() throws Exception {
    ApplicationServiceBean serviceBean = newApplicationServiceBean();
    ServiceTracker testServiceTracker = mock(ServiceTracker.class);
    serviceBean.setServiceTracker(testServiceTracker);
    MetaTypeService testMTS = mock(MetaTypeService.class);
    MetaTypeInformation testMTI = mock(MetaTypeInformation.class);
    when(testServiceTracker.getService()).thenReturn(testMTS);
    when(testMTS.getMetaTypeInformation(any(Bundle.class))).thenReturn(testMTI);
    when(testMTI.getPids()).thenReturn(new String[] { "001", "002" });
    when(testMTI.getFactoryPids()).thenReturn(new String[] { "001", "002" });
    Bundle testBundle = mock(Bundle.class);
    Bundle[] bundles = { testBundle };
    when(bundleContext.getBundles()).thenReturn(bundles);
    when(testBundle.getLocation()).thenReturn(TEST_LOCATION);
    List<Service> services = new ArrayList<>();
    Service testService2 = mock(Service.class);
    Service testService1 = mock(Service.class);
    services.add(testService1);
    services.add(testService2);
    List<Map<String, Object>> testService1Configs = new ArrayList<>();
    Map<String, Object> testConfig1 = new HashMap<>();
    testConfig1.put("bundle_location", TEST_LOCATION);
    testService1Configs.add(testConfig1);
    List<Map<String, Object>> testService2Configs = new ArrayList<>();
    Map<String, Object> testConfig2 = new HashMap<>();
    testConfig2.put("bundle_location", TEST_LOCATION);
    testService1Configs.add(testConfig2);
    when(testService1.get("factory")).thenReturn(true);
    when(testService2.get("factory")).thenReturn(false);
    when(testService1.get("configurations")).thenReturn(testService1Configs);
    when(testService2.get("configurations")).thenReturn(testService2Configs);
    when(testService1.get("id")).thenReturn("001");
    when(testService2.get("id")).thenReturn("002");
    BundleInfo testBundle1 = mock(BundleInfo.class);
    Set<BundleInfo> testBundles = new HashSet<>();
    testBundles.add(testBundle1);
    when(testApp.getBundles()).thenReturn(testBundles);
    when(testBundle1.getLocation()).thenReturn(TEST_LOCATION);
    when(testAppService.getApplication(TEST_APP_NAME)).thenReturn(testApp);
    when(testConfigAdminExt.listServices(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(services);
    assertThat("Should find the given services.", serviceBean.getServices(TEST_APP_NAME).get(0), is(testService1));
}
Also used : MetaTypeService(org.osgi.service.metatype.MetaTypeService) ServiceTracker(org.osgi.util.tracker.ServiceTracker) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) 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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BundleInfo(org.apache.karaf.features.BundleInfo) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with MetaTypeService

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

the class ConfigurationAdminImpl 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<String, ObjectClassDefinition> getObjectClassDefinitions(final IdGetter idGetter, Map<Long, MetaTypeInformation> metaTypeInformationByBundle) {
    final Map<String, ObjectClassDefinition> objectClassesDefinitions = new HashMap<>();
    final MetaTypeService mts = this.getMetaTypeService();
    if (mts == null) {
        return objectClassesDefinitions;
    }
    final Bundle[] bundles = this.getBundleContext().getBundles();
    for (Bundle bundle : bundles) {
        final MetaTypeInformation mti = metaTypeInformationByBundle.computeIfAbsent(bundle.getBundleId(), id -> mts.getMetaTypeInformation(bundle));
        objectClassesDefinitions.putAll(findOcdById(idGetter, mti));
    }
    return objectClassesDefinitions;
}
Also used : MetaTypeService(org.osgi.service.metatype.MetaTypeService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 8 with MetaTypeService

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

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

the class SourceConfigurationHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    parser = new XmlParser();
    adminService = mock(FederationAdminService.class);
    configAdmin = mock(ConfigurationAdmin.class);
    metaTypeService = mock(MetaTypeService.class);
    bundleContext = mock(BundleContext.class);
    executorService = mock(ExecutorService.class);
    sch = new SourceConfigurationHandler(adminService, executorService) {

        @Override
        protected BundleContext getBundleContext() {
            return bundleContext;
        }
    };
    sch.setMetacardMarshaller(new MetacardMarshaller(parser));
    sch.setConfigurationAdmin(configAdmin);
    sch.setMetaTypeService(metaTypeService);
    sch.setSlotHelper(new SlotTypeHelper());
    sch.setRegistryTypeHelper(new RegistryPackageTypeHelper());
    sch.setActivateConfigurations(false);
    sch.setPreserveActiveConfigurations(true);
    sch.setUrlBindingName("urlBindingName");
    sch.setBindingTypeFactoryPid(Collections.singletonList("CSW_2.0.2=Csw_Federated_Source"));
    sch.setSourceActivationPriorityOrder(Collections.singletonList("CSW_2.0.2"));
    sch.setCleanUpOnDelete(false);
    Bundle bundle = mock(Bundle.class);
    mti = mock(MetaTypeInformation.class);
    ocd = mock(ObjectClassDefinition.class);
    config = mock(Configuration.class);
    AttributeDefinition adi = new AttributeDefinitionImpl("attId", "attName", "attDesc", "attValue");
    when(adminService.getRegistryMetacards()).thenReturn(new ArrayList());
    when(bundleContext.getBundles()).thenReturn(new Bundle[] { bundle });
    when(configAdmin.listConfigurations("(id=TestRegNode")).thenReturn(null);
    when(configAdmin.listConfigurations("(registry-id=urn:uuid:2014ca7f59ac46f495e32b4a67a51276")).thenReturn(null);
    when(metaTypeService.getMetaTypeInformation(any(Bundle.class))).thenReturn(mti);
    when(mti.getObjectClassDefinition(anyString(), anyString())).thenReturn(ocd);
    when(ocd.getAttributeDefinitions(anyInt())).thenReturn(new AttributeDefinition[] { adi });
    when(configAdmin.createFactoryConfiguration(anyString(), anyString())).thenReturn(config);
    mcard = new MetacardImpl(new RegistryObjectMetacardType());
    mcard.setTags(Collections.singleton(RegistryConstants.REGISTRY_TAG));
    mcard.setId("2014ca7f59ac46f495e32b4a67a51276");
    mcard.setAttribute(RegistryObjectMetacardType.REGISTRY_ID, "urn:uuid:2014ca7f59ac46f495e32b4a67a51276");
    mcard.setMetadata(getMetadata("/csw-rim-node-csw-binding.xml"));
    mcard.setTitle("TestRegNode");
    Dictionary<String, Object> eventProperties = new Hashtable<>();
    eventProperties.put("ddf.catalog.event.metacard", mcard);
    createEvent = new Event("ddf/catalog/event/CREATED", eventProperties);
    updateEvent = new Event("ddf/catalog/event/UPDATED", eventProperties);
    deleteEvent = new Event("ddf/catalog/event/DELETED", eventProperties);
    System.setProperty(RegistryConstants.REGISTRY_ID_PROPERTY, "myRegId");
}
Also used : MetaTypeService(org.osgi.service.metatype.MetaTypeService) Configuration(org.osgi.service.cm.Configuration) SlotTypeHelper(org.codice.ddf.registry.schemabindings.helper.SlotTypeHelper) ArrayList(java.util.ArrayList) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) Matchers.anyString(org.mockito.Matchers.anyString) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition) XmlParser(org.codice.ddf.parser.xml.XmlParser) MetacardMarshaller(org.codice.ddf.registry.schemabindings.helper.MetacardMarshaller) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) FederationAdminService(org.codice.ddf.registry.federationadmin.service.internal.FederationAdminService) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) RegistryObjectMetacardType(org.codice.ddf.registry.common.metacard.RegistryObjectMetacardType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ExecutorService(java.util.concurrent.ExecutorService) Event(org.osgi.service.event.Event) RegistryPackageTypeHelper(org.codice.ddf.registry.schemabindings.helper.RegistryPackageTypeHelper) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Example 10 with MetaTypeService

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

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