-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JsonOsgiConfigPostProcessor to support reading a combined set of …
…OSGi configuration for run modes from .osgiconfig.json files.
- Loading branch information
1 parent
e7b66a8
commit c73c1b1
Showing
10 changed files
with
463 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ain/java/io/wcm/devops/conga/plugins/sling/postprocessor/JsonOsgiConfigPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2024 wcm.io | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
package io.wcm.devops.conga.plugins.sling.postprocessor; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.sling.provisioning.model.Model; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import io.wcm.devops.conga.generator.GeneratorException; | ||
import io.wcm.devops.conga.generator.spi.PostProcessorPlugin; | ||
import io.wcm.devops.conga.generator.spi.context.FileContext; | ||
import io.wcm.devops.conga.generator.spi.context.PostProcessorContext; | ||
import io.wcm.devops.conga.plugins.sling.util.JsonOsgiConfigUtil; | ||
import io.wcm.devops.conga.plugins.sling.util.ProvisioningUtil; | ||
|
||
/** | ||
* Transforms a combined JSON file containing OSGi configurations into individual OSGi configuration files. | ||
*/ | ||
public class JsonOsgiConfigPostProcessor implements PostProcessorPlugin { | ||
|
||
/** | ||
* Plugin name | ||
*/ | ||
public static final String NAME = "sling-json-osgiconfig"; | ||
|
||
/** | ||
* File extension | ||
*/ | ||
public static final String FILE_EXTENSION = ".osgiconfig.json"; | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public boolean accepts(FileContext file, PostProcessorContext context) { | ||
return StringUtils.endsWith(file.getFile().getName(), FILE_EXTENSION); | ||
} | ||
|
||
@Override | ||
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") | ||
public List<FileContext> apply(FileContext fileContext, PostProcessorContext context) { | ||
File file = fileContext.getFile(); | ||
try { | ||
// read JSON file with combined configurations | ||
Model model = JsonOsgiConfigUtil.readToProvisioningModel(file); | ||
|
||
// generate OSGi configurations | ||
List<FileContext> files = ProvisioningUtil.generateOsgiConfigurations(model, file.getParentFile(), context); | ||
|
||
// delete provisioning file after transformation | ||
Files.delete(file.toPath()); | ||
|
||
// return list of generated osgi configuration files | ||
return files; | ||
} | ||
catch (IOException ex) { | ||
throw new GeneratorException("Unable to parse JSON file with OSGi configurations.", ex); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...sling-plugin/src/main/java/io/wcm/devops/conga/plugins/sling/util/JsonOsgiConfigUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2024 wcm.io | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
package io.wcm.devops.conga.plugins.sling.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collection; | ||
import java.util.Dictionary; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.sling.provisioning.model.Configuration; | ||
import org.apache.sling.provisioning.model.Feature; | ||
import org.apache.sling.provisioning.model.Model; | ||
import org.apache.sling.provisioning.model.RunMode; | ||
import org.apache.sling.provisioning.model.Section; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.wcm.devops.conga.plugins.sling.postprocessor.JsonOsgiConfigPostProcessor; | ||
|
||
/** | ||
* Helper class for reading JSON files. | ||
*/ | ||
public final class JsonOsgiConfigUtil { | ||
|
||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<Map<String, Object>>() { | ||
// default implementation | ||
}; | ||
|
||
private static final Pattern KEY_PATTERN_CONFIGURATIONS = Pattern.compile("^configurations(:(.*))?$"); | ||
private static final Pattern KEY_PATTERN_REPOINIT = Pattern.compile("^repoinit(:(.*))?$"); | ||
private static final int RUNMODES_INDEX = 2; | ||
|
||
private JsonOsgiConfigUtil() { | ||
// static methods only | ||
} | ||
|
||
/** | ||
* Read JSON file content to a map. | ||
* @param file JSON file | ||
* @return Map containing JSON content | ||
* @throws IOException I/O exception | ||
*/ | ||
static Map<String, Object> readToMap(File file) throws IOException { | ||
String jsonString = FileUtils.readFileToString(file, StandardCharsets.UTF_8); | ||
return OBJECT_MAPPER.readValue(jsonString, MAP_TYPE_REFERENCE); | ||
} | ||
|
||
/** | ||
* Read JSON file content to a map. | ||
* @param file JSON file | ||
* @return Map containing JSON content | ||
* @throws IOException I/O exception | ||
*/ | ||
public static Model readToProvisioningModel(File file) throws IOException { | ||
Model model = new Model(); | ||
String featureName = StringUtils.substringBeforeLast(file.getName(), JsonOsgiConfigPostProcessor.FILE_EXTENSION); | ||
Feature feature = model.getOrCreateFeature(featureName); | ||
|
||
Map<String, Object> data = readToMap(file); | ||
for (Map.Entry<String, Object> entry : data.entrySet()) { | ||
processEntry(feature, entry.getKey(), entry.getValue()); | ||
} | ||
|
||
return model; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static void processEntry(Feature feature, String key, Object value) throws IOException { | ||
Matcher configurationsKeyMatcher = KEY_PATTERN_CONFIGURATIONS.matcher(key); | ||
if (configurationsKeyMatcher.matches()) { | ||
if (value instanceof Map) { | ||
String[] runModes = toRunModes(configurationsKeyMatcher.group(RUNMODES_INDEX)); | ||
processOsgiConfiguration(feature, runModes, (Map<String, Object>)value); | ||
} | ||
else { | ||
throw new IOException("Unexpected data for key " + key + ": " + value.getClass().getName()); | ||
} | ||
} | ||
else { | ||
Matcher repoinitKeyMatcher = KEY_PATTERN_REPOINIT.matcher(key); | ||
if (repoinitKeyMatcher.matches()) { | ||
if (value instanceof Collection) { | ||
String[] runModes = toRunModes(repoinitKeyMatcher.group(RUNMODES_INDEX)); | ||
processRepoInit(feature, runModes, (Collection<String>)value); | ||
} | ||
else { | ||
throw new IOException("Unexpected data for key " + key + ": " + value.getClass().getName()); | ||
} | ||
} | ||
else { | ||
throw new IOException("Invalid toplevel key in JSON file: " + key); | ||
} | ||
} | ||
} | ||
|
||
private static String[] toRunModes(String runModesString) { | ||
if (StringUtils.isBlank(runModesString)) { | ||
return null; | ||
} | ||
return StringUtils.split(runModesString, ","); | ||
} | ||
|
||
|
||
@SuppressWarnings("unchecked") | ||
private static void processOsgiConfiguration(Feature feature, String[] runModes, Map<String, Object> configurations) throws IOException { | ||
RunMode runMode = feature.getOrCreateRunMode(runModes); | ||
for (Map.Entry<String, Object> entry : configurations.entrySet()) { | ||
String pid = entry.getKey(); | ||
Object value = entry.getValue(); | ||
if (value instanceof Map) { | ||
Map<String, Object> configProperties = (Map<String, Object>)value; | ||
Configuration config = runMode.getOrCreateConfiguration(pid, null); | ||
Dictionary<String, Object> properties = config.getProperties(); | ||
for (Map.Entry<String, Object> configProperty : configProperties.entrySet()) { | ||
properties.put(configProperty.getKey(), configProperty.getValue()); | ||
} | ||
} | ||
else { | ||
throw new IOException("Unexpected configurations data for " + pid + ": " + value.getClass().getName()); | ||
} | ||
} | ||
} | ||
|
||
private static void processRepoInit(Feature feature, String[] runModes, Collection<String> repoinits) { | ||
Section section = new Section(ProvisioningUtil.REPOINIT_SECTION); | ||
feature.getAdditionalSections().add(section); | ||
if (runModes != null) { | ||
section.getAttributes().put(ProvisioningUtil.REPOINIT_PROPERTY_RUNMODES, StringUtils.join(runModes, ",")); | ||
} | ||
section.setContents(StringUtils.join(repoinits, "\n")); | ||
} | ||
|
||
} |
Oops, something went wrong.