use of org.apache.archiva.redback.components.registry.Registry in project archiva by apache.
the class DefaultArchivaConfiguration method handleUpgradeConfiguration.
/**
* Handle upgrade to newer version
*/
private void handleUpgradeConfiguration() throws RegistryException, IndeterminateConfigurationException {
List<String> dbConsumers = Arrays.asList("update-db-artifact", "update-db-repository-metadata");
// remove database consumers if here
List<String> intersec = ListUtils.intersection(dbConsumers, configuration.getRepositoryScanning().getKnownContentConsumers());
if (!intersec.isEmpty()) {
List<String> knowContentConsumers = new ArrayList<>(configuration.getRepositoryScanning().getKnownContentConsumers().size());
for (String knowContentConsumer : configuration.getRepositoryScanning().getKnownContentConsumers()) {
if (!dbConsumers.contains(knowContentConsumer)) {
knowContentConsumers.add(knowContentConsumer);
}
}
configuration.getRepositoryScanning().setKnownContentConsumers(knowContentConsumers);
}
// ensure create-archiva-metadata is here
if (!configuration.getRepositoryScanning().getKnownContentConsumers().contains("create-archiva-metadata")) {
List<String> knowContentConsumers = new ArrayList<>(configuration.getRepositoryScanning().getKnownContentConsumers());
knowContentConsumers.add("create-archiva-metadata");
configuration.getRepositoryScanning().setKnownContentConsumers(knowContentConsumers);
}
// ensure duplicate-artifacts is here
if (!configuration.getRepositoryScanning().getKnownContentConsumers().contains("duplicate-artifacts")) {
List<String> knowContentConsumers = new ArrayList<>(configuration.getRepositoryScanning().getKnownContentConsumers());
knowContentConsumers.add("duplicate-artifacts");
configuration.getRepositoryScanning().setKnownContentConsumers(knowContentConsumers);
}
Registry defaultOnlyConfiguration = readDefaultOnlyConfiguration();
// Currently we check only for configuration version change, not certain version numbers.
if (hasConfigVersionChanged(configuration, defaultOnlyConfiguration)) {
updateCheckPathDefaults(configuration, defaultOnlyConfiguration);
String newVersion = defaultOnlyConfiguration.getString("version");
if (newVersion == null) {
throw new IndeterminateConfigurationException("The default configuration has no version information!");
}
configuration.setVersion(newVersion);
try {
save(configuration);
} catch (IndeterminateConfigurationException e) {
log.error("Error occured during configuration update to new version: {}", e.getMessage());
} catch (RegistryException e) {
log.error("Error occured during configuration update to new version: {}", e.getMessage());
}
}
}
use of org.apache.archiva.redback.components.registry.Registry in project archiva by apache.
the class DefaultArchivaConfiguration method createDefaultConfigurationFile.
private Registry createDefaultConfigurationFile() throws RegistryException {
// TODO: may not be needed under commons-configuration 1.4 - check
String contents = "<configuration />";
String fileLocation = userConfigFilename;
if (!writeFile("user configuration", userConfigFilename, contents)) {
fileLocation = altConfigFilename;
if (!writeFile("alternative configuration", altConfigFilename, contents)) {
throw new RegistryException("Unable to create configuration file in either user [" + userConfigFilename + "] or alternative [" + altConfigFilename + "] locations on disk, usually happens when not allowed to write to those locations.");
}
}
// olamy hackish I know :-)
contents = "<configuration><xml fileName=\"" + fileLocation + "\" config-forceCreate=\"true\" config-name=\"org.apache.archiva.user\"/>" + "</configuration>";
((CommonsConfigurationRegistry) registry).setProperties(contents);
registry.initialize();
for (RegistryListener regListener : registryListeners) {
addRegistryChangeListener(regListener);
}
triggerEvent(ConfigurationEvent.SAVED);
Registry section = registry.getSection(KEY + ".user");
return section == null ? new CommonsConfigurationRegistry(new BaseConfiguration()) : section;
}
use of org.apache.archiva.redback.components.registry.Registry in project archiva by apache.
the class DefaultArchivaConfiguration method removeChangeListener.
@Override
public void removeChangeListener(RegistryListener listener) {
boolean removed = registryListeners.remove(listener);
log.debug("RegistryListener: '{}' removed {}", listener, removed);
Registry section = registry.getSection(KEY + ".user");
if (section != null) {
section.removeChangeListener(listener);
}
section = registry.getSection(KEY + ".base");
if (section != null) {
section.removeChangeListener(listener);
}
}
use of org.apache.archiva.redback.components.registry.Registry 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;
}
use of org.apache.archiva.redback.components.registry.Registry in project archiva by apache.
the class DefaultArchivaConfiguration method save.
@SuppressWarnings("unchecked")
@Override
public synchronized void save(Configuration configuration) throws IndeterminateConfigurationException, RegistryException {
Registry section = registry.getSection(KEY + ".user");
Registry baseSection = registry.getSection(KEY + ".base");
if (section == null) {
section = baseSection;
if (section == null) {
section = createDefaultConfigurationFile();
}
} else if (baseSection != null) {
Collection<String> keys = baseSection.getKeys();
boolean foundList = false;
for (Iterator<String> i = keys.iterator(); i.hasNext() && !foundList; ) {
String key = i.next();
// that configuration
if (//
key.startsWith("repositories") || //
key.startsWith("proxyConnectors") || //
key.startsWith("networkProxies") || //
key.startsWith("repositoryScanning") || //
key.startsWith("remoteRepositories") || //
key.startsWith("managedRepositories") || //
key.startsWith("repositoryGroups")) {
foundList = true;
}
}
if (foundList) {
this.configuration = null;
throw new IndeterminateConfigurationException("Configuration can not be saved when it is loaded from two sources");
}
}
// escape all cron expressions to handle ','
escapeCronExpressions(configuration);
// [MRM-661] Due to a bug in the modello registry writer, we need to take these out by hand. They'll be put back by the writer.
if (section != null) {
if (configuration.getManagedRepositories().isEmpty()) {
section.removeSubset("managedRepositories");
}
if (configuration.getRemoteRepositories().isEmpty()) {
section.removeSubset("remoteRepositories");
}
if (configuration.getProxyConnectors().isEmpty()) {
section.removeSubset("proxyConnectors");
}
if (configuration.getNetworkProxies().isEmpty()) {
section.removeSubset("networkProxies");
}
if (configuration.getLegacyArtifactPaths().isEmpty()) {
section.removeSubset("legacyArtifactPaths");
}
if (configuration.getRepositoryGroups().isEmpty()) {
section.removeSubset("repositoryGroups");
}
if (configuration.getRepositoryScanning() != null) {
if (configuration.getRepositoryScanning().getKnownContentConsumers().isEmpty()) {
section.removeSubset("repositoryScanning.knownContentConsumers");
}
if (configuration.getRepositoryScanning().getInvalidContentConsumers().isEmpty()) {
section.removeSubset("repositoryScanning.invalidContentConsumers");
}
}
if (configuration.getArchivaRuntimeConfiguration() != null) {
section.removeSubset("archivaRuntimeConfiguration.defaultCheckPaths");
}
new ConfigurationRegistryWriter().write(configuration, section);
section.save();
}
this.configuration = unescapeExpressions(configuration);
isConfigurationDefaulted = false;
triggerEvent(ConfigurationEvent.SAVED);
}
Aggregations