Search in sources :

Example 11 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CallbackAwareClientDelegatingFutureTest method createCache.

private Cache createCache(int blockMilis, boolean throwError) {
    CachingProvider cachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
    CacheManager cacheManager = cachingProvider.getCacheManager();
    CacheConfiguration cacheConfig = new CacheConfig().setReadThrough(true).setCacheLoaderFactory(new BlockableCacheLoaderFactory(blockMilis, throwError));
    return cacheManager.createCache(CACHE_NAME, cacheConfig);
}
Also used : HazelcastCacheManager(com.hazelcast.cache.HazelcastCacheManager) CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig) CacheConfiguration(com.hazelcast.config.CacheConfiguration) CachingProvider(javax.cache.spi.CachingProvider)

Example 12 with CacheManager

use of javax.cache.CacheManager in project requery by requery.

the class RxTest method setup.

@Before
public void setup() throws SQLException {
    Platform platform = new HSQL();
    CommonDataSource dataSource = DatabaseType.getDataSource(platform);
    EntityModel model = io.requery.test.model.Models.DEFAULT;
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();
    Configuration configuration = new ConfigurationBuilder(dataSource, model).useDefaultLogging().setWriteExecutor(Executors.newSingleThreadExecutor()).setEntityCache(new EntityCacheBuilder(model).useReferenceCache(true).useSerializableCache(true).useCacheManager(cacheManager).build()).build();
    SchemaModifier tables = new SchemaModifier(configuration);
    tables.createTables(TableCreationMode.DROP_CREATE);
    data = RxSupport.toReactiveStore(new EntityDataStore<Persistable>(configuration));
}
Also used : ConfigurationBuilder(io.requery.sql.ConfigurationBuilder) Platform(io.requery.sql.Platform) Configuration(io.requery.sql.Configuration) HSQL(io.requery.sql.platform.HSQL) EntityDataStore(io.requery.sql.EntityDataStore) EntityModel(io.requery.meta.EntityModel) CacheManager(javax.cache.CacheManager) EntityCacheBuilder(io.requery.cache.EntityCacheBuilder) CommonDataSource(javax.sql.CommonDataSource) CachingProvider(javax.cache.spi.CachingProvider) SchemaModifier(io.requery.sql.SchemaModifier) Before(org.junit.Before)

Example 13 with CacheManager

use of javax.cache.CacheManager in project ignite by apache.

the class CacheMetricsEnableRuntimeTest method testCacheManagerStatisticsEnable.

/**
 * @throws Exception If failed.
 */
public void testCacheManagerStatisticsEnable() throws Exception {
    final CacheManager mgr1 = Caching.getCachingProvider().getCacheManager();
    final CacheManager mgr2 = Caching.getCachingProvider().getCacheManager();
    CacheConfiguration cfg1 = new CacheConfiguration().setName(CACHE1).setGroupName(GROUP).setCacheMode(CacheMode.PARTITIONED).setAtomicityMode(CacheAtomicityMode.ATOMIC);
    mgr1.createCache(CACHE1, cfg1);
    CacheConfiguration cfg2 = new CacheConfiguration(cfg1).setName(CACHE2).setStatisticsEnabled(true);
    mgr1.createCache(CACHE2, cfg2);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return !isStatisticsEnabled(mgr1, CACHE1) && !isStatisticsEnabled(mgr2, CACHE1) && isStatisticsEnabled(mgr1, CACHE2) && isStatisticsEnabled(mgr2, CACHE2);
        }
    }, WAIT_CONDITION_TIMEOUT));
    mgr1.enableStatistics(CACHE1, true);
    mgr2.enableStatistics(CACHE2, false);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return isStatisticsEnabled(mgr1, CACHE1) && isStatisticsEnabled(mgr2, CACHE1) && !isStatisticsEnabled(mgr1, CACHE2) && !isStatisticsEnabled(mgr2, CACHE2);
        }
    }, WAIT_CONDITION_TIMEOUT));
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CacheManager(javax.cache.CacheManager) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 14 with CacheManager

use of javax.cache.CacheManager in project wso2-synapse by wso2.

the class ThrottleHandler method process.

/**
 * processing through the throttle
 * 1) concurrent throttling
 * 2) access rate based throttling - domain or ip
 *
 * @param throttle       The Throttle object - holds all configuration and state data
 *                       of the throttle
 * @param messageContext The MessageContext , that holds all data per message basis
 * @throws AxisFault         Throws when access must deny for caller
 * @throws ThrottleException    ThrottleException
 */
public void process(Throttle throttle, MessageContext messageContext) throws ThrottleException, AxisFault {
    String throttleId = throttle.getId();
    ConfigurationContext cc = messageContext.getConfigurationContext();
    // acquiring  cache manager.
    Cache<String, ConcurrentAccessController> cache;
    CacheManager cacheManager = Caching.getCacheManagerFactory().getCacheManager(THROTTLING_CACHE_MANAGER);
    if (cacheManager != null) {
        cache = cacheManager.getCache(THROTTLING_CACHE);
    } else {
        cache = Caching.getCacheManager().getCache(THROTTLING_CACHE);
    }
    if (log.isDebugEnabled()) {
        log.debug("created throttling cache : " + cache);
    }
    // Get the concurrent access controller
    ConcurrentAccessController cac;
    String key = null;
    key = ThrottleConstants.THROTTLE_PROPERTY_PREFIX + throttleId + ThrottleConstants.CAC_SUFFIX;
    cac = cache.get(key);
    // check for concurrent access
    boolean canAccess = doConcurrentThrottling(cac, messageContext);
    if (canAccess) {
        if (messageContext.getFLOW() == MessageContext.IN_FLOW) {
            // gets the remote caller domain name
            String domain = null;
            HttpServletRequest request = (HttpServletRequest) messageContext.getPropertyNonReplicable(HTTPConstants.MC_HTTP_SERVLETREQUEST);
            if (request != null) {
                domain = request.getRemoteHost();
            }
            // Domain name based throttling
            // check whether a configuration has been defined for this domain name or not
            String callerId = null;
            if (domain != null) {
                // loads the ThrottleContext
                ThrottleContext throttleCtxt = throttle.getThrottleContext(ThrottleConstants.DOMAIN_BASED_THROTTLE_KEY);
                if (throttleCtxt != null) {
                    // Loads the ThrottleConfiguration
                    ThrottleConfiguration throttleConfig = throttleCtxt.getThrottleConfiguration();
                    if (throttleConfig != null) {
                        // check for configuration for this caller
                        callerId = throttleConfig.getConfigurationKeyOfCaller(domain);
                        if (callerId != null) {
                            // If this is a clustered env.
                            throttleCtxt.setThrottleId(throttleId);
                            AccessInformation infor = accessRateController.canAccess(throttleCtxt, callerId, ThrottleConstants.DOMAIN_BASE);
                            StatCollector.collect(infor, domain, ThrottleConstants.DOMAIN_BASE);
                            // check for the permission for access
                            if (!infor.isAccessAllowed()) {
                                // if the access has denied by rate based throttling
                                if (cac != null) {
                                    cac.incrementAndGet();
                                    cache.put(key, cac);
                                    if (debugOn) {
                                        log.debug("Added the state of ConcurrentAccessController " + "to cache with key : " + key);
                                    }
                                }
                                throw new AxisFault(" Access deny for a " + "caller with Domain " + domain + " " + " : Reason : " + infor.getFaultReason());
                            }
                        } else {
                            if (debugOn) {
                                log.debug("Could not find the Throttle Context for domain-Based " + "Throttling for domain name " + domain + " Throttling for this " + "domain name may not be configured from policy");
                            }
                        }
                    }
                }
            } else {
                if (debugOn) {
                    log.debug("Could not find the domain of the caller - IP-based throttling may occur");
                }
            }
            if (callerId == null) {
                String ip = (String) messageContext.getProperty(MessageContext.REMOTE_ADDR);
                if (ip != null) {
                    // loads IP based throttle context
                    ThrottleContext context = throttle.getThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY);
                    if (context != null) {
                        // Loads the ThrottleConfiguration
                        ThrottleConfiguration config = context.getThrottleConfiguration();
                        if (config != null) {
                            // check for configuration for this ip
                            callerId = config.getConfigurationKeyOfCaller(ip);
                            if (callerId != null) {
                                context.setThrottleId(throttleId);
                                AccessInformation infor = accessRateController.canAccess(context, callerId, ThrottleConstants.IP_BASE);
                                // check for the permission for access
                                StatCollector.collect(infor, ip, ThrottleConstants.IP_BASE);
                                if (!infor.isAccessAllowed()) {
                                    // if the access has denied by rate based throttling
                                    if (cac != null) {
                                        cac.incrementAndGet();
                                        // set back if this is a clustered env
                                        cache.put(key, cac);
                                        if (debugOn) {
                                            log.debug("Added the state of ConcurrentAccessController " + "to cache with key : " + key);
                                        }
                                    }
                                    throw new AxisFault(" Access deny for a " + "caller with IP " + ip + " " + " : Reason : " + infor.getFaultReason());
                                }
                            }
                        }
                    } else {
                        if (debugOn) {
                            log.debug("Could not find the throttle Context for IP-Based throttling");
                        }
                    }
                } else {
                    if (debugOn) {
                        log.debug("Could not find the IP address of the caller " + "- throttling will not occur");
                    }
                }
            }
        }
        // just replicate the current state of ConcurrentAccessController
        if (cac != null) {
            cache.put(key, cac);
            if (debugOn) {
                log.debug("Added the state of ConcurrentAccessController " + "to cache with key : " + key);
            }
        }
        // finally engage rolebased access throttling if available
        doRoleBasedAccessThrottling(throttle, messageContext);
    } else {
        // replicate the current state of ConcurrentAccessController
        if (cac != null) {
            cache.put(key, cac);
            if (debugOn) {
                log.debug("Added the state of ConcurrentAccessController " + "to cache with key : " + key);
            }
        }
        throw new AxisFault("Access has currently been denied since " + " maximum concurrent access have exceeded");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ThrottleContext(org.apache.synapse.commons.throttle.core.ThrottleContext) AxisFault(org.apache.axis2.AxisFault) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AccessInformation(org.apache.synapse.commons.throttle.core.AccessInformation) CacheManager(javax.cache.CacheManager) ThrottleConfiguration(org.apache.synapse.commons.throttle.core.ThrottleConfiguration) ConcurrentAccessController(org.apache.synapse.commons.throttle.core.ConcurrentAccessController)

Example 15 with CacheManager

use of javax.cache.CacheManager in project wso2-synapse by wso2.

the class ThrottleHandler method doRoleBasedAccessThrottling.

/**
 * Helper method for handling role based Access throttling
 *
 * @param messageContext             MessageContext - message level states
 * @return true if access is allowed through concurrent throttling ,o.w false
 */
private boolean doRoleBasedAccessThrottling(Throttle throttle, MessageContext messageContext) throws AxisFault, ThrottleException {
    boolean canAccess = true;
    if (throttle.getThrottleContext(ThrottleConstants.ROLE_BASED_THROTTLE_KEY) == null) {
        // if no role base throttle config return immediately
        return canAccess;
    }
    ConfigurationContext cc = messageContext.getConfigurationContext();
    String throttleId = throttle.getId();
    // acquiring  cache manager.
    Cache<String, ConcurrentAccessController> cache;
    CacheManager cacheManager = Caching.getCacheManagerFactory().getCacheManager(THROTTLING_CACHE_MANAGER);
    if (cacheManager != null) {
        cache = cacheManager.getCache(THROTTLING_CACHE);
    } else {
        cache = Caching.getCacheManager().getCache(THROTTLING_CACHE);
    }
    if (log.isDebugEnabled()) {
        log.debug("created throttling cache : " + cache);
    }
    String key = null;
    ConcurrentAccessController cac = null;
    key = ThrottleConstants.THROTTLE_PROPERTY_PREFIX + throttleId + ThrottleConstants.CAC_SUFFIX;
    cac = cache.get(key);
    if (messageContext.getFLOW() == MessageContext.IN_FLOW) {
        // gets the remote caller role name
        String consumerKey = null;
        boolean isAuthenticated = false;
        String roleID = null;
        HttpServletRequest request = (HttpServletRequest) messageContext.getPropertyNonReplicable(HTTPConstants.MC_HTTP_SERVLETREQUEST);
        if (request != null) {
            String oAuthHeader = request.getHeader("OAuth");
            // consumerKey = Utils.extractCustomerKeyFromAuthHeader(oAuthHeader);
            // roleID = Utils.extractCustomerKeyFromAuthHeader(oAuthHeader);
            DummyAuthenticator authFuture = new DummyAuthenticator(oAuthHeader);
            consumerKey = authFuture.getAPIKey();
            new DummyHandler().authenticateUser(authFuture);
            roleID = (String) authFuture.getAuthorizedRoles().get(0);
            isAuthenticated = authFuture.isAuthenticated();
        }
        if (!isAuthenticated) {
            throw new AxisFault(" Access deny for a " + "caller with consumer Key: " + consumerKey + " " + " : Reason : Authentication failure");
        }
        // Domain name based throttling
        // check whether a configuration has been defined for this role name or not
        String consumerRoleID = null;
        if (consumerKey != null && isAuthenticated) {
            // loads the ThrottleContext
            ThrottleContext context = throttle.getThrottleContext(ThrottleConstants.ROLE_BASED_THROTTLE_KEY);
            if (context != null) {
                // Loads the ThrottleConfiguration
                ThrottleConfiguration config = context.getThrottleConfiguration();
                if (config != null) {
                    // check for configuration for this caller
                    consumerRoleID = config.getConfigurationKeyOfCaller(roleID);
                    if (consumerRoleID != null) {
                        context.setThrottleId(throttleId);
                        AccessInformation infor = roleBasedAccessController.canAccess(context, consumerKey, consumerRoleID);
                        StatCollector.collect(infor, consumerKey, ThrottleConstants.ROLE_BASE);
                        // check for the permission for access
                        if (!infor.isAccessAllowed()) {
                            // if the access has denied by rate based throttling
                            if (cac != null) {
                                cac.incrementAndGet();
                                cache.put(key, cac);
                                if (debugOn) {
                                    log.debug("Added the state of ConcurrentAccessController " + "to cache with key : " + key);
                                }
                            }
                            throw new AxisFault(" Access deny for a " + "caller with Domain " + consumerKey + " " + " : Reason : " + infor.getFaultReason());
                        }
                    } else {
                        if (debugOn) {
                            log.debug("Could not find the Throttle Context for role-Based " + "Throttling for role name " + consumerKey + " Throttling for this " + "role name may not be configured from policy");
                        }
                    }
                }
            }
        } else {
            if (debugOn) {
                log.debug("Could not find the role of the caller - role based throttling NOT applied");
            }
        }
    }
    return canAccess;
}
Also used : AxisFault(org.apache.axis2.AxisFault) ThrottleContext(org.apache.synapse.commons.throttle.core.ThrottleContext) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AccessInformation(org.apache.synapse.commons.throttle.core.AccessInformation) DummyAuthenticator(org.apache.synapse.commons.throttle.module.utils.impl.DummyAuthenticator) ThrottleConfiguration(org.apache.synapse.commons.throttle.core.ThrottleConfiguration) HttpServletRequest(javax.servlet.http.HttpServletRequest) DummyHandler(org.apache.synapse.commons.throttle.module.utils.impl.DummyHandler) CacheManager(javax.cache.CacheManager) ConcurrentAccessController(org.apache.synapse.commons.throttle.core.ConcurrentAccessController)

Aggregations

CacheManager (javax.cache.CacheManager)284 Test (org.junit.Test)182 CachingProvider (javax.cache.spi.CachingProvider)113 QuickTest (com.hazelcast.test.annotation.QuickTest)78 HazelcastInstance (com.hazelcast.core.HazelcastInstance)69 MutableConfiguration (javax.cache.configuration.MutableConfiguration)56 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)55 CacheConfig (com.hazelcast.config.CacheConfig)54 CacheTestSupport.createServerCachingProvider (com.hazelcast.cache.CacheTestSupport.createServerCachingProvider)50 Cache (javax.cache.Cache)41 URI (java.net.URI)31 AssertTask (com.hazelcast.test.AssertTask)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 Config (com.hazelcast.config.Config)20 ClientConfig (com.hazelcast.client.config.ClientConfig)17 SlowTest (com.hazelcast.test.annotation.SlowTest)17 Before (org.junit.Before)17 CacheTestSupport.createClientCachingProvider (com.hazelcast.cache.CacheTestSupport.createClientCachingProvider)16 HazelcastCacheManager (com.hazelcast.cache.HazelcastCacheManager)16 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)16