Search in sources :

Example 1 with ObjectClassDefinition

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

the class NewBndProjectWizardPageOne method getSourceClasspathEntries.

@Override
public IClasspathEntry[] getSourceClasspathEntries() {
    IPath projectPath = new Path(getProjectName()).makeAbsolute();
    ProjectPaths projectPaths = ProjectPaths.DEFAULT;
    List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(2);
    newEntries.add(JavaCore.newSourceEntry(projectPath.append(projectPaths.getSrc()), null, projectPath.append(projectPaths.getBin())));
    boolean enableTestSrcDir;
    try {
        if (template == null)
            enableTestSrcDir = true;
        else {
            ObjectClassDefinition templateMeta = template.getMetadata();
            enableTestSrcDir = findAttribute(templateMeta, ProjectTemplateParam.TEST_SRC_DIR.getString()) != null;
        }
    } catch (Exception e) {
        Plugin.getDefault().getLog().log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error accessing template parameters", e));
        enableTestSrcDir = true;
    }
    if (enableTestSrcDir)
        newEntries.add(JavaCore.newSourceEntry(projectPath.append(projectPaths.getTestSrc()), null, projectPath.append(projectPaths.getTestBin())));
    return newEntries.toArray(new IClasspathEntry[0]);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IPath(org.eclipse.core.runtime.IPath) ProjectPaths(org.bndtools.api.ProjectPaths) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 2 with ObjectClassDefinition

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

the class ConfigurationAdminExt method addConfigurationData.

private void addConfigurationData(Map<String, Object> service, Configuration[] configs) {
    for (Configuration config : configs) {
        // ignore configuration object if it is invalid
        final String pid = config.getPid();
        if (!isAllowedPid(pid)) {
            continue;
        }
        Map<String, Object> configData = new HashMap<String, Object>();
        configData.put(MAP_ENTRY_ID, pid);
        String fpid = config.getFactoryPid();
        if (fpid != null) {
            configData.put(MAP_ENTRY_FPID, fpid);
        }
        // insert an entry for the PID
        try {
            ObjectClassDefinition ocd = getObjectClassDefinition(config);
            if (ocd != null) {
                configData.put(MAP_ENTRY_NAME, ocd.getName());
            } else {
                // no object class definition, use plain PID
                configData.put(MAP_ENTRY_NAME, pid);
            }
        } catch (IllegalArgumentException t) {
            // Catch exception thrown by getObjectClassDefinition so other configurations
            // are displayed
            // no object class definition, use plain PID
            configData.put(MAP_ENTRY_NAME, pid);
        }
        final Bundle bundle = getBoundBundle(config);
        if (null != bundle) {
            configData.put(MAP_ENTRY_BUNDLE, bundle.getBundleId());
            configData.put(MAP_ENTRY_BUNDLE_NAME, getName(bundle));
            configData.put(MAP_ENTRY_BUNDLE_LOCATION, bundle.getLocation());
        }
        Map<String, Object> propertiesTable = new HashMap<String, Object>();
        Dictionary<String, Object> properties = config.getProperties();
        if (properties != null) {
            Enumeration<String> keys = properties.keys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                propertiesTable.put(key, properties.get(key));
            }
        }
        // If the configuration property is a password, set its value to "password" so that
        // the real password value will be hidden.
        List<HashMap<String, Object>> metatypeList = (List<HashMap<String, Object>>) service.get("metatype");
        metatypeList.stream().filter(metatype -> AttributeDefinition.PASSWORD == (Integer) metatype.get("type")).forEach(metatype -> {
            String passwordProperty = (String) metatype.get("id");
            propertiesTable.put(passwordProperty, "password");
        });
        configData.put(MAP_ENTRY_PROPERTIES, propertiesTable);
        Map<String, Object> pluginDataMap = getConfigurationPluginData(configData.get(MAP_ENTRY_ID).toString(), Collections.unmodifiableMap(configData));
        if (pluginDataMap != null && !pluginDataMap.isEmpty()) {
            configData.putAll(pluginDataMap);
        }
        List<Map<String, Object>> configurations = null;
        if (service.containsKey(ENABLED_CONFIGURATION)) {
            configurations = (List<Map<String, Object>>) service.get(ENABLED_CONFIGURATION);
        } else if (service.containsKey(DISABLED_CONFIGURATION)) {
            configurations = (List<Map<String, Object>>) service.get(DISABLED_CONFIGURATION);
        } else {
            configurations = new ArrayList<Map<String, Object>>();
        }
        configurations.add(configData);
        if (((String) configData.get(MAP_ENTRY_ID)).contains(DISABLED_SERVICE_ID)) {
            configData.put(ENABLED, false);
        } else {
            configData.put(ENABLED, true);
        }
        service.put(ENABLED_CONFIGURATION, configurations);
    }
}
Also used : Constants(org.osgi.framework.Constants) Enumeration(java.util.Enumeration) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ArrayList(java.util.ArrayList) Configuration(org.osgi.service.cm.Configuration) Locale(java.util.Locale) Map(java.util.Map) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) KeyValuePermission(ddf.security.permission.KeyValuePermission) IOException(java.io.IOException) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) Collectors(java.util.stream.Collectors) ConfigurationAdminPlugin(org.codice.ddf.ui.admin.api.plugin.ConfigurationAdminPlugin) BundleContext(org.osgi.framework.BundleContext) Sets(com.google.common.collect.Sets) MetaTypeService(org.osgi.service.metatype.MetaTypeService) List(java.util.List) Filter(org.osgi.framework.Filter) Entry(java.util.Map.Entry) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ManagedService(org.osgi.service.cm.ManagedService) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Collections(java.util.Collections) SecurityUtils(org.apache.shiro.SecurityUtils) FrameworkUtil(org.osgi.framework.FrameworkUtil) Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 3 with ObjectClassDefinition

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

the class ConfigurationAdminExt method getServices.

List<Map<String, Object>> getServices(String serviceClass, String serviceFilter, boolean ocdRequired) throws InvalidSyntaxException {
    List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
    // find all ManagedServiceFactories to get the factoryPIDs
    ServiceReference[] refs = this.getBundleContext().getAllServiceReferences(serviceClass, serviceFilter);
    for (int i = 0; refs != null && i < refs.length; i++) {
        Object pidObject = refs[i].getProperty(Constants.SERVICE_PID);
        // only include valid PIDs
        if (pidObject instanceof String && isAllowedPid((String) pidObject)) {
            String pid = (String) pidObject;
            String name = pid;
            boolean haveOcd = !ocdRequired;
            final ObjectClassDefinition ocd = getObjectClassDefinition(refs[i].getBundle(), pid);
            if (ocd != null) {
                name = ocd.getName();
                haveOcd = true;
            }
            if (haveOcd) {
                Map<String, Object> service = new HashMap<String, Object>();
                service.put(MAP_ENTRY_ID, pid);
                service.put(MAP_ENTRY_NAME, name);
                serviceList.add(service);
            }
        }
    }
    return serviceList.stream().filter(service -> isPermittedToViewService((String) service.get("id"))).collect(Collectors.toList());
}
Also used : Constants(org.osgi.framework.Constants) Enumeration(java.util.Enumeration) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ArrayList(java.util.ArrayList) Configuration(org.osgi.service.cm.Configuration) Locale(java.util.Locale) Map(java.util.Map) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) KeyValuePermission(ddf.security.permission.KeyValuePermission) IOException(java.io.IOException) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition) Collectors(java.util.stream.Collectors) ConfigurationAdminPlugin(org.codice.ddf.ui.admin.api.plugin.ConfigurationAdminPlugin) BundleContext(org.osgi.framework.BundleContext) Sets(com.google.common.collect.Sets) MetaTypeService(org.osgi.service.metatype.MetaTypeService) List(java.util.List) Filter(org.osgi.framework.Filter) Entry(java.util.Map.Entry) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ManagedService(org.osgi.service.cm.ManagedService) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Collections(java.util.Collections) SecurityUtils(org.apache.shiro.SecurityUtils) FrameworkUtil(org.osgi.framework.FrameworkUtil) Dictionary(java.util.Dictionary) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) HashMap(java.util.HashMap) Map(java.util.Map) ObjectClassDefinition(org.osgi.service.metatype.ObjectClassDefinition)

Example 4 with ObjectClassDefinition

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

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

Aggregations

ObjectClassDefinition (org.osgi.service.metatype.ObjectClassDefinition)8 HashMap (java.util.HashMap)6 AttributeDefinition (org.osgi.service.metatype.AttributeDefinition)6 ArrayList (java.util.ArrayList)5 Dictionary (java.util.Dictionary)4 Hashtable (java.util.Hashtable)4 Map (java.util.Map)4 Bundle (org.osgi.framework.Bundle)4 MetaTypeInformation (org.osgi.service.metatype.MetaTypeInformation)4 MetaTypeService (org.osgi.service.metatype.MetaTypeService)4 Iterator (java.util.Iterator)3 Locale (java.util.Locale)3 Entry (java.util.Map.Entry)3 BundleContext (org.osgi.framework.BundleContext)3 Filter (org.osgi.framework.Filter)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 ServiceReference (org.osgi.framework.ServiceReference)3 Sets (com.google.common.collect.Sets)2 KeyValueCollectionPermission (ddf.security.permission.KeyValueCollectionPermission)2 KeyValuePermission (ddf.security.permission.KeyValuePermission)2