brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Active partial rebind allowing ca...
Date Mon, 09 Feb 2015 13:04:06 GMT
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<BrooklynClassLoadingContext> contexts = new Stack<BrooklynClassLoadingContext>();
    +    Stack<ClassLoader> cls = new Stack<ClassLoader>();
    +    AtomicReference<Thread> xstreamLockOwner = new AtomicReference<Thread>();
    +    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.
---

Mime
View raw message