KrbServicePermissionCollection.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.kerberos;
  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.ObjectStreamField;
  22. import java.io.Serializable;
  23. import java.security.Permission;
  24. import java.security.PermissionCollection;
  25. import java.util.Enumeration;
  26. import java.util.NoSuchElementException;
  27. import java.util.Vector;
  28. import org.apache.harmony.auth.internal.nls.Messages;
  29. /**
  30. * Specific PermissionCollection for storing ServicePermissions
  31. *
  32. */
  33. final class KrbServicePermissionCollection extends PermissionCollection
  34. implements Serializable {
  35. private static final long serialVersionUID = -4118834211490102011L;
  36. private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
  37. "permissions", Vector.class) }; //$NON-NLS-1$
  38. private transient ServicePermission[] items = new ServicePermission[10];
  39. private transient int offset;
  40. // initialization of a collection
  41. KrbServicePermissionCollection() {
  42. }
  43. /**
  44. * Adds a ServicePermission to the collection.
  45. */
  46. @Override
  47. public void add(Permission permission) {
  48. if (isReadOnly()) {
  49. throw new SecurityException(Messages.getString("auth.21")); //$NON-NLS-1$
  50. }
  51. if (permission == null || !(permission instanceof ServicePermission)) {
  52. throw new IllegalArgumentException(Messages.getString("auth.22",permission)); //$NON-NLS-1$
  53. }
  54. synchronized (this) {
  55. if (offset == items.length) {
  56. ServicePermission[] sp = new ServicePermission[items.length * 2];
  57. System.arraycopy(items, 0, sp, 0, offset);
  58. items = sp;
  59. }
  60. items[offset++] = (ServicePermission) permission;
  61. }
  62. }
  63. /**
  64. * Returns enumeration of the collection.
  65. */
  66. @Override
  67. public Enumeration<Permission> elements() {
  68. return new Enumeration<Permission>() {
  69. private int index = 0;
  70. public boolean hasMoreElements() {
  71. return index < offset;
  72. }
  73. public Permission nextElement() {
  74. if (index == offset) {
  75. throw new NoSuchElementException();
  76. }
  77. return items[index++];
  78. }
  79. };
  80. }
  81. /**
  82. * Returns true if this collection implies the specified permission.
  83. */
  84. @Override
  85. public boolean implies(Permission permission) {
  86. if (permission == null || !(permission instanceof ServicePermission)) {
  87. return false;
  88. }
  89. synchronized (this) {
  90. for (int i = 0; i < offset; i++) {
  91. if (items[i].implies(permission)) {
  92. return true;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. // white collection to stream
  99. private void writeObject(java.io.ObjectOutputStream out) throws IOException {
  100. Vector<ServicePermission> permissions;
  101. permissions = new Vector<ServicePermission>(offset);
  102. for (int i = 0; i < offset; permissions.add(items[i++])) {
  103. }
  104. ObjectOutputStream.PutField fields = out.putFields();
  105. fields.put("permissions", permissions); //$NON-NLS-1$
  106. out.writeFields();
  107. }
  108. // read collection from stream
  109. private void readObject(java.io.ObjectInputStream in) throws IOException,
  110. ClassNotFoundException {
  111. ObjectInputStream.GetField fields = in.readFields();
  112. Vector<?> permissions = (Vector<?>) fields.get("permissions", null); //$NON-NLS-1$
  113. items = new ServicePermission[permissions.size() * 2];
  114. for (offset = 0; offset < items.length / 2;) {
  115. Object obj = permissions.get(offset);
  116. if (obj == null || !(obj instanceof ServicePermission)) {
  117. throw new IllegalArgumentException(Messages.getString("auth.22", obj)); //$NON-NLS-1$
  118. }
  119. items[offset++] = (ServicePermission) obj;
  120. }
  121. }
  122. }