Search in sources :

Example 1 with PriorEntitlementResult

use of org.killbill.billing.entitlement.plugin.api.PriorEntitlementResult in project killbill by killbill.

the class EntitlementPluginExecution method executeWithPlugin.

public <T> T executeWithPlugin(final WithEntitlementPlugin<T> callback, final EntitlementContext pluginContext) throws EntitlementApiException {
    try {
        final PriorEntitlementResult priorEntitlementResult = executePluginPriorCalls(pluginContext);
        if (priorEntitlementResult != null && priorEntitlementResult.isAborted()) {
            throw new EntitlementApiException(ErrorCode.ENT_PLUGIN_API_ABORTED);
        }
        final EntitlementContext updatedPluginContext = new DefaultEntitlementContext(pluginContext, priorEntitlementResult);
        try {
            final T result = callback.doCall(entitlementApi, updatedPluginContext);
            executePluginOnSuccessCalls(updatedPluginContext);
            return result;
        } catch (final EntitlementApiException e) {
            executePluginOnFailureCalls(updatedPluginContext);
            throw e;
        }
    } catch (final EntitlementPluginApiException e) {
        throw new EntitlementApiException(ErrorCode.ENT_PLUGIN_API_ABORTED, e.getMessage());
    }
}
Also used : PriorEntitlementResult(org.killbill.billing.entitlement.plugin.api.PriorEntitlementResult) EntitlementPluginApiException(org.killbill.billing.entitlement.plugin.api.EntitlementPluginApiException) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext)

Example 2 with PriorEntitlementResult

use of org.killbill.billing.entitlement.plugin.api.PriorEntitlementResult in project killbill by killbill.

the class EntitlementPluginExecution method executePluginPriorCalls.

private PriorEntitlementResult executePluginPriorCalls(final EntitlementContext entitlementContextArg) throws EntitlementPluginApiException {
    // Return as soon as the first plugin aborts, or the last result for the last plugin
    PriorEntitlementResult prevResult = null;
    EntitlementContext currentContext = entitlementContextArg;
    for (final String pluginName : pluginRegistry.getAllServices()) {
        final EntitlementPluginApi plugin = pluginRegistry.getServiceForName(pluginName);
        if (plugin == null) {
            // First call to plugin, we log warn, if plugin is not registered
            log.warn("Skipping unknown entitlement control plugin {} when fetching results", pluginName);
            continue;
        }
        prevResult = plugin.priorCall(currentContext, currentContext.getPluginProperties());
        if (prevResult != null && prevResult.isAborted()) {
            break;
        }
        currentContext = new DefaultEntitlementContext(currentContext, prevResult);
    }
    return prevResult;
}
Also used : PriorEntitlementResult(org.killbill.billing.entitlement.plugin.api.PriorEntitlementResult) EntitlementPluginApi(org.killbill.billing.entitlement.plugin.api.EntitlementPluginApi) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext)

Example 3 with PriorEntitlementResult

use of org.killbill.billing.entitlement.plugin.api.PriorEntitlementResult in project killbill by killbill.

the class EntitlementPluginExecution method executeWithPlugin.

public void executeWithPlugin(final Callable<Void> preCallbacksCallback, final List<WithEntitlementPlugin> callbacks, final Iterable<EntitlementContext> pluginContexts) throws EntitlementApiException {
    final List<EntitlementContext> updatedPluginContexts = new LinkedList<EntitlementContext>();
    try {
        for (final EntitlementContext pluginContext : pluginContexts) {
            final PriorEntitlementResult priorEntitlementResult = executePluginPriorCalls(pluginContext);
            if (priorEntitlementResult != null && priorEntitlementResult.isAborted()) {
                throw new EntitlementApiException(ErrorCode.ENT_PLUGIN_API_ABORTED);
            }
            updatedPluginContexts.add(new DefaultEntitlementContext(pluginContext, priorEntitlementResult));
        }
        preCallbacksCallback.call();
        try {
            for (int i = 0; i < updatedPluginContexts.size(); i++) {
                final EntitlementContext updatedPluginContext = updatedPluginContexts.get(i);
                final WithEntitlementPlugin callback = callbacks.get(i);
                callback.doCall(entitlementApi, updatedPluginContext);
                executePluginOnSuccessCalls(updatedPluginContext);
            }
        } catch (final EntitlementApiException e) {
            for (final EntitlementContext updatedPluginContext : updatedPluginContexts) {
                executePluginOnFailureCalls(updatedPluginContext);
            }
            throw e;
        }
    } catch (final EntitlementPluginApiException e) {
        throw new EntitlementApiException(ErrorCode.ENT_PLUGIN_API_ABORTED, e.getMessage());
    } catch (final Exception e) {
        throw new EntitlementApiException(ErrorCode.ENT_PLUGIN_API_ABORTED, e.getMessage());
    }
}
Also used : PriorEntitlementResult(org.killbill.billing.entitlement.plugin.api.PriorEntitlementResult) EntitlementPluginApiException(org.killbill.billing.entitlement.plugin.api.EntitlementPluginApiException) LinkedList(java.util.LinkedList) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext) EntitlementPluginApiException(org.killbill.billing.entitlement.plugin.api.EntitlementPluginApiException)

Aggregations

EntitlementContext (org.killbill.billing.entitlement.plugin.api.EntitlementContext)3 PriorEntitlementResult (org.killbill.billing.entitlement.plugin.api.PriorEntitlementResult)3 EntitlementPluginApiException (org.killbill.billing.entitlement.plugin.api.EntitlementPluginApiException)2 LinkedList (java.util.LinkedList)1 EntitlementPluginApi (org.killbill.billing.entitlement.plugin.api.EntitlementPluginApi)1