use of org.mule.runtime.globalconfig.api.exception.RuntimeGlobalConfigException in project mule by mulesoft.
the class GlobalConfigLoader method initialiseGlobalConfig.
/**
* Initialise the global config if not yet initialised.
* <p>
* Validates the provided configuration against a JSON schema
*/
private static void initialiseGlobalConfig() {
Config config = ConfigFactory.load(GlobalConfigLoader.class.getClassLoader(), "mule-config", ConfigParseOptions.defaults().setSyntax(JSON), ConfigResolveOptions.defaults());
Config muleRuntimeConfig = config.hasPath(CONFIG_ROOT_ELEMENT_NAME) ? config.getConfig(CONFIG_ROOT_ELEMENT_NAME) : null;
if (muleRuntimeConfig == null) {
mavenConfig = buildNullMavenConfig();
} else {
String effectiveConfigAsJson = muleRuntimeConfig.root().render(ConfigRenderOptions.concise().setJson(true).setComments(false));
String prettyPrintConfig = muleRuntimeConfig.root().render(ConfigRenderOptions.defaults().setComments(true).setJson(true).setFormatted(true));
try (InputStream schemaStream = GlobalConfigLoader.class.getClassLoader().getResourceAsStream(MULE_SCHEMA_JSON_LOCATION)) {
JSONObject rawSchema = new JSONObject(new JSONTokener(schemaStream));
Schema schema = SchemaLoader.load(rawSchema);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Using effective mule-config.json configuration: \n" + prettyPrintConfig);
}
schema.validate(new JSONObject(effectiveConfigAsJson));
Config mavenConfig = muleRuntimeConfig.getConfig("maven");
if (mavenConfig != null) {
GlobalConfigLoader.mavenConfig = buildMavenConfig(mavenConfig);
} else {
GlobalConfigLoader.mavenConfig = buildNullMavenConfig();
}
} catch (ValidationException e) {
LOGGER.info("Mule global config exception. Effective configuration is (config is a merge of MULE_HOME/conf/mule-config.json and system properties): \n " + prettyPrintConfig);
throw new RuntimeGlobalConfigException(e);
} catch (IOException e) {
throw new RuntimeGlobalConfigException(createStaticMessage(format("resources %s missing from the runtime classpath", MULE_SCHEMA_JSON_LOCATION)), e);
}
}
}
use of org.mule.runtime.globalconfig.api.exception.RuntimeGlobalConfigException in project mule by mulesoft.
the class MavenConfigBuilder method buildMavenConfig.
/**
* @param mavenConfig the maven configuration set by the user
* @return a {@link MavenConfiguration} created by using the user configuration and default values set by mule.
*/
public static MavenConfiguration buildMavenConfig(Config mavenConfig) {
try {
String globalSettingsLocation = mavenConfig.hasPath("globalSettingsLocation") ? mavenConfig.getString("globalSettingsLocation") : null;
String userSettingsLocation = mavenConfig.hasPath("userSettingsLocation") ? mavenConfig.getString("userSettingsLocation") : null;
String repositoryLocation = mavenConfig.hasPath("repositoryLocation") ? mavenConfig.getString("repositoryLocation") : null;
boolean ignoreArtifactDescriptorRepositories = mavenConfig.hasPath("ignoreArtifactDescriptorRepositories") ? mavenConfig.getBoolean("ignoreArtifactDescriptorRepositories") : true;
File globalSettingsFile = findResource(globalSettingsLocation);
File userSettingsFile = findResource(userSettingsLocation);
File repositoryFolder = getRuntimeRepositoryFolder();
if (repositoryLocation != null) {
repositoryFolder = new File(repositoryLocation);
if (!repositoryFolder.exists()) {
throw new RuntimeGlobalConfigException(I18nMessageFactory.createStaticMessage(format("Repository folder %s configured for the mule runtime does not exists", repositoryLocation)));
}
}
MavenConfiguration.MavenConfigurationBuilder mavenConfigurationBuilder = MavenConfiguration.newMavenConfigurationBuilder().localMavenRepositoryLocation(repositoryFolder).ignoreArtifactDescriptorRepositories(ignoreArtifactDescriptorRepositories);
if (globalSettingsFile != null) {
mavenConfigurationBuilder.globalSettingsLocation(globalSettingsFile);
}
if (userSettingsFile != null) {
mavenConfigurationBuilder.userSettingsLocation(userSettingsFile);
}
ConfigObject repositories = mavenConfig.hasPath("repositories") ? mavenConfig.getObject("repositories") : null;
if (repositories != null) {
Map<String, Object> repositoriesAsMap = repositories.unwrapped();
repositoriesAsMap.entrySet().stream().sorted(remoteRepositoriesComparator()).forEach((repoEntry) -> {
String repositoryId = repoEntry.getKey();
Map<String, String> repositoryConfig = (Map<String, String>) repoEntry.getValue();
String url = repositoryConfig.get("url");
String username = repositoryConfig.get("username");
String password = repositoryConfig.get("password");
try {
RemoteRepository.RemoteRepositoryBuilder remoteRepositoryBuilder = RemoteRepository.newRemoteRepositoryBuilder().id(repositoryId).url(new URL(url));
if (username != null || password != null) {
Authentication.AuthenticationBuilder authenticationBuilder = Authentication.newAuthenticationBuilder();
if (username != null) {
authenticationBuilder.username(username);
}
if (password != null) {
authenticationBuilder.password(password);
}
remoteRepositoryBuilder.authentication(authenticationBuilder.build());
}
mavenConfigurationBuilder.remoteRepository(remoteRepositoryBuilder.build());
} catch (MalformedURLException e) {
throw new MuleRuntimeException(e);
}
});
}
return mavenConfigurationBuilder.build();
} catch (Exception e) {
if (e instanceof RuntimeGlobalConfigException) {
throw e;
}
throw new RuntimeGlobalConfigException(e);
}
}
Aggregations