[KARAF-2287] FeaturesSynchronizer now check the local status of a features repository before adding
git-svn-id: https://svn.apache.org/repos/asf/karaf/cellar/branches/cellar-2.2.x@1470896 13f79535-47bb-0310-9956-ffa450edef68 Project: http://git-wip-us.apache.org/repos/asf/karaf-cellar/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf-cellar/commit/f352b40f Tree: http://git-wip-us.apache.org/repos/asf/karaf-cellar/tree/f352b40f Diff: http://git-wip-us.apache.org/repos/asf/karaf-cellar/diff/f352b40f Branch: refs/heads/cellar-2.2.x Commit: f352b40f546ae1af057863e80b036debb763a26c Parents: ae925e8 Author: jbonofre <jbonofre@13f79535-47bb-0310-9956-ffa450edef68> Authored: Tue Apr 23 11:08:13 2013 +0000 Committer: jbonofre <jbonofre@13f79535-47bb-0310-9956-ffa450edef68> Committed: Tue Apr 23 11:08:13 2013 +0000 ---------------------------------------------------------------------- .../karaf/cellar/features/FeaturesSupport.java | 68 ++++++++----- .../cellar/features/FeaturesSynchronizer.java | 100 +++++++++---------- 2 files changed, 89 insertions(+), 79 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/f352b40f/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java ---------------------------------------------------------------------- diff --git a/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java b/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java index fffb4d9..2396c4f 100644 --- a/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java +++ b/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java @@ -36,25 +36,20 @@ public class FeaturesSupport extends CellarSupport { protected FeaturesService featuresService; - /** - * Initialization method - */ public void init() { + // nothing to do } - /** - * Destruction method - */ public void destroy() { - + // nothing to do } /** - * Returns true if the specified feature is installed. + * Check if a feature is installed locally. * - * @param name - * @param version - * @return + * @param name the feature name. + * @param version the feature version. + * @return true if the feature is installed locally, false else. */ public Boolean isInstalled(String name, String version) { if (featuresService != null) { @@ -71,9 +66,26 @@ public class FeaturesSupport extends CellarSupport { } /** - * Pushes a {@code Feature} and its status to the distributed list of features. + * Check if a features repository is already registered locally. + * + * @param uri the features repository URI. + * @return true if the features repository is already registered locally, false else. + */ + public Boolean isRepositoryRegisteredLocally(String uri) { + Repository[] localRepositories = featuresService.listRepositories(); + for (Repository localRepository : localRepositories) { + if (localRepository.getURI().toString().equals(uri)) { + return true; + } + } + return false; + } + + /** + * Push a {@code Feature} and its status to a cluster group. * - * @param feature + * @param feature the feature to push. + * @param group the cluster group where to push the feature. */ public void pushFeature(Feature feature, Group group) { if (feature != null) { @@ -91,10 +103,12 @@ public class FeaturesSupport extends CellarSupport { } /** - * Pushes a {@code Feature} and its status to the distributed list of features. + * Push a {@code Feature} and its status to a cluster group. * This version of the method force the bundle status, without looking the features service. * - * @param feature + * @param feature the feature to push. + * @param group the cluster group where to push the feature. + * @param force true to force the update of the bundle status as well, false else. */ public void pushFeature(Feature feature, Group group, Boolean force) { if (feature != null) { @@ -111,39 +125,41 @@ public class FeaturesSupport extends CellarSupport { } /** - * Pushed a {@code Repository} to the distributed list of repositories. + * Push a features {@code Repository} to a cluster group. * - * @param repository + * @param repository the features repository to push. + * @param group the cluster group where to push. */ public void pushRepository(Repository repository, Group group) { String groupName = group.getName(); - List<String> repositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName); + List<String> clusterRepositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName); boolean found = false; - for (String registeredRepository : repositories) { - if (registeredRepository.equals(repository.getURI().toString())) { + for (String clusterRepository : clusterRepositories) { + if (clusterRepository.equals(repository.getURI().toString())) { found = true; break; } } if (!found) { - repositories.add(repository.getURI().toString()); + clusterRepositories.add(repository.getURI().toString()); } } /** - * Removes a {@code Repository} to the distributed list of repositories. + * Remove a features {@code Repository} from a cluster group. * - * @param repository + * @param repository the feature repository to remove. + * @param group the cluster group where to remove. */ public void removeRepository(Repository repository, Group group) { String groupName = group.getName(); - List<String> repositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName); + List<String> clusterRepositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName); - if (featuresService != null && repositories != null) { + if (featuresService != null && clusterRepositories != null) { URI uri = repository.getURI(); - repositories.remove(uri.toString()); + clusterRepositories.remove(uri.toString()); } } public FeaturesService getFeaturesService() { http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/f352b40f/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSynchronizer.java ---------------------------------------------------------------------- diff --git a/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSynchronizer.java b/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSynchronizer.java index d7f6165..754c1ff 100644 --- a/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSynchronizer.java +++ b/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSynchronizer.java @@ -13,14 +13,11 @@ */ package org.apache.karaf.cellar.features; -import org.apache.karaf.cellar.core.ClusterManager; import org.apache.karaf.cellar.core.Configurations; import org.apache.karaf.cellar.core.Group; import org.apache.karaf.cellar.core.Synchronizer; -import org.apache.karaf.cellar.core.event.EventProducer; import org.apache.karaf.cellar.core.event.EventType; import org.apache.karaf.features.Feature; -import org.apache.karaf.features.FeaturesService; import org.apache.karaf.features.Repository; import org.osgi.service.cm.Configuration; import org.slf4j.Logger; @@ -41,9 +38,6 @@ public class FeaturesSynchronizer extends FeaturesSupport implements Synchronize private static final transient Logger LOGGER = LoggerFactory.getLogger(FeaturesSynchronizer.class); - /** - * Initialization method - */ public void init() { super.init(); Set<Group> groups = groupManager.listLocalGroups(); @@ -57,31 +51,34 @@ public class FeaturesSynchronizer extends FeaturesSupport implements Synchronize } } - /** - * Destruction method - */ public void destroy() { super.destroy(); } /** - * Pulls the features from the cluster. + * Pull features repositories and features status from a cluster group. + * + * @param group the cluster group where to get features status. */ public void pull(Group group) { if (group != null) { String groupName = group.getName(); - List<String> repositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName); - Map<FeatureInfo, Boolean> features = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + groupName); + LOGGER.info("CELLAR FEATURES: pulling features from cluster group {}.",groupName); + List<String> clusterRepositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName); + Map<FeatureInfo, Boolean> clusterFeatures = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + groupName); clusterManager.getList(Constants.FEATURES + Configurations.SEPARATOR + groupName); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); - //Retrieve remote feautre URLs. - if (repositories != null && !repositories.isEmpty()) { - for (String url : repositories) { + + // get the features repositories from the cluster group + if (clusterRepositories != null && !clusterRepositories.isEmpty()) { + for (String url : clusterRepositories) { try { - LOGGER.debug("CELLAR FEATURES: adding repository {}", url); - featuresService.addRepository(new URI(url)); + if (!isRepositoryRegisteredLocally(url)) { + LOGGER.debug("CELLAR FEATURES: adding repository {}", url); + featuresService.addRepository(new URI(url)); + } } catch (MalformedURLException e) { LOGGER.warn("CELLAR FEATURES: failed to add features repository {} (URL is malformed)", url, e); } catch (Exception e) { @@ -90,25 +87,33 @@ public class FeaturesSynchronizer extends FeaturesSupport implements Synchronize } } - // retrieve remote feature status. - if (features != null && !features.isEmpty()) { - for (FeatureInfo info : features.keySet()) { + // get the features status from the cluster group + if (clusterFeatures != null && !clusterFeatures.isEmpty()) { + for (FeatureInfo info : clusterFeatures.keySet()) { String name = info.getName(); - //Check if feature is blocked. + // check if the feature is blocked if (isAllowed(group, Constants.FEATURES_CATEGORY, name, EventType.INBOUND)) { - Boolean remotelyInstalled = features.get(info); - Boolean localyInstalled = isInstalled(info.getName(), info.getVersion()); + Boolean clusterInstalled = clusterFeatures.get(info); + Boolean locallyInstalled = isInstalled(info.getName(), info.getVersion()); - //If feature needs to be installed locally. - if (remotelyInstalled && !localyInstalled) { + // prevent NPE + if (clusterInstalled == null) { + clusterInstalled = false; + } + if (locallyInstalled == null) { + locallyInstalled = false; + } + + // if feature has to be installed locally + if (clusterInstalled && !locallyInstalled) { try { LOGGER.debug("CELLAR FEATURES: installing feature {}/{}", info.getName(), info.getVersion()); featuresService.installFeature(info.getName(), info.getVersion()); } catch (Exception e) { LOGGER.warn("CELLAR FEATURES: failed to install feature {}/{} ", new Object[]{ info.getName(), info.getVersion() }, e); } - //If feature needs to be localy uninstalled. - } else if (!remotelyInstalled && localyInstalled) { + // if feature has to be uninstalled locally + } else if (!clusterInstalled && locallyInstalled) { try { LOGGER.debug("CELLAR FEATURES: un-installing feature {}/{}", info.getName(), info.getVersion()); featuresService.uninstallFeature(info.getName(), info.getVersion()); @@ -126,15 +131,14 @@ public class FeaturesSynchronizer extends FeaturesSupport implements Synchronize } /** - * Push features to the cluster. + * Push local features repositories and features status to a cluster group. + * + * @param group the cluster group where to push. */ public void push(Group group) { if (group != null) { String groupName = group.getName(); - LOGGER.info("CELLAR FEATURES: Pulling features from group {}.",groupName); - //List<String> repositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName); - Map<FeatureInfo, Boolean> features = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + groupName); - clusterManager.getList(Constants.FEATURES + Configurations.SEPARATOR + groupName); + LOGGER.info("CELLAR FEATURES: pushing features to cluster group {}.",groupName); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { @@ -147,22 +151,22 @@ public class FeaturesSynchronizer extends FeaturesSupport implements Synchronize repositoryList = featuresService.listRepositories(); featuresList = featuresService.listFeatures(); } catch (Exception e) { - LOGGER.warn("CELLAR FEATURES: unable to list features", e); + LOGGER.warn("CELLAR FEATURES: unable to list features repository or features", e); } - //Process repository list + // push local features repositories to the cluster group if (repositoryList != null && repositoryList.length > 0) { for (Repository repository : repositoryList) { pushRepository(repository, group); - LOGGER.debug("CELLAR FEATURES: pushing repository {} in group {}", repository.getName(), group.getName()); + LOGGER.debug("CELLAR FEATURES: pushing features repository {} in cluster group {}", repository.getName(), group.getName()); } } - //Process features list + // push local features status to the cluster group if (featuresList != null && featuresList.length > 0) { for (Feature feature : featuresList) { pushFeature(feature, group); - LOGGER.debug("CELLAR FEATURES: pushing feature {} in group {}", feature.getName(), group.getName()); + LOGGER.debug("CELLAR FEATURES: pushing feature {} in cluster group {}", feature.getName(), group.getName()); } } } finally { @@ -171,6 +175,12 @@ public class FeaturesSynchronizer extends FeaturesSupport implements Synchronize } } + /** + * Check if features sync is enabled for a cluster group. + * + * @param group the cluster group to check the sync. + * @return true if sync is enabled for the cluster group, false else. + */ public Boolean isSyncEnabled(Group group) { Boolean result = Boolean.FALSE; String groupName = group.getName(); @@ -189,20 +199,4 @@ public class FeaturesSynchronizer extends FeaturesSupport implements Synchronize return result; } - public ClusterManager getCollectionManager() { - return clusterManager; - } - - public void setCollectionManager(ClusterManager clusterManager) { - this.clusterManager = clusterManager; - } - - public FeaturesService getFeaturesService() { - return featuresService; - } - - public void setFeaturesService(FeaturesService featuresService) { - this.featuresService = featuresService; - } - }
