You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2017/10/20 11:39:52 UTC
karaf git commit: Fix meta command / completer to not print a warning
when MetaType is not available
Repository: karaf
Updated Branches:
refs/heads/master 180942b50 -> 475be9310
Fix meta command / completer to not print a warning when MetaType is not available
I think this is a regression caused by https://github.com/apache/karaf/commit/bf666631fe84f0394a9b948b5088d205bf52db36#diff-290cb1e2332aeecaf61aaa6aa3602768
which removes the anonymous inner classes and thus cause a CNFE to be thrown when the MetaCompleter getMethods() is called for introspection.
So this commit slightly change the mechanism and a warning is displayed when the completer is initialized, while a message is printed on the console when calling the command.
Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/475be931
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/475be931
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/475be931
Branch: refs/heads/master
Commit: 475be9310ebdc168b73d7b6a49763aab51698084
Parents: 180942b
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Fri Oct 20 13:39:41 2017 +0200
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Fri Oct 20 13:39:41 2017 +0200
----------------------------------------------------------------------
.../karaf/config/command/MetaCommand.java | 36 ++++++++++----
.../command/completers/MetaCompleter.java | 50 ++++++++++----------
.../config/core/impl/MetaServiceCaller.java | 46 +++++++++++++-----
.../config/core/impl/MetatypeCallable.java | 23 ---------
4 files changed, 86 insertions(+), 69 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/karaf/blob/475be931/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java
----------------------------------------------------------------------
diff --git a/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java b/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java
index c65e7e2..6b33f81 100644
--- a/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java
+++ b/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java
@@ -23,15 +23,16 @@ import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
+import java.util.function.Function;
import org.apache.karaf.config.command.completers.MetaCompleter;
-import org.apache.karaf.config.core.impl.MetatypeCallable;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Completion;
import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.support.CommandException;
import org.apache.karaf.shell.support.table.ShellTable;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -40,10 +41,15 @@ import org.osgi.service.metatype.AttributeDefinition;
import org.osgi.service.metatype.MetaTypeInformation;
import org.osgi.service.metatype.MetaTypeService;
import org.osgi.service.metatype.ObjectClassDefinition;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
@Command(scope = "config", name = "meta", description = "Lists meta type information.")
@Service
public class MetaCommand extends ConfigCommandSupport {
+
+ private static final Logger LOG = LoggerFactory.getLogger(MetaCommand.class);
+
@Argument(name = "pid", description = "The configuration pid", required = true, multiValued = false)
@Completion(MetaCompleter.class)
protected String pid;
@@ -72,15 +78,27 @@ public class MetaCommand extends ConfigCommandSupport {
@Override
public Object doExecute() throws Exception {
- if (create) {
- withMetaTypeService(context, new Create());
- } else {
- withMetaTypeService(context, new Print());
+ try {
+ if (create) {
+ withMetaTypeService(context, new Create());
+ } else {
+ withMetaTypeService(context, new Print());
+ }
+ return null;
+ } catch (Throwable e) {
+ Throwable ncdfe = e;
+ while (ncdfe != null && !(ncdfe instanceof NoClassDefFoundError)) {
+ ncdfe = ncdfe.getCause();
+ }
+ if (ncdfe != null && ncdfe.getMessage().equals("org/osgi/service/metatype/MetaTypeService")) {
+ throw new CommandException("config:meta disabled because the org.osgi.service.metatype package is not wired", e);
+ } else {
+ throw e;
+ }
}
- return null;
}
- abstract class AbstractMeta implements MetatypeCallable<Void> {
+ abstract class AbstractMeta implements Function<MetaTypeService, Void> {
protected String getDefaultValueStr(String[] defaultValues) {
if (defaultValues == null) {
return "";
@@ -117,7 +135,7 @@ public class MetaCommand extends ConfigCommandSupport {
class Create extends AbstractMeta {
- public Void callWith(MetaTypeService metaTypeService) {
+ public Void apply(MetaTypeService metaTypeService) {
ObjectClassDefinition def = getMetatype(metaTypeService, pid);
if (def == null) {
System.out.println("No meta type definition found for pid: " + pid);
@@ -151,7 +169,7 @@ public class MetaCommand extends ConfigCommandSupport {
}
class Print extends AbstractMeta {
- public Void callWith(MetaTypeService metaTypeService) {
+ public Void apply(MetaTypeService metaTypeService) {
ObjectClassDefinition def = getMetatype(metaTypeService, pid);
if (def == null) {
System.out.println("No meta type definition found for pid: " + pid);
http://git-wip-us.apache.org/repos/asf/karaf/blob/475be931/config/src/main/java/org/apache/karaf/config/command/completers/MetaCompleter.java
----------------------------------------------------------------------
diff --git a/config/src/main/java/org/apache/karaf/config/command/completers/MetaCompleter.java b/config/src/main/java/org/apache/karaf/config/command/completers/MetaCompleter.java
index 2aa39b8..c35ccde 100644
--- a/config/src/main/java/org/apache/karaf/config/command/completers/MetaCompleter.java
+++ b/config/src/main/java/org/apache/karaf/config/command/completers/MetaCompleter.java
@@ -16,8 +16,6 @@
*/
package org.apache.karaf.config.command.completers;
-import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import org.apache.karaf.config.core.impl.MetaServiceCaller;
@@ -28,23 +26,42 @@ import org.apache.karaf.shell.api.console.CommandLine;
import org.apache.karaf.shell.api.console.Completer;
import org.apache.karaf.shell.api.console.Session;
import org.apache.karaf.shell.support.completers.StringsCompleter;
-import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;
-import org.osgi.service.metatype.MetaTypeInformation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
@Service
public class MetaCompleter implements Completer, BundleListener {
+
+ private static final Logger LOG = LoggerFactory.getLogger(MetaCompleter.class);
+
private final StringsCompleter delegate = new StringsCompleter();
-
+
@Reference
BundleContext context;
@Init
public void init() {
- context.registerService(BundleListener.class, this, null);
- updateMeta();
+ try {
+ updateMeta();
+ context.registerService(BundleListener.class, this, null);
+ } catch (Throwable e) {
+ Throwable ncdfe = e;
+ while (ncdfe != null && !(ncdfe instanceof NoClassDefFoundError)) {
+ ncdfe = ncdfe.getCause();
+ }
+ if (ncdfe != null && ncdfe.getMessage().equals("org/osgi/service/metatype/MetaTypeService")) {
+ if (LOG.isDebugEnabled()) {
+ LOG.warn("config:meta disabled because the org.osgi.service.metatype package is not wired", e);
+ } else {
+ LOG.warn("config:meta disabled because the org.osgi.service.metatype package is not wired (enable debug logging for full stack trace).");
+ }
+ } else {
+ throw e;
+ }
+ }
}
@Override
@@ -58,24 +75,7 @@ public class MetaCompleter implements Completer, BundleListener {
}
private synchronized void updateMeta() {
- List<String> pids = MetaServiceCaller.withMetaTypeService(context, metatypeService -> {
- List<String> pids1 = new ArrayList<>();
- Bundle[] bundles = context.getBundles();
- for (Bundle bundle : bundles) {
-
- MetaTypeInformation info = metatypeService.getMetaTypeInformation(bundle);
- if (info == null) {
- continue;
- }
- if (info.getFactoryPids() != null) {
- pids1.addAll(Arrays.asList(info.getFactoryPids()));
- }
- if (info.getPids() != null) {
- pids1.addAll(Arrays.asList(info.getPids()));
- }
- }
- return pids1;
- });
+ List<String> pids = MetaServiceCaller.getPidsWithMetaInfo(context);
if (pids != null) {
delegate.getStrings().clear();
delegate.getStrings().addAll(pids);
http://git-wip-us.apache.org/repos/asf/karaf/blob/475be931/config/src/main/java/org/apache/karaf/config/core/impl/MetaServiceCaller.java
----------------------------------------------------------------------
diff --git a/config/src/main/java/org/apache/karaf/config/core/impl/MetaServiceCaller.java b/config/src/main/java/org/apache/karaf/config/core/impl/MetaServiceCaller.java
index 88bc1f3..5401df0 100644
--- a/config/src/main/java/org/apache/karaf/config/core/impl/MetaServiceCaller.java
+++ b/config/src/main/java/org/apache/karaf/config/core/impl/MetaServiceCaller.java
@@ -16,30 +16,52 @@
*/
package org.apache.karaf.config.core.impl;
+import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
+import org.osgi.service.metatype.MetaTypeInformation;
import org.osgi.service.metatype.MetaTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Function;
+
/**
* Allows to use the MetaTypeService as an optional dependency
*/
public class MetaServiceCaller {
- private static Logger LOG = LoggerFactory.getLogger(MetaServiceCaller.class);
- public static <T> T withMetaTypeService(BundleContext context, MetatypeCallable<T> callable) {
+ public static <T> T withMetaTypeService(BundleContext context, Function<MetaTypeService, T> callable) {
+ ServiceReference<MetaTypeService> ref = context.getServiceReference(MetaTypeService.class);
try {
- ServiceReference<MetaTypeService> ref = context.getServiceReference(MetaTypeService.class);
- try {
- MetaTypeService metaService = context.getService(ref);
- return callable.callWith(metaService);
- } finally {
- context.ungetService(ref);
- }
- } catch (NoClassDefFoundError e) {
- LOG.warn("No Metatype Service present");
- return null;
+ MetaTypeService metaService = context.getService(ref);
+ return callable.apply(metaService);
+ } finally {
+ context.ungetService(ref);
}
}
+
+ public static List<String> getPidsWithMetaInfo(BundleContext context) {
+ return withMetaTypeService(context, metatypeService -> {
+ List<String> pids1 = new ArrayList<>();
+ Bundle[] bundles = context.getBundles();
+ for (Bundle bundle : bundles) {
+
+ MetaTypeInformation info = metatypeService.getMetaTypeInformation(bundle);
+ if (info == null) {
+ continue;
+ }
+ if (info.getFactoryPids() != null) {
+ pids1.addAll(Arrays.asList(info.getFactoryPids()));
+ }
+ if (info.getPids() != null) {
+ pids1.addAll(Arrays.asList(info.getPids()));
+ }
+ }
+ return pids1;
+ });
+ }
}
http://git-wip-us.apache.org/repos/asf/karaf/blob/475be931/config/src/main/java/org/apache/karaf/config/core/impl/MetatypeCallable.java
----------------------------------------------------------------------
diff --git a/config/src/main/java/org/apache/karaf/config/core/impl/MetatypeCallable.java b/config/src/main/java/org/apache/karaf/config/core/impl/MetatypeCallable.java
deleted file mode 100644
index 39075f9..0000000
--- a/config/src/main/java/org/apache/karaf/config/core/impl/MetatypeCallable.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.karaf.config.core.impl;
-
-import org.osgi.service.metatype.MetaTypeService;
-
-public interface MetatypeCallable <T> {
- T callWith(MetaTypeService metatypeService);
-}