stataru8 commented on code in PR #317: URL: https://github.com/apache/camel-karaf/pull/317#discussion_r1625926436
########## 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: Done 👍 ########## 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: Yes, fixed 👍 -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
