public void bundleChanged()

in bundle/src/main/java/org/apache/karaf/cellar/bundle/LocalBundleListener.java [50:146]


    public void bundleChanged(BundleEvent event) {

        if (event.getBundle().getBundleId() == 0 && (event.getType() == BundleEvent.STOPPING || event.getType() == BundleEvent.STOPPED)) {
            LOGGER.debug("CELLAR BUNDLE: Karaf shutdown detected, removing Cellar LocalBundleListener");
            bundleContext.removeBundleListener(this);
            return;
        }

        if (!isEnabled()) {
            LOGGER.trace("CELLAR BUNDLE: local listener is disabled");
            return;
        }

        if (event.getBundle().getBundleId() == 0) {
            return;
        }

        // check if the producer is ON
        if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
            LOGGER.debug("CELLAR BUNDLE: cluster event producer is OFF");
            return;
        }

        if (event != null && event.getBundle() != null) {
            Set<Group> groups = null;
            try {
                groups = groupManager.listLocalGroups();
            } catch (Exception ex) {
                LOGGER.warn("Failed to list local groups. Is Cellar uninstalling ?");
            }

            if (groups != null && !groups.isEmpty()) {
                for (Group group : groups) {

                    // get the bundle name or location.
                    String name = (String) event.getBundle().getHeaders().get(org.osgi.framework.Constants.BUNDLE_NAME);
                    // if there is no name, then default to symbolic name.
                    name = (name == null) ? event.getBundle().getSymbolicName() : name;
                    // if there is no symbolic name, resort to location.
                    name = (name == null) ? event.getBundle().getLocation() : name;
                    String symbolicName = event.getBundle().getSymbolicName();
                    String version = event.getBundle().getHeaders().get(org.osgi.framework.Constants.BUNDLE_VERSION);
                    String bundleLocation = event.getBundle().getLocation();
                    int status = event.getBundle().getState();
                    int startLevel = event.getBundle().adapt(BundleStartLevel.class).getStartLevel();

                    if (isAllowed(group, Constants.CATEGORY, bundleLocation, EventType.OUTBOUND)) {

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

                        try {
                            // update bundles in the cluster group
                            Map<String, BundleState> clusterBundles = clusterManager.getMap(Constants.BUNDLE_MAP + Configurations.SEPARATOR + group.getName());
                            if (event.getType() == BundleEvent.UNINSTALLED) {
                                clusterBundles.remove(symbolicName + "/" + version);
                            } else {
                                BundleState state = clusterBundles.get(symbolicName + "/" + version);
                                if (state == null) {
                                    state = new BundleState();
                                }
                                state.setId(event.getBundle().getBundleId());
                                state.setName(name);
                                state.setVersion(version);
                                state.setSymbolicName(symbolicName);
                                state.setStatus(status);
                                state.setStartLevel(startLevel);
                                state.setLocation(bundleLocation);
                                clusterBundles.put(symbolicName + "/" + version, state);
                            }

                            // check the features first
                        	List<Feature> matchingFeatures = retrieveFeature(bundleLocation);
                        	for (Feature feature : matchingFeatures) {
            					if (!isAllowed(group, "feature", feature.getName(), EventType.OUTBOUND)) {
            						LOGGER.trace("CELLAR BUNDLE: bundle {} is contained in feature {} marked BLOCKED OUTBOUND for cluster group {}", bundleLocation, feature.getName(), group.getName());
            						return;
            					}
            				}
                            
                            // broadcast the cluster event
                            ClusterBundleEvent clusterBundleEvent = new ClusterBundleEvent(symbolicName, version, bundleLocation, startLevel, status);
                            clusterBundleEvent.setSourceGroup(group);
                            clusterBundleEvent.setSourceNode(clusterManager.getNode());
                            clusterBundleEvent.setLocal(clusterManager.getNode());
                            eventProducer.produce(clusterBundleEvent);
                        } catch (Exception e) {
                        	LOGGER.error("CELLAR BUNDLE: failed to create bundle event", e);
						} finally {
                            Thread.currentThread().setContextClassLoader(originalClassLoader);
                        }

                    } else LOGGER.trace("CELLAR BUNDLE: bundle {} is marked BLOCKED OUTBOUND for cluster group {}", bundleLocation, group.getName());
                }
            }
        }
    }