Path: blob/master/src/java.base/share/classes/sun/nio/fs/FileOwnerAttributeViewImpl.java
41159 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.nio.file.attribute.*;28import java.util.*;29import java.io.IOException;3031/**32* An implementation of FileOwnerAttributeView that delegates to a given33* PosixFileAttributeView or AclFileAttributeView object.34*/3536final class FileOwnerAttributeViewImpl37implements FileOwnerAttributeView, DynamicFileAttributeView38{39private static final String OWNER_NAME = "owner";4041private final FileAttributeView view;42private final boolean isPosixView;4344FileOwnerAttributeViewImpl(PosixFileAttributeView view) {45this.view = view;46this.isPosixView = true;47}4849FileOwnerAttributeViewImpl(AclFileAttributeView view) {50this.view = view;51this.isPosixView = false;52}5354@Override55public String name() {56return "owner";57}5859@Override60public void setAttribute(String attribute, Object value)61throws IOException62{63if (attribute.equals(OWNER_NAME)) {64setOwner((UserPrincipal)value);65} else {66throw new IllegalArgumentException("'" + name() + ":" +67attribute + "' not recognized");68}69}7071@Override72public Map<String,Object> readAttributes(String[] attributes) throws IOException {73Map<String,Object> result = new HashMap<>();74for (String attribute: attributes) {75if (attribute.equals("*") || attribute.equals(OWNER_NAME)) {76result.put(OWNER_NAME, getOwner());77} else {78throw new IllegalArgumentException("'" + name() + ":" +79attribute + "' not recognized");80}81}82return result;83}8485@Override86public UserPrincipal getOwner() throws IOException {87if (isPosixView) {88return ((PosixFileAttributeView)view).readAttributes().owner();89} else {90return ((AclFileAttributeView)view).getOwner();91}92}9394@Override95public void setOwner(UserPrincipal owner)96throws IOException97{98if (isPosixView) {99((PosixFileAttributeView)view).setOwner(owner);100} else {101((AclFileAttributeView)view).setOwner(owner);102}103}104}105106107