001/*
002 * (C) Copyright 2014-2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.nuxeo.runtime.api.login;
017
018import java.util.concurrent.atomic.AtomicInteger;
019
020import javax.security.auth.login.AppConfigurationEntry;
021import javax.security.auth.login.Configuration;
022
023public class LoginConfiguration extends Configuration {
024
025    public static final LoginConfiguration INSTANCE = new LoginConfiguration();
026
027    protected Configuration parent = null;
028
029    protected Provider provider = null;
030
031    protected final AtomicInteger counter = new AtomicInteger(0);
032
033    public interface Provider {
034
035        AppConfigurationEntry[] getAppConfigurationEntry(String name);
036
037    }
038
039    @Override
040    public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
041        AppConfigurationEntry[] entries = provider != null ? provider.getAppConfigurationEntry(name) : null;
042        if (entries == null) {
043            entries = parent != null ? parent.getAppConfigurationEntry(name) : null;
044        }
045        return entries;
046    }
047
048    @Override
049    public void refresh() {
050        if (parent != null) {
051            parent.refresh();
052        }
053    }
054
055    public void install(Provider provider) {
056        int count = counter.incrementAndGet();
057        if (count == 1) {
058            this.provider = provider;
059            this.parent = Configuration.getConfiguration();
060            Configuration.setConfiguration(this);
061        }
062    }
063
064    public void uninstall() {
065        int count = counter.decrementAndGet();
066        if (count == 0) {
067            Configuration.setConfiguration(parent);
068            this.provider = null;
069            this.parent = null;
070        }
071    }
072
073}