Search in sources :

Example 6 with MavenRepositoryURL

use of org.apache.karaf.maven.core.MavenRepositoryURL in project karaf by apache.

the class RepositoryAddCommand method edit.

@Override
protected void edit(String prefix, Dictionary<String, Object> config, MavenRepositoryURL[] allRepos, MavenRepositoryURL[] pidRepos, MavenRepositoryURL[] settingsRepos) throws Exception {
    if (idx > pidRepos.length) {
        // TOCONSIDER: should we allow to add repository to settings.xml too?
        System.err.printf("List of %s repositories has %d elements. Can't insert at position %s.\n", (defaultRepository ? "default" : "remote"), pidRepos.length, id);
        return;
    }
    Optional<MavenRepositoryURL> first = Arrays.stream(allRepos).filter((repo) -> id.equals(repo.getId())).findAny();
    if (first.isPresent()) {
        System.err.printf("Repository with ID \"%s\" is already configured for URL %s\n", id, first.get().getURL());
        return;
    }
    SourceAnd<String> up = updatePolicy(updatePolicy);
    if (!up.valid) {
        System.err.println("Unknown value of update policy: \"" + updatePolicy + "\"");
        return;
    }
    SourceAnd<String> cp = checksumPolicy(checksumPolicy);
    if (!cp.valid) {
        System.err.println("Unknown value of checksum policy: \"" + checksumPolicy + "\"");
        return;
    }
    SourceAnd<String> urlResolved = validateRepositoryURL(uri, defaultRepository);
    if (!urlResolved.valid) {
        return;
    }
    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
    }
    // credentials for remote repository can be stored only in settings.xml
    if (!defaultRepository && hasCredentials) {
        if (!updateCredentials(force, id, username, password, prefix, config)) {
            return;
        }
        updateSettings(prefix, config);
    }
    StringBuilder sb = new StringBuilder();
    sb.append(urlResolved.val());
    sb.append(ServiceConstants.SEPARATOR_OPTIONS).append(ServiceConstants.OPTION_ID).append("=").append(id);
    if (snapshots) {
        sb.append(ServiceConstants.SEPARATOR_OPTIONS).append(ServiceConstants.OPTION_ALLOW_SNAPSHOTS);
    }
    if (noReleases) {
        sb.append(ServiceConstants.SEPARATOR_OPTIONS).append(ServiceConstants.OPTION_DISALLOW_RELEASES);
    }
    sb.append(ServiceConstants.SEPARATOR_OPTIONS).append(ServiceConstants.OPTION_UPDATE).append("=").append(updatePolicy);
    sb.append(ServiceConstants.SEPARATOR_OPTIONS).append(ServiceConstants.OPTION_CHECKSUM).append("=").append(checksumPolicy);
    MavenRepositoryURL newRepository = new MavenRepositoryURL(sb.toString());
    List<MavenRepositoryURL> newRepos = new LinkedList<>(Arrays.asList(pidRepos));
    if (idx >= 0) {
        newRepos.add(idx, newRepository);
    } else {
        newRepos.add(newRepository);
    }
    updatePidRepositories(prefix, config, defaultRepository, newRepos, settingsRepos.length > 0);
    Configuration cmConfig = cm.getConfiguration(PID);
    cmConfig.update(config);
    success = true;
}
Also used : Arrays(java.util.Arrays) List(java.util.List) MavenRepositoryURL(org.apache.karaf.maven.core.MavenRepositoryURL) Configuration(org.osgi.service.cm.Configuration) ServiceConstants(org.ops4j.pax.url.mvn.ServiceConstants) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Optional(java.util.Optional) Argument(org.apache.karaf.shell.api.action.Argument) LinkedList(java.util.LinkedList) Option(org.apache.karaf.shell.api.action.Option) Dictionary(java.util.Dictionary) Command(org.apache.karaf.shell.api.action.Command) Configuration(org.osgi.service.cm.Configuration) MavenRepositoryURL(org.apache.karaf.maven.core.MavenRepositoryURL) LinkedList(java.util.LinkedList)

Aggregations

MavenRepositoryURL (org.apache.karaf.maven.core.MavenRepositoryURL)6 Arrays (java.util.Arrays)3 Dictionary (java.util.Dictionary)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Optional (java.util.Optional)3 Command (org.apache.karaf.shell.api.action.Command)3 Service (org.apache.karaf.shell.api.action.lifecycle.Service)3 Profile (org.apache.maven.settings.Profile)3 Repository (org.apache.maven.settings.Repository)3 Configuration (org.osgi.service.cm.Configuration)3 Argument (org.apache.karaf.shell.api.action.Argument)2 Option (org.apache.karaf.shell.api.action.Option)2 ShellTable (org.apache.karaf.shell.support.table.ShellTable)2 Server (org.apache.maven.settings.Server)2 URL (java.net.URL)1 LinkedHashSet (java.util.LinkedHashSet)1 Collectors (java.util.stream.Collectors)1 Row (org.apache.karaf.shell.support.table.Row)1 Proxy (org.apache.maven.settings.Proxy)1