All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.cake.cache.CacheServices Maven / Gradle / Ivy

/*
 * Copyright 2008 Kasper Nielsen.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://cake.codehaus.org/LICENSE
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.codehaus.cake.cache;

import org.codehaus.cake.attribute.AttributeMap;
import org.codehaus.cake.cache.service.loading.CacheLoadingService;
import org.codehaus.cake.cache.service.memorystore.MemoryStoreService;
import org.codehaus.cake.service.ServiceManager;
import org.codehaus.cake.service.executor.ExecutorsService;

/**
 * A utility class to get hold of different cache services in an easy and typesafe manner. For example, the following
 * will return the {@link MemoryStoreService} for a given cache.
 * 
 * 
 * Cache<Integer, String> cache = somecache;
 * MemoryStoreService<Integer, String> service = cache.with().memoryStore();
 * service.trimToSize(10);
 * 
* * @author Kasper Nielsen * @version $Id: CacheServices.java 469 2007-11-17 14:32:25Z kasper $ */ public class CacheServices { private static final AttributeMap FORCE_LOAD = CacheLoadingService.IS_FORCED.singleton(true); /** The service manager to extract cache services from. */ private final ServiceManager serviceManager; /** * Creates a new {@link CacheServices} from the specified {@link Cache} * * @param cache * the cache to retrieve services from */ public CacheServices(Cache cache) { this.serviceManager = cache; } /** * Returns the worker service. * * @return the worker service for the cache * @throws UnsupportedOperationException * if no worker service is available */ public ExecutorsService executors() { return getService(ExecutorsService.class); } // // public ExecutorService executorService() { // return executors().getExecutorService(); // } /** * This method can be called by subclasses to retrieve services from the cache that this object is wrapping. * * @param * the type of service * @param serviceType * the type of services * @return an instance of the specified type * @throws UnsupportedOperationException * if no service of the specified type is available */ protected T getService(Class serviceType) { return serviceManager.getService(serviceType); } /** * This method can be called by subclasses to retrieve services from the cache that this object is wrapping. * * @param * the type of service * @param serviceType * the type of services * @param attributes * map of attributes * @return an instance of the specified type with the specified attributes * @throws UnsupportedOperationException * if no service of the specified type and attributes is available */ protected T getService(Class serviceType, AttributeMap attributes) { return serviceManager.getService(serviceType, attributes); } /** * Returns the cache loading service. * * @return the cache loading service for the cache * @throws UnsupportedOperationException * if no cache loading service is available */ @SuppressWarnings("unchecked") public CacheLoadingService loading() { return getService(CacheLoadingService.class); } @SuppressWarnings("unchecked") public CacheLoadingService loadingForced() { return getService(CacheLoadingService.class, FORCE_LOAD); } /** * Returns the memory store service. * * @return the memory store service for the cache * @throws UnsupportedOperationException * if no memory store service is available */ @SuppressWarnings("unchecked") public MemoryStoreService memoryStore() { return getService(MemoryStoreService.class); } // public ScheduledExecutorService scheduledExecutorService() { // return executors().getScheduledExecutorService(); // } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy