Search in sources :

Example 1 with RepositoryEvent

use of org.apache.karaf.features.RepositoryEvent in project karaf by apache.

the class FeaturesServiceMBeanImpl method getFeaturesListener.

public FeaturesListener getFeaturesListener() {
    return new FeaturesListener() {

        public void featureEvent(FeatureEvent event) {
            if (!event.isReplay()) {
                Notification notification = new Notification(FEATURE_EVENT_TYPE, objectName, sequenceNumber++);
                notification.setUserData(new JmxFeatureEvent(event).asCompositeData());
                sendNotification(notification);
            }
        }

        public void repositoryEvent(RepositoryEvent event) {
            if (!event.isReplay()) {
                Notification notification = new Notification(REPOSITORY_EVENT_TYPE, objectName, sequenceNumber++);
                notification.setUserData(new JmxRepositoryEvent(event).asCompositeData());
                sendNotification(notification);
            }
        }
    };
}
Also used : FeaturesListener(org.apache.karaf.features.FeaturesListener) FeatureEvent(org.apache.karaf.features.FeatureEvent) JmxFeatureEvent(org.apache.karaf.features.management.codec.JmxFeatureEvent) RepositoryEvent(org.apache.karaf.features.RepositoryEvent) JmxRepositoryEvent(org.apache.karaf.features.management.codec.JmxRepositoryEvent) Notification(javax.management.Notification) JmxFeatureEvent(org.apache.karaf.features.management.codec.JmxFeatureEvent) JmxRepositoryEvent(org.apache.karaf.features.management.codec.JmxRepositoryEvent)

Example 2 with RepositoryEvent

use of org.apache.karaf.features.RepositoryEvent in project karaf by apache.

the class FeaturesServiceImpl method removeRepository.

@Override
public void removeRepository(URI uri, boolean uninstall) throws Exception {
    Repository repo = getRepository(uri);
    if (repo == null) {
        return;
    }
    Set<Repository> repos = getReposToRemove(repo);
    Set<String> features = new HashSet<>();
    for (Repository tranRepo : repos) {
        features.addAll(getRequiredFeatureIds(tranRepo));
    }
    if (!features.isEmpty()) {
        if (uninstall) {
            uninstallFeatures(features, EnumSet.noneOf(Option.class));
        } else {
            throw new IllegalStateException("The following features are required from the repository: " + String.join(", ", features));
        }
    }
    synchronized (lock) {
        // Remove repo
        if (!state.repositories.remove(uri.toString())) {
            return;
        }
        // Clean cache
        featureCache = null;
        repositories.removeRepository(uri);
        saveState();
    }
    callListeners(new RepositoryEvent(repo, RepositoryEvent.EventType.RepositoryRemoved, false));
}
Also used : Repository(org.apache.karaf.features.Repository) RepositoryEvent(org.apache.karaf.features.RepositoryEvent) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with RepositoryEvent

use of org.apache.karaf.features.RepositoryEvent in project karaf by apache.

the class FeaturesServiceImpl method registerListener.

//
// Listeners support
//
@Override
public void registerListener(FeaturesListener listener) {
    listeners.add(listener);
    try {
        Set<String> repositories = new TreeSet<>();
        Map<String, Set<String>> installedFeatures = new TreeMap<>();
        synchronized (lock) {
            repositories.addAll(state.repositories);
            installedFeatures.putAll(copy(state.installedFeatures));
        }
        for (String uri : repositories) {
            Repository repository = new RepositoryImpl(URI.create(uri), cfg.blacklisted);
            listener.repositoryEvent(new RepositoryEvent(repository, RepositoryEvent.EventType.RepositoryAdded, true));
        }
        for (Map.Entry<String, Set<String>> entry : installedFeatures.entrySet()) {
            for (String id : entry.getValue()) {
                Feature feature = org.apache.karaf.features.internal.model.Feature.valueOf(id);
                listener.featureEvent(new FeatureEvent(FeatureEvent.EventType.FeatureInstalled, feature, entry.getKey(), true));
            }
        }
    } catch (Exception e) {
        LOGGER.error("Error notifying listener about the current state", e);
    }
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) TreeMap(java.util.TreeMap) Feature(org.apache.karaf.features.Feature) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Repository(org.apache.karaf.features.Repository) FeatureEvent(org.apache.karaf.features.FeatureEvent) TreeSet(java.util.TreeSet) RepositoryEvent(org.apache.karaf.features.RepositoryEvent) StateStorage.toStringStringSetMap(org.apache.karaf.features.internal.service.StateStorage.toStringStringSetMap) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 4 with RepositoryEvent

use of org.apache.karaf.features.RepositoryEvent in project karaf by apache.

the class FeaturesServiceImpl method addRepository.

@Override
public void addRepository(URI uri, boolean install) throws Exception {
    Repository repository = repositories.loadAndValidate(uri);
    synchronized (lock) {
        repositories.addRepository(repository);
        featureCache = null;
        // Add repo
        if (!state.repositories.add(uri.toString())) {
            return;
        }
        saveState();
    }
    callListeners(new RepositoryEvent(repository, RepositoryEvent.EventType.RepositoryAdded, false));
    // install the features in the repo
    if (install) {
        HashSet<String> features = new HashSet<>();
        for (Feature feature : repository.getFeatures()) {
            features.add(feature.getId());
        }
        installFeatures(features, EnumSet.noneOf(FeaturesService.Option.class));
    }
}
Also used : Repository(org.apache.karaf.features.Repository) RepositoryEvent(org.apache.karaf.features.RepositoryEvent) Feature(org.apache.karaf.features.Feature) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with RepositoryEvent

use of org.apache.karaf.features.RepositoryEvent in project karaf by apache.

the class EventAdminListener method repositoryEvent.

public void repositoryEvent(RepositoryEvent event) {
    try {
        EventAdmin eventAdmin = tracker.getService();
        if (eventAdmin == null) {
            return;
        }
        Dictionary<String, Object> props = new Hashtable<>();
        props.put(EventConstants.TYPE, event.getType());
        props.put(EventConstants.EVENT, event);
        props.put(EventConstants.TIMESTAMP, System.currentTimeMillis());
        props.put(EventConstants.REPOSITORY_URI, event.getRepository().getURI().toString());
        String topic;
        switch(event.getType()) {
            case RepositoryAdded:
                topic = EventConstants.TOPIC_REPOSITORY_ADDED;
                break;
            case RepositoryRemoved:
                topic = EventConstants.TOPIC_REPOSITORY_REMOVED;
                break;
            default:
                throw new IllegalStateException("Unknown repository event type: " + event.getType());
        }
        eventAdmin.postEvent(new Event(topic, props));
    } catch (IllegalStateException e) {
        LOGGER.warn("Unable to post event to EventAdmin", e);
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event) FeatureEvent(org.apache.karaf.features.FeatureEvent) RepositoryEvent(org.apache.karaf.features.RepositoryEvent)

Aggregations

RepositoryEvent (org.apache.karaf.features.RepositoryEvent)5 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 FeatureEvent (org.apache.karaf.features.FeatureEvent)3 Repository (org.apache.karaf.features.Repository)3 Feature (org.apache.karaf.features.Feature)2 IOException (java.io.IOException)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 Notification (javax.management.Notification)1 FeaturesListener (org.apache.karaf.features.FeaturesListener)1 StateStorage.toStringStringSetMap (org.apache.karaf.features.internal.service.StateStorage.toStringStringSetMap)1 JmxFeatureEvent (org.apache.karaf.features.management.codec.JmxFeatureEvent)1 JmxRepositoryEvent (org.apache.karaf.features.management.codec.JmxRepositoryEvent)1