{"action":"created","comment":{"url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/comments/44572814","id":44572814,"diff_hunk":"@@ -0,0 +1,142 @@\n+/*\n+ * Copyright 2013-2015 the original author or authors.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *      http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+package org.springframework.cloud.consul.config.watch;\n+\n+import java.math.BigInteger;\n+import java.util.Arrays;\n+import java.util.Collection;\n+import java.util.HashMap;\n+import java.util.HashSet;\n+import java.util.List;\n+import java.util.Map;\n+import java.util.Set;\n+\n+import org.apache.commons.collections4.map.AbstractReferenceMap.ReferenceStrength;\n+import org.apache.commons.collections4.map.ReferenceMap;\n+import org.springframework.cloud.bootstrap.BootstrapApplicationListener;\n+import org.springframework.cloud.consul.config.ConsulConfigProperties;\n+import org.springframework.cloud.consul.config.ConsulPropertySource;\n+import org.springframework.context.ApplicationEventPublisher;\n+import org.springframework.context.ApplicationEventPublisherAware;\n+import org.springframework.context.EnvironmentAware;\n+import org.springframework.core.env.CompositePropertySource;\n+import org.springframework.core.env.ConfigurableEnvironment;\n+import org.springframework.core.env.Environment;\n+import org.springframework.core.env.PropertySource;\n+import org.springframework.scheduling.annotation.Scheduled;\n+\n+import com.ecwid.consul.v1.QueryParams;\n+import com.ecwid.consul.v1.Response;\n+import com.ecwid.consul.v1.kv.model.GetValue;\n+\n+import lombok.extern.slf4j.Slf4j;\n+\n+import static org.springframework.util.Base64Utils.decodeFromString;\n+\n+/**\n+ * A kv store watch that publishes an EnvironmentChangeEvent whenever a value under one of the property sources is changed\n+ * \n+ * @author Andrew DePompa\n+ */\n+@Slf4j\n+public class ConsulConfigWatch implements ApplicationEventPublisherAware, EnvironmentAware {\n+\n+\tprivate ApplicationEventPublisher publisher;\n+\n+\tprivate ConfigurableEnvironment environment;\n+\n+\tprivate Set<ConsulPropertySource> consulPropertySources = null;\n+\n+\tprivate ConsulConfigProperties properties;\n+\t\n+\tprivate Map<String, BigInteger> kvIndexes = new ReferenceMap<String, BigInteger>(ReferenceStrength.SOFT, ReferenceStrength.SOFT);\n+\n+\tpublic ConsulConfigWatch(ConsulConfigProperties properties) {\n+\t\tthis.properties = properties;\n+\t}\n+\n+\t@SuppressWarnings(\"boxing\")\n+\t@Scheduled(fixedDelayString = \"${spring.cloud.consul.config.kvWatchDelay:10}\")\n+\tpublic void kvWatch() {\n+\t\tgetConsulPropertySources();\n+\t\tMap<String, String> changedProps = new HashMap<>();\n+\t\tfor (ConsulPropertySource source : consulPropertySources) {\n+\t\t\tlong index = -1;\n+\t\t\tif(kvIndexes.get(source.getName()) != null) {\n+\t\t\t\tindex = kvIndexes.get(source.getName()).longValue();\n+\t\t\t}\n+\t\t\tResponse<List<GetValue>> response = source.getSource().getKVValues(source.getContext(),\n+\t\t\t\t\tnew QueryParams(properties.getKvWatchTimeout(), index));\n+\t\t\tLong consulIndex = response.getConsulIndex();\n+\t\t\tif (consulIndex != null) {\n+\t\t\t\tkvIndexes.put(source.getName(), BigInteger.valueOf(consulIndex));\n+\t\t\t\tif (index != consulIndex) {\n+\t\t\t\t\tif (response.getValue() != null) {\n+\t\t\t\t\t\tSet<String> existingProps = new HashSet<String>(Arrays.asList(source.getPropertyNames()));\n+\t\t\t\t\t\tfor (GetValue getValue : response.getValue()) {\n+\t\t\t\t\t\t\tif (getValue.getModifyIndex() > index) {\n+\t\t\t\t\t\t\t\tchangedProps.put(getValue.getKey(), getValue.getValue() == null ? \"\" : new String(decodeFromString(getValue.getValue())));\n+\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t\texistingProps.remove(getValue.getKey().replace(source.getContext(), \"\").replace(\"/\", \".\"));\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\tfor(String key : existingProps){\n+\t\t\t\t\t\t\tchangedProps.put(key, null);\n+\t\t\t\t\t\t}\n+\t\t\t\t\t}\n+\t\t\t\t}\n+\t\t\t}\n+\t\t}\n+\t\tif (changedProps.size() > 0) {\n+\t\t\tlog.trace(\"Received kv update from consul: {}\", changedProps.toString());\n+\t\t\tpublisher.publishEvent(new ConsulKeyValueChangeEvent(changedProps));\n+\t\t}\n+\t}\n+\n+\tprivate void getConsulPropertySources() {","path":"spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/watch/ConsulConfigWatch.java","position":108,"original_position":108,"commit_id":"e2f694d716aa400ecdafa40bec3d0b034c8e1ba9","original_commit_id":"e2f694d716aa400ecdafa40bec3d0b034c8e1ba9","user":{"login":"spencergibb","id":594085,"avatar_url":"https://avatars.githubusercontent.com/u/594085?v=3","gravatar_id":"","url":"https://api.github.com/users/spencergibb","html_url":"https://github.com/spencergibb","followers_url":"https://api.github.com/users/spencergibb/followers","following_url":"https://api.github.com/users/spencergibb/following{/other_user}","gists_url":"https://api.github.com/users/spencergibb/gists{/gist_id}","starred_url":"https://api.github.com/users/spencergibb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spencergibb/subscriptions","organizations_url":"https://api.github.com/users/spencergibb/orgs","repos_url":"https://api.github.com/users/spencergibb/repos","events_url":"https://api.github.com/users/spencergibb/events{/privacy}","received_events_url":"https://api.github.com/users/spencergibb/received_events","type":"User","site_admin":false},"body":"If it doesn't return anything, don't call it `get`, maybe find.","created_at":"2015-11-11T19:14:36Z","updated_at":"2015-11-11T19:14:36Z","html_url":"https://github.com/spring-cloud/spring-cloud-consul/pull/97#discussion_r44572814","pull_request_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/97","_links":{"self":{"href":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/comments/44572814"},"html":{"href":"https://github.com/spring-cloud/spring-cloud-consul/pull/97#discussion_r44572814"},"pull_request":{"href":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/97"}}},"pull_request":{"url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/97","id":47543419,"html_url":"https://github.com/spring-cloud/spring-cloud-consul/pull/97","diff_url":"https://github.com/spring-cloud/spring-cloud-consul/pull/97.diff","patch_url":"https://github.com/spring-cloud/spring-cloud-consul/pull/97.patch","issue_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/issues/97","number":97,"state":"open","locked":false,"title":"Adds configurable kv watch and listener","user":{"login":"toast2e","id":2729779,"avatar_url":"https://avatars.githubusercontent.com/u/2729779?v=3","gravatar_id":"","url":"https://api.github.com/users/toast2e","html_url":"https://github.com/toast2e","followers_url":"https://api.github.com/users/toast2e/followers","following_url":"https://api.github.com/users/toast2e/following{/other_user}","gists_url":"https://api.github.com/users/toast2e/gists{/gist_id}","starred_url":"https://api.github.com/users/toast2e/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/toast2e/subscriptions","organizations_url":"https://api.github.com/users/toast2e/orgs","repos_url":"https://api.github.com/users/toast2e/repos","events_url":"https://api.github.com/users/toast2e/events{/privacy}","received_events_url":"https://api.github.com/users/toast2e/received_events","type":"User","site_admin":false},"body":"Pulled fresh copy of project and added the kv watch on top. No more issues with 3 way merge. Also updated the event thrown to contain a full map of changed keys and values at the request of a user.","created_at":"2015-10-13T14:52:09Z","updated_at":"2015-11-11T19:14:36Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b4a3a4e2433c2a96aa781b9609d919a75dfe2975","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/97/commits","review_comments_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/97/comments","review_comment_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/comments{/number}","comments_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/issues/97/comments","statuses_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/statuses/e2f694d716aa400ecdafa40bec3d0b034c8e1ba9","head":{"label":"toast2e:master","ref":"master","sha":"e2f694d716aa400ecdafa40bec3d0b034c8e1ba9","user":{"login":"toast2e","id":2729779,"avatar_url":"https://avatars.githubusercontent.com/u/2729779?v=3","gravatar_id":"","url":"https://api.github.com/users/toast2e","html_url":"https://github.com/toast2e","followers_url":"https://api.github.com/users/toast2e/followers","following_url":"https://api.github.com/users/toast2e/following{/other_user}","gists_url":"https://api.github.com/users/toast2e/gists{/gist_id}","starred_url":"https://api.github.com/users/toast2e/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/toast2e/subscriptions","organizations_url":"https://api.github.com/users/toast2e/orgs","repos_url":"https://api.github.com/users/toast2e/repos","events_url":"https://api.github.com/users/toast2e/events{/privacy}","received_events_url":"https://api.github.com/users/toast2e/received_events","type":"User","site_admin":false},"repo":{"id":44183178,"name":"spring-cloud-consul","full_name":"toast2e/spring-cloud-consul","owner":{"login":"toast2e","id":2729779,"avatar_url":"https://avatars.githubusercontent.com/u/2729779?v=3","gravatar_id":"","url":"https://api.github.com/users/toast2e","html_url":"https://github.com/toast2e","followers_url":"https://api.github.com/users/toast2e/followers","following_url":"https://api.github.com/users/toast2e/following{/other_user}","gists_url":"https://api.github.com/users/toast2e/gists{/gist_id}","starred_url":"https://api.github.com/users/toast2e/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/toast2e/subscriptions","organizations_url":"https://api.github.com/users/toast2e/orgs","repos_url":"https://api.github.com/users/toast2e/repos","events_url":"https://api.github.com/users/toast2e/events{/privacy}","received_events_url":"https://api.github.com/users/toast2e/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/toast2e/spring-cloud-consul","description":"Developer preview of Spring Cloud Consul","fork":true,"url":"https://api.github.com/repos/toast2e/spring-cloud-consul","forks_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/forks","keys_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/keys{/key_id}","collaborators_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/teams","hooks_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/hooks","issue_events_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/issues/events{/number}","events_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/events","assignees_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/assignees{/user}","branches_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/branches{/branch}","tags_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/tags","blobs_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/git/refs{/sha}","trees_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/git/trees{/sha}","statuses_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/statuses/{sha}","languages_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/languages","stargazers_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/stargazers","contributors_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/contributors","subscribers_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/subscribers","subscription_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/subscription","commits_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/commits{/sha}","git_commits_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/git/commits{/sha}","comments_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/comments{/number}","issue_comment_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/issues/comments{/number}","contents_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/contents/{+path}","compare_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/compare/{base}...{head}","merges_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/merges","archive_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/downloads","issues_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/issues{/number}","pulls_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/pulls{/number}","milestones_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/milestones{/number}","notifications_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/labels{/name}","releases_url":"https://api.github.com/repos/toast2e/spring-cloud-consul/releases{/id}","created_at":"2015-10-13T14:47:10Z","updated_at":"2015-10-13T14:47:11Z","pushed_at":"2015-10-13T14:49:21Z","git_url":"git://github.com/toast2e/spring-cloud-consul.git","ssh_url":"git@github.com:toast2e/spring-cloud-consul.git","clone_url":"https://github.com/toast2e/spring-cloud-consul.git","svn_url":"https://github.com/toast2e/spring-cloud-consul","homepage":null,"size":1593,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"spring-cloud:master","ref":"master","sha":"915da382e7aecd6794c73af7514fc84612f6d463","user":{"login":"spring-cloud","id":7815877,"avatar_url":"https://avatars.githubusercontent.com/u/7815877?v=3","gravatar_id":"","url":"https://api.github.com/users/spring-cloud","html_url":"https://github.com/spring-cloud","followers_url":"https://api.github.com/users/spring-cloud/followers","following_url":"https://api.github.com/users/spring-cloud/following{/other_user}","gists_url":"https://api.github.com/users/spring-cloud/gists{/gist_id}","starred_url":"https://api.github.com/users/spring-cloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spring-cloud/subscriptions","organizations_url":"https://api.github.com/users/spring-cloud/orgs","repos_url":"https://api.github.com/users/spring-cloud/repos","events_url":"https://api.github.com/users/spring-cloud/events{/privacy}","received_events_url":"https://api.github.com/users/spring-cloud/received_events","type":"Organization","site_admin":false},"repo":{"id":27404315,"name":"spring-cloud-consul","full_name":"spring-cloud/spring-cloud-consul","owner":{"login":"spring-cloud","id":7815877,"avatar_url":"https://avatars.githubusercontent.com/u/7815877?v=3","gravatar_id":"","url":"https://api.github.com/users/spring-cloud","html_url":"https://github.com/spring-cloud","followers_url":"https://api.github.com/users/spring-cloud/followers","following_url":"https://api.github.com/users/spring-cloud/following{/other_user}","gists_url":"https://api.github.com/users/spring-cloud/gists{/gist_id}","starred_url":"https://api.github.com/users/spring-cloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spring-cloud/subscriptions","organizations_url":"https://api.github.com/users/spring-cloud/orgs","repos_url":"https://api.github.com/users/spring-cloud/repos","events_url":"https://api.github.com/users/spring-cloud/events{/privacy}","received_events_url":"https://api.github.com/users/spring-cloud/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/spring-cloud/spring-cloud-consul","description":"Developer preview of Spring Cloud Consul","fork":false,"url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul","forks_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/forks","keys_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/keys{/key_id}","collaborators_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/teams","hooks_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/hooks","issue_events_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/issues/events{/number}","events_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/events","assignees_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/assignees{/user}","branches_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/branches{/branch}","tags_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/tags","blobs_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/git/refs{/sha}","trees_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/git/trees{/sha}","statuses_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/statuses/{sha}","languages_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/languages","stargazers_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/stargazers","contributors_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/contributors","subscribers_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/subscribers","subscription_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/subscription","commits_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/commits{/sha}","git_commits_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/git/commits{/sha}","comments_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/comments{/number}","issue_comment_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/issues/comments{/number}","contents_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/contents/{+path}","compare_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/compare/{base}...{head}","merges_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/merges","archive_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/downloads","issues_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/issues{/number}","pulls_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls{/number}","milestones_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/milestones{/number}","notifications_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/labels{/name}","releases_url":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/releases{/id}","created_at":"2014-12-01T22:56:34Z","updated_at":"2015-11-10T03:13:12Z","pushed_at":"2015-11-11T18:59:44Z","git_url":"git://github.com/spring-cloud/spring-cloud-consul.git","ssh_url":"git@github.com:spring-cloud/spring-cloud-consul.git","clone_url":"https://github.com/spring-cloud/spring-cloud-consul.git","svn_url":"https://github.com/spring-cloud/spring-cloud-consul","homepage":null,"size":2522,"stargazers_count":68,"watchers_count":68,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":44,"mirror_url":null,"open_issues_count":23,"forks":44,"open_issues":23,"watchers":68,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/97"},"html":{"href":"https://github.com/spring-cloud/spring-cloud-consul/pull/97"},"issue":{"href":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/issues/97"},"comments":{"href":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/issues/97/comments"},"review_comments":{"href":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/97/comments"},"review_comment":{"href":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/pulls/97/commits"},"statuses":{"href":"https://api.github.com/repos/spring-cloud/spring-cloud-consul/statuses/e2f694d716aa400ecdafa40bec3d0b034c8e1ba9"}}}}