public void removeRepository()

in features/src/main/java/org/apache/karaf/cellar/features/management/internal/CellarFeaturesMBeanImpl.java [513:592]


    public void removeRepository(String groupName, String repo, boolean uninstall) throws Exception {
        // check if the group exists
        Group group = groupManager.findGroupByName(groupName);
        if (group == null) {
            throw new IllegalArgumentException("Cluster group " + groupName + " doesn't exist");
        }

        // check if the event producer is ON
        if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
            throw new IllegalStateException("Cluster event producer is OFF");
        }

        // get the features repositories in the cluster group
        Map<String, String> clusterRepositories = clusterManager.getMap(Constants.REPOSITORIES_MAP + Configurations.SEPARATOR + groupName);
        // get the features in the cluster group
        Map<FeatureState, Boolean> clusterFeatures = clusterManager.getMap(Constants.FEATURES_MAP + Configurations.SEPARATOR + groupName);

        ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

            List<String> urls = new ArrayList<String>();
            Pattern pattern = Pattern.compile(repo);
            for (String repositoryUrl : clusterRepositories.keySet()) {
                String repositoryName = clusterRepositories.get(repositoryUrl);
                if (repositoryName != null && !repositoryName.isEmpty()) {
                    // repository has name, try regex on the name
                    Matcher nameMatcher = pattern.matcher(repositoryName);
                    if (nameMatcher.matches()) {
                        urls.add(repositoryUrl);
                    } else {
                        // the name regex doesn't match, fallback to repository URI regex
                        Matcher uriMatcher = pattern.matcher(repositoryUrl);
                        if (uriMatcher.matches()) {
                            urls.add(repositoryUrl);
                        }
                    }
                } else {
                    // the repository name is not defined, use repository URI regex
                    Matcher uriMatcher = pattern.matcher(repositoryUrl);
                    if (uriMatcher.matches()) {
                        urls.add(repositoryUrl);
                    }
                }
            }

            for (String url : urls) {
                // looking for the URL in the list
                boolean found = false;
                for (String repository : clusterRepositories.keySet()) {
                    if (repository.equals(url)) {
                        found = true;
                        break;
                    }
                }
                if (found) {
                    Features repositoryModel = JaxbUtil.unmarshal(url, true);

                    // update the features repositories in the cluster group
                    clusterRepositories.remove(url);

                    // update the features in the cluster group
                    for (Feature feature : repositoryModel.getFeature()) {
                        clusterFeatures.remove(feature.getName() + "/" + feature.getVersion());
                    }

                    // broadcast a cluster event
                    ClusterRepositoryEvent event = new ClusterRepositoryEvent(url, RepositoryEvent.EventType.RepositoryRemoved);
                    event.setUninstall(uninstall);
                    event.setSourceGroup(group);
                    event.setSourceNode(clusterManager.getNode());
                    eventProducer.produce(event);
                } else {
                    throw new IllegalArgumentException("Features repository URL " + url + " not found in cluster group " + groupName);
                }
            }
        } finally {
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }
    }