essobedo commented on code in PR #317: URL: https://github.com/apache/camel-karaf/pull/317#discussion_r1625537933
########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( + //"Bundle-Version", + "DynamicImport-Package", + "Export-Package", + "Export-Service", + "Fragment-Host", + "Import-Package", + "Import-Service", + "Provide-Capability", + "Require-Bundle", + "Require-Capability"); + + private static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; + private static final String LICENCE_HEADER = """ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +-->"""; + + @Parameter(property = "featuresFilePath", required = true) + private String featuresFilePath; + + @Parameter(property = "targetFeature", required = false) + private String targetFeature = null; + + public String getFeaturesFilePath() { + return featuresFilePath; + } + + public void setFeaturesFilePath(String featuresFilePath) { + this.featuresFilePath = featuresFilePath; + } + + public String getTargetFeature() { + return targetFeature; + } + + public void setTargetFeature(String targetFeature) { + this.targetFeature = targetFeature; + } + + @Override + public void execute() throws MojoExecutionException { + Features featuresData = JaxbUtil.unmarshal(getFeaturesFilePath(), false); + List<Feature> features = featuresData.getFeature(); + + if (getTargetFeature() != null) { + boolean featureFound = false; + for (Feature feature : features) { + // all the feature versions will be modified + if (getTargetFeature().equals(feature.getName())) { + featureFound = true; + processFeature(feature); + } + } + if (!featureFound) { + getLog().warn("Feature %s not found. File '%s' wasn't modified".formatted(getTargetFeature(), + getFeaturesFilePath())); + return; + } + } else { + processFeatures(features); + } + + marshal(featuresData); + } + + private void marshal(Features featuresData) throws MojoExecutionException { + try (StringWriter writer = new StringWriter()) { + JaxbUtil.marshal(featuresData, writer); + + String result = writer.toString().replace(DEFAULT_HEADER, LICENCE_HEADER); + + Path path = Paths.get(getFeaturesFilePath().replaceFirst(FILE_PROTOCOL, "")); + Files.writeString(path, result); + + getLog().info("File '%s' was successfully modified and saved".formatted(getFeaturesFilePath())); + } catch (Exception e) { + getLog().error("File '%s' was successfully modified but an error occurred while saving it" + .formatted(getFeaturesFilePath()), e); + throw new MojoExecutionException(e); + } + } + + private void processFeatures(List<Feature> features) { + for (Feature feature : features) { + processFeature(feature); + } + } + + private void processFeature(Feature feature) { + for (Bundle bundle : feature.getBundle()) { + String location = bundle.getLocation(); + if (location != null && location.startsWith(WRAP_PROTOCOL)) { + try { + bundle.setLocation(processLocation(location)); + } catch (Exception e) { + getLog().error("Bundle location '%s' was ignored: %s".formatted(location, e.getMessage()), e); + } + } + } + } + + String processLocation(String localtion) throws Exception { Review Comment: Did you mean "location"? ########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( Review Comment: Do they need to be public? Maybe private or private-package is good enough, don't you agree? ########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( + //"Bundle-Version", + "DynamicImport-Package", + "Export-Package", + "Export-Service", + "Fragment-Host", + "Import-Package", + "Import-Service", + "Provide-Capability", + "Require-Bundle", + "Require-Capability"); + + private static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; + private static final String LICENCE_HEADER = """ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +-->"""; + + @Parameter(property = "featuresFilePath", required = true) + private String featuresFilePath; + + @Parameter(property = "targetFeature", required = false) + private String targetFeature = null; + + public String getFeaturesFilePath() { + return featuresFilePath; + } + + public void setFeaturesFilePath(String featuresFilePath) { + this.featuresFilePath = featuresFilePath; + } + + public String getTargetFeature() { + return targetFeature; + } + + public void setTargetFeature(String targetFeature) { + this.targetFeature = targetFeature; + } + + @Override + public void execute() throws MojoExecutionException { + Features featuresData = JaxbUtil.unmarshal(getFeaturesFilePath(), false); + List<Feature> features = featuresData.getFeature(); + + if (getTargetFeature() != null) { + boolean featureFound = false; + for (Feature feature : features) { + // all the feature versions will be modified + if (getTargetFeature().equals(feature.getName())) { + featureFound = true; + processFeature(feature); + } + } + if (!featureFound) { + getLog().warn("Feature %s not found. File '%s' wasn't modified".formatted(getTargetFeature(), + getFeaturesFilePath())); + return; + } + } else { + processFeatures(features); + } + + marshal(featuresData); + } + + private void marshal(Features featuresData) throws MojoExecutionException { + try (StringWriter writer = new StringWriter()) { + JaxbUtil.marshal(featuresData, writer); + + String result = writer.toString().replace(DEFAULT_HEADER, LICENCE_HEADER); + + Path path = Paths.get(getFeaturesFilePath().replaceFirst(FILE_PROTOCOL, "")); + Files.writeString(path, result); + + getLog().info("File '%s' was successfully modified and saved".formatted(getFeaturesFilePath())); + } catch (Exception e) { + getLog().error("File '%s' was successfully modified but an error occurred while saving it" + .formatted(getFeaturesFilePath()), e); + throw new MojoExecutionException(e); + } + } + + private void processFeatures(List<Feature> features) { + for (Feature feature : features) { + processFeature(feature); + } + } + + private void processFeature(Feature feature) { + for (Bundle bundle : feature.getBundle()) { + String location = bundle.getLocation(); + if (location != null && location.startsWith(WRAP_PROTOCOL)) { + try { + bundle.setLocation(processLocation(location)); + } catch (Exception e) { + getLog().error("Bundle location '%s' was ignored: %s".formatted(location, e.getMessage()), e); + } + } + } + } + + String processLocation(String localtion) throws Exception { + int versionStartIndex = getVersionStartIndex(localtion); + int versionEndIndex = getVersionEndIndex(localtion, versionStartIndex); + + String rawVersion = getVersion(localtion, versionStartIndex, versionEndIndex); + String version = getValidVersion(localtion, rawVersion); + + String bundleVersionHeader = "%s=%s".formatted(BUNDLE_VERSION, version); + + if (localtion.contains(bundleVersionHeader)) { + return localtion; + } else if (localtion.contains(BUNDLE_VERSION)) { + return updateExistingVersion(localtion, bundleVersionHeader); + } + + String wrapProtocolOptions = localtion.substring(versionEndIndex + 1, localtion.length()); + StringBuilder sb = new StringBuilder(localtion); + + // insert before existing headers header + for (String header : HEADERS_AFTER_BUNDLE_VEIRSION) { + // add Bundle-Version before + if (localtion.contains(header)) { + int versionHeaderStartIndex = localtion.indexOf(header); + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "%s&".formatted(bundleVersionHeader)).toString(); + } else { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "$%s&".formatted(bundleVersionHeader)).toString(); + } + } + } + + // insert at the end + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(localtion.length(), "&%s".formatted(bundleVersionHeader)).toString(); + } else { + return sb.insert(localtion.length(), "$%s".formatted(bundleVersionHeader)).toString(); + } + } + + /** + * @param location + * @return artifact version first char index, inclusive + */ + int getVersionStartIndex(String location) { + char[] chars = location.toCharArray(); + + boolean artifactIdFound = false; + for (int i = 0; i < chars.length; i++) { + if ('/' == chars[i]) { + if (!artifactIdFound) { + artifactIdFound = true; + } else { + return i + 1; + } + } + } + + return -1; + } + + int getVersionEndIndex(String location) { + return getVersionEndIndex(location, getVersionStartIndex(location)); + } + + /** + * @param location + * @param versionStartIndex + * @return artifact version last char index, inclusive + */ + int getVersionEndIndex(String location, int versionStartIndex) { + char[] chars = location.toCharArray(); + + // start at + 1 to ignore the potential $ coming from version placeholder + for (int i = versionStartIndex + 1; i < chars.length; i++) { + if ('$' == chars[i]) { + return i - 1; + } + } + + return chars.length - 1; + } + + String getVersion(String Location) { + return getVersion(Location, getVersionStartIndex(Location), getVersionEndIndex(Location)); + } + + String getVersion(String location, int versionStartIndex, int versionEndIndex) { + return location.substring(versionStartIndex, versionEndIndex + 1); + } + + String getValidVersion(String location, String version) throws Exception { + if (version.charAt(0) == '$') { + throw new Exception("Maven version placeholder '%s' wasn't resolved".formatted(version)); + } + + try { + // Test if version will work in Karaf + new Version(version); + } catch (Exception e) { + + // TODO: only use cleanVersion if the artifact is non-osgi + String cleanVersion = VersionCleaner.clean(version); + try { + // Test if clean version will work in Karaf again! + new Version(cleanVersion); + + getLog().debug( + "Bundle location '%s' will be set with Bundle-Version '%s', the output of org.apache.felix.utils.version.VersionCleaner.clean(%s)" + .formatted(location, cleanVersion, version)); + return cleanVersion; + + } catch (Exception newException) { + throw new Exception("Version '%s' is not OSGi compliant".formatted(cleanVersion), newException); + } + } + return version; + } + + String updateExistingVersion(String location, String bundleVersioHeader) throws Exception { + int versionHeaderStartIndex = location.indexOf(BUNDLE_VERSION); + int versionHeaderEndIndex = getBundleVersionHeaderEndIndex(location, versionHeaderStartIndex); + + // BUNDLE_VERSION.length() + 1 will include '=' + String currentVersion = location.substring(versionHeaderStartIndex + BUNDLE_VERSION.length() + 1, + versionHeaderEndIndex + 1); + if (currentVersion.charAt(0) == '$' || !currentVersion.equals(getValidVersion(location, currentVersion))) { + String currentBundleVersionHeader = location.substring(versionHeaderStartIndex, versionHeaderEndIndex + 1); + + return location.replaceAll(Pattern.quote(currentBundleVersionHeader), + Matcher.quoteReplacement(bundleVersioHeader)); + } + + return location; + } + + /** + * @param location + * @param versionHeaderStartIndex Review Comment: Document them or remove them but don't keep them empty ########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( + //"Bundle-Version", + "DynamicImport-Package", + "Export-Package", + "Export-Service", + "Fragment-Host", + "Import-Package", + "Import-Service", + "Provide-Capability", + "Require-Bundle", + "Require-Capability"); + + private static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; + private static final String LICENCE_HEADER = """ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +-->"""; + + @Parameter(property = "featuresFilePath", required = true) + private String featuresFilePath; + + @Parameter(property = "targetFeature", required = false) + private String targetFeature = null; + + public String getFeaturesFilePath() { + return featuresFilePath; + } + + public void setFeaturesFilePath(String featuresFilePath) { + this.featuresFilePath = featuresFilePath; + } + + public String getTargetFeature() { + return targetFeature; + } + + public void setTargetFeature(String targetFeature) { + this.targetFeature = targetFeature; + } + + @Override + public void execute() throws MojoExecutionException { + Features featuresData = JaxbUtil.unmarshal(getFeaturesFilePath(), false); + List<Feature> features = featuresData.getFeature(); + + if (getTargetFeature() != null) { + boolean featureFound = false; + for (Feature feature : features) { + // all the feature versions will be modified + if (getTargetFeature().equals(feature.getName())) { + featureFound = true; + processFeature(feature); + } + } + if (!featureFound) { + getLog().warn("Feature %s not found. File '%s' wasn't modified".formatted(getTargetFeature(), + getFeaturesFilePath())); + return; + } + } else { + processFeatures(features); + } + + marshal(featuresData); + } + + private void marshal(Features featuresData) throws MojoExecutionException { + try (StringWriter writer = new StringWriter()) { + JaxbUtil.marshal(featuresData, writer); + + String result = writer.toString().replace(DEFAULT_HEADER, LICENCE_HEADER); + + Path path = Paths.get(getFeaturesFilePath().replaceFirst(FILE_PROTOCOL, "")); + Files.writeString(path, result); + + getLog().info("File '%s' was successfully modified and saved".formatted(getFeaturesFilePath())); + } catch (Exception e) { + getLog().error("File '%s' was successfully modified but an error occurred while saving it" + .formatted(getFeaturesFilePath()), e); + throw new MojoExecutionException(e); + } + } + + private void processFeatures(List<Feature> features) { + for (Feature feature : features) { + processFeature(feature); + } + } + + private void processFeature(Feature feature) { + for (Bundle bundle : feature.getBundle()) { + String location = bundle.getLocation(); + if (location != null && location.startsWith(WRAP_PROTOCOL)) { + try { + bundle.setLocation(processLocation(location)); + } catch (Exception e) { + getLog().error("Bundle location '%s' was ignored: %s".formatted(location, e.getMessage()), e); + } + } + } + } + + String processLocation(String localtion) throws Exception { + int versionStartIndex = getVersionStartIndex(localtion); + int versionEndIndex = getVersionEndIndex(localtion, versionStartIndex); + + String rawVersion = getVersion(localtion, versionStartIndex, versionEndIndex); + String version = getValidVersion(localtion, rawVersion); + + String bundleVersionHeader = "%s=%s".formatted(BUNDLE_VERSION, version); + + if (localtion.contains(bundleVersionHeader)) { + return localtion; + } else if (localtion.contains(BUNDLE_VERSION)) { + return updateExistingVersion(localtion, bundleVersionHeader); + } + + String wrapProtocolOptions = localtion.substring(versionEndIndex + 1, localtion.length()); + StringBuilder sb = new StringBuilder(localtion); + + // insert before existing headers header + for (String header : HEADERS_AFTER_BUNDLE_VEIRSION) { + // add Bundle-Version before + if (localtion.contains(header)) { + int versionHeaderStartIndex = localtion.indexOf(header); + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "%s&".formatted(bundleVersionHeader)).toString(); + } else { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "$%s&".formatted(bundleVersionHeader)).toString(); + } + } + } + + // insert at the end + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(localtion.length(), "&%s".formatted(bundleVersionHeader)).toString(); + } else { + return sb.insert(localtion.length(), "$%s".formatted(bundleVersionHeader)).toString(); + } + } + + /** + * @param location + * @return artifact version first char index, inclusive + */ + int getVersionStartIndex(String location) { + char[] chars = location.toCharArray(); + + boolean artifactIdFound = false; + for (int i = 0; i < chars.length; i++) { + if ('/' == chars[i]) { + if (!artifactIdFound) { + artifactIdFound = true; + } else { + return i + 1; + } + } + } + + return -1; + } + + int getVersionEndIndex(String location) { + return getVersionEndIndex(location, getVersionStartIndex(location)); + } + + /** + * @param location + * @param versionStartIndex + * @return artifact version last char index, inclusive + */ + int getVersionEndIndex(String location, int versionStartIndex) { + char[] chars = location.toCharArray(); + + // start at + 1 to ignore the potential $ coming from version placeholder + for (int i = versionStartIndex + 1; i < chars.length; i++) { + if ('$' == chars[i]) { + return i - 1; + } + } + + return chars.length - 1; + } + + String getVersion(String Location) { + return getVersion(Location, getVersionStartIndex(Location), getVersionEndIndex(Location)); + } + + String getVersion(String location, int versionStartIndex, int versionEndIndex) { + return location.substring(versionStartIndex, versionEndIndex + 1); + } + + String getValidVersion(String location, String version) throws Exception { + if (version.charAt(0) == '$') { + throw new Exception("Maven version placeholder '%s' wasn't resolved".formatted(version)); + } + + try { + // Test if version will work in Karaf + new Version(version); Review Comment: Why do we need this step? why not call directly `VersionCleaner.clean(version)`? in other words, we could do directly what we have in the catch block, or maybe I miss something? ########## tooling/camel-karaf-feature-maven-plugin/src/test/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojoTest.java: ########## @@ -0,0 +1,142 @@ +/* + * 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.camel.karaf.feature.maven; + + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class EnsureWrapBundleVersionMojoTest { + + private final EnsureWrapBundleVersionMojo ensureVersionMojo = new EnsureWrapBundleVersionMojo(); + + @Test + void modifyLocationTest() throws Exception { + // add bundle version at the end + String location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0"; + String expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$Bundle-Version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge"; + expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Bundle-Version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Export-Package=org.apache.olingo.*;version=5.0.0"; + expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Bundle-Version=5.0.0&Export-Package=org.apache.olingo.*;version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // original version won't work in the karaf container + location = "wrap:mvn:com.google.apis/google-api-services-storage/v1-rev20240209-2.0.0"; + expected = "wrap:mvn:com.google.apis/google-api-services-storage/v1-rev20240209-2.0.0$Bundle-Version=0.0.0.v1-rev20240209-2_0_0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // bundle version header is present but it won't work in the karaf container + location = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + expected = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + ; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // bundle version header is present but it points to the wrong value + location = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + expected = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + ; + assertEquals(expected, ensureVersionMojo.processLocation(location)); Review Comment: Not clear what is the difference with the previous test. Bad copy/paste? ########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( + //"Bundle-Version", + "DynamicImport-Package", + "Export-Package", + "Export-Service", + "Fragment-Host", + "Import-Package", + "Import-Service", + "Provide-Capability", + "Require-Bundle", + "Require-Capability"); + + private static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; + private static final String LICENCE_HEADER = """ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +-->"""; + + @Parameter(property = "featuresFilePath", required = true) + private String featuresFilePath; + + @Parameter(property = "targetFeature", required = false) + private String targetFeature = null; + + public String getFeaturesFilePath() { + return featuresFilePath; + } + + public void setFeaturesFilePath(String featuresFilePath) { + this.featuresFilePath = featuresFilePath; + } + + public String getTargetFeature() { + return targetFeature; + } + + public void setTargetFeature(String targetFeature) { + this.targetFeature = targetFeature; + } + + @Override + public void execute() throws MojoExecutionException { + Features featuresData = JaxbUtil.unmarshal(getFeaturesFilePath(), false); + List<Feature> features = featuresData.getFeature(); + + if (getTargetFeature() != null) { + boolean featureFound = false; + for (Feature feature : features) { + // all the feature versions will be modified + if (getTargetFeature().equals(feature.getName())) { + featureFound = true; + processFeature(feature); + } + } + if (!featureFound) { + getLog().warn("Feature %s not found. File '%s' wasn't modified".formatted(getTargetFeature(), + getFeaturesFilePath())); + return; + } + } else { + processFeatures(features); + } + + marshal(featuresData); + } + + private void marshal(Features featuresData) throws MojoExecutionException { + try (StringWriter writer = new StringWriter()) { + JaxbUtil.marshal(featuresData, writer); + + String result = writer.toString().replace(DEFAULT_HEADER, LICENCE_HEADER); + + Path path = Paths.get(getFeaturesFilePath().replaceFirst(FILE_PROTOCOL, "")); + Files.writeString(path, result); + + getLog().info("File '%s' was successfully modified and saved".formatted(getFeaturesFilePath())); + } catch (Exception e) { + getLog().error("File '%s' was successfully modified but an error occurred while saving it" + .formatted(getFeaturesFilePath()), e); + throw new MojoExecutionException(e); + } + } + + private void processFeatures(List<Feature> features) { + for (Feature feature : features) { + processFeature(feature); + } + } + + private void processFeature(Feature feature) { + for (Bundle bundle : feature.getBundle()) { + String location = bundle.getLocation(); + if (location != null && location.startsWith(WRAP_PROTOCOL)) { + try { + bundle.setLocation(processLocation(location)); + } catch (Exception e) { + getLog().error("Bundle location '%s' was ignored: %s".formatted(location, e.getMessage()), e); Review Comment: The error message should rather be something like "Could not process the Bundle location '%s': '%s'" ########## tooling/camel-karaf-feature-maven-plugin/pom.xml: ########## @@ -0,0 +1,111 @@ +<!-- + 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. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel.karaf</groupId> + <artifactId>tooling</artifactId> + <version>4.6.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-karaf-feature-maven-plugin</artifactId> + <packaging>maven-plugin</packaging> + <name>Apache Camel :: Karaf :: Tooling :: Feature Maven Plugin</name> + + <properties> + <felix.utils.version>1.11.8</felix.utils.version> + <junit.jupiter.version>5.10.2</junit.jupiter.version> Review Comment: Not needed as it is already set in the parent pom ########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( + //"Bundle-Version", + "DynamicImport-Package", + "Export-Package", + "Export-Service", + "Fragment-Host", + "Import-Package", + "Import-Service", + "Provide-Capability", + "Require-Bundle", + "Require-Capability"); + + private static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; + private static final String LICENCE_HEADER = """ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +-->"""; + + @Parameter(property = "featuresFilePath", required = true) + private String featuresFilePath; + + @Parameter(property = "targetFeature", required = false) + private String targetFeature = null; + + public String getFeaturesFilePath() { + return featuresFilePath; + } + + public void setFeaturesFilePath(String featuresFilePath) { + this.featuresFilePath = featuresFilePath; + } + + public String getTargetFeature() { + return targetFeature; + } + + public void setTargetFeature(String targetFeature) { + this.targetFeature = targetFeature; + } + + @Override + public void execute() throws MojoExecutionException { + Features featuresData = JaxbUtil.unmarshal(getFeaturesFilePath(), false); + List<Feature> features = featuresData.getFeature(); + + if (getTargetFeature() != null) { + boolean featureFound = false; + for (Feature feature : features) { + // all the feature versions will be modified + if (getTargetFeature().equals(feature.getName())) { + featureFound = true; + processFeature(feature); + } + } + if (!featureFound) { + getLog().warn("Feature %s not found. File '%s' wasn't modified".formatted(getTargetFeature(), + getFeaturesFilePath())); + return; + } + } else { + processFeatures(features); + } + + marshal(featuresData); + } + + private void marshal(Features featuresData) throws MojoExecutionException { + try (StringWriter writer = new StringWriter()) { + JaxbUtil.marshal(featuresData, writer); + + String result = writer.toString().replace(DEFAULT_HEADER, LICENCE_HEADER); + + Path path = Paths.get(getFeaturesFilePath().replaceFirst(FILE_PROTOCOL, "")); + Files.writeString(path, result); + + getLog().info("File '%s' was successfully modified and saved".formatted(getFeaturesFilePath())); + } catch (Exception e) { + getLog().error("File '%s' was successfully modified but an error occurred while saving it" + .formatted(getFeaturesFilePath()), e); + throw new MojoExecutionException(e); + } + } + + private void processFeatures(List<Feature> features) { + for (Feature feature : features) { + processFeature(feature); + } + } + + private void processFeature(Feature feature) { + for (Bundle bundle : feature.getBundle()) { + String location = bundle.getLocation(); + if (location != null && location.startsWith(WRAP_PROTOCOL)) { + try { + bundle.setLocation(processLocation(location)); + } catch (Exception e) { + getLog().error("Bundle location '%s' was ignored: %s".formatted(location, e.getMessage()), e); + } + } + } + } + + String processLocation(String localtion) throws Exception { + int versionStartIndex = getVersionStartIndex(localtion); + int versionEndIndex = getVersionEndIndex(localtion, versionStartIndex); + + String rawVersion = getVersion(localtion, versionStartIndex, versionEndIndex); + String version = getValidVersion(localtion, rawVersion); + + String bundleVersionHeader = "%s=%s".formatted(BUNDLE_VERSION, version); + + if (localtion.contains(bundleVersionHeader)) { + return localtion; + } else if (localtion.contains(BUNDLE_VERSION)) { + return updateExistingVersion(localtion, bundleVersionHeader); + } + + String wrapProtocolOptions = localtion.substring(versionEndIndex + 1, localtion.length()); + StringBuilder sb = new StringBuilder(localtion); + + // insert before existing headers header + for (String header : HEADERS_AFTER_BUNDLE_VEIRSION) { + // add Bundle-Version before + if (localtion.contains(header)) { + int versionHeaderStartIndex = localtion.indexOf(header); + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "%s&".formatted(bundleVersionHeader)).toString(); + } else { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "$%s&".formatted(bundleVersionHeader)).toString(); + } + } + } + + // insert at the end + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(localtion.length(), "&%s".formatted(bundleVersionHeader)).toString(); + } else { + return sb.insert(localtion.length(), "$%s".formatted(bundleVersionHeader)).toString(); + } + } + + /** + * @param location + * @return artifact version first char index, inclusive + */ + int getVersionStartIndex(String location) { + char[] chars = location.toCharArray(); + + boolean artifactIdFound = false; + for (int i = 0; i < chars.length; i++) { + if ('/' == chars[i]) { + if (!artifactIdFound) { + artifactIdFound = true; + } else { + return i + 1; + } + } + } + + return -1; + } + + int getVersionEndIndex(String location) { + return getVersionEndIndex(location, getVersionStartIndex(location)); + } + + /** + * @param location + * @param versionStartIndex + * @return artifact version last char index, inclusive + */ + int getVersionEndIndex(String location, int versionStartIndex) { + char[] chars = location.toCharArray(); + + // start at + 1 to ignore the potential $ coming from version placeholder + for (int i = versionStartIndex + 1; i < chars.length; i++) { + if ('$' == chars[i]) { + return i - 1; + } + } + + return chars.length - 1; + } + + String getVersion(String Location) { + return getVersion(Location, getVersionStartIndex(Location), getVersionEndIndex(Location)); + } + + String getVersion(String location, int versionStartIndex, int versionEndIndex) { + return location.substring(versionStartIndex, versionEndIndex + 1); + } + + String getValidVersion(String location, String version) throws Exception { + if (version.charAt(0) == '$') { + throw new Exception("Maven version placeholder '%s' wasn't resolved".formatted(version)); + } + + try { + // Test if version will work in Karaf + new Version(version); + } catch (Exception e) { + + // TODO: only use cleanVersion if the artifact is non-osgi + String cleanVersion = VersionCleaner.clean(version); + try { + // Test if clean version will work in Karaf again! + new Version(cleanVersion); + + getLog().debug( + "Bundle location '%s' will be set with Bundle-Version '%s', the output of org.apache.felix.utils.version.VersionCleaner.clean(%s)" + .formatted(location, cleanVersion, version)); + return cleanVersion; + + } catch (Exception newException) { + throw new Exception("Version '%s' is not OSGi compliant".formatted(cleanVersion), newException); + } + } + return version; + } + + String updateExistingVersion(String location, String bundleVersioHeader) throws Exception { + int versionHeaderStartIndex = location.indexOf(BUNDLE_VERSION); + int versionHeaderEndIndex = getBundleVersionHeaderEndIndex(location, versionHeaderStartIndex); + + // BUNDLE_VERSION.length() + 1 will include '=' + String currentVersion = location.substring(versionHeaderStartIndex + BUNDLE_VERSION.length() + 1, + versionHeaderEndIndex + 1); + if (currentVersion.charAt(0) == '$' || !currentVersion.equals(getValidVersion(location, currentVersion))) { + String currentBundleVersionHeader = location.substring(versionHeaderStartIndex, versionHeaderEndIndex + 1); + + return location.replaceAll(Pattern.quote(currentBundleVersionHeader), + Matcher.quoteReplacement(bundleVersioHeader)); + } + + return location; + } + + /** + * @param location + * @param versionHeaderStartIndex + * @return wrap protocol Bundle-Version header last char index, inclusive + */ + int getBundleVersionHeaderEndIndex(String location, int versionHeaderStartIndex) { + char[] chars = location.toCharArray(); Review Comment: Warning `toCharArray` creates a new char array while here your goal is only to read some characters in the String, so use `charAt` instead ########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( + //"Bundle-Version", + "DynamicImport-Package", + "Export-Package", + "Export-Service", + "Fragment-Host", + "Import-Package", + "Import-Service", + "Provide-Capability", + "Require-Bundle", + "Require-Capability"); + + private static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; + private static final String LICENCE_HEADER = """ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +-->"""; + + @Parameter(property = "featuresFilePath", required = true) + private String featuresFilePath; + + @Parameter(property = "targetFeature", required = false) + private String targetFeature = null; + + public String getFeaturesFilePath() { + return featuresFilePath; + } + + public void setFeaturesFilePath(String featuresFilePath) { + this.featuresFilePath = featuresFilePath; + } + + public String getTargetFeature() { + return targetFeature; + } + + public void setTargetFeature(String targetFeature) { + this.targetFeature = targetFeature; + } + + @Override + public void execute() throws MojoExecutionException { + Features featuresData = JaxbUtil.unmarshal(getFeaturesFilePath(), false); + List<Feature> features = featuresData.getFeature(); + + if (getTargetFeature() != null) { + boolean featureFound = false; + for (Feature feature : features) { + // all the feature versions will be modified + if (getTargetFeature().equals(feature.getName())) { + featureFound = true; + processFeature(feature); + } + } + if (!featureFound) { + getLog().warn("Feature %s not found. File '%s' wasn't modified".formatted(getTargetFeature(), + getFeaturesFilePath())); + return; + } + } else { + processFeatures(features); + } + + marshal(featuresData); + } + + private void marshal(Features featuresData) throws MojoExecutionException { + try (StringWriter writer = new StringWriter()) { + JaxbUtil.marshal(featuresData, writer); + + String result = writer.toString().replace(DEFAULT_HEADER, LICENCE_HEADER); + + Path path = Paths.get(getFeaturesFilePath().replaceFirst(FILE_PROTOCOL, "")); + Files.writeString(path, result); + + getLog().info("File '%s' was successfully modified and saved".formatted(getFeaturesFilePath())); + } catch (Exception e) { + getLog().error("File '%s' was successfully modified but an error occurred while saving it" + .formatted(getFeaturesFilePath()), e); + throw new MojoExecutionException(e); + } + } + + private void processFeatures(List<Feature> features) { + for (Feature feature : features) { + processFeature(feature); + } + } + + private void processFeature(Feature feature) { + for (Bundle bundle : feature.getBundle()) { + String location = bundle.getLocation(); + if (location != null && location.startsWith(WRAP_PROTOCOL)) { + try { + bundle.setLocation(processLocation(location)); + } catch (Exception e) { + getLog().error("Bundle location '%s' was ignored: %s".formatted(location, e.getMessage()), e); + } + } + } + } + + String processLocation(String localtion) throws Exception { + int versionStartIndex = getVersionStartIndex(localtion); + int versionEndIndex = getVersionEndIndex(localtion, versionStartIndex); + + String rawVersion = getVersion(localtion, versionStartIndex, versionEndIndex); + String version = getValidVersion(localtion, rawVersion); + + String bundleVersionHeader = "%s=%s".formatted(BUNDLE_VERSION, version); + + if (localtion.contains(bundleVersionHeader)) { + return localtion; + } else if (localtion.contains(BUNDLE_VERSION)) { + return updateExistingVersion(localtion, bundleVersionHeader); + } + + String wrapProtocolOptions = localtion.substring(versionEndIndex + 1, localtion.length()); + StringBuilder sb = new StringBuilder(localtion); + + // insert before existing headers header + for (String header : HEADERS_AFTER_BUNDLE_VEIRSION) { + // add Bundle-Version before + if (localtion.contains(header)) { + int versionHeaderStartIndex = localtion.indexOf(header); + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "%s&".formatted(bundleVersionHeader)).toString(); + } else { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "$%s&".formatted(bundleVersionHeader)).toString(); + } + } + } + + // insert at the end + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(localtion.length(), "&%s".formatted(bundleVersionHeader)).toString(); + } else { + return sb.insert(localtion.length(), "$%s".formatted(bundleVersionHeader)).toString(); + } + } + + /** + * @param location + * @return artifact version first char index, inclusive + */ + int getVersionStartIndex(String location) { + char[] chars = location.toCharArray(); + + boolean artifactIdFound = false; + for (int i = 0; i < chars.length; i++) { + if ('/' == chars[i]) { + if (!artifactIdFound) { + artifactIdFound = true; + } else { + return i + 1; + } + } + } + + return -1; + } + + int getVersionEndIndex(String location) { + return getVersionEndIndex(location, getVersionStartIndex(location)); + } + + /** + * @param location + * @param versionStartIndex + * @return artifact version last char index, inclusive + */ + int getVersionEndIndex(String location, int versionStartIndex) { + char[] chars = location.toCharArray(); + + // start at + 1 to ignore the potential $ coming from version placeholder + for (int i = versionStartIndex + 1; i < chars.length; i++) { + if ('$' == chars[i]) { + return i - 1; + } + } + + return chars.length - 1; + } + + String getVersion(String Location) { + return getVersion(Location, getVersionStartIndex(Location), getVersionEndIndex(Location)); + } + + String getVersion(String location, int versionStartIndex, int versionEndIndex) { + return location.substring(versionStartIndex, versionEndIndex + 1); + } + + String getValidVersion(String location, String version) throws Exception { + if (version.charAt(0) == '$') { + throw new Exception("Maven version placeholder '%s' wasn't resolved".formatted(version)); + } + + try { + // Test if version will work in Karaf + new Version(version); + } catch (Exception e) { + + // TODO: only use cleanVersion if the artifact is non-osgi + String cleanVersion = VersionCleaner.clean(version); + try { + // Test if clean version will work in Karaf again! + new Version(cleanVersion); + + getLog().debug( + "Bundle location '%s' will be set with Bundle-Version '%s', the output of org.apache.felix.utils.version.VersionCleaner.clean(%s)" + .formatted(location, cleanVersion, version)); Review Comment: Consider adding `if (getLog().isDebugEnabled())` for all debug message with non static messages like in this case ########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( + //"Bundle-Version", + "DynamicImport-Package", + "Export-Package", + "Export-Service", + "Fragment-Host", + "Import-Package", + "Import-Service", + "Provide-Capability", + "Require-Bundle", + "Require-Capability"); + + private static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; + private static final String LICENCE_HEADER = """ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +-->"""; + + @Parameter(property = "featuresFilePath", required = true) + private String featuresFilePath; + + @Parameter(property = "targetFeature", required = false) + private String targetFeature = null; + + public String getFeaturesFilePath() { + return featuresFilePath; + } + + public void setFeaturesFilePath(String featuresFilePath) { + this.featuresFilePath = featuresFilePath; + } + + public String getTargetFeature() { + return targetFeature; + } + + public void setTargetFeature(String targetFeature) { + this.targetFeature = targetFeature; + } + + @Override + public void execute() throws MojoExecutionException { + Features featuresData = JaxbUtil.unmarshal(getFeaturesFilePath(), false); + List<Feature> features = featuresData.getFeature(); + + if (getTargetFeature() != null) { + boolean featureFound = false; + for (Feature feature : features) { + // all the feature versions will be modified + if (getTargetFeature().equals(feature.getName())) { + featureFound = true; + processFeature(feature); + } + } + if (!featureFound) { + getLog().warn("Feature %s not found. File '%s' wasn't modified".formatted(getTargetFeature(), + getFeaturesFilePath())); + return; + } + } else { + processFeatures(features); + } + + marshal(featuresData); + } + + private void marshal(Features featuresData) throws MojoExecutionException { + try (StringWriter writer = new StringWriter()) { + JaxbUtil.marshal(featuresData, writer); + + String result = writer.toString().replace(DEFAULT_HEADER, LICENCE_HEADER); + + Path path = Paths.get(getFeaturesFilePath().replaceFirst(FILE_PROTOCOL, "")); + Files.writeString(path, result); + + getLog().info("File '%s' was successfully modified and saved".formatted(getFeaturesFilePath())); + } catch (Exception e) { + getLog().error("File '%s' was successfully modified but an error occurred while saving it" + .formatted(getFeaturesFilePath()), e); + throw new MojoExecutionException(e); + } + } + + private void processFeatures(List<Feature> features) { + for (Feature feature : features) { + processFeature(feature); + } + } + + private void processFeature(Feature feature) { + for (Bundle bundle : feature.getBundle()) { + String location = bundle.getLocation(); + if (location != null && location.startsWith(WRAP_PROTOCOL)) { + try { + bundle.setLocation(processLocation(location)); + } catch (Exception e) { + getLog().error("Bundle location '%s' was ignored: %s".formatted(location, e.getMessage()), e); + } + } + } + } + + String processLocation(String localtion) throws Exception { + int versionStartIndex = getVersionStartIndex(localtion); + int versionEndIndex = getVersionEndIndex(localtion, versionStartIndex); + + String rawVersion = getVersion(localtion, versionStartIndex, versionEndIndex); + String version = getValidVersion(localtion, rawVersion); + + String bundleVersionHeader = "%s=%s".formatted(BUNDLE_VERSION, version); + + if (localtion.contains(bundleVersionHeader)) { + return localtion; + } else if (localtion.contains(BUNDLE_VERSION)) { + return updateExistingVersion(localtion, bundleVersionHeader); + } + + String wrapProtocolOptions = localtion.substring(versionEndIndex + 1, localtion.length()); + StringBuilder sb = new StringBuilder(localtion); + + // insert before existing headers header + for (String header : HEADERS_AFTER_BUNDLE_VEIRSION) { + // add Bundle-Version before + if (localtion.contains(header)) { + int versionHeaderStartIndex = localtion.indexOf(header); + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "%s&".formatted(bundleVersionHeader)).toString(); + } else { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "$%s&".formatted(bundleVersionHeader)).toString(); + } + } + } + + // insert at the end + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(localtion.length(), "&%s".formatted(bundleVersionHeader)).toString(); + } else { + return sb.insert(localtion.length(), "$%s".formatted(bundleVersionHeader)).toString(); + } + } + + /** + * @param location + * @return artifact version first char index, inclusive + */ + int getVersionStartIndex(String location) { + char[] chars = location.toCharArray(); + + boolean artifactIdFound = false; + for (int i = 0; i < chars.length; i++) { + if ('/' == chars[i]) { + if (!artifactIdFound) { + artifactIdFound = true; + } else { + return i + 1; + } + } + } + + return -1; + } + + int getVersionEndIndex(String location) { + return getVersionEndIndex(location, getVersionStartIndex(location)); + } + + /** + * @param location + * @param versionStartIndex + * @return artifact version last char index, inclusive + */ + int getVersionEndIndex(String location, int versionStartIndex) { + char[] chars = location.toCharArray(); + + // start at + 1 to ignore the potential $ coming from version placeholder + for (int i = versionStartIndex + 1; i < chars.length; i++) { + if ('$' == chars[i]) { + return i - 1; + } + } + + return chars.length - 1; + } + + String getVersion(String Location) { + return getVersion(Location, getVersionStartIndex(Location), getVersionEndIndex(Location)); + } + + String getVersion(String location, int versionStartIndex, int versionEndIndex) { + return location.substring(versionStartIndex, versionEndIndex + 1); + } + + String getValidVersion(String location, String version) throws Exception { + if (version.charAt(0) == '$') { + throw new Exception("Maven version placeholder '%s' wasn't resolved".formatted(version)); + } + + try { + // Test if version will work in Karaf + new Version(version); + } catch (Exception e) { + + // TODO: only use cleanVersion if the artifact is non-osgi + String cleanVersion = VersionCleaner.clean(version); + try { + // Test if clean version will work in Karaf again! + new Version(cleanVersion); + + getLog().debug( + "Bundle location '%s' will be set with Bundle-Version '%s', the output of org.apache.felix.utils.version.VersionCleaner.clean(%s)" + .formatted(location, cleanVersion, version)); + return cleanVersion; + + } catch (Exception newException) { + throw new Exception("Version '%s' is not OSGi compliant".formatted(cleanVersion), newException); + } + } + return version; + } + + String updateExistingVersion(String location, String bundleVersioHeader) throws Exception { + int versionHeaderStartIndex = location.indexOf(BUNDLE_VERSION); + int versionHeaderEndIndex = getBundleVersionHeaderEndIndex(location, versionHeaderStartIndex); + + // BUNDLE_VERSION.length() + 1 will include '=' + String currentVersion = location.substring(versionHeaderStartIndex + BUNDLE_VERSION.length() + 1, + versionHeaderEndIndex + 1); + if (currentVersion.charAt(0) == '$' || !currentVersion.equals(getValidVersion(location, currentVersion))) { + String currentBundleVersionHeader = location.substring(versionHeaderStartIndex, versionHeaderEndIndex + 1); + + return location.replaceAll(Pattern.quote(currentBundleVersionHeader), + Matcher.quoteReplacement(bundleVersioHeader)); Review Comment: if your goal is to replace string literals, you can use `replace` instead ########## tooling/camel-karaf-feature-maven-plugin/src/test/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojoTest.java: ########## @@ -0,0 +1,142 @@ +/* + * 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.camel.karaf.feature.maven; + + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class EnsureWrapBundleVersionMojoTest { + + private final EnsureWrapBundleVersionMojo ensureVersionMojo = new EnsureWrapBundleVersionMojo(); + + @Test + void modifyLocationTest() throws Exception { + // add bundle version at the end + String location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0"; + String expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$Bundle-Version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge"; + expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Bundle-Version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Export-Package=org.apache.olingo.*;version=5.0.0"; + expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Bundle-Version=5.0.0&Export-Package=org.apache.olingo.*;version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // original version won't work in the karaf container + location = "wrap:mvn:com.google.apis/google-api-services-storage/v1-rev20240209-2.0.0"; + expected = "wrap:mvn:com.google.apis/google-api-services-storage/v1-rev20240209-2.0.0$Bundle-Version=0.0.0.v1-rev20240209-2_0_0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // bundle version header is present but it won't work in the karaf container + location = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + expected = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + ; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // bundle version header is present but it points to the wrong value + location = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + expected = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + ; Review Comment: ditto ########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( + //"Bundle-Version", + "DynamicImport-Package", + "Export-Package", + "Export-Service", + "Fragment-Host", + "Import-Package", + "Import-Service", + "Provide-Capability", + "Require-Bundle", + "Require-Capability"); + + private static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; + private static final String LICENCE_HEADER = """ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +-->"""; + + @Parameter(property = "featuresFilePath", required = true) + private String featuresFilePath; + + @Parameter(property = "targetFeature", required = false) + private String targetFeature = null; + + public String getFeaturesFilePath() { + return featuresFilePath; + } + + public void setFeaturesFilePath(String featuresFilePath) { + this.featuresFilePath = featuresFilePath; + } + + public String getTargetFeature() { + return targetFeature; + } + + public void setTargetFeature(String targetFeature) { + this.targetFeature = targetFeature; + } + + @Override + public void execute() throws MojoExecutionException { + Features featuresData = JaxbUtil.unmarshal(getFeaturesFilePath(), false); + List<Feature> features = featuresData.getFeature(); + + if (getTargetFeature() != null) { + boolean featureFound = false; + for (Feature feature : features) { + // all the feature versions will be modified + if (getTargetFeature().equals(feature.getName())) { + featureFound = true; + processFeature(feature); + } + } + if (!featureFound) { + getLog().warn("Feature %s not found. File '%s' wasn't modified".formatted(getTargetFeature(), + getFeaturesFilePath())); + return; + } + } else { + processFeatures(features); + } + + marshal(featuresData); + } + + private void marshal(Features featuresData) throws MojoExecutionException { + try (StringWriter writer = new StringWriter()) { + JaxbUtil.marshal(featuresData, writer); + + String result = writer.toString().replace(DEFAULT_HEADER, LICENCE_HEADER); + + Path path = Paths.get(getFeaturesFilePath().replaceFirst(FILE_PROTOCOL, "")); + Files.writeString(path, result); + + getLog().info("File '%s' was successfully modified and saved".formatted(getFeaturesFilePath())); + } catch (Exception e) { + getLog().error("File '%s' was successfully modified but an error occurred while saving it" + .formatted(getFeaturesFilePath()), e); + throw new MojoExecutionException(e); + } + } + + private void processFeatures(List<Feature> features) { + for (Feature feature : features) { + processFeature(feature); + } + } + + private void processFeature(Feature feature) { + for (Bundle bundle : feature.getBundle()) { + String location = bundle.getLocation(); + if (location != null && location.startsWith(WRAP_PROTOCOL)) { + try { + bundle.setLocation(processLocation(location)); + } catch (Exception e) { + getLog().error("Bundle location '%s' was ignored: %s".formatted(location, e.getMessage()), e); + } + } + } + } + + String processLocation(String localtion) throws Exception { + int versionStartIndex = getVersionStartIndex(localtion); + int versionEndIndex = getVersionEndIndex(localtion, versionStartIndex); + + String rawVersion = getVersion(localtion, versionStartIndex, versionEndIndex); + String version = getValidVersion(localtion, rawVersion); + + String bundleVersionHeader = "%s=%s".formatted(BUNDLE_VERSION, version); + + if (localtion.contains(bundleVersionHeader)) { + return localtion; + } else if (localtion.contains(BUNDLE_VERSION)) { + return updateExistingVersion(localtion, bundleVersionHeader); + } + + String wrapProtocolOptions = localtion.substring(versionEndIndex + 1, localtion.length()); + StringBuilder sb = new StringBuilder(localtion); + + // insert before existing headers header + for (String header : HEADERS_AFTER_BUNDLE_VEIRSION) { + // add Bundle-Version before + if (localtion.contains(header)) { + int versionHeaderStartIndex = localtion.indexOf(header); + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "%s&".formatted(bundleVersionHeader)).toString(); + } else { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "$%s&".formatted(bundleVersionHeader)).toString(); + } + } + } + + // insert at the end + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(localtion.length(), "&%s".formatted(bundleVersionHeader)).toString(); + } else { + return sb.insert(localtion.length(), "$%s".formatted(bundleVersionHeader)).toString(); + } + } + + /** + * @param location + * @return artifact version first char index, inclusive + */ + int getVersionStartIndex(String location) { + char[] chars = location.toCharArray(); Review Comment: same remark as below ########## tooling/camel-karaf-feature-maven-plugin/src/test/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojoTest.java: ########## @@ -0,0 +1,142 @@ +/* + * 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.camel.karaf.feature.maven; + + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class EnsureWrapBundleVersionMojoTest { + + private final EnsureWrapBundleVersionMojo ensureVersionMojo = new EnsureWrapBundleVersionMojo(); + + @Test + void modifyLocationTest() throws Exception { + // add bundle version at the end + String location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0"; + String expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$Bundle-Version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge"; + expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Bundle-Version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Export-Package=org.apache.olingo.*;version=5.0.0"; + expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Bundle-Version=5.0.0&Export-Package=org.apache.olingo.*;version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // original version won't work in the karaf container + location = "wrap:mvn:com.google.apis/google-api-services-storage/v1-rev20240209-2.0.0"; + expected = "wrap:mvn:com.google.apis/google-api-services-storage/v1-rev20240209-2.0.0$Bundle-Version=0.0.0.v1-rev20240209-2_0_0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // bundle version header is present but it won't work in the karaf container + location = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + expected = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + ; Review Comment: remove me ########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( + //"Bundle-Version", + "DynamicImport-Package", + "Export-Package", + "Export-Service", + "Fragment-Host", + "Import-Package", + "Import-Service", + "Provide-Capability", + "Require-Bundle", + "Require-Capability"); + + private static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; + private static final String LICENCE_HEADER = """ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +-->"""; + + @Parameter(property = "featuresFilePath", required = true) + private String featuresFilePath; + + @Parameter(property = "targetFeature", required = false) + private String targetFeature = null; + + public String getFeaturesFilePath() { + return featuresFilePath; + } + + public void setFeaturesFilePath(String featuresFilePath) { + this.featuresFilePath = featuresFilePath; + } + + public String getTargetFeature() { + return targetFeature; + } + + public void setTargetFeature(String targetFeature) { + this.targetFeature = targetFeature; + } + + @Override + public void execute() throws MojoExecutionException { + Features featuresData = JaxbUtil.unmarshal(getFeaturesFilePath(), false); + List<Feature> features = featuresData.getFeature(); + + if (getTargetFeature() != null) { + boolean featureFound = false; + for (Feature feature : features) { + // all the feature versions will be modified + if (getTargetFeature().equals(feature.getName())) { + featureFound = true; + processFeature(feature); + } + } + if (!featureFound) { + getLog().warn("Feature %s not found. File '%s' wasn't modified".formatted(getTargetFeature(), + getFeaturesFilePath())); + return; + } + } else { + processFeatures(features); + } + + marshal(featuresData); + } + + private void marshal(Features featuresData) throws MojoExecutionException { + try (StringWriter writer = new StringWriter()) { + JaxbUtil.marshal(featuresData, writer); + + String result = writer.toString().replace(DEFAULT_HEADER, LICENCE_HEADER); + + Path path = Paths.get(getFeaturesFilePath().replaceFirst(FILE_PROTOCOL, "")); + Files.writeString(path, result); + + getLog().info("File '%s' was successfully modified and saved".formatted(getFeaturesFilePath())); + } catch (Exception e) { + getLog().error("File '%s' was successfully modified but an error occurred while saving it" + .formatted(getFeaturesFilePath()), e); + throw new MojoExecutionException(e); + } + } + + private void processFeatures(List<Feature> features) { + for (Feature feature : features) { + processFeature(feature); + } + } + + private void processFeature(Feature feature) { + for (Bundle bundle : feature.getBundle()) { + String location = bundle.getLocation(); + if (location != null && location.startsWith(WRAP_PROTOCOL)) { + try { + bundle.setLocation(processLocation(location)); + } catch (Exception e) { + getLog().error("Bundle location '%s' was ignored: %s".formatted(location, e.getMessage()), e); + } + } + } + } + + String processLocation(String localtion) throws Exception { + int versionStartIndex = getVersionStartIndex(localtion); + int versionEndIndex = getVersionEndIndex(localtion, versionStartIndex); + + String rawVersion = getVersion(localtion, versionStartIndex, versionEndIndex); + String version = getValidVersion(localtion, rawVersion); + + String bundleVersionHeader = "%s=%s".formatted(BUNDLE_VERSION, version); + + if (localtion.contains(bundleVersionHeader)) { + return localtion; + } else if (localtion.contains(BUNDLE_VERSION)) { + return updateExistingVersion(localtion, bundleVersionHeader); + } + + String wrapProtocolOptions = localtion.substring(versionEndIndex + 1, localtion.length()); + StringBuilder sb = new StringBuilder(localtion); + + // insert before existing headers header + for (String header : HEADERS_AFTER_BUNDLE_VEIRSION) { + // add Bundle-Version before + if (localtion.contains(header)) { + int versionHeaderStartIndex = localtion.indexOf(header); + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "%s&".formatted(bundleVersionHeader)).toString(); + } else { + // "amp;" is automatically added + return sb.insert(versionHeaderStartIndex, "$%s&".formatted(bundleVersionHeader)).toString(); + } + } + } + + // insert at the end + if (wrapProtocolOptions.contains("$")) { + // "amp;" is automatically added + return sb.insert(localtion.length(), "&%s".formatted(bundleVersionHeader)).toString(); + } else { + return sb.insert(localtion.length(), "$%s".formatted(bundleVersionHeader)).toString(); + } + } + + /** + * @param location + * @return artifact version first char index, inclusive + */ + int getVersionStartIndex(String location) { + char[] chars = location.toCharArray(); + + boolean artifactIdFound = false; + for (int i = 0; i < chars.length; i++) { + if ('/' == chars[i]) { + if (!artifactIdFound) { + artifactIdFound = true; + } else { + return i + 1; + } + } + } + + return -1; + } + + int getVersionEndIndex(String location) { + return getVersionEndIndex(location, getVersionStartIndex(location)); + } + + /** + * @param location + * @param versionStartIndex + * @return artifact version last char index, inclusive + */ + int getVersionEndIndex(String location, int versionStartIndex) { + char[] chars = location.toCharArray(); Review Comment: Same remark as below ########## tooling/camel-karaf-feature-maven-plugin/src/main/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojo.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.camel.karaf.feature.maven; + +import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.felix.utils.version.VersionCleaner; +import org.apache.karaf.features.internal.model.Bundle; +import org.apache.karaf.features.internal.model.Feature; +import org.apache.karaf.features.internal.model.Features; +import org.apache.karaf.features.internal.model.JaxbUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.osgi.framework.Version; + +@Mojo(name = "ensure-wrap-bundle-version", defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +public class EnsureWrapBundleVersionMojo extends AbstractMojo { + + public static final String FILE_PROTOCOL = "file:"; + + public static final String WRAP_PROTOCOL = "wrap:mvn:"; + public static final String BUNDLE_VERSION = "Bundle-Version"; + public static final List<String> HEADERS_AFTER_BUNDLE_VEIRSION = Arrays.asList( + //"Bundle-Version", + "DynamicImport-Package", + "Export-Package", + "Export-Service", + "Fragment-Host", + "Import-Package", + "Import-Service", + "Provide-Capability", + "Require-Bundle", + "Require-Capability"); + + private static final String DEFAULT_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; + private static final String LICENCE_HEADER = """ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +-->"""; + + @Parameter(property = "featuresFilePath", required = true) + private String featuresFilePath; + + @Parameter(property = "targetFeature", required = false) + private String targetFeature = null; + + public String getFeaturesFilePath() { + return featuresFilePath; + } + + public void setFeaturesFilePath(String featuresFilePath) { + this.featuresFilePath = featuresFilePath; + } + + public String getTargetFeature() { + return targetFeature; + } + + public void setTargetFeature(String targetFeature) { + this.targetFeature = targetFeature; + } + + @Override + public void execute() throws MojoExecutionException { + Features featuresData = JaxbUtil.unmarshal(getFeaturesFilePath(), false); + List<Feature> features = featuresData.getFeature(); + + if (getTargetFeature() != null) { Review Comment: The more I think about this feature (the ability to set a target feature), the more I wonder in which case it will be useful in our case. IMHO, it adds extra complexity to the code for limited value. In practice, it should never be used so it could be removed ########## tooling/camel-karaf-feature-maven-plugin/src/test/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojoTest.java: ########## @@ -0,0 +1,142 @@ +/* + * 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.camel.karaf.feature.maven; + + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class EnsureWrapBundleVersionMojoTest { + + private final EnsureWrapBundleVersionMojo ensureVersionMojo = new EnsureWrapBundleVersionMojo(); + + @Test + void modifyLocationTest() throws Exception { + // add bundle version at the end + String location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0"; + String expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$Bundle-Version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge"; + expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Bundle-Version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Export-Package=org.apache.olingo.*;version=5.0.0"; + expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Bundle-Version=5.0.0&Export-Package=org.apache.olingo.*;version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // original version won't work in the karaf container + location = "wrap:mvn:com.google.apis/google-api-services-storage/v1-rev20240209-2.0.0"; + expected = "wrap:mvn:com.google.apis/google-api-services-storage/v1-rev20240209-2.0.0$Bundle-Version=0.0.0.v1-rev20240209-2_0_0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // bundle version header is present but it won't work in the karaf container + location = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + expected = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + ; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // bundle version header is present but it points to the wrong value + location = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + expected = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + ; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + } + + @Test + void getVersionStartIndexTest() { + assertEquals(51, + ensureVersionMojo.getVersionStartIndex("wrap:mvn:org.apache.httpcomponents.core5/httpcore5/5.2.1")); + assertEquals(51, ensureVersionMojo.getVersionStartIndex( + "wrap:mvn:org.eclipse.californium/element-connector/3.11.0$overwrite=merge&Import-Package=net.i2p.crypto.eddsa;resolution:=optional")); + } + + @Test + void getVersionEndIndexTest() { + assertEquals(55, + ensureVersionMojo.getVersionEndIndex("wrap:mvn:org.apache.httpcomponents.core5/httpcore5/5.2.1")); + assertEquals(56, ensureVersionMojo.getVersionEndIndex( + "wrap:mvn:org.eclipse.californium/element-connector/3.11.0$overwrite=merge&Import-Package=net.i2p.crypto.eddsa;resolution:=optional")); + } + + @Test + void getVersionTest() { + assertEquals("${google-oauth-client-version}", ensureVersionMojo.getVersion( + "wrap:mvn:com.google.oauth-client/google-oauth-client-jetty/${google-oauth-client-version}$overwrite=merge&Import-Package=com.sun.net.httpserver;resolution:=optional,*")); Review Comment: This should never happen since the place holders should be resolved at this point ########## tooling/camel-karaf-feature-maven-plugin/src/test/java/org/apache/camel/karaf/feature/maven/EnsureWrapBundleVersionMojoTest.java: ########## @@ -0,0 +1,142 @@ +/* + * 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.camel.karaf.feature.maven; + + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class EnsureWrapBundleVersionMojoTest { + + private final EnsureWrapBundleVersionMojo ensureVersionMojo = new EnsureWrapBundleVersionMojo(); + + @Test + void modifyLocationTest() throws Exception { + // add bundle version at the end + String location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0"; + String expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$Bundle-Version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge"; + expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Bundle-Version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + location = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Export-Package=org.apache.olingo.*;version=5.0.0"; + expected = "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Bundle-Version=5.0.0&Export-Package=org.apache.olingo.*;version=5.0.0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // original version won't work in the karaf container + location = "wrap:mvn:com.google.apis/google-api-services-storage/v1-rev20240209-2.0.0"; + expected = "wrap:mvn:com.google.apis/google-api-services-storage/v1-rev20240209-2.0.0$Bundle-Version=0.0.0.v1-rev20240209-2_0_0"; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // bundle version header is present but it won't work in the karaf container + location = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + expected = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + ; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + + // bundle version header is present but it points to the wrong value + location = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + expected = "mvn:commons-io/commons-io/2.15.1$Bundle-Version=2.15.0"; + ; + assertEquals(expected, ensureVersionMojo.processLocation(location)); + } + + @Test + void getVersionStartIndexTest() { + assertEquals(51, + ensureVersionMojo.getVersionStartIndex("wrap:mvn:org.apache.httpcomponents.core5/httpcore5/5.2.1")); + assertEquals(51, ensureVersionMojo.getVersionStartIndex( + "wrap:mvn:org.eclipse.californium/element-connector/3.11.0$overwrite=merge&Import-Package=net.i2p.crypto.eddsa;resolution:=optional")); + } + + @Test + void getVersionEndIndexTest() { + assertEquals(55, + ensureVersionMojo.getVersionEndIndex("wrap:mvn:org.apache.httpcomponents.core5/httpcore5/5.2.1")); + assertEquals(56, ensureVersionMojo.getVersionEndIndex( + "wrap:mvn:org.eclipse.californium/element-connector/3.11.0$overwrite=merge&Import-Package=net.i2p.crypto.eddsa;resolution:=optional")); + } + + @Test + void getVersionTest() { + assertEquals("${google-oauth-client-version}", ensureVersionMojo.getVersion( + "wrap:mvn:com.google.oauth-client/google-oauth-client-jetty/${google-oauth-client-version}$overwrite=merge&Import-Package=com.sun.net.httpserver;resolution:=optional,*")); + + assertEquals("8.44.0.Final", ensureVersionMojo.getVersion("wrap:mvn:org.kie/kie-api/8.44.0.Final")); + + assertEquals("5.0.0", ensureVersionMojo.getVersion( + "wrap:mvn:org.apache.olingo/odata-server-core/5.0.0$overwrite=merge&Export-Package=org.apache.olingo.*;version=5.0.0")); + + assertEquals("${grpc-version}", + ensureVersionMojo.getVersion("wrap:mvn:io.grpc/grpc-core/${grpc-version}$${spi-provider}")); Review Comment: ditto, this should never happen -- 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