blob: dc8bb35f32866bd8398d45cd8d311b3d3f9d1a70 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.acl;
27
28import java.util.*;
29import java.security.*;
30import java.security.acl.*;
31
32/**
33 * Class implementing the Owner interface. The
34 * initial owner principal is configured as
35 * part of the constructor.
36 * @author Satish Dharmaraj
37 */
38public class OwnerImpl implements Owner {
39 private Group ownerGroup;
40
41 public OwnerImpl(Principal owner) {
42 ownerGroup = new GroupImpl("AclOwners");
43 ownerGroup.addMember(owner);
44 }
45
46 /**
47 * Adds an owner. Owners can modify ACL contents and can disassociate
48 * ACLs from the objects they protect in the AclConfig interface.
49 * The caller principal must be a part of the owners list of the ACL in
50 * order to invoke this method. The initial owner is configured
51 * at ACL construction time.
52 * @param caller the principal who is invoking this method.
53 * @param owner The owner that should be added to the owners list.
54 * @return true if success, false if already an owner.
55 * @exception NotOwnerException if the caller principal is not on
56 * the owners list of the Acl.
57 */
58 public synchronized boolean addOwner(Principal caller, Principal owner)
59 throws NotOwnerException
60 {
61 if (!isOwner(caller))
62 throw new NotOwnerException();
63
64 ownerGroup.addMember(owner);
65 return false;
66 }
67
68 /**
69 * Delete owner. If this is the last owner in the ACL, an exception is
70 * raised.
71 * The caller principal must be a part of the owners list of the ACL in
72 * order to invoke this method.
73 * @param caller the principal who is invoking this method.
74 * @param owner The owner to be removed from the owners list.
75 * @return true if the owner is removed, false if the owner is not part
76 * of the owners list.
77 * @exception NotOwnerException if the caller principal is not on
78 * the owners list of the Acl.
79 * @exception LastOwnerException if there is only one owner left in the group, then
80 * deleteOwner would leave the ACL owner-less. This exception is raised in such a case.
81 */
82 public synchronized boolean deleteOwner(Principal caller, Principal owner)
83 throws NotOwnerException, LastOwnerException
84 {
85 if (!isOwner(caller))
86 throw new NotOwnerException();
87
88 Enumeration<? extends Principal> e = ownerGroup.members();
89 //
90 // check if there is atleast 2 members left.
91 //
92 Object o = e.nextElement();
93 if (e.hasMoreElements())
94 return ownerGroup.removeMember(owner);
95 else
96 throw new LastOwnerException();
97
98 }
99
100 /**
101 * returns if the given principal belongs to the owner list.
102 * @param owner The owner to check if part of the owners list
103 * @return true if the passed principal is in the owner list, false if not.
104 */
105 public synchronized boolean isOwner(Principal owner) {
106 return ownerGroup.isMember(owner);
107 }
108}