protected LoadedClass load()

in core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/RebindIteration.java [1101:1253]


        protected <T extends BrooklynObject> LoadedClass<? extends T> load(Class<T> bType, String jType,
                                                                           String catalogItemId, List<String> searchPath, String contextSuchAsId) {
            checkNotNull(jType, "Type of %s (%s) must not be null", contextSuchAsId, bType.getSimpleName());

            CatalogUpgrades.markerForCodeThatLoadsJavaTypesButShouldLoadRegisteredType();

            List<String> warnings = MutableList.of();
            List<String> reboundSearchPath = MutableList.of();
            if (searchPath != null && !searchPath.isEmpty()) {
                for (String searchItemId : searchPath) {
                    String fixedSearchItemId = null;
                    VersionedName searchItemVersionedName = VersionedName.fromString(searchItemId);

                    OsgiManager osgi = managementContext.getOsgiManager().orNull();

                    String bundleUpgraded = CatalogUpgrades.getBundleUpgradedIfNecessary(managementContext, searchItemId);
                    if (bundleUpgraded!=null && !bundleUpgraded.equals(searchItemId)) {
                        logRebindingDebug("Upgrading search path entry of " + bType.getSimpleName().toLowerCase() + " " + contextSuchAsId + " from " + searchItemId + " to bundle " + bundleUpgraded);
                        searchItemVersionedName = VersionedName.fromString(bundleUpgraded);
                    }

                    if (osgi != null) {
                        ManagedBundle bundle = osgi.getManagedBundle(searchItemVersionedName);
                        if (bundle != null) {
                            // found as bundle
                            fixedSearchItemId = searchItemVersionedName.toOsgiString();
                            reboundSearchPath.add(fixedSearchItemId);
                            continue;
                        }
                    }

                    // look for as a type now
                    RegisteredType t1 = managementContext.getTypeRegistry().get(searchItemId);
                    if (t1 == null) {
                        String newSearchItemId = CatalogUpgrades.getTypeUpgradedIfNecessary(managementContext, searchItemId);
                        if (!newSearchItemId.equals(searchItemId)) {
                            logRebindingDebug("Upgrading search path entry of " + bType.getSimpleName().toLowerCase() + " " + contextSuchAsId + " from " + searchItemId + " to type " + newSearchItemId);
                            searchItemId = newSearchItemId;
                            t1 = managementContext.getTypeRegistry().get(newSearchItemId);
                        }
                    }
                    if (t1 != null) fixedSearchItemId = t1.getId();
                    if (fixedSearchItemId == null) {
                        CatalogItem<?, ?> ci = findCatalogItemInReboundCatalog(bType, searchItemId, contextSuchAsId);
                        if (ci != null) {
                            fixedSearchItemId = ci.getCatalogItemId();
                            logRebindingWarn("Needed rebind catalog to resolve search path entry " + searchItemId + " (now " + fixedSearchItemId + ") for " + bType.getSimpleName().toLowerCase() + " " + contextSuchAsId +
                                    ", persistence should remove this in future but future versions will not support this and definitions should be fixed");
                        } else {
                            // could do some magic if there is a peer with the same version use it, might be handy for snapshots especially
                            // (snapshots upgrading bundle version "*" drop the qualifier so don't replace peers;
                            // but as soon as you have a larger datestamp, it will replace the previous ones)
                            logRebindingWarn("Could not find search path entry " + searchItemId + " for " + bType.getSimpleName().toLowerCase() + " " + contextSuchAsId + ", ignoring");
                        }
                    }
                    if (fixedSearchItemId != null) {
                        reboundSearchPath.add(fixedSearchItemId);
                    } else {
                        warnings.add("unable to resolve search path entry " + searchItemId);
                    }
                }
            }

            if (catalogItemId != null) {
                String transformedCatalogItemId = null;

                Maybe<RegisteredType> contextRegisteredType = managementContext.getTypeRegistry().getMaybe(catalogItemId,
                        // this is context RT, not item we are loading, so bType does not apply here
                        // if we were instantiating from an RT instead of a JT (ideal) then we would use bType to filter
                        null);
                if (contextRegisteredType.isAbsent()) {
                    transformedCatalogItemId = CatalogUpgrades.getTypeUpgradedIfNecessary(managementContext, catalogItemId);
                    if (!transformedCatalogItemId.equals(catalogItemId)) {
                        // catalog item id is sometimes the type of the item, but sometimes just the first part of the search path
                        logRebindingInfo("Upgrading " + bType.getSimpleName().toLowerCase() + " " + contextSuchAsId +
                                " stored catalog item context on rebind" +
                                " from " + catalogItemId + " to " + transformedCatalogItemId);

                        // again ignore bType
                        contextRegisteredType = managementContext.getTypeRegistry().getMaybe(transformedCatalogItemId, null);

                    } else {
                        transformedCatalogItemId = null;
                    }
                }

                if (contextRegisteredType.isPresent()) {
                    transformedCatalogItemId = contextRegisteredType.get().getId();
                } else {
                    CatalogItem<?, ?> catalogItem = findCatalogItemInReboundCatalog(bType, catalogItemId, contextSuchAsId);
                    if (catalogItem != null) {
                        transformedCatalogItemId = catalogItem.getCatalogItemId();
                    }
                }
                if (transformedCatalogItemId != null) {
                    try {
                        BrooklynClassLoadingContextSequential loader =
                                new BrooklynClassLoadingContextSequential(managementContext);
                        loader.add(newClassLoadingContextForCatalogItems(managementContext, transformedCatalogItemId,
                                reboundSearchPath));
                        return new LoadedClass<T>(loader.loadClass(jType, bType), transformedCatalogItemId, reboundSearchPath);
                    } catch (Exception e) {
                        Exceptions.propagateIfFatal(e);
                        warnings.add("unable to load class " + jType + " for resovled context type " + transformedCatalogItemId);
                    }
                } else {
                    // TODO fail, rather than fallback to java?
                    warnings.add("unable to resolve context type " + catalogItemId);
                }
            } else {
                // can happen for enrichers etc added by java, and for BasicApplication when things are deployed;
                // no need to warn
            }

            try {
                Class<T> jTypeC = (Class<T>) loadClass(jType);
                if (!warnings.isEmpty()) {
                    LOG.warn("Loaded java type " + jType + " for " + bType.getSimpleName().toLowerCase() + " " + contextSuchAsId + " but had errors: " + Strings.join(warnings, ";"));
                }
                return new LoadedClass<T>(jTypeC, catalogItemId, reboundSearchPath);
            } catch (Exception e) {
                Exceptions.propagateIfFatal(e);
            }

            if (catalogItemId != null) {
                String msg = "Class " + jType + " not found for " + bType.getSimpleName().toLowerCase() + " " + contextSuchAsId + " (" + catalogItemId + "): " + Strings.join(warnings, ";");
                LOG.warn(msg + " (rethrowing)");
                throw new IllegalStateException(msg);

            } else if (BrooklynFeatureEnablement.isEnabled(FEATURE_BACKWARDS_COMPATIBILITY_INFER_CATALOG_ITEM_ON_REBIND)) {
                //Try loading from whichever catalog bundle succeeds (legacy CI items only; also disabling this, as no longer needed 2017-09)
                BrooklynCatalog catalog = managementContext.getCatalog();
                for (CatalogItem<?, ?> item : catalog.getCatalogItemsLegacy()) {
                    BrooklynClassLoadingContext catalogLoader = CatalogUtils.newClassLoadingContext(managementContext, item);
                    Maybe<Class<?>> catalogClass = catalogLoader.tryLoadClass(jType);
                    if (catalogClass.isPresent()) {
                        LOG.warn("Falling back to java type " + jType + " for " + bType.getSimpleName().toLowerCase() + " " + contextSuchAsId + " using catalog search paths, found on " + item +
                                (warnings.isEmpty() ? "" : ", after errors: " + Strings.join(warnings, ";")));
                        return new LoadedClass<T>((Class<? extends T>) catalogClass.get(), catalogItemId, reboundSearchPath);
                    }
                }
                String msg = "Class " + jType + " not found for " + bType.getSimpleName().toLowerCase() + " " + contextSuchAsId + ", even after legacy global classpath search" +
                        (warnings.isEmpty() ? "" : ": " + Strings.join(warnings, ";"));
                LOG.warn(msg + " (rethrowing)");
                throw new IllegalStateException(msg);

            } else {
                String msg = "Class " + jType + " not found for " + bType.getSimpleName().toLowerCase() + " " + contextSuchAsId +
                        (warnings.isEmpty() ? "" : ": " + Strings.join(warnings, ";"));
                LOG.warn(msg + " (rethrowing)");
                throw new IllegalStateException(msg);
            }
        }