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   * PageSecurityConstraintsDefList
26   *
27   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
28   * @version $Id$
29   */
30  class PageSecurityConstraintsDefList extends AbstractList
31  {
32      private PageSecurityImpl pageSecurity;
33  
34      private List removedConstraintsDefs;
35  
36      PageSecurityConstraintsDefList(PageSecurityImpl pageSecurity)
37      {
38          super();
39          this.pageSecurity = pageSecurity;
40      }
41  
42      /***
43       * validateConstraintsDefForAdd
44       *
45       * Validates constraints def to be added to this list.
46       *
47       * @param constraintsDef constraints definition to add
48       * @return list element to add
49       */
50      private SecurityConstraintsDefImpl validateConstraintsDefForAdd(SecurityConstraintsDefImpl constraintsDef)
51      {
52          // only non-null definitions supported
53          if (constraintsDef == null)
54          {
55              throw new NullPointerException("Unable to add null to list.");
56          }
57          // make sure element is unique
58          if (pageSecurity.accessConstraintsDefs().contains(constraintsDef))
59          {
60              throw new IllegalArgumentException("Unable to add duplicate entry to list: " + constraintsDef.getName());
61          }
62          // retrieve from removed list to reuse
63          // previously removed element copying
64          // security constraint defs
65          if (removedConstraintsDefs != null)
66          {
67              int removedIndex = removedConstraintsDefs.indexOf(constraintsDef);
68              if (removedIndex >= 0)
69              {
70                  SecurityConstraintsDefImpl addConstraintsDef = constraintsDef;
71                  constraintsDef = (SecurityConstraintsDefImpl)removedConstraintsDefs.remove(removedIndex);
72                  // TODO: move this logic to copy methods on implementations
73                  constraintsDef.setSecurityConstraints(addConstraintsDef.getSecurityConstraints());
74              }
75          }
76          return constraintsDef;
77      }
78  
79      /***
80       * getRemovedConstraintsDefs
81       *
82       * @return removed constraints defs tracking collection
83       */
84      private List getRemovedConstraintsDefs()
85      {
86          if (removedConstraintsDefs == null)
87          {
88              removedConstraintsDefs = DatabasePageManagerUtils.createList();
89          }
90          return removedConstraintsDefs;
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.accessConstraintsDefs().size()))
101         {
102             throw new IndexOutOfBoundsException("Unable to add to list at index: " + index);
103         }
104         // verify constraints definition
105         SecurityConstraintsDefImpl constraintsDef = validateConstraintsDefForAdd((SecurityConstraintsDefImpl)element);
106         // add to underlying ordered list
107         pageSecurity.accessConstraintsDefs().add(index, constraintsDef);
108         // clear cached security constraints definition map
109         pageSecurity.clearSecurityConstraintsDefsMap();
110     }
111 
112     /* (non-Javadoc)
113      * @see java.util.List#get(int)
114      */
115     public Object get(int index)
116     {
117         // implement for modifiable AbstractList
118         return pageSecurity.accessConstraintsDefs().get(index);
119     }
120 
121     /* (non-Javadoc)
122      * @see java.util.List#remove(int)
123      */
124     public Object remove(int index)
125     {
126         // implement for modifiable AbstractList
127         SecurityConstraintsDefImpl removed = (SecurityConstraintsDefImpl)pageSecurity.accessConstraintsDefs().remove(index);
128         if (removed != null)
129         {
130             // save removed element 
131             getRemovedConstraintsDefs().add(removed);
132             // clear cached security constraints definition map
133             pageSecurity.clearSecurityConstraintsDefsMap();
134         }
135         return removed;
136     }
137 
138     /* (non-Javadoc)
139      * @see java.util.List#set(int,java.lang.Object)
140      */
141     public Object set(int index, Object element)
142     {
143         // implement for modifiable AbstractList:
144         // verify constraints definition
145         SecurityConstraintsDefImpl newConstraintsDef = validateConstraintsDefForAdd((SecurityConstraintsDefImpl)element);
146         // set in underlying ordered list
147         SecurityConstraintsDefImpl constraintsDef = (SecurityConstraintsDefImpl)pageSecurity.accessConstraintsDefs().set(index, newConstraintsDef);
148         // save replaced element
149         getRemovedConstraintsDefs().add(constraintsDef);
150         // clear cached security constraints definition map
151         pageSecurity.clearSecurityConstraintsDefsMap();
152         // return constraints definition
153         return constraintsDef;
154     }
155 
156     /* (non-Javadoc)
157      * @see java.util.List#size()
158      */
159     public int size()
160     {
161         // implement for modifiable AbstractList
162         return pageSecurity.accessConstraintsDefs().size();
163     }
164 }