Configuration.java 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. package javax.security.auth.login;
  18. import java.security.AccessController;
  19. import javax.security.auth.AuthPermission;
  20. import org.apache.harmony.security.fortress.PolicyUtils;
  21. public abstract class Configuration {
  22. // the current configuration
  23. private static Configuration configuration;
  24. // creates a AuthPermission object with a specify property
  25. private static final AuthPermission GET_LOGIN_CONFIGURATION = new AuthPermission(
  26. "getLoginConfiguration"); //$NON-NLS-1$
  27. // creates a AuthPermission object with a specify property
  28. private static final AuthPermission SET_LOGIN_CONFIGURATION = new AuthPermission(
  29. "setLoginConfiguration"); //$NON-NLS-1$
  30. // Key to security properties, defining default configuration provider.
  31. private static final String LOGIN_CONFIGURATION_PROVIDER = "login.configuration.provider"; //$NON-NLS-1$
  32. protected Configuration() {
  33. super();
  34. }
  35. public static Configuration getConfiguration() {
  36. SecurityManager sm = System.getSecurityManager();
  37. if (sm != null) {
  38. sm.checkPermission(GET_LOGIN_CONFIGURATION);
  39. }
  40. return getAccessibleConfiguration();
  41. }
  42. /**
  43. * Reads name of default configuration provider from security.properties,
  44. * loads the class and instantiates the provider.<br> In case of any
  45. * exception, wraps it with SecurityException and throws further.
  46. */
  47. private static final Configuration getDefaultProvider() {
  48. return AccessController.doPrivileged(new PolicyUtils.ProviderLoader<Configuration>(
  49. LOGIN_CONFIGURATION_PROVIDER, Configuration.class));
  50. }
  51. /**
  52. * Shortcut accessor for friendly classes, to skip security checks.
  53. * If active configuration was set to <code>null</code>, tries to load a default
  54. * provider, so this method never returns <code>null</code>. <br>
  55. * This method is synchronized with setConfiguration()
  56. */
  57. static Configuration getAccessibleConfiguration() {
  58. Configuration current = configuration;
  59. if (current == null) {
  60. synchronized (Configuration.class) {
  61. if (configuration == null) {
  62. configuration = getDefaultProvider();
  63. }
  64. return configuration;
  65. }
  66. }
  67. return current;
  68. }
  69. public static void setConfiguration(Configuration configuration) {
  70. SecurityManager sm = System.getSecurityManager();
  71. if (sm != null) {
  72. sm.checkPermission(SET_LOGIN_CONFIGURATION);
  73. }
  74. Configuration.configuration = configuration;
  75. }
  76. public abstract AppConfigurationEntry[] getAppConfigurationEntry(String applicationName);
  77. public abstract void refresh();
  78. }