stataru8 commented on code in PR #341:
URL: https://github.com/apache/camel-karaf/pull/341#discussion_r1635252616


##########
tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/ConfigureWrapSpiProvider.java:
##########
@@ -0,0 +1,134 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.camel.karaf.feature.maven;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.zip.ZipFile;
+
+import org.apache.karaf.features.internal.model.Bundle;
+import org.apache.karaf.features.internal.model.Dependency;
+import org.apache.karaf.features.internal.model.Feature;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.eclipse.aether.RepositorySystem;
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.artifact.DefaultArtifact;
+import org.eclipse.aether.repository.RemoteRepository;
+import org.eclipse.aether.resolution.ArtifactRequest;
+
+
+@Mojo(name = "configure-wrap-spi-provider", defaultPhase = 
LifecyclePhase.PROCESS_RESOURCES)
+public class ConfigureWrapSpiProvider extends AbstractWrapBundleMojo {
+
+    private static final Pattern WRAP_PROTOCOL = 
Pattern.compile("wrap:mvn:([^/]+)/([^/]+)/([^$]+)(\\$([^=]+=[^&]+)(&([^=]+=[^&]+))*)?");
+    private static final String SPI_PROVIDER = "SPI-Provider";
+    private static final String SPI_HEADER = "%s=*".formatted(SPI_PROVIDER);
+
+    @Component
+    private RepositorySystem repoSystem;
+
+    @Parameter(defaultValue = "${repositorySystemSession}", readonly = true, 
required = true)
+    private RepositorySystemSession repoSession;
+
+    @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly 
= true, required = true)
+    private List<RemoteRepository> repositories;
+
+    @Override
+    protected void onFeatureUpdated(Feature feature) {
+        if (!containsSpiFly(feature)) {
+            // Add spifly as a prerequisite if not exists
+            Dependency dependency = new Dependency("spifly", null);
+            dependency.setPrerequisite(true);
+            feature.getFeature().add(dependency);
+        }
+    }
+
+    private static boolean containsSpiFly(Feature feature) {
+        return feature.getFeature().stream().anyMatch(d -> 
"spifly".equals(d.getName()));
+    }
+
+    @Override
+    protected boolean processWrappedBundle(Bundle bundle) {
+        String location = bundle.getLocation();
+        Matcher matcher = WRAP_PROTOCOL.matcher(location);
+        if (matcher.matches()) {
+            String groupId = matcher.group(1);
+            String artifactId = matcher.group(2);
+            String version = matcher.group(3);
+            String options = matcher.group(4);
+            if (options != null && options.contains(SPI_PROVIDER)) {
+                return false;
+            } else if (provideSPI(groupId, artifactId, version)) {
+                addHeader(bundle, options, location);
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static void addHeader(Bundle bundle, String options, String 
location) {
+        String separator;
+        if (options == null) {
+            separator = "$";
+        } else if (options.endsWith("&") || options.endsWith("$")) {
+            separator = "";
+        } else {
+            separator = "&";
+        }
+        bundle.setLocation("%s%s%s".formatted(location, separator, 
SPI_HEADER));
+    }
+
+    private boolean provideSPI(String groupId, String artifactId, String 
version) {
+        File file = downloadArtifact(groupId, artifactId, version);
+        if (file == null) {
+            getLog().warn("Could not download artifact 
%s:%s:%s".formatted(groupId, artifactId, version));
+            return false;
+        }
+        try {
+            return containsSPI(file);
+        } catch (IOException e) {
+            getLog().warn("Could not check artifact 
%s:%s:%s".formatted(groupId, artifactId, version), e);
+        }
+        return false;
+    }
+
+    private static boolean containsSPI(File file) throws IOException {
+        try (ZipFile zip = new ZipFile(file)) {
+            return zip.getEntry("META-INF/services") != null;
+        }
+    }
+
+    private File downloadArtifact(String groupId, String artifactId, String 
version) {

Review Comment:
   maybe rename this method to `resolveArtifact` or `resolveArtifact`? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to