diff --git a/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/utils/Constants.java b/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/utils/Constants.java index 3a22eea641..9a71596c1c 100644 --- a/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/utils/Constants.java +++ b/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/utils/Constants.java @@ -25,12 +25,18 @@ public class Constants { public static final String MI_HOST = "MI_HOST"; public static final String MI_PORT = "MI_PORT"; public static final String MI_URL = "MI_URL"; + public static final String MI_GROUP_ID = "MI_GROUP_ID"; public static final String HOST = "{MI_HOST}"; public static final String PORT = "{MI_PORT}"; public static final String URL = "{MI_URL}"; + public static final String GROUP_ID = "{MI_GROUP_ID}"; + public static final String GROUP_ID_REGEX = "\\{MI_GROUP_ID\\}"; public static final String SERVICE_URL = "serviceUrl"; public static final String SERVICE_KEY = "serviceKey"; public static final String METADATA_KEY = "key"; + public static final String METADATA_NAME = "name"; + public static final String METADATA_DISPLAY_NAME = "displayName"; + public static final String METADATA_DESCRIPTION = "description"; public static final String MD5 = "md5"; public static final String VERIFIER = "verifier"; public static final String LIST_STRING = "list"; diff --git a/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/utils/ServiceCatalogUtils.java b/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/utils/ServiceCatalogUtils.java index bc81f3fea0..9174ccd42c 100644 --- a/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/utils/ServiceCatalogUtils.java +++ b/components/org.wso2.micro.integrator.initializer/src/main/java/org/wso2/micro/integrator/initializer/utils/ServiceCatalogUtils.java @@ -78,6 +78,7 @@ public class ServiceCatalogUtils { private static List md5List = new ArrayList<>(); private static Boolean alreadyUploaded = false; private static String resolvedHostName; + private static String resolvedGroupId; private static String httpListenerPort; private static String httpsListenerPort; private static String resolvedUrl; @@ -161,6 +162,39 @@ private static String updateServiceUrl(String currentUrl) throws ResolverExcepti return currentUrl; } + /** + * Dynamically resolve the {MI_GROUP_ID} placeholder in the metadata file using env variables. + * @param yaml metadata file. + */ + private static void replaceGroupIdPlaceholder(Map yaml) { + SystemResolver resolver = new SystemResolver(); + if (resolvedGroupId == null) { + try { + resolver.setVariable(MI_GROUP_ID); + resolvedGroupId = resolver.resolve(); + } catch (ResolverException e) { + // if the env variable is not set, use the existing value + return; + } + } + String key = (String) yaml.get(METADATA_KEY); + if (key.contains(GROUP_ID)) { + yaml.put(METADATA_KEY, key.replace(GROUP_ID, resolvedGroupId)); + } + String name = (String) yaml.get(METADATA_NAME); + if (name.contains(GROUP_ID)) { + yaml.put(METADATA_NAME, name.replace(GROUP_ID, resolvedGroupId)); + } + String displayName = (String) yaml.get(METADATA_DISPLAY_NAME); + if (displayName.contains(GROUP_ID)) { + yaml.put(METADATA_DISPLAY_NAME, displayName.replace(GROUP_ID, resolvedGroupId)); + } + String description = (String) yaml.get(METADATA_DESCRIPTION); + if (description.contains(GROUP_ID)) { + yaml.put(METADATA_DESCRIPTION, description.replaceAll(GROUP_ID_REGEX, resolvedGroupId)); + } + } + /** * Update the serviceUrl of the given metadata file (if required) and return its key value. * @@ -175,6 +209,7 @@ public static String updateMetadataWithServiceUrl(File yamlFile) throws IOExcept Map obj = (Map) yaml.load(yamlStream); String currentServiceUrl = (String) obj.get(SERVICE_URL); obj.put(SERVICE_URL, updateServiceUrl(currentServiceUrl)); + replaceGroupIdPlaceholder(obj); // Additional configurations DumperOptions options = new DumperOptions();