Search in sources :

Example 1 with RepositoryPolicy

use of org.apache.maven.settings.RepositoryPolicy in project karaf by apache.

the class RepositoryChangeCommand method edit.

@Override
protected void edit(String prefix, Dictionary<String, Object> config, MavenRepositoryURL[] allRepos, MavenRepositoryURL[] pidRepos, MavenRepositoryURL[] settingsRepos) throws Exception {
    Optional<MavenRepositoryURL> first = Arrays.stream(allRepos).filter((repo) -> id.equals(repo.getId())).findFirst();
    if (!first.isPresent()) {
        System.err.printf("Can't find %s repository with ID \"%s\"\n", (defaultRepository ? "default" : "remote"), id);
        return;
    }
    MavenRepositoryURL changedRepository = first.get();
    changedRepository.setSnapshotsEnabled(snapshots);
    changedRepository.setReleasesEnabled(!noReleases);
    if (updatePolicy != null) {
        SourceAnd<String> up = updatePolicy(updatePolicy);
        if (!up.valid) {
            System.err.println("Unknown value of update policy: \"" + updatePolicy + "\"");
            return;
        }
        changedRepository.setReleasesUpdatePolicy(up.val());
        changedRepository.setSnapshotsUpdatePolicy(up.val());
    }
    if (checksumPolicy != null) {
        SourceAnd<String> cp = checksumPolicy(checksumPolicy);
        if (!cp.valid) {
            System.err.println("Unknown value of checksum policy: \"" + checksumPolicy + "\"");
            return;
        }
        changedRepository.setReleasesChecksumPolicy(cp.val());
        changedRepository.setSnapshotsChecksumPolicy(cp.val());
    }
    if (uri != null) {
        SourceAnd<String> urlResolved = validateRepositoryURL(uri, defaultRepository);
        if (!urlResolved.valid) {
            return;
        }
        changedRepository.setURL(new URL(urlResolved.val()));
    }
    boolean hasUsername = username != null && !"".equals(username.trim());
    boolean hasPassword = password != null && !"".equals(password.trim());
    boolean hasCredentials = hasUsername && hasPassword;
    if ((hasUsername && !hasPassword) || (!hasUsername && hasPassword)) {
        System.err.println("Please specify both username and password");
        return;
    }
    if (defaultRepository && hasCredentials) {
        System.out.println("User credentials won't be used for default repository");
    // no return
    }
    boolean credentialsUpdated = false;
    // credentials for remote repository can be stored only in settings.xml
    if (!defaultRepository && hasCredentials) {
        if (!updateCredentials(force, id, username, password, prefix, config)) {
            return;
        }
        credentialsUpdated = true;
    }
    if (!defaultRepository && changedRepository.getFrom() == MavenRepositoryURL.FROM.SETTINGS) {
        // find <repository> in any active profile to change it
        for (Profile profile : mavenSettings.getProfiles()) {
            Optional<Repository> repository = profile.getRepositories().stream().filter((r) -> id.equals(r.getId())).findFirst();
            if (repository.isPresent()) {
                // I can't imagine it's not...
                Repository r = repository.get();
                r.setUrl(changedRepository.getURL().toString());
                if (!changedRepository.isSnapshotsEnabled()) {
                    r.setSnapshots(new RepositoryPolicy());
                    r.getSnapshots().setEnabled(false);
                } else {
                    RepositoryPolicy rp = r.getSnapshots() == null ? new RepositoryPolicy() : r.getSnapshots();
                    rp.setEnabled(true);
                    if (checksumPolicy != null) {
                        rp.setChecksumPolicy(changedRepository.getSnapshotsChecksumPolicy());
                    } else if (rp.getChecksumPolicy() == null) {
                        rp.setChecksumPolicy("warn");
                    }
                    if (updatePolicy != null) {
                        rp.setUpdatePolicy(changedRepository.getSnapshotsUpdatePolicy());
                    } else if (rp.getUpdatePolicy() == null) {
                        rp.setUpdatePolicy("daily");
                    }
                    r.setSnapshots(rp);
                }
                if (!changedRepository.isReleasesEnabled()) {
                    r.setReleases(new RepositoryPolicy());
                    r.getReleases().setEnabled(false);
                } else {
                    RepositoryPolicy rp = r.getReleases() == null ? new RepositoryPolicy() : r.getReleases();
                    rp.setEnabled(true);
                    if (checksumPolicy != null) {
                        rp.setChecksumPolicy(changedRepository.getReleasesChecksumPolicy());
                    } else if (rp.getChecksumPolicy() == null) {
                        rp.setChecksumPolicy("warn");
                    }
                    if (updatePolicy != null) {
                        rp.setUpdatePolicy(changedRepository.getReleasesUpdatePolicy());
                    } else if (rp.getUpdatePolicy() == null) {
                        rp.setUpdatePolicy("daily");
                    }
                    r.setReleases(rp);
                }
                updateSettings(prefix, config);
                break;
            }
        }
    } else if (changedRepository.getFrom() == MavenRepositoryURL.FROM.PID) {
        List<MavenRepositoryURL> newRepos = new LinkedList<>();
        for (MavenRepositoryURL repo : pidRepos) {
            MavenRepositoryURL _r = repo;
            if (id.equals(repo.getId())) {
                _r = changedRepository;
            }
            newRepos.add(_r);
        }
        updatePidRepositories(prefix, config, defaultRepository, newRepos, settingsRepos.length > 0);
        if (credentialsUpdated) {
            updateSettings(prefix, config);
        }
    }
    Configuration cmConfig = cm.getConfiguration(PID);
    cmConfig.update(config);
    success = true;
}
Also used : Arrays(java.util.Arrays) Repository(org.apache.maven.settings.Repository) URL(java.net.URL) Argument(org.apache.karaf.shell.api.action.Argument) Command(org.apache.karaf.shell.api.action.Command) List(java.util.List) MavenRepositoryURL(org.apache.karaf.maven.core.MavenRepositoryURL) Configuration(org.osgi.service.cm.Configuration) Profile(org.apache.maven.settings.Profile) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Optional(java.util.Optional) RepositoryPolicy(org.apache.maven.settings.RepositoryPolicy) LinkedList(java.util.LinkedList) Option(org.apache.karaf.shell.api.action.Option) Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) RepositoryPolicy(org.apache.maven.settings.RepositoryPolicy) URL(java.net.URL) MavenRepositoryURL(org.apache.karaf.maven.core.MavenRepositoryURL) Profile(org.apache.maven.settings.Profile) Repository(org.apache.maven.settings.Repository) List(java.util.List) LinkedList(java.util.LinkedList) MavenRepositoryURL(org.apache.karaf.maven.core.MavenRepositoryURL)

Aggregations

URL (java.net.URL)1 Arrays (java.util.Arrays)1 Dictionary (java.util.Dictionary)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Optional (java.util.Optional)1 MavenRepositoryURL (org.apache.karaf.maven.core.MavenRepositoryURL)1 Argument (org.apache.karaf.shell.api.action.Argument)1 Command (org.apache.karaf.shell.api.action.Command)1 Option (org.apache.karaf.shell.api.action.Option)1 Service (org.apache.karaf.shell.api.action.lifecycle.Service)1 Profile (org.apache.maven.settings.Profile)1 Repository (org.apache.maven.settings.Repository)1 RepositoryPolicy (org.apache.maven.settings.RepositoryPolicy)1 Configuration (org.osgi.service.cm.Configuration)1