1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.javax.security.auth.login;
19
20import java.security.AccessController;
21import org.apache.harmony.javax.security.auth.AuthPermission;
22
23public abstract class Configuration {
24
25    // the current configuration
26    private static Configuration configuration;
27
28    // creates a AuthPermission object with a specify property
29    private static final AuthPermission GET_LOGIN_CONFIGURATION = new AuthPermission(
30            "getLoginConfiguration"); //$NON-NLS-1$
31
32    // creates a AuthPermission object with a specify property
33    private static final AuthPermission SET_LOGIN_CONFIGURATION = new AuthPermission(
34            "setLoginConfiguration"); //$NON-NLS-1$
35
36    // Key to security properties, defining default configuration provider.
37    private static final String LOGIN_CONFIGURATION_PROVIDER = "login.configuration.provider"; //$NON-NLS-1$
38
39    protected Configuration() {
40        super();
41    }
42
43    public static Configuration getConfiguration() {
44        SecurityManager sm = System.getSecurityManager();
45        if (sm != null) {
46            sm.checkPermission(GET_LOGIN_CONFIGURATION);
47        }
48        return getAccessibleConfiguration();
49    }
50
51    /**
52     * Reads name of default configuration provider from security.properties,
53     * loads the class and instantiates the provider.<br> In case of any
54     * exception, wraps it with SecurityException and throws further.
55     */
56    private static final Configuration getDefaultProvider() {
57        return new Configuration() {
58
59			@Override
60			public void refresh() {
61			}
62
63			@Override
64			public AppConfigurationEntry[] getAppConfigurationEntry(
65					String applicationName) {
66				return new AppConfigurationEntry[0];
67			}
68		};
69    }
70
71    /**
72     * Shortcut accessor for friendly classes, to skip security checks.
73     * If active configuration was set to <code>null</code>, tries to load a default
74     * provider, so this method never returns <code>null</code>. <br>
75     * This method is synchronized with setConfiguration()
76     */
77    static Configuration getAccessibleConfiguration() {
78        Configuration current = configuration;
79        if (current == null) {
80            synchronized (Configuration.class) {
81                if (configuration == null) {
82                    configuration = getDefaultProvider();
83                }
84                return configuration;
85            }
86        }
87        return current;
88    }
89
90    public static void setConfiguration(Configuration configuration) {
91        SecurityManager sm = System.getSecurityManager();
92        if (sm != null) {
93            sm.checkPermission(SET_LOGIN_CONFIGURATION);
94        }
95        Configuration.configuration = configuration;
96    }
97
98    public abstract AppConfigurationEntry[] getAppConfigurationEntry(String applicationName);
99
100    public abstract void refresh();
101
102}
103