You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2023/11/30 18:34:22 UTC
(sling-whiteboard) branch provider-type-check-bnd-plugin updated: Cleanup and allow to ignore certain provider types
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch provider-type-check-bnd-plugin
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
The following commit(s) were added to refs/heads/provider-type-check-bnd-plugin by this push:
new ca37ee66 Cleanup and allow to ignore certain provider types
ca37ee66 is described below
commit ca37ee661a6a3e2b4c81916cbad55c2af0f3f08f
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu Nov 30 19:34:15 2023 +0100
Cleanup and allow to ignore certain provider types
---
org.apache.sling.bnd.providertype/pom.xml | 6 +++
org.apache.sling.bnd.providertype/readme.md | 8 +++-
.../bnd/providertype/ProviderTypeScanner.java | 51 ++++++++++++++++++++--
.../bnd/providertype/BndBuilderExtension.java | 2 -
.../bnd/providertype/ProviderTypeScannerTest.java | 8 ++--
.../org/apache/sling/bnd/providertype/TypeA.java | 18 ++++++++
.../apache/sling/bnd/providertype/TypeAImpl.java | 18 ++++++++
.../org/apache/sling/bnd/providertype/TypeB.java | 18 ++++++++
.../sling/bnd/providertype/TypeBExtension.java | 18 ++++++++
9 files changed, 137 insertions(+), 10 deletions(-)
diff --git a/org.apache.sling.bnd.providertype/pom.xml b/org.apache.sling.bnd.providertype/pom.xml
index 3e8a3ebe..c451eebb 100644
--- a/org.apache.sling.bnd.providertype/pom.xml
+++ b/org.apache.sling.bnd.providertype/pom.xml
@@ -35,6 +35,7 @@
<properties>
<sling.java.version>11</sling.java.version>
+ <minimalJavaBuildVersion>${sling.java.version}</minimalJavaBuildVersion>
<project.build.outputTimestamp>1675867676</project.build.outputTimestamp>
<bnd.version>6.0.0</bnd.version>
</properties>
@@ -76,5 +77,10 @@
<version>5.6.0</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git a/org.apache.sling.bnd.providertype/readme.md b/org.apache.sling.bnd.providertype/readme.md
index 54538124..87507ddf 100644
--- a/org.apache.sling.bnd.providertype/readme.md
+++ b/org.apache.sling.bnd.providertype/readme.md
@@ -28,7 +28,13 @@ For usage with Maven the Bnd plugin has to be added to the plugin dependencies o
In addition the `bnd.bnd` file needs to register the Bnd plugin with the [plugin instruction](https://bnd.bndtools.org/instructions/plugin.html)
```
--plugin.providertype=org.apache.sling.bnd.providertype.ProviderTypeScanner
+-plugin.providertype:org.apache.sling.bnd.providertype.ProviderTypeScanner
+```
+
+To explicitly ignore certain provider types (i.e. don't fail when these are extended/implemented) one can use the attribute `ignored` with one or multiple comma-separated fully qualified provider type names. For example
+
+```
+-plugin.providertype:org.apache.sling.bnd.providertype.ProviderTypeScanner;ignored=org.apache.jackrabbit.api.security.user.User
```
## Prerequisites
diff --git a/org.apache.sling.bnd.providertype/src/main/java/org/apache/sling/bnd/providertype/ProviderTypeScanner.java b/org.apache.sling.bnd.providertype/src/main/java/org/apache/sling/bnd/providertype/ProviderTypeScanner.java
index 8f5cb7e2..dfc90dac 100644
--- a/org.apache.sling.bnd.providertype/src/main/java/org/apache/sling/bnd/providertype/ProviderTypeScanner.java
+++ b/org.apache.sling.bnd.providertype/src/main/java/org/apache/sling/bnd/providertype/ProviderTypeScanner.java
@@ -1,8 +1,28 @@
+/*
+ * 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.sling.bnd.providertype;
import java.io.InputStream;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -14,6 +34,7 @@ import aQute.bnd.osgi.Clazz;
import aQute.bnd.osgi.Descriptors.TypeRef;
import aQute.bnd.osgi.Resource;
import aQute.bnd.service.AnalyzerPlugin;
+import aQute.bnd.service.Plugin;
import aQute.lib.json.Decoder;
import aQute.lib.json.JSONCodec;
import aQute.service.reporter.Reporter;
@@ -23,11 +44,26 @@ import aQute.service.reporter.Reporter;
* Provider types are retrieved from the resource "META-INF/api-info.json" which is expected to be provided
* in the class path.
*/
-public class ProviderTypeScanner implements AnalyzerPlugin {
+public class ProviderTypeScanner implements AnalyzerPlugin, Plugin {
private static final String API_INFO_JSON_RESOURCE_PATH = "META-INF/api-info.json";
private static final String FIELD_PROVIDER_TYPES = "providerTypes";
private static final String MESSAGE = "Type \"%s\" %s provider type \"%s\". This is not allowed!";
+ private static final String ATTRIBUTE_IGNORED_PROVIDER_TYPES = "ignored";
+
+ private Map<String,String> parameters = new HashMap<>();
+
+ @Override
+ public void setProperties(Map<String, String> map) throws Exception {
+ // https://docs.osgi.org/specification/osgi.core/8.0.0/framework.module.html#framework.common.header.syntax
+ parameters.clear();
+ parameters.putAll(map);
+ }
+
+ @Override
+ public void setReporter(Reporter processor) {
+ // no need to store it as passed in analyzeJar(...) as well
+ }
@Override
public boolean analyzeJar(Analyzer analyzer) throws Exception {
@@ -43,6 +79,14 @@ public class ProviderTypeScanner implements AnalyzerPlugin {
throw new IllegalStateException("Could not parse JSON from resource " + apiInfoJsonResource, e);
}
}
+ // remove ignored provider types
+ Arrays.stream(parameters.getOrDefault(ATTRIBUTE_IGNORED_PROVIDER_TYPES, "").split(",")).filter(s -> !s.isBlank()).forEach(ignored -> {
+ if (providerTypes.remove(ignored)) {
+ analyzer.trace("Ignore extensions of provider type \"%s\" due to plugin configuration", ignored);
+ } else {
+ analyzer.warning("Ignored class \"%s\" is not defined as provider type at all, you can safely remove the according plugin parameter", ignored);
+ }
+ });
checkIfExtendingType(analyzer, analyzer.getClassspace().values(), providerTypes);
}
return false;
@@ -51,11 +95,11 @@ public class ProviderTypeScanner implements AnalyzerPlugin {
private void checkIfExtendingType(Reporter reporter, Collection<Clazz> clazzes, Set<String> providerTypes) {
for (Clazz clazz : clazzes) {
if (clazz.getSuper() != null && (providerTypes.contains(clazz.getSuper().getFQN()))) {
- reporter.error(MESSAGE, clazz.getFQN(), "extends", clazz.getSuper().getFQN());
+ reporter.error(MESSAGE, clazz.getFQN(), "extends", clazz.getSuper().getFQN()).file(clazz.getSourceFile());
}
for (TypeRef interfaceType : clazz.interfaces()) {
if (providerTypes.contains(interfaceType.getFQN())) {
- reporter.error(MESSAGE, clazz.getFQN(), "implements", interfaceType.getFQN());
+ reporter.error(MESSAGE, clazz.getFQN(), "implements", interfaceType.getFQN()).file(clazz.getSourceFile());
}
}
}
@@ -79,4 +123,5 @@ public class ProviderTypeScanner implements AnalyzerPlugin {
return Collections.emptySet();
}
+
}
\ No newline at end of file
diff --git a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/BndBuilderExtension.java b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/BndBuilderExtension.java
index 8c72decd..e21ee713 100644
--- a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/BndBuilderExtension.java
+++ b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/BndBuilderExtension.java
@@ -18,8 +18,6 @@
*/
package org.apache.sling.bnd.providertype;
-import static org.junit.jupiter.api.Assertions.fail;
-
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
diff --git a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/ProviderTypeScannerTest.java b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/ProviderTypeScannerTest.java
index 7089ae58..b1319d20 100644
--- a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/ProviderTypeScannerTest.java
+++ b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/ProviderTypeScannerTest.java
@@ -19,6 +19,7 @@
package org.apache.sling.bnd.providertype;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
@@ -79,16 +80,15 @@ class ProviderTypeScannerTest {
}
}
}
-
+
@Test
void testBuildWithInvalidProviderTypeMetadata2() throws Exception {
Builder builder = bndBuilderExtension.builder;
// add classpath entry with api-info.json
builder.setClasspath(new File[] { new File("src/test/resources3") });
try (Jar jar = builder.build()) {
- List<String> expectedErrors = Arrays.asList(
- "Resource \"META-INF/api-info.json\" does not contain a field named \"providerTypes\"");
- assertEquals(expectedErrors, builder.getErrors());
+ assertEquals(2, builder.getErrors().size());
+ assertTrue(builder.getErrors().get(0).startsWith("Exception: java.lang.IllegalStateException: Could not parse JSON from resource"));
if (!builder.getWarnings().isEmpty()) {
fail(String.join("\n", builder.getWarnings()));
}
diff --git a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeA.java b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeA.java
index e958bdb0..84c0e806 100644
--- a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeA.java
+++ b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeA.java
@@ -1,3 +1,21 @@
+/*
+ * 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.sling.bnd.providertype;
public interface TypeA {
diff --git a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeAImpl.java b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeAImpl.java
index 5fbe0eb5..afcc422a 100644
--- a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeAImpl.java
+++ b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeAImpl.java
@@ -1,3 +1,21 @@
+/*
+ * 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.sling.bnd.providertype;
public class TypeAImpl implements TypeA {
diff --git a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeB.java b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeB.java
index 31ac1938..013c3235 100644
--- a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeB.java
+++ b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeB.java
@@ -1,3 +1,21 @@
+/*
+ * 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.sling.bnd.providertype;
public class TypeB {
diff --git a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeBExtension.java b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeBExtension.java
index 228d5013..5b81b6f6 100644
--- a/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeBExtension.java
+++ b/org.apache.sling.bnd.providertype/src/test/java/org/apache/sling/bnd/providertype/TypeBExtension.java
@@ -1,3 +1,21 @@
+/*
+ * 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.sling.bnd.providertype;
public class TypeBExtension extends TypeB {