001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.aci;
021    
022    
023    import java.io.Serializable;
024    import java.util.Collection;
025    import java.util.HashSet;
026    import java.util.Set;
027    
028    import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
029    
030    
031    /**
032     * An abstract class that provides common properties and operations for
033     * {@link ItemFirstACIItem} and {@link UserFirstACIItem} as specified X.501
034     * specification.
035     * 
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev: 586528 $, $Date: 2007-10-19 18:47:06 +0200 (Ven, 19 oct 2007) $
038     */
039    public abstract class ACIItem implements Serializable
040    {
041        private String identificationTag;
042    
043        /* 0 ~ 255 */
044        private int precedence = 0;
045    
046        private AuthenticationLevel authenticationLevel;
047    
048    
049        /**
050         * Creates a new instance
051         * 
052         * @param identificationTag
053         *            the id string of this item
054         * @param precedence
055         *            the precedence of this item
056         * @param authenticationLevel
057         *            the level of authentication required to this item
058         */
059        protected ACIItem(String identificationTag, int precedence, AuthenticationLevel authenticationLevel)
060        {
061            if ( identificationTag == null )
062            {
063                throw new NullPointerException( "identificationTag" );
064            }
065            if ( precedence < 0 || precedence > 255 )
066            {
067                throw new IllegalArgumentException( "precedence: " + precedence );
068            }
069            if ( authenticationLevel == null )
070            {
071                throw new NullPointerException( "authenticationLevel" );
072            }
073    
074            this.identificationTag = identificationTag;
075            this.precedence = precedence;
076            this.authenticationLevel = authenticationLevel;
077        }
078    
079    
080        /**
081         * Returns the id string of this item.
082         */
083        public String getIdentificationTag()
084        {
085            return identificationTag;
086        }
087    
088    
089        /**
090         * Returns the precedence of this item.
091         */
092        public int getPrecedence()
093        {
094            return precedence;
095        }
096    
097    
098        /**
099         * Returns the level of authentication required to this item.
100         */
101        public AuthenticationLevel getAuthenticationLevel()
102        {
103            return authenticationLevel;
104        }
105    
106    
107        /**
108         * Converts this item into a collection of {@link ACITuple}s and returns
109         * it.
110         */
111        public abstract Collection<ACITuple> toTuples();
112    
113    
114        /**
115         * Converts a set of {@link GrantAndDenial}s into a set of
116         * {@link MicroOperation}s and returns it.
117         */
118        protected static Set<MicroOperation> toMicroOperations( Set<GrantAndDenial> grantsAndDenials )
119        {
120            Set<MicroOperation> microOps = new HashSet<MicroOperation>();
121            
122            for ( GrantAndDenial grantAndDenial:grantsAndDenials )
123            {
124                microOps.add( grantAndDenial.getMicroOperation() );
125            }
126            
127            return microOps;
128        }
129    }