From 502ed3719fba58e8e2dbf91a991ff2df31036d2d Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 25 Mar 2016 11:03:38 +0100 Subject: [PATCH] Added Javadocs for Zookeeper Config --- .../AbstractZookeeperPropertySource.java | 6 +++-- .../cloud/zookeeper/config/ConfigWatcher.java | 17 ++++++++------ .../ZookeeperConfigAutoConfiguration.java | 4 ++++ ...ZookeeperConfigBootstrapConfiguration.java | 4 ++++ .../config/ZookeeperConfigProperties.java | 14 ++++++++++++ .../config/ZookeeperPropertySource.java | 10 +++++---- .../ZookeeperPropertySourceLocator.java | 22 +++++++++++++++++++ 7 files changed, 64 insertions(+), 13 deletions(-) diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/AbstractZookeeperPropertySource.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/AbstractZookeeperPropertySource.java index e86d186f..c5d11b88 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/AbstractZookeeperPropertySource.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/AbstractZookeeperPropertySource.java @@ -20,7 +20,11 @@ import org.apache.curator.framework.CuratorFramework; import org.springframework.core.env.EnumerablePropertySource; /** + * A {@link EnumerablePropertySource} that has a notion of a context which is + * the root folder in Zookeeper. + * * @author Spencer Gibb + * @since 1.0.0 */ public abstract class AbstractZookeeperPropertySource extends EnumerablePropertySource { @@ -28,9 +32,7 @@ public abstract class AbstractZookeeperPropertySource extends EnumerableProperty public AbstractZookeeperPropertySource(String context, CuratorFramework source) { super(context, source); - this.context = context; - if (!this.context.startsWith("/")) { this.context = "/" + this.context; } diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ConfigWatcher.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ConfigWatcher.java index cb72925c..5083960f 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ConfigWatcher.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ConfigWatcher.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.TreeCache; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; @@ -38,12 +39,15 @@ import static org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type.NOD import static org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type.NODE_UPDATED; /** + * Class that registers a {@link TreeCache} for each context. + * It publishes events upon element change in Zookeeper. + * * @author Spencer Gibb + * @since 1.0.0 */ public class ConfigWatcher implements Closeable, TreeCacheListener, ApplicationEventPublisherAware{ - private static final Log log = org.apache.commons.logging.LogFactory - .getLog(ConfigWatcher.class); + private static final Log log = LogFactory.getLog(ConfigWatcher.class); private AtomicBoolean running = new AtomicBoolean(false); private List contexts; @@ -69,7 +73,6 @@ public class ConfigWatcher implements Closeable, TreeCacheListener, ApplicationE if (!context.startsWith("/")) { context = "/" + context; } - try { TreeCache cache = TreeCache.newBuilder(this.source, context).build(); cache.getListenable().addListener(this); @@ -105,12 +108,12 @@ public class ConfigWatcher implements Closeable, TreeCacheListener, ApplicationE } public String getEventDesc(TreeCacheEvent event) { - StringBuffer out = new StringBuffer(); - out.append("type="+event.getType()); - out.append(", path="+event.getData().getPath()); + StringBuilder out = new StringBuilder(); + out.append("type=").append(event.getType()); + out.append(", path=").append(event.getData().getPath()); byte[] data = event.getData().getData(); if (data != null && data.length > 0) { - out.append(", data="+new String(data, Charset.forName("UTF-8"))); + out.append(", data=").append(new String(data, Charset.forName("UTF-8"))); } return out.toString(); } diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigAutoConfiguration.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigAutoConfiguration.java index 8ceefe2f..3cd827ad 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigAutoConfiguration.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigAutoConfiguration.java @@ -24,7 +24,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** + * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration} + * that registers a Zookeeper configuration watcher. + * * @author Spencer Gibb + * @since 1.0.0 */ @Configuration public class ZookeeperConfigAutoConfiguration { diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigBootstrapConfiguration.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigBootstrapConfiguration.java index 35cf71db..87c9e3be 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigBootstrapConfiguration.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigBootstrapConfiguration.java @@ -25,12 +25,16 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; /** + * Bootstrap Configuration for Zookeeper Configuration + * * @author Spencer Gibb + * @since 1.0.0 */ @Configuration @Import(ZookeeperAutoConfiguration.class) @EnableConfigurationProperties public class ZookeeperConfigBootstrapConfiguration { + @Bean @ConditionalOnMissingBean public ZookeeperPropertySourceLocator zookeeperPropertySourceLocator( diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigProperties.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigProperties.java index c2584833..c03ffaf0 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigProperties.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigProperties.java @@ -20,17 +20,31 @@ import org.hibernate.validator.constraints.NotEmpty; import org.springframework.boot.context.properties.ConfigurationProperties; /** + * Properties related to keeping configuration in Zookeeper. + * * @author Spencer Gibb + * @since 1.0.0 + * + * @see ZookeeperPropertySourceLocator */ @ConfigurationProperties("spring.cloud.zookeeper.config") public class ZookeeperConfigProperties { private boolean enabled = true; + /** + * Root folder where the configuration for Zookeeper is kept + */ private String root = "config"; + /** + * The name of the default context + */ @NotEmpty private String defaultContext = "application"; + /** + * Separator for profile appended to the application name + */ @NotEmpty private String profileSeparator = ","; diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySource.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySource.java index 38eee619..4bda748c 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySource.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySource.java @@ -23,23 +23,26 @@ import java.util.Map; import java.util.Set; import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.curator.framework.CuratorFramework; import org.apache.zookeeper.KeeperException; import org.springframework.util.ReflectionUtils; /** + * {@link org.springframework.core.env.PropertySource} that stores properties + * from Zookeeper inside a map. Properties are loaded upon class initialization. + * * @author Spencer Gibb + * @since 1.0.0 */ public class ZookeeperPropertySource extends AbstractZookeeperPropertySource { - private static final Log log = org.apache.commons.logging.LogFactory - .getLog(ZookeeperPropertySource.class); + private static final Log log = LogFactory.getLog(ZookeeperPropertySource.class); private Map properties = new LinkedHashMap<>(); public ZookeeperPropertySource(String context, CuratorFramework source) { super(context, source); - findProperties(this.getContext()); } @@ -87,7 +90,6 @@ public class ZookeeperPropertySource extends AbstractZookeeperPropertySource { if (children == null || children.isEmpty()) { return; } - for (String child : children) { String childPath = path + "/" + child; byte[] bytes = getPropertyBytes(childPath); diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java index 27a35ea0..c33ba481 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java @@ -30,7 +30,29 @@ import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; /** + * Zookeeper provides a hierarchical namespace that allows + * clients to store arbitrary data, such as configuration data. Spring Cloud Zookeeper Config is an alternative to the + * Config Server and Client. Configuration is loaded into the Spring Environment during + * the special "bootstrap" phase. Configuration is stored in the {@code /config} namespace by default. Multiple + * {@code PropertySource} instances are created based on the application's name and the active profiles that mimicks the Spring Cloud Config + * order of resolving properties. For example, an application with the name "testApp" and with the "dev" profile will have the following property sources + * created: + * + *
{@code
+ * config/testApp,dev
+ * config/testApp
+ * config/application,dev
+ * config/application
+ * }
+ * + *

+ * The most specific property source is at the top, with the least specific at the + * bottom. Properties is the {@code config/application} namespace are applicable to all applications + * using zookeeper for configuration. Properties in the {@code config/testApp} namespace are only available + * to the instances of the service named "testApp". + * * @author Spencer Gibb + * @since 1.0.0 */ public class ZookeeperPropertySourceLocator implements PropertySourceLocator {