1 /*
2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.nio.fs;
27 
28 import java.nio.file.attribute.*;
29 import java.util.*;
30 import java.io.IOException;
31 
32 /**
33  * An implementation of FileOwnerAttributeView that delegates to a given
34  * PosixFileAttributeView or AclFileAttributeView object.
35  */
36 
37 final class FileOwnerAttributeViewImpl
38     implements FileOwnerAttributeView, DynamicFileAttributeView
39 {
40     private static final String OWNER_NAME = "owner";
41 
42     private final FileAttributeView view;
43     private final boolean isPosixView;
44 
FileOwnerAttributeViewImpl(PosixFileAttributeView view)45     FileOwnerAttributeViewImpl(PosixFileAttributeView view) {
46         this.view = view;
47         this.isPosixView = true;
48     }
49 
FileOwnerAttributeViewImpl(AclFileAttributeView view)50     FileOwnerAttributeViewImpl(AclFileAttributeView view) {
51         this.view = view;
52         this.isPosixView = false;
53     }
54 
55     @Override
name()56     public String name() {
57         return "owner";
58     }
59 
60     @Override
setAttribute(String attribute, Object value)61     public void setAttribute(String attribute, Object value)
62         throws IOException
63     {
64         if (attribute.equals(OWNER_NAME)) {
65             setOwner((UserPrincipal)value);
66         } else {
67             throw new IllegalArgumentException("'" + name() + ":" +
68                 attribute + "' not recognized");
69         }
70     }
71 
72     @Override
readAttributes(String[] attributes)73     public Map<String,Object> readAttributes(String[] attributes) throws IOException {
74         Map<String,Object> result = new HashMap<>();
75         for (String attribute: attributes) {
76             if (attribute.equals("*") || attribute.equals(OWNER_NAME)) {
77                 result.put(OWNER_NAME, getOwner());
78             } else {
79                 throw new IllegalArgumentException("'" + name() + ":" +
80                     attribute + "' not recognized");
81             }
82         }
83         return result;
84     }
85 
86     @Override
getOwner()87     public UserPrincipal getOwner() throws IOException {
88         if (isPosixView) {
89             return ((PosixFileAttributeView)view).readAttributes().owner();
90         } else {
91             return ((AclFileAttributeView)view).getOwner();
92         }
93     }
94 
95     @Override
setOwner(UserPrincipal owner)96     public void setOwner(UserPrincipal owner)
97         throws IOException
98     {
99         if (isPosixView) {
100             ((PosixFileAttributeView)view).setOwner(owner);
101         } else {
102             ((AclFileAttributeView)view).setOwner(owner);
103         }
104     }
105  }
106