Return-Path: X-Original-To: apmail-brooklyn-dev-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8A8A11010A for ; Mon, 9 Feb 2015 13:04:28 +0000 (UTC) Received: (qmail 34429 invoked by uid 500); 9 Feb 2015 13:04:28 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 34391 invoked by uid 500); 9 Feb 2015 13:04:28 -0000 Mailing-List: contact dev-help@brooklyn.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.incubator.apache.org Delivered-To: mailing list dev@brooklyn.incubator.apache.org Received: (qmail 34380 invoked by uid 99); 9 Feb 2015 13:04:28 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 09 Feb 2015 13:04:28 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 09 Feb 2015 13:04:26 +0000 Received: (qmail 34301 invoked by uid 99); 9 Feb 2015 13:04:06 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 09 Feb 2015 13:04:06 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 39EDCE01CE; Mon, 9 Feb 2015 13:04:06 +0000 (UTC) From: aledsage To: dev@brooklyn.incubator.apache.org Reply-To: dev@brooklyn.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-brooklyn pull request: Active partial rebind allowing ca... Content-Type: text/plain Message-Id: <20150209130406.39EDCE01CE@git1-us-west.apache.org> Date: Mon, 9 Feb 2015 13:04:06 +0000 (UTC) X-Virus-Checked: Checked by ClamAV on apache.org Github user aledsage commented on a diff in the pull request: https://github.com/apache/incubator-brooklyn/pull/506#discussion_r24326173 --- Diff: core/src/main/java/brooklyn/entity/rebind/persister/XmlMementoSerializer.java --- @@ -341,5 +353,151 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co return lookupContext.lookupManagementContext(); } } + + /** When reading/writing specs, it checks whether there is a catalog item id set and uses it to load */ + public class SpecConverter extends ReflectionConverter { + SpecConverter() { + super(xstream.getMapper(), xstream.getReflectionProvider()); + } + @Override + public boolean canConvert(@SuppressWarnings("rawtypes") Class type) { + return AbstractBrooklynObjectSpec.class.isAssignableFrom(type); + } + @Override + public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { + if (source == null) return; + AbstractBrooklynObjectSpec spec = (AbstractBrooklynObjectSpec) source; + String catalogItemId = spec.getCatalogItemId(); + if (Strings.isNonBlank(catalogItemId)) { + // write this field first, so we can peek at it when we read + writer.startNode("catalogItemId"); + writer.setValue(catalogItemId); + writer.endNode(); + + // we're going to write the catalogItemId field twice :( but that's okay. + // better solution would be to have mark/reset on reader so we can peek for such a field; + // see comment below + super.marshal(source, writer, context); + } else { + super.marshal(source, writer, context); + } + } + @Override + public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { + String catalogItemId = null; + instantiateNewInstanceSettingCache(reader, context); + + if (reader instanceof PathTrackingReader) { + // have to assume this is first; there is no mark/reset support on these readers + // (if there were then it would be easier, we could just look for that child anywhere, + // and not need a custom writer!) + if ("catalogItemId".equals( ((PathTrackingReader)reader).peekNextChild() )) { + // cache the instance + + reader.moveDown(); + catalogItemId = reader.getValue(); + reader.moveUp(); + } + } + boolean customLoaderSet = false; + try { + if (Strings.isNonBlank(catalogItemId)) { + if (lookupContext==null) throw new NullPointerException("lookupContext required to load catalog item "+catalogItemId); + CatalogItem cat = CatalogUtils.getCatalogItemOptionalVersion(lookupContext.lookupManagementContext(), catalogItemId); + if (cat==null) throw new NoSuchElementException("catalog item: "+catalogItemId); + BrooklynClassLoadingContext clcNew = CatalogUtils.newClassLoadingContext(lookupContext.lookupManagementContext(), cat); + pushXstreamCustomClassLoader(clcNew); + customLoaderSet = true; + } + + AbstractBrooklynObjectSpec result = (AbstractBrooklynObjectSpec) super.unmarshal(reader, context); + // we wrote it twice so this shouldn't be necessary; but if we fix it so we only write once, we'd need this + result.catalogItemId(catalogItemId); + return result; + } finally { + instance = null; + if (customLoaderSet) { + popXstreamCustomClassLoader(); + } + } + } + + Object instance; + + @Override + protected Object instantiateNewInstance(HierarchicalStreamReader reader, UnmarshallingContext context) { + // the super calls getAttribute which requires that we have not yet done moveDown, + // so we do this earlier and cache it for when we call super.unmarshal + if (instance==null) + throw new IllegalStateException("Instance should be created and cached"); + return instance; + } + protected void instantiateNewInstanceSettingCache(HierarchicalStreamReader reader, UnmarshallingContext context) { + instance = super.instantiateNewInstance(reader, context); + } + } + + Stack contexts = new Stack(); + Stack cls = new Stack(); + AtomicReference xstreamLockOwner = new AtomicReference(); + int lockCount; + /** Must be accompanied by a corresponding {@link #popXstreamCustomClassLoader()} when finished. */ + @SuppressWarnings("deprecation") + protected void pushXstreamCustomClassLoader(BrooklynClassLoadingContext clcNew) { + acquireXstreamLock(); + BrooklynClassLoadingContext oldClc; + if (!contexts.isEmpty()) { + oldClc = contexts.peek(); + } else { + // TODO XmlMementoSerializer should take a BCLC instead of a CL + oldClc = JavaBrooklynClassLoadingContext.create(lookupContext.lookupManagementContext(), xstream.getClassLoader()); + } + BrooklynClassLoadingContextSequential clcMerged = new BrooklynClassLoadingContextSequential(lookupContext.lookupManagementContext(), + oldClc, clcNew); + contexts.push(clcMerged); + cls.push(xstream.getClassLoader()); + ClassLoader newCL = ClassLoaderFromBrooklynClassLoadingContext.of(clcMerged); + xstream.setClassLoader(newCL); + } + + protected void popXstreamCustomClassLoader() { + synchronized (xstreamLockOwner) { + releaseXstreamLock(); + xstream.setClassLoader(cls.pop()); + contexts.pop(); + } + } + + protected void acquireXstreamLock() { + synchronized (xstreamLockOwner) { + while (true) { + if (xstreamLockOwner.compareAndSet(null, Thread.currentThread()) || + Thread.currentThread().equals( xstreamLockOwner.get() )) { + break; + } + try { + xstreamLockOwner.wait(1000); --- End diff -- Why is this calling `wait(1000)` when nothing calls `notifyAll()`? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---