Java Reference
In-Depth Information
Principal: CORPORATE\ksharan (User)
Type: ALLOW
Permissions are:
WRITE_OWNER
DELETE_CHILD
EXECUTE
READ_DATA
...
------------------------
Principal: NT AUTHORITY\SYSTEM (Well-known group)
Type: ALLOW
Permissions are:
WRITE_OWNER
...
Updating ACL entries for a file is more involved than reading them. You need to create an
AclEntry
object
using the
AclEntry.Builder
class. The
newBuilder()
method of the
AclEntry
class returns an empty
AclEntry.Builder
object, which acts as a staging area for a new
AclEntry
object. You need to call various setter
methods such as
setPrincipal()
,
setType()
,
setPermissions()
, etc. on the builder object. When you are finished
with setting all properties, call the
build()
method on the builder object to create an
AclEntry
object. The following
snippet of code demonstrates these steps, assuming that
bRiceUser
is a
UserPrincipal
and
permissions
is a
Set
of
AclEntryPermission
:
// Let's build an ACL entry
AclEntry.Builder builder = AclEntry.newBuilder();
builder.setPrincipal(bRiceUser);
builder.setType(AclEntryType.ALLOW);
builder.setPermissions(permissions);
AclEntry newEntry = builder.build();
Once you prepare a new
AclEntry
, you need to add it to the existing ACL entries for the file. The following
snippet of code adds the new ACL entry to the existing ones and sets them back using an ACL attribute view:
// Get the ACL entry for the path
List<AclEntry> aclEntries = aclView.getAcl();
// Add the ACL entry to the existing list
aclEntries.add(newEntry);
// Update the ACL entries for the file
aclView.setAcl(aclEntries);
Listing 10-18 demonstrates how to add a new ACL entry for a user named
brice
. It adds
DATA_READ
and
DATA_
WRITE
permissions for the user
brice
on the
C:\poems\luci1.txt
file. Make sure that the file
C:\poems\luci1.txt
and a user with the user id
brice
exist on the machine. Please change the file and user id that exist on the machine to
set the ACL entries for another file and user id.