PluginConfigUtil.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2018 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.devops.conga.tooling.maven.plugin.util;

  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.Map;

  24. import org.apache.commons.lang3.StringUtils;
  25. import org.apache.sling.commons.osgi.ManifestHeader;
  26. import org.apache.sling.commons.osgi.ManifestHeader.NameValuePair;

  27. import io.wcm.devops.conga.generator.util.ValueUtil;

  28. /**
  29.  * Helper methos for managing plugin configuration.
  30.  */
  31. public final class PluginConfigUtil {

  32.   private PluginConfigUtil() {
  33.     // static methods only
  34.   }

  35.   /**
  36.    * Convert configuration string in OSGI manifest header style to nested configuration map.
  37.    * @param pluginConfig OSGi manifest header-style config
  38.    * @return Nested configuration map
  39.    */
  40.   public static Map<String, Map<String, Object>> getConfigMap(String pluginConfig) {
  41.     if (StringUtils.isEmpty(pluginConfig)) {
  42.       return Collections.emptyMap();
  43.     }
  44.     Map<String, Map<String, Object>> valueProviderConfig = new HashMap<>();
  45.     ManifestHeader header = ManifestHeader.parse(pluginConfig);
  46.     for (ManifestHeader.Entry entry : header.getEntries()) {
  47.       Map<String, Object> config = new HashMap<>();
  48.       for (NameValuePair nameValue : entry.getAttributes()) {
  49.         config.put(nameValue.getName(), ValueUtil.stringToValue(nameValue.getValue()));
  50.       }
  51.       valueProviderConfig.put(entry.getValue(), Collections.unmodifiableMap(config));
  52.     }
  53.     return Collections.unmodifiableMap(valueProviderConfig);
  54.   }

  55. }