View Javadoc
1   package org.codehaus.plexus.components.io.attributes;
2   
3   /*
4    * Copyright 2007 The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import javax.annotation.Nullable;
20  
21  /*
22   * A very simple pojo based PlexusIoResourceAttributes without any kind of backing
23   */
24  public class SimpleResourceAttributes implements PlexusIoResourceAttributes {
25  
26      private Integer gid;
27  
28      private Integer uid;
29  
30      private String userName;
31  
32      private String groupName;
33  
34      private int mode = PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE;
35  
36      private boolean isSymbolicLink;
37  
38      public SimpleResourceAttributes(Integer uid, String userName, Integer gid, String groupName, int mode) {
39          this.uid = uid;
40          this.userName = userName;
41          this.gid = gid;
42          this.groupName = groupName;
43          this.mode = mode;
44      }
45  
46      public SimpleResourceAttributes(
47              Integer uid, String userName, Integer gid, String groupName, int mode, boolean isSymbolicLink) {
48          this.uid = uid;
49          this.userName = userName;
50          this.gid = gid;
51          this.groupName = groupName;
52          this.mode = mode;
53          this.isSymbolicLink = isSymbolicLink;
54      }
55  
56      public static PlexusIoResourceAttributes lastResortDummyAttributesForBrokenOS() {
57          return new SimpleResourceAttributes();
58      }
59  
60      SimpleResourceAttributes() {}
61  
62      public int getOctalMode() {
63          return mode;
64      }
65  
66      @Nullable
67      public Integer getGroupId() {
68          return gid;
69      }
70  
71      @Nullable
72      public String getGroupName() {
73          return groupName;
74      }
75  
76      public Integer getUserId() {
77          return uid;
78      }
79  
80      public String getUserName() {
81          return userName;
82      }
83  
84      public boolean isGroupExecutable() {
85          return PlexusIoResourceAttributeUtils.isGroupExecutableInOctal(mode);
86      }
87  
88      public boolean isGroupReadable() {
89          return PlexusIoResourceAttributeUtils.isGroupReadableInOctal(mode);
90      }
91  
92      public boolean isGroupWritable() {
93          return PlexusIoResourceAttributeUtils.isGroupWritableInOctal(mode);
94      }
95  
96      public boolean isOwnerExecutable() {
97          return PlexusIoResourceAttributeUtils.isOwnerExecutableInOctal(mode);
98      }
99  
100     public boolean isOwnerReadable() {
101         return PlexusIoResourceAttributeUtils.isOwnerReadableInOctal(mode);
102     }
103 
104     public boolean isOwnerWritable() {
105         return PlexusIoResourceAttributeUtils.isOwnerWritableInOctal(mode);
106     }
107 
108     public boolean isWorldExecutable() {
109         return PlexusIoResourceAttributeUtils.isWorldExecutableInOctal(mode);
110     }
111 
112     public boolean isWorldReadable() {
113         return PlexusIoResourceAttributeUtils.isWorldReadableInOctal(mode);
114     }
115 
116     public boolean isWorldWritable() {
117         return PlexusIoResourceAttributeUtils.isWorldWritableInOctal(mode);
118     }
119 
120     public String getOctalModeString() {
121         return Integer.toString(mode, 8);
122     }
123 
124     public PlexusIoResourceAttributes setOctalMode(int mode) {
125         this.mode = mode;
126         return this;
127     }
128 
129     public PlexusIoResourceAttributes setGroupId(Integer gid) {
130         this.gid = gid;
131         return this;
132     }
133 
134     public PlexusIoResourceAttributes setGroupName(String name) {
135         this.groupName = name;
136         return this;
137     }
138 
139     public PlexusIoResourceAttributes setUserId(Integer uid) {
140         this.uid = uid;
141         return this;
142     }
143 
144     public PlexusIoResourceAttributes setUserName(String name) {
145         this.userName = name;
146         return this;
147     }
148 
149     public PlexusIoResourceAttributes setOctalModeString(String mode) {
150         setOctalMode(Integer.parseInt(mode, 8));
151         return this;
152     }
153 
154     public String toString() {
155         return String.format(
156                 "%nResource Attributes:%n------------------------------%nuser: %s%ngroup: %s%nuid: %d%ngid: %d%nmode: %06o",
157                 userName == null ? "" : userName,
158                 groupName == null ? "" : groupName,
159                 uid != null ? uid : 0,
160                 gid != null ? gid : 0,
161                 mode);
162     }
163 
164     public void setSymbolicLink(boolean isSymbolicLink) {
165         this.isSymbolicLink = isSymbolicLink;
166     }
167 
168     public boolean isSymbolicLink() {
169         return isSymbolicLink;
170     }
171 }