{"action":"created","comment":{"url":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/comments/24326247","id":24326247,"diff_hunk":"@@ -341,5 +353,151 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co\n             return lookupContext.lookupManagementContext();\n         }\n     }\n+\n+    /** When reading/writing specs, it checks whether there is a catalog item id set and uses it to load */\n+    public class SpecConverter extends ReflectionConverter {\n+        SpecConverter() {\n+            super(xstream.getMapper(), xstream.getReflectionProvider());\n+        }\n+        @Override\n+        public boolean canConvert(@SuppressWarnings(\"rawtypes\") Class type) {\n+            return AbstractBrooklynObjectSpec.class.isAssignableFrom(type);\n+        }\n+        @Override\n+        public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {\n+            if (source == null) return;\n+            AbstractBrooklynObjectSpec<?, ?> spec = (AbstractBrooklynObjectSpec<?, ?>) source;\n+            String catalogItemId = spec.getCatalogItemId();\n+            if (Strings.isNonBlank(catalogItemId)) {\n+                // write this field first, so we can peek at it when we read\n+                writer.startNode(\"catalogItemId\");\n+                writer.setValue(catalogItemId);\n+                writer.endNode();\n+                \n+                // we're going to write the catalogItemId field twice :( but that's okay.\n+                // better solution would be to have mark/reset on reader so we can peek for such a field;\n+                // see comment below\n+                super.marshal(source, writer, context);\n+            } else {\n+                super.marshal(source, writer, context);\n+            }\n+        }\n+        @Override\n+        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {\n+            String catalogItemId = null;\n+            instantiateNewInstanceSettingCache(reader, context);\n+            \n+            if (reader instanceof PathTrackingReader) {\n+                // have to assume this is first; there is no mark/reset support on these readers\n+                // (if there were then it would be easier, we could just look for that child anywhere,\n+                // and not need a custom writer!)\n+                if (\"catalogItemId\".equals( ((PathTrackingReader)reader).peekNextChild() )) {\n+                    // cache the instance\n+                    \n+                    reader.moveDown();\n+                    catalogItemId = reader.getValue();\n+                    reader.moveUp();\n+                }\n+            }\n+            boolean customLoaderSet = false;\n+            try {\n+                if (Strings.isNonBlank(catalogItemId)) {\n+                    if (lookupContext==null) throw new NullPointerException(\"lookupContext required to load catalog item \"+catalogItemId);\n+                    CatalogItem<?, ?> cat = CatalogUtils.getCatalogItemOptionalVersion(lookupContext.lookupManagementContext(), catalogItemId);\n+                    if (cat==null) throw new NoSuchElementException(\"catalog item: \"+catalogItemId);\n+                    BrooklynClassLoadingContext clcNew = CatalogUtils.newClassLoadingContext(lookupContext.lookupManagementContext(), cat);\n+                    pushXstreamCustomClassLoader(clcNew);\n+                    customLoaderSet = true;\n+                }\n+                \n+                AbstractBrooklynObjectSpec<?, ?> result = (AbstractBrooklynObjectSpec<?, ?>) super.unmarshal(reader, context);\n+                // we wrote it twice so this shouldn't be necessary; but if we fix it so we only write once, we'd need this\n+                result.catalogItemId(catalogItemId);\n+                return result;\n+            } finally {\n+                instance = null;\n+                if (customLoaderSet) {\n+                    popXstreamCustomClassLoader();\n+                }\n+            }\n+        }\n+\n+        Object instance;\n+        \n+        @Override\n+        protected Object instantiateNewInstance(HierarchicalStreamReader reader, UnmarshallingContext context) {\n+            // the super calls getAttribute which requires that we have not yet done moveDown,\n+            // so we do this earlier and cache it for when we call super.unmarshal\n+            if (instance==null)\n+                throw new IllegalStateException(\"Instance should be created and cached\");\n+            return instance;\n+        }\n+        protected void instantiateNewInstanceSettingCache(HierarchicalStreamReader reader, UnmarshallingContext context) {\n+            instance = super.instantiateNewInstance(reader, context);\n+        }\n+    }\n+    \n+    Stack<BrooklynClassLoadingContext> contexts = new Stack<BrooklynClassLoadingContext>();\n+    Stack<ClassLoader> cls = new Stack<ClassLoader>();\n+    AtomicReference<Thread> xstreamLockOwner = new AtomicReference<Thread>();\n+    int lockCount;\n     \n+    /** Must be accompanied by a corresponding {@link #popXstreamCustomClassLoader()} when finished. */\n+    @SuppressWarnings(\"deprecation\")\n+    protected void pushXstreamCustomClassLoader(BrooklynClassLoadingContext clcNew) {\n+        acquireXstreamLock();\n+        BrooklynClassLoadingContext oldClc;\n+        if (!contexts.isEmpty()) {\n+            oldClc = contexts.peek();","path":"core/src/main/java/brooklyn/entity/rebind/persister/XmlMementoSerializer.java","position":157,"original_position":157,"commit_id":"491ca49efdfe4f8a7bb2bf5cbba5cd592bf4798a","original_commit_id":"71c82da572ae28d73c31f1c102f4ac4c5a7f2280","user":{"login":"aledsage","id":593113,"avatar_url":"https://avatars.githubusercontent.com/u/593113?v=3","gravatar_id":"","url":"https://api.github.com/users/aledsage","html_url":"https://github.com/aledsage","followers_url":"https://api.github.com/users/aledsage/followers","following_url":"https://api.github.com/users/aledsage/following{/other_user}","gists_url":"https://api.github.com/users/aledsage/gists{/gist_id}","starred_url":"https://api.github.com/users/aledsage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aledsage/subscriptions","organizations_url":"https://api.github.com/users/aledsage/orgs","repos_url":"https://api.github.com/users/aledsage/repos","events_url":"https://api.github.com/users/aledsage/events{/privacy}","received_events_url":"https://api.github.com/users/aledsage/received_events","type":"User","site_admin":false},"body":"Looks dangerous; no synchronization on `xstreamLockOwner` so is assuming no other thread will have popped in between the check to `isEmpty` and `peek`?","created_at":"2015-02-09T13:05:54Z","updated_at":"2015-02-09T13:05:54Z","html_url":"https://github.com/apache/incubator-brooklyn/pull/506#discussion_r24326247","pull_request_url":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/506","_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/comments/24326247"},"html":{"href":"https://github.com/apache/incubator-brooklyn/pull/506#discussion_r24326247"},"pull_request":{"href":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/506"}}},"pull_request":{"url":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/506","id":28806295,"html_url":"https://github.com/apache/incubator-brooklyn/pull/506","diff_url":"https://github.com/apache/incubator-brooklyn/pull/506.diff","patch_url":"https://github.com/apache/incubator-brooklyn/pull/506.patch","issue_url":"https://api.github.com/repos/apache/incubator-brooklyn/issues/506","number":506,"state":"open","locked":false,"title":"Active partial rebind allowing catalog item version change dynamically","user":{"login":"ahgittin","id":496540,"avatar_url":"https://avatars.githubusercontent.com/u/496540?v=3","gravatar_id":"","url":"https://api.github.com/users/ahgittin","html_url":"https://github.com/ahgittin","followers_url":"https://api.github.com/users/ahgittin/followers","following_url":"https://api.github.com/users/ahgittin/following{/other_user}","gists_url":"https://api.github.com/users/ahgittin/gists{/gist_id}","starred_url":"https://api.github.com/users/ahgittin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahgittin/subscriptions","organizations_url":"https://api.github.com/users/ahgittin/orgs","repos_url":"https://api.github.com/users/ahgittin/repos","events_url":"https://api.github.com/users/ahgittin/events{/privacy}","received_events_url":"https://api.github.com/users/ahgittin/received_events","type":"User","site_admin":false},"body":"No GUI support (yet) for changing versions, but you can test this in groovy console with\r\n\r\n    mgmt.rebindManager.rebindPartialActive(\r\n        brooklyn.entity.rebind.transformer.CompoundTransformer.builder().changeCatalogItemId(\r\n            \"item\", \"1.0\", \"item\", \"2.0\" /* old and new catalog item+version */).build(),\r\n        \"abcd1234\" /* entity id */);\r\n","created_at":"2015-02-06T12:53:37Z","updated_at":"2015-02-09T13:05:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fc672aaa976ab70a0b9d67030ece75dfd6d0298f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/506/commits","review_comments_url":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/506/comments","review_comment_url":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/comments/{number}","comments_url":"https://api.github.com/repos/apache/incubator-brooklyn/issues/506/comments","statuses_url":"https://api.github.com/repos/apache/incubator-brooklyn/statuses/491ca49efdfe4f8a7bb2bf5cbba5cd592bf4798a","head":{"label":"ahgittin:versioning-switch-on-rebind","ref":"versioning-switch-on-rebind","sha":"491ca49efdfe4f8a7bb2bf5cbba5cd592bf4798a","user":{"login":"ahgittin","id":496540,"avatar_url":"https://avatars.githubusercontent.com/u/496540?v=3","gravatar_id":"","url":"https://api.github.com/users/ahgittin","html_url":"https://github.com/ahgittin","followers_url":"https://api.github.com/users/ahgittin/followers","following_url":"https://api.github.com/users/ahgittin/following{/other_user}","gists_url":"https://api.github.com/users/ahgittin/gists{/gist_id}","starred_url":"https://api.github.com/users/ahgittin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahgittin/subscriptions","organizations_url":"https://api.github.com/users/ahgittin/orgs","repos_url":"https://api.github.com/users/ahgittin/repos","events_url":"https://api.github.com/users/ahgittin/events{/privacy}","received_events_url":"https://api.github.com/users/ahgittin/received_events","type":"User","site_admin":false},"repo":{"id":21164564,"name":"incubator-brooklyn","full_name":"ahgittin/incubator-brooklyn","owner":{"login":"ahgittin","id":496540,"avatar_url":"https://avatars.githubusercontent.com/u/496540?v=3","gravatar_id":"","url":"https://api.github.com/users/ahgittin","html_url":"https://github.com/ahgittin","followers_url":"https://api.github.com/users/ahgittin/followers","following_url":"https://api.github.com/users/ahgittin/following{/other_user}","gists_url":"https://api.github.com/users/ahgittin/gists{/gist_id}","starred_url":"https://api.github.com/users/ahgittin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahgittin/subscriptions","organizations_url":"https://api.github.com/users/ahgittin/orgs","repos_url":"https://api.github.com/users/ahgittin/repos","events_url":"https://api.github.com/users/ahgittin/events{/privacy}","received_events_url":"https://api.github.com/users/ahgittin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ahgittin/incubator-brooklyn","description":"Mirror of Apache Brooklyn","fork":true,"url":"https://api.github.com/repos/ahgittin/incubator-brooklyn","forks_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/forks","keys_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/teams","hooks_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/hooks","issue_events_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/issues/events{/number}","events_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/events","assignees_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/assignees{/user}","branches_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/branches{/branch}","tags_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/tags","blobs_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/git/refs{/sha}","trees_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/statuses/{sha}","languages_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/languages","stargazers_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/stargazers","contributors_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/contributors","subscribers_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/subscribers","subscription_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/subscription","commits_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/commits{/sha}","git_commits_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/git/commits{/sha}","comments_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/comments{/number}","issue_comment_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/issues/comments/{number}","contents_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/contents/{+path}","compare_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/merges","archive_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/downloads","issues_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/issues{/number}","pulls_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/pulls{/number}","milestones_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/milestones{/number}","notifications_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/labels{/name}","releases_url":"https://api.github.com/repos/ahgittin/incubator-brooklyn/releases{/id}","created_at":"2014-06-24T12:59:32Z","updated_at":"2015-02-06T14:02:59Z","pushed_at":"2015-02-09T12:32:42Z","git_url":"git://github.com/ahgittin/incubator-brooklyn.git","ssh_url":"git@github.com:ahgittin/incubator-brooklyn.git","clone_url":"https://github.com/ahgittin/incubator-brooklyn.git","svn_url":"https://github.com/ahgittin/incubator-brooklyn","homepage":null,"size":235579,"stargazers_count":1,"watchers_count":1,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"21d9a91fc31bb4b4b93cc77800cbe6643642cb01","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":19368221,"name":"incubator-brooklyn","full_name":"apache/incubator-brooklyn","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/incubator-brooklyn","description":"Mirror of Apache Brooklyn","fork":false,"url":"https://api.github.com/repos/apache/incubator-brooklyn","forks_url":"https://api.github.com/repos/apache/incubator-brooklyn/forks","keys_url":"https://api.github.com/repos/apache/incubator-brooklyn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/incubator-brooklyn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/incubator-brooklyn/teams","hooks_url":"https://api.github.com/repos/apache/incubator-brooklyn/hooks","issue_events_url":"https://api.github.com/repos/apache/incubator-brooklyn/issues/events{/number}","events_url":"https://api.github.com/repos/apache/incubator-brooklyn/events","assignees_url":"https://api.github.com/repos/apache/incubator-brooklyn/assignees{/user}","branches_url":"https://api.github.com/repos/apache/incubator-brooklyn/branches{/branch}","tags_url":"https://api.github.com/repos/apache/incubator-brooklyn/tags","blobs_url":"https://api.github.com/repos/apache/incubator-brooklyn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/incubator-brooklyn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/incubator-brooklyn/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/incubator-brooklyn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/incubator-brooklyn/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/incubator-brooklyn/languages","stargazers_url":"https://api.github.com/repos/apache/incubator-brooklyn/stargazers","contributors_url":"https://api.github.com/repos/apache/incubator-brooklyn/contributors","subscribers_url":"https://api.github.com/repos/apache/incubator-brooklyn/subscribers","subscription_url":"https://api.github.com/repos/apache/incubator-brooklyn/subscription","commits_url":"https://api.github.com/repos/apache/incubator-brooklyn/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/incubator-brooklyn/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/incubator-brooklyn/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/incubator-brooklyn/issues/comments/{number}","contents_url":"https://api.github.com/repos/apache/incubator-brooklyn/contents/{+path}","compare_url":"https://api.github.com/repos/apache/incubator-brooklyn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/incubator-brooklyn/merges","archive_url":"https://api.github.com/repos/apache/incubator-brooklyn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/incubator-brooklyn/downloads","issues_url":"https://api.github.com/repos/apache/incubator-brooklyn/issues{/number}","pulls_url":"https://api.github.com/repos/apache/incubator-brooklyn/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/incubator-brooklyn/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/incubator-brooklyn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/incubator-brooklyn/labels{/name}","releases_url":"https://api.github.com/repos/apache/incubator-brooklyn/releases{/id}","created_at":"2014-05-02T07:00:07Z","updated_at":"2015-02-09T10:47:39Z","pushed_at":"2015-02-09T10:47:39Z","git_url":"git://github.com/apache/incubator-brooklyn.git","ssh_url":"git@github.com:apache/incubator-brooklyn.git","clone_url":"https://github.com/apache/incubator-brooklyn.git","svn_url":"https://github.com/apache/incubator-brooklyn","homepage":null,"size":245340,"stargazers_count":47,"watchers_count":47,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":45,"mirror_url":"git://git.apache.org/incubator-brooklyn.git","open_issues_count":17,"forks":45,"open_issues":17,"watchers":47,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/506"},"html":{"href":"https://github.com/apache/incubator-brooklyn/pull/506"},"issue":{"href":"https://api.github.com/repos/apache/incubator-brooklyn/issues/506"},"comments":{"href":"https://api.github.com/repos/apache/incubator-brooklyn/issues/506/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/506/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/apache/incubator-brooklyn/pulls/506/commits"},"statuses":{"href":"https://api.github.com/repos/apache/incubator-brooklyn/statuses/491ca49efdfe4f8a7bb2bf5cbba5cd592bf4798a"}}}}