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  
21  import org.apache.jetspeed.om.page.SecurityConstraintImpl;
22  
23  /***
24   * SecurityConstraintList
25   *
26   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
27   * @version $Id$
28   */
29  class SecurityConstraintList extends AbstractList
30  {
31      private SecurityConstraintsImpl constraints;
32  
33      SecurityConstraintList(SecurityConstraintsImpl constraints)
34      {
35          super();
36          this.constraints = constraints;
37      }
38  
39      /***
40       * validateConstraintForAdd
41       *
42       * Validates constraint to be added to this list.
43       *
44       * @param constraint to add
45       * @return list element to add
46       */
47      private SecurityConstraintImpl validateConstraintForAdd(SecurityConstraintImpl constraint)
48      {
49          // validate constraint instance class
50          if (constraint == null)
51          {
52              throw new NullPointerException("Unable to add null to list.");
53          }
54          if ((constraints.getSecurityConstraintClass() != null) &&
55              !constraints.getSecurityConstraintClass().isInstance(constraint))
56          {
57              throw new ClassCastException("Unable to add list element instance: expected " + constraints.getSecurityConstraintClass().getName() + ", got " + constraint.getClass().getName() + ".");
58          }
59          return constraint;
60      }
61  
62      /* (non-Javadoc)
63       * @see java.util.List#add(int,java.lang.Object)
64       */
65      public void add(int index, Object element)
66      {
67          // implement for modifiable AbstractList:
68          // validate index
69          if ((index < 0) || (index > constraints.accessConstraints().size()))
70          {
71              throw new IndexOutOfBoundsException("Unable to add to list at index: " + index);
72          }
73          // verify constraint
74          SecurityConstraintImpl constraint = validateConstraintForAdd((SecurityConstraintImpl)element);
75          // add to underlying ordered list
76          constraints.accessConstraints().add(index, constraint);
77          // set apply order in added element
78          if (index > 0)
79          {
80              constraint.setApplyOrder(((SecurityConstraintImpl)constraints.accessConstraints().get(index-1)).getApplyOrder() + 1);
81          }
82          else
83          {
84              constraint.setApplyOrder(0);
85          }
86          // maintain apply order in subsequent elements
87          for (int i = index, limit = constraints.accessConstraints().size() - 1; (i < limit); i++)
88          {
89              SecurityConstraintImpl nextConstraint = (SecurityConstraintImpl)constraints.accessConstraints().get(i + 1);
90              if (nextConstraint.getApplyOrder() <= constraint.getApplyOrder())
91              {
92                  // adjust apply order for next element
93                  nextConstraint.setApplyOrder(constraint.getApplyOrder() + 1);
94                  constraint = nextConstraint;
95              }
96              else
97              {
98                  // apply order maintained for remaining list elements
99                  break;
100             }
101         }
102         // clear all cached security constraints
103         constraints.clearAllSecurityConstraints();
104     }
105 
106     /* (non-Javadoc)
107      * @see java.util.List#get(int)
108      */
109     public Object get(int index)
110     {
111         // implement for modifiable AbstractList
112         return constraints.accessConstraints().get(index);
113     }
114 
115     /* (non-Javadoc)
116      * @see java.util.List#remove(int)
117      */
118     public Object remove(int index)
119     {
120         // implement for modifiable AbstractList
121         Object removed = constraints.accessConstraints().remove(index);
122         if (removed != null)
123         {
124             // clear all cached security constraints
125             constraints.clearAllSecurityConstraints();
126         }
127         return removed;
128     }
129 
130     /* (non-Javadoc)
131      * @see java.util.List#set(int,java.lang.Object)
132      */
133     public Object set(int index, Object element)
134     {
135         // implement for modifiable AbstractList:
136         // verify constraint
137         SecurityConstraintImpl newConstraint = validateConstraintForAdd((SecurityConstraintImpl)element);
138         // set in underlying ordered list
139         SecurityConstraintImpl constraint = (SecurityConstraintImpl)constraints.accessConstraints().set(index, newConstraint);
140         // set apply order in new element
141         newConstraint.setApplyOrder(constraint.getApplyOrder());
142         // clear all cached security constraints
143         constraints.clearAllSecurityConstraints();
144         // return constraint
145         return constraint;
146     }
147 
148     /* (non-Javadoc)
149      * @see java.util.List#size()
150      */
151     public int size()
152     {
153         // implement for modifiable AbstractList
154         return constraints.accessConstraints().size();
155     }
156 }