File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\security\PermissionGroup.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.security;
25
26 import hudson.CopyOnWrite;
27 import hudson.model.Hudson;
28
29 import java.util.List;
30 import java.util.Collections;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.Map;
34 import java.util.concurrent.CopyOnWriteArrayList;
35 import java.util.concurrent.ConcurrentHashMap;
36
37 import org.jvnet.localizer.Localizable;
38
39 /**
40 * Group of {@link Permission}s that share the same {@link Permission#owner owner}.
41 *
42 * Sortable by the owner class name.
43 */
44 public final class PermissionGroup implements Iterable<Permission>, Comparable<PermissionGroup> {
45 private final List<Permission> permisisons = new CopyOnWriteArrayList<Permission>();
46 private final List<Permission> permisisonsView = Collections.unmodifiableList(permisisons);
47 public final Class owner;
48
49 /**
50 * Human readable title of this permission group.
51 * This should be short.
52 */
53 public final Localizable title;
54
55 public PermissionGroup(Class owner, Localizable title) {
56 this.owner = owner;
57 this.title = title;
58
59 synchronized(PermissionGroup.class) {
60 List<PermissionGroup> allGroups = new ArrayList<PermissionGroup>(ALL);
61 allGroups.add(this);
62 Collections.sort(allGroups);
63 ALL = Collections.unmodifiableList(allGroups);
64 }
65
66 PERMISSIONS.put(owner,this);
67 }
68
69 public Iterator<Permission> iterator() {
70 return permisisons.iterator();
71 }
72
73 /*package*/ void add(Permission p) {
74 permisisons.add(p);
75 }
76
77 /**
78 * Lists up all the permissions in this group.
79 */
80 public List<Permission> getPermissions() {
81 return permisisonsView;
82 }
83
84 public boolean hasPermissionContainedBy(PermissionScope scope) {
85 for (Permission p : permisisons)
86 if (p.isContainedBy(scope))
87 return true;
88 return false;
89 }
90
91 /**
92 * Finds a permission that has the given name.
93 */
94 public Permission find(String name) {
95 for (Permission p : permisisons) {
96 if(p.name.equals(name))
97 return p;
98 }
99 return null;
100 }
101
102 public int compareTo(PermissionGroup that) {
103 // first, sort by the 'compare order' number. This is so that
104 // we can put Hudson.PERMISSIONS first.
105 int r= this.compareOrder()-that.compareOrder();
106 if(r!=0) return r;
107
108 // among the permissions of the same group, just sort by their names
109 // so that the sort order is consistent regardless of classloading order.
110 return this.owner.getName().compareTo(that.owner.getName());
111 }
112
113 private int compareOrder() {
114 if(owner==Hudson.class) return 0;
115 return 1;
116 }
117
118 public int size() {
119 return permisisons.size();
120 }
121
122 /**
123 * All groups. Sorted.
124 */
125 @CopyOnWrite
126 private static List<PermissionGroup> ALL = Collections.emptyList();
127
128 /**
129 * Returns all the {@link PermissionGroup}s available in the system.
130 * @return
131 * always non-null. Read-only.
132 */
133 public static List<PermissionGroup> getAll() {
134 return ALL;
135 }
136
137 /**
138 * Gets the {@link PermissionGroup} whose {@link PermissionGroup#owner} is the given class.
139 *
140 * @return null if not found.
141 */
142 public static PermissionGroup get(Class owner) {
143 return PERMISSIONS.get(owner);
144 }
145
146 /**
147 * All the permissions in the system, keyed by their owners.
148 */
149 private static final Map<Class, PermissionGroup> PERMISSIONS = new ConcurrentHashMap<Class, PermissionGroup>();
150 }
151