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   * PageSecurityConstraintsRefList
26   *
27   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
28   * @version $Id$
29   */
30  class PageSecurityConstraintsRefList extends AbstractList
31  {
32      private PageSecurityImpl pageSecurity;
33  
34      private List removedConstraintsRefs;
35  
36      PageSecurityConstraintsRefList(PageSecurityImpl pageSecurity)
37      {
38          super();
39          this.pageSecurity = pageSecurity;
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 PageSecurityGlobalSecurityConstraintsRef 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
59          PageSecurityGlobalSecurityConstraintsRef constraintsRef = new PageSecurityGlobalSecurityConstraintsRef();
60          constraintsRef.setName(name);
61          // make sure element is unique
62          if (pageSecurity.accessGlobalConstraintsRefs().contains(constraintsRef))
63          {
64              throw new IllegalArgumentException("Unable to add duplicate entry to list: " + constraintsRef.getName());
65          }
66          // retrieve from removed list to reuse
67          // previously removed element
68          if (removedConstraintsRefs != null)
69          {
70              int removedIndex = removedConstraintsRefs.indexOf(constraintsRef);
71              if (removedIndex >= 0)
72              {
73                  constraintsRef = (PageSecurityGlobalSecurityConstraintsRef)removedConstraintsRefs.remove(removedIndex);
74              }
75          }
76          return constraintsRef;
77      }
78  
79      /***
80       * getRemovedConstraintsRefs
81       *
82       * @return removed constraints refs tracking collection
83       */
84      private List getRemovedConstraintsRefs()
85      {
86          if (removedConstraintsRefs == null)
87          {
88              removedConstraintsRefs = DatabasePageManagerUtils.createList();
89          }
90          return removedConstraintsRefs;
91      }
92  
93      /* (non-Javadoc)
94       * @see java.util.List#add(int,java.lang.Object)
95       */
96      public void add(int index, Object element)
97      {
98          // implement for modifiable AbstractList:
99          // validate index
100         if ((index < 0) || (index > pageSecurity.accessGlobalConstraintsRefs().size()))
101         {
102             throw new IndexOutOfBoundsException("Unable to add to list at index: " + index);
103         }
104         // wrap and verify constraints ref name string
105         PageSecurityGlobalSecurityConstraintsRef constraintsRef = wrapNameStringForAdd((String)element);
106         // add to underlying ordered list
107         pageSecurity.accessGlobalConstraintsRefs().add(index, constraintsRef);
108         // set apply order in added element
109         if (index > 0)
110         {
111             constraintsRef.setApplyOrder(((PageSecurityGlobalSecurityConstraintsRef)pageSecurity.accessGlobalConstraintsRefs().get(index-1)).getApplyOrder() + 1);
112         }
113         else
114         {
115             constraintsRef.setApplyOrder(0);
116         }
117         // maintain apply order in subsequent elements
118         for (int i = index, limit = pageSecurity.accessGlobalConstraintsRefs().size() - 1; (i < limit); i++)
119         {
120             PageSecurityGlobalSecurityConstraintsRef nextConstraintsRef = (PageSecurityGlobalSecurityConstraintsRef)pageSecurity.accessGlobalConstraintsRefs().get(i + 1);
121             if (nextConstraintsRef.getApplyOrder() <= constraintsRef.getApplyOrder())
122             {
123                 // adjust apply order for next element
124                 nextConstraintsRef.setApplyOrder(constraintsRef.getApplyOrder() + 1);
125                 constraintsRef = nextConstraintsRef;
126             }
127             else
128             {
129                 // apply order maintained for remaining list elements
130                 break;
131             }
132         }
133     }
134 
135     /* (non-Javadoc)
136      * @see java.util.List#get(int)
137      */
138     public Object get(int index)
139     {
140         // implement for modifiable AbstractList:
141         // unwrap constraints ref name string
142         return ((PageSecurityGlobalSecurityConstraintsRef)pageSecurity.accessGlobalConstraintsRefs().get(index)).getName();
143     }
144 
145     /* (non-Javadoc)
146      * @see java.util.List#remove(int)
147      */
148     public Object remove(int index)
149     {
150         // implement for modifiable AbstractList:
151         // save removed element 
152         PageSecurityGlobalSecurityConstraintsRef removed = (PageSecurityGlobalSecurityConstraintsRef)pageSecurity.accessGlobalConstraintsRefs().remove(index);
153         if (removed != null)
154         {
155             getRemovedConstraintsRefs().add(removed);
156         }
157         return removed;
158     }
159 
160     /* (non-Javadoc)
161      * @see java.util.List#set(int,java.lang.Object)
162      */
163     public Object set(int index, Object element)
164     {
165         // implement for modifiable AbstractList:
166         // wrap and verify constraints ref name string
167         PageSecurityGlobalSecurityConstraintsRef newConstraintsRef = wrapNameStringForAdd((String)element);
168         // set in underlying ordered list
169         PageSecurityGlobalSecurityConstraintsRef constraintsRef = (PageSecurityGlobalSecurityConstraintsRef)pageSecurity.accessGlobalConstraintsRefs().set(index, newConstraintsRef);
170         // set apply order in new element
171         newConstraintsRef.setApplyOrder(constraintsRef.getApplyOrder());
172         // save replaced element
173         getRemovedConstraintsRefs().add(constraintsRef);
174         // return unwrapped constraints ref name string
175         return constraintsRef.getName();
176     }
177 
178     /* (non-Javadoc)
179      * @see java.util.List#size()
180      */
181     public int size()
182     {
183         // implement for modifiable AbstractList
184         return pageSecurity.accessGlobalConstraintsRefs().size();
185     }
186 }