Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import org.apache.karaf.config.command.completers.MetaCompleter;
Expand Down Expand Up @@ -93,7 +92,7 @@ public Object doExecute() throws Exception {
}
}

abstract class AbstractMeta implements Function<Optional<MetaInfo>, Void> {
abstract class AbstractMeta implements Function<MetaInfo, Void> {
protected String getDefaultValueStr(String[] defaultValues) {
if (defaultValues == null) {
return "";
Expand All @@ -115,14 +114,14 @@ protected String getDefaultValueStr(String[] defaultValues) {

class Create extends AbstractMeta {

public Void apply(Optional<MetaInfo> info) {
if (!info.isPresent()) {
public Void apply(MetaInfo info) {
if (info == null) {
System.out.println("No meta type definition found for pid: " + pid);
return null;
}

try {
createDefaultConfig(pid, info.get());
createDefaultConfig(pid, info);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
Expand Down Expand Up @@ -155,12 +154,12 @@ private void createDefaultConfig(String pid, MetaInfo info) throws IOException {
}

class Print extends AbstractMeta {
public Void apply(Optional<MetaInfo> info) {
if (!info.isPresent()) {
public Void apply(MetaInfo info) {
if (info == null) {
System.out.println("No meta type definition found for pid: " + pid);
return null;
}
if(info.get().isFactory()) {
if (info.isFactory()) {
System.out.println("Meta type informations for factory pid: " + pid);
}
else {
Expand All @@ -172,7 +171,7 @@ public Void apply(Optional<MetaInfo> info) {
table.column("type");
table.column("default");
table.column("description").wrap();
AttributeDefinition[] attrs = info.get().getDefinition().getAttributeDefinitions(ObjectClassDefinition.ALL);
AttributeDefinition[] attrs = info.getDefinition().getAttributeDefinitions(ObjectClassDefinition.ALL);
if (attrs != null) {
for (AttributeDefinition attr : attrs) {
table.addRow().addContent(attr.getID(), attr.getName(), getType(attr.getType()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.apache.karaf.config.command.completers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Dictionary;
Expand All @@ -46,6 +45,8 @@
import org.osgi.service.metatype.AttributeDefinition;
import org.osgi.service.metatype.ObjectClassDefinition;

import static java.util.stream.Collectors.toList;

/**
* {@link Completer} for Configuration Admin properties.
*
Expand Down Expand Up @@ -157,16 +158,14 @@ private List<String> getMetaTypeProperties(String pid){

}

private List<String> collectMetaConfigProperties(Optional<MetaInfo> info){
Optional<AttributeDefinition[]> attrs = info.map(e -> e.getDefinition().getAttributeDefinitions(ObjectClassDefinition.ALL));
if(attrs.isPresent()) {
List<String> properties = new ArrayList<>(attrs.get().length);
for (AttributeDefinition attr : attrs.get()) {
properties.add(attr.getID());
}
return properties;
}
return Collections.emptyList();
private List<String> collectMetaConfigProperties(MetaInfo info){
return Optional.ofNullable(info)
.map(MetaInfo::getDefinition)
.map(ocd -> ocd.getAttributeDefinitions(ObjectClassDefinition.ALL))
.stream()
.flatMap(Arrays::stream)
.map(AttributeDefinition::getID)
.collect(toList());
}

public ConfigurationAdmin getConfigAdmin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -73,13 +72,15 @@ public static List<String> getPidsWithMetaInfo(BundleContext context) {
* Attempts to find MetaType information for the specified PID and
* invokes the supplied callback with the result of the search
*/
public static <T> T doWithMetaType(BundleContext context, String pid, Function<Optional<MetaInfo>, T> function) {
public static <T> T doWithMetaType(BundleContext context, String pid, Function<MetaInfo, T> function) {
ServiceReference<MetaTypeService> ref = context.getServiceReference(MetaTypeService.class);
if (ref != null) {
try {
MetaTypeService metaService = context.getService(ref);
MetaInfo metaInfo = getMetatype(context, metaService, pid);
return function.apply(Optional.ofNullable(metaInfo));
if (metaInfo != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems to have an effect on semantics for the null case: before, function.apply of an empty Optional was returned, now the fall-through may just return null.
This could lead to an NPE in the statement from the other file above (return MetaServiceCaller.doWithMetaType(...)).

return function.apply(metaInfo);
}
} finally {
context.ungetService(ref);
}
Expand Down