Search in sources :

Example 1 with AttributeDefinition

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

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

the class SourceConfigurationHandler method getMetatypeDefaults.

private Map<String, Object> getMetatypeDefaults(String factoryPid) {
    Map<String, Object> properties = new HashMap<>();
    ObjectClassDefinition bundleMetatype = getObjectClassDefinition(factoryPid);
    if (bundleMetatype != null) {
        for (AttributeDefinition attributeDef : bundleMetatype.getAttributeDefinitions(ObjectClassDefinition.ALL)) {
            if (attributeDef.getID() != null) {
                if (attributeDef.getDefaultValue() != null) {
                    if (attributeDef.getCardinality() == 0) {
                        properties.put(attributeDef.getID(), getAttributeValue(attributeDef.getDefaultValue()[0], attributeDef.getType()));
                    } else {
                        properties.put(attributeDef.getID(), attributeDef.getDefaultValue());
                    }
                } else if (attributeDef.getCardinality() != 0) {
                    properties.put(attributeDef.getID(), new String[0]);
                }
            }
        }
    }
    return properties;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 3 with AttributeDefinition

use of org.osgi.service.metatype.AttributeDefinition in project bndtools by bndtools.

the class TemplateParamsWizardPage method updateUI.

void updateUI() {
    if (currentPanel != null && !currentPanel.isDisposed()) {
        currentPanel.dispose();
        currentPanel = null;
    }
    Composite panel = new Composite(container, SWT.NONE);
    panel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    skip = true;
    if (template == null) {
        setErrorMessage(null);
        setMessage("No template is loaded", WARNING);
    } else {
        GridLayout layout = new GridLayout(2, false);
        layout.horizontalSpacing = 15;
        panel.setLayout(layout);
        List<Control> fieldControls = new LinkedList<>();
        try {
            ocd = template.getMetadata();
            int count = 0;
            Set<String> requiredIds = new HashSet<>();
            for (AttributeDefinition ad : ocd.getAttributeDefinitions(ObjectClassDefinition.REQUIRED)) {
                requiredIds.add(ad.getID());
            }
            AttributeDefinition[] ads = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);
            for (AttributeDefinition ad : ads) {
                String attrib = ad.getID();
                if (!fixedAttribs.contains(attrib)) {
                    Label label = new Label(panel, SWT.NONE);
                    String labelText = ad.getID();
                    if (requiredIds.contains(ad.getID())) {
                        label.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
                    }
                    label.setText(labelText);
                    Control fieldControl = createFieldControl(panel, ad);
                    fieldControl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
                    fieldControls.add(fieldControl);
                    skip = false;
                    count++;
                }
            }
            if (count == 0) {
                setMessage("No editable parameters for template: " + ocd.getName(), INFORMATION);
            } else {
                setMessage("Edit parameters for template: " + ocd.getDescription(), INFORMATION);
            }
            setErrorMessage(null);
        } catch (Exception e) {
            setErrorMessage("Error loading template metadata: " + e.getMessage());
            for (Control fieldControl : fieldControls) {
                fieldControl.setEnabled(false);
            }
        }
    }
    currentPanel = panel;
    container.layout(true, true);
    updateValidation();
}
Also used : Composite(org.eclipse.swt.widgets.Composite) Label(org.eclipse.swt.widgets.Label) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) LinkedList(java.util.LinkedList) GridLayout(org.eclipse.swt.layout.GridLayout) Control(org.eclipse.swt.widgets.Control) GridData(org.eclipse.swt.layout.GridData) HashSet(java.util.HashSet)

Example 4 with AttributeDefinition

use of org.osgi.service.metatype.AttributeDefinition in project bndtools by bndtools.

the class TemplateParamsWizardPage method updateValidation.

private void updateValidation() {
    boolean complete = true;
    // Check required attribs
    AttributeDefinition[] ads = ocd != null ? ocd.getAttributeDefinitions(ObjectClassDefinition.REQUIRED) : new AttributeDefinition[0];
    for (AttributeDefinition ad : ads) {
        // Skip attribs provided the wizard
        if (fixedAttribs.contains(ad.getID()))
            continue;
        String value = values.get(ad.getID());
        if (value == null || value.trim().isEmpty()) {
            complete = false;
            break;
        }
    }
    // Validate values
    // TODO
    setPageComplete(complete);
}
Also used : AttributeDefinition(org.osgi.service.metatype.AttributeDefinition)

Example 5 with AttributeDefinition

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

the class ServiceManagerImpl method getMetatypeDefaults.

@Override
public Map<String, Object> getMetatypeDefaults(String symbolicName, String factoryPid) {
    Map<String, Object> properties = new HashMap<>();
    ObjectClassDefinition bundleMetatype = getObjectClassDefinition(symbolicName, factoryPid);
    if (bundleMetatype != null) {
        for (AttributeDefinition attributeDef : bundleMetatype.getAttributeDefinitions(ObjectClassDefinition.ALL)) {
            if (attributeDef.getID() != null) {
                if (attributeDef.getDefaultValue() != null) {
                    if (attributeDef.getCardinality() == 0) {
                        properties.put(attributeDef.getID(), getAttributeValue(attributeDef.getDefaultValue()[0], attributeDef.getType()));
                    } else {
                        properties.put(attributeDef.getID(), attributeDef.getDefaultValue());
                    }
                } else if (attributeDef.getCardinality() != 0) {
                    properties.put(attributeDef.getID(), new String[0]);
                }
            }
        }
    } else {
        LOGGER.debug("Metatype was null, returning an empty properties Map");
    }
    return properties;
}
Also used : HashMap(java.util.HashMap) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Aggregations

AttributeDefinition (org.osgi.service.metatype.AttributeDefinition)8 ObjectClassDefinition (org.osgi.service.metatype.ObjectClassDefinition)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Hashtable (java.util.Hashtable)3 Dictionary (java.util.Dictionary)2 Map (java.util.Map)2 Bundle (org.osgi.framework.Bundle)2 BundleContext (org.osgi.framework.BundleContext)2 MetaTypeInformation (org.osgi.service.metatype.MetaTypeInformation)2 MetaTypeService (org.osgi.service.metatype.MetaTypeService)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 XmlParser (org.codice.ddf.parser.xml.XmlParser)1 RegistryObjectMetacardType (org.codice.ddf.registry.common.metacard.RegistryObjectMetacardType)1