Search in sources :

Example 1 with ConfigurationRegistryReader

use of org.apache.archiva.configuration.io.registry.ConfigurationRegistryReader in project archiva by apache.

the class DefaultArchivaConfiguration method load.

@SuppressWarnings("unchecked")
private Configuration load() {
    // TODO: should this be the same as section? make sure unnamed sections still work (eg, sys properties)
    Registry subset = registry.getSubset(KEY);
    if (subset.getString("version") == null) {
        if (subset.getSubset("repositoryScanning").isEmpty()) {
            // only for empty
            subset = readDefaultConfiguration();
        } else {
            throw new RuntimeException("No version tag found in configuration. Archiva configuration version 1.x is not longer supported.");
        }
    }
    Configuration config = new ConfigurationRegistryReader().read(subset);
    // Resolving data and repositories directories
    // If the config entries are absolute, the path is used as it is
    // if the config entries are empty, they are resolved:
    // dataDirectory = ${appserver.base}/data
    // repositoryDirectory = ${dataDirectory}/repositories
    // If the entries are relative they are resolved
    // relative to the appserver.base, for dataDirectory
    // relative to dataDirectory for repositoryBase
    String dataDir = config.getArchivaRuntimeConfiguration().getDataDirectory();
    if (StringUtils.isEmpty(dataDir)) {
        dataDirectory = getAppServerBaseDir().resolve("data");
    } else {
        Path tmpDataDir = Paths.get(dataDir);
        if (tmpDataDir.isAbsolute()) {
            dataDirectory = tmpDataDir;
        } else {
            dataDirectory = getAppServerBaseDir().resolve(tmpDataDir);
        }
    }
    config.getArchivaRuntimeConfiguration().setDataDirectory(dataDirectory.normalize().toString());
    String repoBaseDir = config.getArchivaRuntimeConfiguration().getRepositoryBaseDirectory();
    if (StringUtils.isEmpty(repoBaseDir)) {
        repositoryBaseDirectory = dataDirectory.resolve("repositories");
    } else {
        Path tmpRepoBaseDir = Paths.get(repoBaseDir);
        if (tmpRepoBaseDir.isAbsolute()) {
            repositoryBaseDirectory = tmpRepoBaseDir;
        } else {
            dataDirectory.resolve(tmpRepoBaseDir);
        }
    }
    String remoteRepoBaseDir = config.getArchivaRuntimeConfiguration().getRemoteRepositoryBaseDirectory();
    if (StringUtils.isEmpty(remoteRepoBaseDir)) {
        remoteRepositoryBaseDirectory = dataDirectory.resolve("remotes");
    } else {
        Path tmpRemoteRepoDir = Paths.get(remoteRepoBaseDir);
        if (tmpRemoteRepoDir.isAbsolute()) {
            remoteRepositoryBaseDirectory = tmpRemoteRepoDir;
        } else {
            dataDirectory.resolve(tmpRemoteRepoDir);
        }
    }
    config.getRepositoryGroups();
    config.getRepositoryGroupsAsMap();
    if (!CollectionUtils.isEmpty(config.getRemoteRepositories())) {
        List<RemoteRepositoryConfiguration> remoteRepos = config.getRemoteRepositories();
        for (RemoteRepositoryConfiguration repo : remoteRepos) {
            // [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
            if (StringUtils.isBlank(repo.getUsername())) {
                repo.setUsername(null);
            }
            if (StringUtils.isBlank(repo.getPassword())) {
                repo.setPassword(null);
            }
        }
    }
    if (!config.getProxyConnectors().isEmpty()) {
        // Fix Proxy Connector Settings.
        // Create a copy of the list to read from (to prevent concurrent modification exceptions)
        List<ProxyConnectorConfiguration> proxyConnectorList = new ArrayList<>(config.getProxyConnectors());
        // Remove the old connector list.
        config.getProxyConnectors().clear();
        for (ProxyConnectorConfiguration connector : proxyConnectorList) {
            // Fix policies
            boolean connectorValid = true;
            Map<String, String> policies = new HashMap<>();
            // Make copy of policies
            policies.putAll(connector.getPolicies());
            // Clear out policies
            connector.getPolicies().clear();
            // Work thru policies. cleaning them up.
            for (Entry<String, String> entry : policies.entrySet()) {
                String policyId = entry.getKey();
                String setting = entry.getValue();
                // Upgrade old policy settings.
                if ("releases".equals(policyId) || "snapshots".equals(policyId)) {
                    if ("ignored".equals(setting)) {
                        setting = AbstractUpdatePolicy.ALWAYS;
                    } else if ("disabled".equals(setting)) {
                        setting = AbstractUpdatePolicy.NEVER;
                    }
                } else if ("cache-failures".equals(policyId)) {
                    if ("ignored".equals(setting)) {
                        setting = CachedFailuresPolicy.NO;
                    } else if ("cached".equals(setting)) {
                        setting = CachedFailuresPolicy.YES;
                    }
                } else if ("checksum".equals(policyId)) {
                    if ("ignored".equals(setting)) {
                        setting = ChecksumPolicy.IGNORE;
                    }
                }
                // Validate existance of policy key.
                if (policyExists(policyId)) {
                    Policy policy = findPolicy(policyId);
                    // Does option exist?
                    if (!policy.getOptions().contains(setting)) {
                        setting = policy.getDefaultOption();
                    }
                    connector.addPolicy(policyId, setting);
                } else {
                    // Policy key doesn't exist. Don't add it to golden version.
                    log.warn("Policy [{}] does not exist.", policyId);
                }
            }
            if (connectorValid) {
                config.addProxyConnector(connector);
            }
        }
        // Normalize the order fields in the proxy connectors.
        Map<String, java.util.List<ProxyConnectorConfiguration>> proxyConnectorMap = config.getProxyConnectorAsMap();
        for (List<ProxyConnectorConfiguration> connectors : proxyConnectorMap.values()) {
            // Sort connectors by order field.
            Collections.sort(connectors, ProxyConnectorConfigurationOrderComparator.getInstance());
            // Normalize the order field values.
            int order = 1;
            for (ProxyConnectorConfiguration connector : connectors) {
                connector.setOrder(order++);
            }
        }
    }
    this.defaultLocale = Locale.forLanguageTag(config.getArchivaRuntimeConfiguration().getDefaultLanguage());
    this.languagePriorities = Locale.LanguageRange.parse(config.getArchivaRuntimeConfiguration().getLanguageRange());
    return config;
}
Also used : Path(java.nio.file.Path) PreDownloadPolicy(org.apache.archiva.policies.PreDownloadPolicy) AbstractUpdatePolicy(org.apache.archiva.policies.AbstractUpdatePolicy) DownloadErrorPolicy(org.apache.archiva.policies.DownloadErrorPolicy) ChecksumPolicy(org.apache.archiva.policies.ChecksumPolicy) Policy(org.apache.archiva.policies.Policy) CachedFailuresPolicy(org.apache.archiva.policies.CachedFailuresPolicy) PostDownloadPolicy(org.apache.archiva.policies.PostDownloadPolicy) BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) ConfigurationRegistryReader(org.apache.archiva.configuration.io.registry.ConfigurationRegistryReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Registry(org.apache.archiva.redback.components.registry.Registry) CommonsConfigurationRegistry(org.apache.archiva.redback.components.registry.commons.CommonsConfigurationRegistry) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ConfigurationRegistryReader

use of org.apache.archiva.configuration.io.registry.ConfigurationRegistryReader in project archiva by apache.

the class FileTypes method initialize.

@PostConstruct
public void initialize() {
    // TODO: why is this done by hand?
    // TODO: ideally, this would be instantiated by configuration instead, and not need to be a component
    String errMsg = "Unable to load default archiva configuration for FileTypes: ";
    try {
        CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
        // Configure commonsRegistry
        Field fld = commonsRegistry.getClass().getDeclaredField("configuration");
        fld.setAccessible(true);
        fld.set(commonsRegistry, new CombinedConfiguration());
        commonsRegistry.addConfigurationFromResource("org/apache/archiva/configuration/default-archiva.xml");
        // Read configuration as it was intended.
        ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
        Configuration defaultConfig = configReader.read(commonsRegistry);
        initialiseTypeMap(defaultConfig);
    } catch (RegistryException e) {
        throw new RuntimeException(errMsg + e.getMessage(), e);
    } catch (SecurityException e) {
        throw new RuntimeException(errMsg + e.getMessage(), e);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(errMsg + e.getMessage(), e);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(errMsg + e.getMessage(), e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(errMsg + e.getMessage(), e);
    }
    this.archivaConfiguration.addChangeListener(this);
}
Also used : Field(java.lang.reflect.Field) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) CommonsConfigurationRegistry(org.apache.archiva.redback.components.registry.commons.CommonsConfigurationRegistry) ConfigurationRegistryReader(org.apache.archiva.configuration.io.registry.ConfigurationRegistryReader) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) RegistryException(org.apache.archiva.redback.components.registry.RegistryException) PostConstruct(javax.annotation.PostConstruct)

Aggregations

ConfigurationRegistryReader (org.apache.archiva.configuration.io.registry.ConfigurationRegistryReader)2 CommonsConfigurationRegistry (org.apache.archiva.redback.components.registry.commons.CommonsConfigurationRegistry)2 Field (java.lang.reflect.Field)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 PostConstruct (javax.annotation.PostConstruct)1 AbstractUpdatePolicy (org.apache.archiva.policies.AbstractUpdatePolicy)1 CachedFailuresPolicy (org.apache.archiva.policies.CachedFailuresPolicy)1 ChecksumPolicy (org.apache.archiva.policies.ChecksumPolicy)1 DownloadErrorPolicy (org.apache.archiva.policies.DownloadErrorPolicy)1 Policy (org.apache.archiva.policies.Policy)1 PostDownloadPolicy (org.apache.archiva.policies.PostDownloadPolicy)1 PreDownloadPolicy (org.apache.archiva.policies.PreDownloadPolicy)1 Registry (org.apache.archiva.redback.components.registry.Registry)1 RegistryException (org.apache.archiva.redback.components.registry.RegistryException)1 BaseConfiguration (org.apache.commons.configuration.BaseConfiguration)1 CombinedConfiguration (org.apache.commons.configuration.CombinedConfiguration)1