View Javadoc

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 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          // only non-null names supported
54          if (name == null)
55          {
56              throw new NullPointerException("Unable to add null to list.");
57          }
58          // wrap constraints ref name string; use
59          // specific constraints ref name string wrapper
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          // make sure element is unique
82          if (constraints.accessConstraintsRefs().contains(constraintsRef))
83          {
84              throw new IllegalArgumentException("Unable to add duplicate entry to list: " + constraintsRef.getName());
85          }
86          // retrieve from removed list to reuse
87          // previously removed element
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     /* (non-Javadoc)
114      * @see java.util.List#add(int,java.lang.Object)
115      */
116     public void add(int index, Object element)
117     {
118         // implement for modifiable AbstractList:
119         // validate index
120         if ((index < 0) || (index > constraints.accessConstraintsRefs().size()))
121         {
122             throw new IndexOutOfBoundsException("Unable to add to list at index: " + index);
123         }
124         // wrap and verify constraints ref name string
125         BaseSecurityConstraintsRef constraintsRef = wrapNameStringForAdd((String)element);
126         // add to underlying ordered list
127         constraints.accessConstraintsRefs().add(index, constraintsRef);
128         // set apply order in added element
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         // maintain apply order in subsequent elements
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                 // adjust apply order for next element
144                 nextConstraintsRef.setApplyOrder(constraintsRef.getApplyOrder() + 1);
145                 constraintsRef = nextConstraintsRef;
146             }
147             else
148             {
149                 // apply order maintained for remaining list elements
150                 break;
151             }
152         }
153         // clear all cached security constraints
154         constraints.clearAllSecurityConstraints();
155     }
156 
157     /* (non-Javadoc)
158      * @see java.util.List#get(int)
159      */
160     public Object get(int index)
161     {
162         // implement for modifiable AbstractList:
163         // unwrap constraints ref name string
164         return ((BaseSecurityConstraintsRef)constraints.accessConstraintsRefs().get(index)).getName();
165     }
166 
167     /* (non-Javadoc)
168      * @see java.util.List#remove(int)
169      */
170     public Object remove(int index)
171     {
172         // implement for modifiable AbstractList
173         BaseSecurityConstraintsRef removed = (BaseSecurityConstraintsRef)constraints.accessConstraintsRefs().remove(index);
174         if (removed != null)
175         {
176             // save removed element 
177             getRemovedConstraintsRefs().add(removed);
178             // clear all cached security constraints
179             constraints.clearAllSecurityConstraints();
180         }
181         return removed;
182     }
183 
184     /* (non-Javadoc)
185      * @see java.util.List#set(int,java.lang.Object)
186      */
187     public Object set(int index, Object element)
188     {
189         // implement for modifiable AbstractList:
190         // wrap and verify constraints ref name string
191         BaseSecurityConstraintsRef newConstraintsRef = wrapNameStringForAdd((String)element);
192         // set in underlying ordered list
193         BaseSecurityConstraintsRef constraintsRef = (BaseSecurityConstraintsRef)constraints.accessConstraintsRefs().set(index, newConstraintsRef);
194         // set apply order in new element
195         newConstraintsRef.setApplyOrder(constraintsRef.getApplyOrder());
196         // save replaced element
197         getRemovedConstraintsRefs().add(constraintsRef);
198         // clear all cached security constraints
199         constraints.clearAllSecurityConstraints();
200         // return unwrapped constraints ref name string
201         return constraintsRef.getName();
202     }
203 
204     /* (non-Javadoc)
205      * @see java.util.List#size()
206      */
207     public int size()
208     {
209         // implement for modifiable AbstractList
210         return constraints.accessConstraintsRefs().size();
211     }
212 }