Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/683#discussion_r32022058
  
    --- Diff: core/src/main/java/brooklyn/entity/rebind/RebindIteration.java ---
    @@ -899,48 +914,64 @@ protected void setCatalogItemId(BrooklynObject item, 
String catalogItemId) {
                 }
             }
     
    -        protected <T extends BrooklynObject> Class<? extends T> 
load(Class<T> bType, Memento memento) {
    +        protected <T extends BrooklynObject> LoadedClass<? extends T> 
load(Class<T> bType, Memento memento) {
                 return load(bType, memento.getType(), 
memento.getCatalogItemId(), memento.getId());
             }
    +        
             @SuppressWarnings("unchecked")
    -        protected <T extends BrooklynObject> Class<? extends T> 
load(Class<T> bType, String jType, String catalogItemId, String 
contextSuchAsId) {
    +        protected <T extends BrooklynObject> LoadedClass<? extends T> 
load(Class<T> bType, String jType, String catalogItemId, String 
contextSuchAsId) {
                 checkNotNull(jType, "Type of %s (%s) must not be null", 
contextSuchAsId, bType.getSimpleName());
    +            
                 if (catalogItemId != null) {
    -                BrooklynClassLoadingContext loader = 
getLoadingContextFromCatalogItemId(catalogItemId, classLoader, rebindContext);
    -                return loader.loadClass(jType, bType);
    -            } else {
    -                // we have previously used reflections; not sure if that's 
needed?
    -                try {
    -                    return (Class<T>)reflections.loadClass(jType);
    -                } catch (Exception e) {
    -                    LOG.warn("Unable to load "+jType+" using reflections; 
will try standard context");
    -                }
    -
    -                if 
(BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_INFER_CATALOG_ITEM_ON_REBIND))
 {
    -                    //Try loading from whichever catalog bundle succeeds.
    -                    BrooklynCatalog catalog = 
managementContext.getCatalog();
    -                    for (CatalogItem<?, ?> item : 
catalog.getCatalogItems()) {
    -                        BrooklynClassLoadingContext catalogLoader = 
CatalogUtils.newClassLoadingContext(managementContext, item);
    -                        Maybe<Class<?>> catalogClass = 
catalogLoader.tryLoadClass(jType);
    -                        if (catalogClass.isPresent()) {
    -                            return (Class<? extends T>) catalogClass.get();
    +                CatalogItem<?, ?> catalogItem = 
rebindContext.lookup().lookupCatalogItem(catalogItemId);
    +                if (catalogItem == null) {
    +                    if 
(BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_AUTO_FIX_CATALOG_REF_ON_REBIND))
 {
    +                        // See 
https://issues.apache.org/jira/browse/BROOKLYN-149
    +                        // This is a dangling reference to the catalog 
item (which will have been logged by lookupCatalogItem).
    +                        // Try loading as any version.
    +                        if 
(CatalogUtils.looksLikeVersionedId(catalogItemId)) {
    +                            String symbolicName = 
CatalogUtils.getIdFromVersionedId(catalogItemId);
    +                            catalogItem = 
rebindContext.lookup().lookupCatalogItem(symbolicName);
    +                            
    +                            if (catalogItem != null) {
    +                                LOG.warn("Unable to load catalog item 
"+catalogItemId+" for "+contextSuchAsId
    +                                        +" ("+bType.getSimpleName()+"); 
will auto-upgrade to "+catalogItem.getCatalogItemId());
    +                                catalogItemId = 
catalogItem.getCatalogItemId();
    +                            }
                             }
                         }
    -                    throw new IllegalStateException("No catalogItemId 
specified and can't load class from either classpath of catalog items");
    +                }
    +                if (catalogItem != null) {
    +                    BrooklynClassLoadingContext loader = 
CatalogUtils.newClassLoadingContext(managementContext, catalogItem);
    +                    return new LoadedClass<T>(loader.loadClass(jType, 
bType), catalogItemId);
                     } else {
    -                    throw new IllegalStateException("No catalogItemId 
specified and can't load class from classpath");
    +                    LOG.warn("Unable to load catalog item 
"+catalogItemId+" for "+contextSuchAsId
    +                            +" ("+bType.getSimpleName()+"); will try 
default class loader");
                     }
    -
                 }
    -        }
    +            
    +            try {
    +                return new 
LoadedClass<T>((Class<T>)reflections.loadClass(jType), catalogItemId);
    --- End diff --
    
    Reason I didn't return null was that it would override the `catalogItemId` 
to be persisted, so it would then say null. I didn't want to lose the 
catalogItemId in the situation where it wasn't available. Instead, we'd leave 
that itemId in and fall back to default class loader.
    
    I wondered about guarding it behind the feature enablement. However, 
neither of the (renamed) feature options seem quite right:
    * `FEATURE_BACKWARDS_COMPATIBILITY_INFER_CATALOG_ITEM_ON_REBIND` is about 
backwards compatibility, where the `catalogItemId` was missing from old things.
    * `FEATURE_AUTO_FIX_CATALOG_REF_ON_REBIND` is for updating the ref (I think 
that deleting the ref seems extreme there).
    
    We should revisit this in follow-up pull requests. I'd like us to start up 
such that errors are shown, and can be fixed, rather than aborting entirely or 
just logging and ignoring.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to