Search in sources :

Example 6 with CacheLoaderArgument

use of org.killbill.billing.util.cache.CacheLoaderArgument in project killbill by killbill.

the class CacheConfig method initializeCacheLoaderArgument.

private CacheLoaderArgument initializeCacheLoaderArgument() {
    final LoaderCallback loaderCallback = new LoaderCallback() {

        @Override
        public Object loadConfig(@Nullable final String inputJson) throws IOException {
            return inputJson != null ? objectMapper.readValue(inputJson, PerTenantConfig.class) : new PerTenantConfig();
        }
    };
    final Object[] args = new Object[1];
    args[0] = loaderCallback;
    final ObjectType irrelevant = null;
    final InternalTenantContext notUsed = null;
    return new CacheLoaderArgument(irrelevant, args, notUsed);
}
Also used : ObjectType(org.killbill.billing.ObjectType) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) LoaderCallback(org.killbill.billing.util.cache.TenantConfigCacheLoader.LoaderCallback) Nullable(javax.annotation.Nullable) CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument)

Example 7 with CacheLoaderArgument

use of org.killbill.billing.util.cache.CacheLoaderArgument in project killbill by killbill.

the class EntitySqlDaoWrapperInvocationHandler method invokeWithCaching.

private Object invokeWithCaching(final Cachable cachableAnnotation, final Method method, final Object[] args) throws Throwable {
    final ObjectType objectType = getObjectType();
    final CacheType cacheType = cachableAnnotation.value();
    final CacheController<Object, Object> cache = cacheControllerDispatcher.getCacheController(cacheType);
    Object result = null;
    if (cache != null) {
        // Find all arguments marked with @CachableKey
        final Map<Integer, Object> keyPieces = new LinkedHashMap<Integer, Object>();
        final Annotation[][] annotations = method.getParameterAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            for (int j = 0; j < annotations[i].length; j++) {
                final Annotation annotation = annotations[i][j];
                if (CachableKey.class.equals(annotation.annotationType())) {
                    // CachableKey position starts at 1
                    keyPieces.put(((CachableKey) annotation).value() - 1, args[i]);
                    break;
                }
            }
        }
        // Build the Cache key
        final String cacheKey = buildCacheKey(keyPieces);
        final InternalTenantContext internalTenantContext = (InternalTenantContext) Iterables.find(ImmutableList.copyOf(args), new Predicate<Object>() {

            @Override
            public boolean apply(final Object input) {
                return input instanceof InternalTenantContext;
            }
        }, null);
        final CacheLoaderArgument cacheLoaderArgument = new CacheLoaderArgument(objectType, args, internalTenantContext, handle);
        return cache.get(cacheKey, cacheLoaderArgument);
    }
    if (result == null) {
        result = prof.executeWithProfiling(ProfilingFeatureType.DAO_DETAILS, sqlDaoClass.getSimpleName() + "(raw) :" + method.getName(), new WithProfilingCallback() {

            @Override
            public Object execute() throws Throwable {
                return method.invoke(sqlDao, args);
            }
        });
    }
    return result;
}
Also used : Annotation(java.lang.annotation.Annotation) CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument) CacheType(org.killbill.billing.util.cache.Cachable.CacheType) LinkedHashMap(java.util.LinkedHashMap) Predicate(com.google.common.base.Predicate) WithProfilingCallback(org.killbill.commons.profiling.Profiling.WithProfilingCallback) ObjectType(org.killbill.billing.ObjectType) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) CachableKey(org.killbill.billing.util.cache.CachableKey)

Example 8 with CacheLoaderArgument

use of org.killbill.billing.util.cache.CacheLoaderArgument in project killbill by killbill.

the class EhCacheCatalogCache method initializeCacheLoaderArgument.

//
// Build the LoaderCallback that is required to build the catalog from the xml from a module that knows
// nothing about catalog.
//
// This is a contract between the TenantCatalogCacheLoader and the EhCacheCatalogCache
private CacheLoaderArgument initializeCacheLoaderArgument(final boolean filterTemplateCatalog) {
    final LoaderCallback loaderCallback = new LoaderCallback() {

        @Override
        public Object loadCatalog(final List<String> catalogXMLs, final Long tenantRecordId) throws CatalogApiException {
            return loader.load(catalogXMLs, filterTemplateCatalog, tenantRecordId);
        }
    };
    final Object[] args = new Object[1];
    args[0] = loaderCallback;
    final ObjectType irrelevant = null;
    final InternalTenantContext notUsed = null;
    return new CacheLoaderArgument(irrelevant, args, notUsed);
}
Also used : ObjectType(org.killbill.billing.ObjectType) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) LoaderCallback(org.killbill.billing.util.cache.TenantCatalogCacheLoader.LoaderCallback) CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument)

Example 9 with CacheLoaderArgument

use of org.killbill.billing.util.cache.CacheLoaderArgument in project killbill by killbill.

the class EhCacheStateMachineConfigCache method getPaymentStateMachineConfig.

@Override
public StateMachineConfig getPaymentStateMachineConfig(final String pluginName, final InternalTenantContext tenantContext) throws PaymentApiException {
    if (tenantContext.getTenantRecordId() == InternalCallContextFactory.INTERNAL_TENANT_RECORD_ID || cacheController == null) {
        return defaultPaymentStateMachineConfig;
    }
    final String pluginConfigKey = getCacheKeyName(pluginName, tenantContext);
    final CacheLoaderArgument cacheLoaderArgument = createCacheLoaderArgument(pluginName);
    try {
        StateMachineConfig pluginPaymentStateMachineConfig = (StateMachineConfig) cacheController.get(pluginConfigKey, cacheLoaderArgument);
        // It means we are using the default state machine config in a multi-tenant deployment
        if (pluginPaymentStateMachineConfig == null) {
            pluginPaymentStateMachineConfig = defaultPaymentStateMachineConfig;
            cacheController.add(pluginConfigKey, pluginPaymentStateMachineConfig);
        }
        return pluginPaymentStateMachineConfig;
    } catch (final IllegalStateException e) {
        // TODO 0.17 proper error code
        throw new PaymentApiException(e, ErrorCode.PAYMENT_INTERNAL_ERROR, "Invalid payment state machine");
    }
}
Also used : StateMachineConfig(org.killbill.automaton.StateMachineConfig) DefaultStateMachineConfig(org.killbill.automaton.DefaultStateMachineConfig) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument)

Example 10 with CacheLoaderArgument

use of org.killbill.billing.util.cache.CacheLoaderArgument in project killbill by killbill.

the class DefaultTenantUserApi method getCachedTenantValuesForKey.

private List<String> getCachedTenantValuesForKey(final String key, final InternalTenantContext internalContext) {
    final String tenantKey = getCacheKeyName(key, internalContext);
    final Object cachedTenantValues = tenantKVCache.get(tenantKey, new CacheLoaderArgument(ObjectType.TENANT_KVS));
    if (cachedTenantValues == null) {
        return ImmutableList.<String>of();
    } else {
        // Current, we only cache single-value keys
        return ImmutableList.<String>of((String) cachedTenantValues);
    }
}
Also used : CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument)

Aggregations

CacheLoaderArgument (org.killbill.billing.util.cache.CacheLoaderArgument)11 ObjectType (org.killbill.billing.ObjectType)7 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)7 Predicate (com.google.common.base.Predicate)1 ImmutableList (com.google.common.collect.ImmutableList)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 URI (java.net.URI)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 UUID (java.util.UUID)1 Nullable (javax.annotation.Nullable)1 DefaultStateMachineConfig (org.killbill.automaton.DefaultStateMachineConfig)1 StateMachineConfig (org.killbill.automaton.StateMachineConfig)1 Account (org.killbill.billing.account.api.Account)1 DefaultAccount (org.killbill.billing.account.api.DefaultAccount)1 DefaultImmutableAccountData (org.killbill.billing.account.api.DefaultImmutableAccountData)1 DefaultPlan (org.killbill.billing.catalog.DefaultPlan)1 DefaultPlanPhasePriceOverride (org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)1