org.apigw.authserver.svc.impl.CachingPermissionServices.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.authserver.svc.impl.CachingPermissionServices.java

Source

/**
 *   Copyright 2013 Stockholm County Council
 *
 *   This file is part of APIGW
 *
 *   APIGW is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *   License as published by the Free Software Foundation.
 *
 *   APIGW is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with APIGW; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *   Boston, MA 02111-1307  USA
 *
 */

package org.apigw.authserver.svc.impl;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.apigw.authserver.svc.PermissionServices;
import org.apigw.authserver.types.domain.Permission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by martin on 26/09/14.
 */
public class CachingPermissionServices implements PermissionServices {

    private static final Logger logger = LoggerFactory.getLogger(CachingPermissionServices.class);
    private PermissionServices permissionServices;
    private LoadingCache<String, Permission> scopeCache;
    private long expireAfterWriteTime;
    private TimeUnit expireAfterWriteTimeUnit;

    @PostConstruct
    protected void init() {
        scopeCache = CacheBuilder.newBuilder().expireAfterWrite(expireAfterWriteTime, expireAfterWriteTimeUnit)
                .build(new PermissionByNameCacheLoader(permissionServices));
    }

    @Override
    public Permission get(Long id) {
        return permissionServices.get(id); //do not cache
    }

    @Override
    public Permission getPermissionByName(String scopeName) {
        try {
            return scopeCache.get(scopeName);
        } catch (Exception e) {
            logger.error("caught exception while looking up scope", e);
            return null;
        }
    }

    @Override
    public List<Permission> getAllPermissions() {
        return permissionServices.getAllPermissions(); //do not cache
    }

    public void setPermissionServices(PermissionServices permissionServices) {
        this.permissionServices = permissionServices;
    }

    public void setExpireAfterWriteTime(long expireAfterWriteTime) {
        this.expireAfterWriteTime = expireAfterWriteTime;
    }

    public void setExpireAfterWriteTimeUnit(TimeUnit expireAfterWriteTimeUnit) {
        this.expireAfterWriteTimeUnit = expireAfterWriteTimeUnit;
    }

    private static final class PermissionByNameCacheLoader extends CacheLoader<String, Permission> {

        private PermissionServices permissionServices;

        private PermissionByNameCacheLoader(PermissionServices scopeServices) {
            this.permissionServices = scopeServices;
        }

        @Override
        public Permission load(String key) throws Exception {
            return permissionServices.getPermissionByName(key);
        }
    }
}