Policy.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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;
  18. import java.security.AccessController;
  19. import java.security.CodeSource;
  20. import java.security.PermissionCollection;
  21. import java.security.PrivilegedAction;
  22. import org.apache.harmony.security.fortress.PolicyUtils;
  23. import org.apache.harmony.auth.DefaultSubjectPolicy;
  24. import org.apache.harmony.auth.internal.nls.Messages;
  25. /**
  26. * @deprecated Use
  27. * {@link java.security.Policy#getPermissions(java.security.ProtectionDomain)}
  28. * and
  29. * {@link java.security.ProtectionDomain#ProtectionDomain(java.security.CodeSource, java.security.PermissionCollection, ClassLoader, java.security.Principal[])}
  30. * to establish a policy's permissions for a principal.
  31. */
  32. @Deprecated
  33. public abstract class Policy {
  34. // Key to security properties, defining default policy provider.
  35. private static final String POLICY_PROVIDER = "auth.policy.provider"; //$NON-NLS-1$
  36. // The AuthPermission required to set custom Policy.
  37. private static final AuthPermission SET_POLICY = new AuthPermission("setPolicy"); //$NON-NLS-1$
  38. // The AuthPermission required to get current Policy.
  39. private static final AuthPermission GET_POLICY = new AuthPermission("getPolicy"); //$NON-NLS-1$
  40. // the current policy object
  41. private static Policy activePolicy;
  42. public abstract PermissionCollection getPermissions(Subject subject, CodeSource cs);
  43. public abstract void refresh();
  44. protected Policy() {
  45. super();
  46. }
  47. public static Policy getPolicy() {
  48. SecurityManager sm = System.getSecurityManager();
  49. if (sm != null) {
  50. sm.checkPermission(GET_POLICY);
  51. }
  52. return getAccessiblePolicy();
  53. }
  54. /**
  55. * Shortcut accessor for friendly classes, to skip security checks. If
  56. * active policy was set to <code>null</code>, tries to load a default
  57. * provider, so this method never returns <code>null</code>. <br>
  58. * This method is synchronized with setPolicy()
  59. */
  60. static Policy getAccessiblePolicy() {
  61. Policy current = activePolicy;
  62. if (current == null) {
  63. synchronized (Policy.class) {
  64. // double check in case value has been reassigned
  65. // while we've been awaiting monitor
  66. if (activePolicy == null) {
  67. activePolicy = getDefaultProvider();
  68. }
  69. return activePolicy;
  70. }
  71. }
  72. return current;
  73. }
  74. /**
  75. * Reads name of default policy provider from security.properties, loads the
  76. * class and instantiates the provider. In case of any exception, wraps it
  77. * with SecurityException and throws further.
  78. */
  79. private static final Policy getDefaultProvider() {
  80. final String defaultClass = AccessController
  81. .doPrivileged(new PolicyUtils.SecurityPropertyAccessor(POLICY_PROVIDER));
  82. if (defaultClass == null) {
  83. return new DefaultSubjectPolicy();
  84. }
  85. Object policy = AccessController.doPrivileged(new PrivilegedAction<Object>() {
  86. public Object run() {
  87. try {
  88. return Class
  89. .forName(defaultClass, true, ClassLoader.getSystemClassLoader())
  90. .newInstance();
  91. } catch (Exception e) {
  92. SecurityException se = new SecurityException(Messages.getString("auth.08")); //$NON-NLS-1$
  93. se.initCause(e);
  94. throw se;
  95. }
  96. }
  97. });
  98. if (!(policy instanceof Policy)) {
  99. throw new SecurityException(Messages.getString("auth.08")); //$NON-NLS-1$
  100. }
  101. return (Policy) policy;
  102. }
  103. public static void setPolicy(Policy policy) {
  104. SecurityManager sm = System.getSecurityManager();
  105. if (sm != null) {
  106. sm.checkPermission(SET_POLICY);
  107. }
  108. synchronized (Policy.class) {
  109. activePolicy = policy;
  110. }
  111. }
  112. }