Search in sources :

Example 1 with RuntimeGlobalConfigException

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);
        }
    }
}
Also used : JSONTokener(org.json.JSONTokener) RuntimeGlobalConfigException(org.mule.runtime.globalconfig.api.exception.RuntimeGlobalConfigException) ValidationException(org.everit.json.schema.ValidationException) JSONObject(org.json.JSONObject) Config(com.typesafe.config.Config) MavenConfigBuilder.buildMavenConfig(org.mule.runtime.globalconfig.internal.MavenConfigBuilder.buildMavenConfig) MavenConfigBuilder.buildNullMavenConfig(org.mule.runtime.globalconfig.internal.MavenConfigBuilder.buildNullMavenConfig) InputStream(java.io.InputStream) Schema(org.everit.json.schema.Schema) IOException(java.io.IOException)

Example 2 with RuntimeGlobalConfigException

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);
    }
}
Also used : MavenConfiguration(org.mule.maven.client.api.model.MavenConfiguration) MalformedURLException(java.net.MalformedURLException) RemoteRepository(org.mule.maven.client.api.model.RemoteRepository) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) RuntimeGlobalConfigException(org.mule.runtime.globalconfig.api.exception.RuntimeGlobalConfigException) RuntimeGlobalConfigException(org.mule.runtime.globalconfig.api.exception.RuntimeGlobalConfigException) Authentication(org.mule.maven.client.api.model.Authentication) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConfigObject(com.typesafe.config.ConfigObject) ConfigObject(com.typesafe.config.ConfigObject) File(java.io.File) Map(java.util.Map)

Aggregations

RuntimeGlobalConfigException (org.mule.runtime.globalconfig.api.exception.RuntimeGlobalConfigException)2 Config (com.typesafe.config.Config)1 ConfigObject (com.typesafe.config.ConfigObject)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Map (java.util.Map)1 Schema (org.everit.json.schema.Schema)1 ValidationException (org.everit.json.schema.ValidationException)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1 Authentication (org.mule.maven.client.api.model.Authentication)1 MavenConfiguration (org.mule.maven.client.api.model.MavenConfiguration)1 RemoteRepository (org.mule.maven.client.api.model.RemoteRepository)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 MavenConfigBuilder.buildMavenConfig (org.mule.runtime.globalconfig.internal.MavenConfigBuilder.buildMavenConfig)1 MavenConfigBuilder.buildNullMavenConfig (org.mule.runtime.globalconfig.internal.MavenConfigBuilder.buildNullMavenConfig)1