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]);
}
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);
}
}
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());
}
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;
}
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;
}
Aggregations