You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2013/04/23 13:08:13 UTC
svn commit: r1470896 - in
/karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features:
FeaturesSupport.java FeaturesSynchronizer.java
Author: jbonofre
Date: Tue Apr 23 11:08:13 2013
New Revision: 1470896
URL: http://svn.apache.org/r1470896
Log:
[KARAF-2287] FeaturesSynchronizer now check the local status of a features repository before adding
Modified:
karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java
karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSynchronizer.java
Modified: karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java?rev=1470896&r1=1470895&r2=1470896&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java (original)
+++ karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java Tue Apr 23 11:08:13 2013
@@ -36,25 +36,20 @@ public class FeaturesSupport extends Cel
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 Cel
}
/**
- * 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 Cel
}
/**
- * 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 Cel
}
/**
- * 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() {
Modified: karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSynchronizer.java
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSynchronizer.java?rev=1470896&r1=1470895&r2=1470896&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSynchronizer.java (original)
+++ karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSynchronizer.java Tue Apr 23 11:08:13 2013
@@ -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 extend
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 extend
}
}
- /**
- * 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 extend
}
}
- // 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 extend
}
/**
- * 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 extend
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 extend
}
}
+ /**
+ * 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 extend
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;
- }
-
}