1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.om.page.impl;
18
19 import java.util.AbstractList;
20 import java.util.List;
21
22 import org.apache.jetspeed.page.impl.DatabasePageManagerUtils;
23
24 /***
25 * SecurityConstraintsRefList
26 *
27 * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
28 * @version $Id$
29 */
30 class SecurityConstraintsRefList extends AbstractList
31 {
32 private SecurityConstraintsImpl constraints;
33
34 private List removedConstraintsRefs;
35
36 SecurityConstraintsRefList(SecurityConstraintsImpl constraints)
37 {
38 super();
39 this.constraints = constraints;
40 }
41
42 /***
43 * wrapNameStringForAdd
44 *
45 * Wraps and validates constraints ref name string
46 * to be added to this list.
47 *
48 * @param name constraints ref name string to add
49 * @return list element to add
50 */
51 private BaseSecurityConstraintsRef wrapNameStringForAdd(String name)
52 {
53
54 if (name == null)
55 {
56 throw new NullPointerException("Unable to add null to list.");
57 }
58
59
60 BaseSecurityConstraintsRef constraintsRef = null;
61 if (constraints.getSecurityConstraintsRefClass() != null)
62 {
63 try
64 {
65 constraintsRef = (BaseSecurityConstraintsRef)constraints.getSecurityConstraintsRefClass().newInstance();
66 }
67 catch (InstantiationException ie)
68 {
69 throw new ClassCastException("Unable to create constratins reference list element instance: " + constraints.getSecurityConstraintsRefClass().getName() + ", (" + ie + ").");
70 }
71 catch (IllegalAccessException iae)
72 {
73 throw new ClassCastException("Unable to create constraints reference list element instance: " + constraints.getSecurityConstraintsRefClass().getName() + ", (" + iae + ").");
74 }
75 }
76 else
77 {
78 constraintsRef = new BaseSecurityConstraintsRef();
79 }
80 constraintsRef.setName(name);
81
82 if (constraints.accessConstraintsRefs().contains(constraintsRef))
83 {
84 throw new IllegalArgumentException("Unable to add duplicate entry to list: " + constraintsRef.getName());
85 }
86
87
88 if (removedConstraintsRefs != null)
89 {
90 int removedIndex = removedConstraintsRefs.indexOf(constraintsRef);
91 if (removedIndex >= 0)
92 {
93 constraintsRef = (BaseSecurityConstraintsRef)removedConstraintsRefs.remove(removedIndex);
94 }
95 }
96 return constraintsRef;
97 }
98
99 /***
100 * getRemovedConstraintsRefs
101 *
102 * @return removed constraints refs tracking collection
103 */
104 private List getRemovedConstraintsRefs()
105 {
106 if (removedConstraintsRefs == null)
107 {
108 removedConstraintsRefs = DatabasePageManagerUtils.createList();
109 }
110 return removedConstraintsRefs;
111 }
112
113
114
115
116 public void add(int index, Object element)
117 {
118
119
120 if ((index < 0) || (index > constraints.accessConstraintsRefs().size()))
121 {
122 throw new IndexOutOfBoundsException("Unable to add to list at index: " + index);
123 }
124
125 BaseSecurityConstraintsRef constraintsRef = wrapNameStringForAdd((String)element);
126
127 constraints.accessConstraintsRefs().add(index, constraintsRef);
128
129 if (index > 0)
130 {
131 constraintsRef.setApplyOrder(((BaseSecurityConstraintsRef)constraints.accessConstraintsRefs().get(index-1)).getApplyOrder() + 1);
132 }
133 else
134 {
135 constraintsRef.setApplyOrder(0);
136 }
137
138 for (int i = index, limit = constraints.accessConstraintsRefs().size() - 1; (i < limit); i++)
139 {
140 BaseSecurityConstraintsRef nextConstraintsRef = (BaseSecurityConstraintsRef)constraints.accessConstraintsRefs().get(i + 1);
141 if (nextConstraintsRef.getApplyOrder() <= constraintsRef.getApplyOrder())
142 {
143
144 nextConstraintsRef.setApplyOrder(constraintsRef.getApplyOrder() + 1);
145 constraintsRef = nextConstraintsRef;
146 }
147 else
148 {
149
150 break;
151 }
152 }
153
154 constraints.clearAllSecurityConstraints();
155 }
156
157
158
159
160 public Object get(int index)
161 {
162
163
164 return ((BaseSecurityConstraintsRef)constraints.accessConstraintsRefs().get(index)).getName();
165 }
166
167
168
169
170 public Object remove(int index)
171 {
172
173 BaseSecurityConstraintsRef removed = (BaseSecurityConstraintsRef)constraints.accessConstraintsRefs().remove(index);
174 if (removed != null)
175 {
176
177 getRemovedConstraintsRefs().add(removed);
178
179 constraints.clearAllSecurityConstraints();
180 }
181 return removed;
182 }
183
184
185
186
187 public Object set(int index, Object element)
188 {
189
190
191 BaseSecurityConstraintsRef newConstraintsRef = wrapNameStringForAdd((String)element);
192
193 BaseSecurityConstraintsRef constraintsRef = (BaseSecurityConstraintsRef)constraints.accessConstraintsRefs().set(index, newConstraintsRef);
194
195 newConstraintsRef.setApplyOrder(constraintsRef.getApplyOrder());
196
197 getRemovedConstraintsRefs().add(constraintsRef);
198
199 constraints.clearAllSecurityConstraints();
200
201 return constraintsRef.getName();
202 }
203
204
205
206
207 public int size()
208 {
209
210 return constraints.accessConstraintsRefs().size();
211 }
212 }