public void invoke()

in src/main/java/com/microsoft/azure/functions/worker/chain/SdkTypeMiddleware.java [43:99]


    public void invoke(MiddlewareContext context, MiddlewareChain chain) throws Exception {

        // save the current loader
        ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
        // set class loader for the reflection calls
        Thread.currentThread().setContextClassLoader(this.classLoader);

        try {
            ExecutionContextDataSource execCtx = (ExecutionContextDataSource) context;
            BindingDataStore dataStore = execCtx.getDataStore();
            WorkerObjectCache<CacheKey> cache = execCtx.getCache();

            for (SdkTypeMetaData metaData : this.sdkTypesMetaData) {
                Set<String> requiredKeys = metaData.getRequiredFields();

                for (String key : requiredKeys) {
                    Object val = dataStore.getDataByName(key, String.class)
                            .map(b -> b.getValue())
                            .orElseThrow(() -> new IllegalArgumentException("Missing " + key));

                    metaData.setFieldValue(key, val);
                }

                SdkType<?> sdkType = this.sdkTypeRegistry.createSdkType(metaData);

                Object instance = null;
                if (sdkType instanceof CachableSdkType) {
                    CacheKey key = ((CachableSdkType<?>) sdkType).buildCacheKey();
                    instance = cache.computeIfAbsent(
                            this.getClass(),
                            key,
                            () -> {
                                try {
                                    return sdkType.buildInstance();
                                } catch (Exception ex) {
                                    throw new RuntimeException(ex);
                                }
                            }
                    );
                } else {
                    instance = sdkType.buildInstance();
                }

                // update in data store
                Parameter param = metaData.getParam();
                ParamBindInfo paramBindInfo = new ParamBindInfo(param);
                execCtx.updateParameterValue(paramBindInfo.getName(), instance);

                LOGGER.info("SdkTypeMiddleware: Successfully created instance for param "
                        + param.getName() + " of type " + param.getType());
            }
        } finally {
            Thread.currentThread().setContextClassLoader(prevCL);
        }

        chain.doNext(context);
    }