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, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.fs.permission;
019    
020    import java.util.List;
021    
022    import org.apache.hadoop.classification.InterfaceAudience;
023    import org.apache.hadoop.classification.InterfaceStability;
024    
025    import com.google.common.base.Objects;
026    import com.google.common.collect.Lists;
027    
028    /**
029     * An AclStatus contains the ACL information of a specific file. AclStatus
030     * instances are immutable. Use a {@link Builder} to create a new instance.
031     */
032    @InterfaceAudience.Public
033    @InterfaceStability.Evolving
034    public class AclStatus {
035      private final String owner;
036      private final String group;
037      private final boolean stickyBit;
038      private final List<AclEntry> entries;
039    
040      /**
041       * Returns the file owner.
042       *
043       * @return String file owner
044       */
045      public String getOwner() {
046        return owner;
047      }
048    
049      /**
050       * Returns the file group.
051       *
052       * @return String file group
053       */
054      public String getGroup() {
055        return group;
056      }
057    
058      /**
059       * Returns the sticky bit.
060       * 
061       * @return boolean sticky bit
062       */
063      public boolean isStickyBit() {
064        return stickyBit;
065      }
066    
067      /**
068       * Returns the list of all ACL entries, ordered by their natural ordering.
069       *
070       * @return List<AclEntry> unmodifiable ordered list of all ACL entries
071       */
072      public List<AclEntry> getEntries() {
073        return entries;
074      }
075    
076      @Override
077      public boolean equals(Object o) {
078        if (o == null) {
079          return false;
080        }
081        if (getClass() != o.getClass()) {
082          return false;
083        }
084        AclStatus other = (AclStatus)o;
085        return Objects.equal(owner, other.owner)
086            && Objects.equal(group, other.group)
087            && stickyBit == other.stickyBit
088            && Objects.equal(entries, other.entries);
089      }
090    
091      @Override
092      public int hashCode() {
093        return Objects.hashCode(owner, group, stickyBit, entries);
094      }
095    
096      @Override
097      public String toString() {
098        return new StringBuilder()
099          .append("owner: ").append(owner)
100          .append(", group: ").append(group)
101          .append(", acl: {")
102          .append("entries: ").append(entries)
103          .append(", stickyBit: ").append(stickyBit)
104          .append('}')
105          .toString();
106      }
107    
108      /**
109       * Builder for creating new Acl instances.
110       */
111      public static class Builder {
112        private String owner;
113        private String group;
114        private boolean stickyBit;
115        private List<AclEntry> entries = Lists.newArrayList();
116    
117        /**
118         * Sets the file owner.
119         *
120         * @param owner String file owner
121         * @return Builder this builder, for call chaining
122         */
123        public Builder owner(String owner) {
124          this.owner = owner;
125          return this;
126        }
127    
128        /**
129         * Sets the file group.
130         *
131         * @param group String file group
132         * @return Builder this builder, for call chaining
133         */
134        public Builder group(String group) {
135          this.group = group;
136          return this;
137        }
138    
139        /**
140         * Adds an ACL entry.
141         *
142         * @param e AclEntry entry to add
143         * @return Builder this builder, for call chaining
144         */
145        public Builder addEntry(AclEntry e) {
146          this.entries.add(e);
147          return this;
148        }
149    
150        /**
151         * Adds a list of ACL entries.
152         *
153         * @param entries AclEntry entries to add
154         * @return Builder this builder, for call chaining
155         */
156        public Builder addEntries(Iterable<AclEntry> entries) {
157          for (AclEntry e : entries)
158            this.entries.add(e);
159          return this;
160        }
161    
162        /**
163         * Sets sticky bit. If this method is not called, then the builder assumes
164         * false.
165         *
166         * @param stickyBit
167         *          boolean sticky bit
168         * @return Builder this builder, for call chaining
169         */
170        public Builder stickyBit(boolean stickyBit) {
171          this.stickyBit = stickyBit;
172          return this;
173        }
174    
175        /**
176         * Builds a new AclStatus populated with the set properties.
177         *
178         * @return AclStatus new AclStatus
179         */
180        public AclStatus build() {
181          return new AclStatus(owner, group, stickyBit, entries);
182        }
183      }
184    
185      /**
186       * Private constructor.
187       *
188       * @param file Path file associated to this ACL
189       * @param owner String file owner
190       * @param group String file group
191       * @param stickyBit the sticky bit
192       * @param entries the ACL entries
193       */
194      private AclStatus(String owner, String group, boolean stickyBit,
195          Iterable<AclEntry> entries) {
196        this.owner = owner;
197        this.group = group;
198        this.stickyBit = stickyBit;
199        this.entries = Lists.newArrayList(entries);
200      }
201    }