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

    https://github.com/apache/incubator-brooklyn/pull/683#discussion_r32023110
  
    --- 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 --
    
    This is possible even now if you use `exceptionHandler` to log the errors. 
Depending on the startup options the process will abort or show a warning in 
the web console.


---
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