Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   *
   * Copyright 2013 Red Hat, Inc. and/or its affiliates.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.picketlink.permission.internal;
 
 import java.util.Map;
 import java.util.Set;
 
Stored resource permissions can either be persisted as a comma-separated list of values, or as a bit-masked numerical value where each bit represents a specific permission for that class. This is a helper class that handles the conversion automatically and presents a unified API for dealing with these persistent actions.

Author(s):
Shane Bryzak
 
 public abstract class BaseAbstractPermissionHandler implements PermissionHandler {
     private Map<Class<?>, BooleanusesMask = new HashMap<Class<?>, Boolean>();
 
     private Map<Class<?>, Map<StringLong>> classPermissions = new HashMap<Class<?>, Map<StringLong>>();
 
     private synchronized void initClassPermissions(Class<?> cls) {
         if (!.containsKey(cls)) {
             Map<StringLongactions = new HashMap<StringLong>();
 
             boolean useMask = false;
 
             AllowedPermissions p = (AllowedPermissionscls.getAnnotation(AllowedPermissions.class);
 
             if (p != null) {
                 AllowedPermission[] permissions = p.value();
                 if (permissions != null) {
                     for (AllowedPermission permission : permissions) {
                         actions.put(permission.name(), permission.mask());
 
                         if (permission.mask() != 0) {
                             useMask = true;
                         }
                     }
                 }
             }
 
             // Validate that all actions have a proper mask
             if (useMask) {
                 Set<Longmasks = new HashSet<Long>();
 
                 for (String action : actions.keySet()) {
                     Long mask = actions.get(action);
                     if (masks.contains(mask)) {
                         throw new IllegalArgumentException("Class " + cls.getName() +
                                 " defines a duplicate mask for permission action [" + action + "]");
                     }
 
                     if (mask == 0) {
                         throw new IllegalArgumentException("Class " + cls.getName() +
                                 " must define a valid mask value for action [" + action + "]");
                     }
 
                     if ((mask & (mask - 1)) != 0) {
                         throw new IllegalArgumentException("Class " + cls.getName() +
                                 " must define a mask value that is a power of 2 for action [" + action + "]");
                     }
 
                     masks.add(mask);
                 }
             }
 
             .put(clsuseMask);
             .put(clsactions);
         }
     }
 
     protected class PermissionSet {
         private Set<Stringpermissions = new HashSet<String>();
 
         private Class<?> resourceClass;
 
         public PermissionSet(Class<?> resourceClassString members) {
            this. = resourceClass;
            addMembers(members);
        }
        public void addMembers(String members) {
            if (members == null) {
                return;
            }
            if (.get()) {
                // bit mask-based actions
                long vals = Long.valueOf(members);
                Map<StringLongpermissions = .get();
                for (String permission : permissions.keySet()) {
                    long mask = permissions.get(permission).longValue();
                    if ((vals & mask) != 0) {
                        this..add(permission);
                    }
                }
            } 
            else {
                // comma-separated string based actions
                String[] permissions = members.split(",");
                for (String permission : permissions) {
                    this..add(permission);
                }
            }
        }
        public boolean contains(String action) {
            return .contains(action);
        }
        public PermissionSet add(String action) {
            .add(action);
            return this;
        }
        public PermissionSet remove(String action) {
            .remove(action);
            return this;
        }
        public Set<StringgetPermissions() {
            return ;
        }
        public boolean isEmpty() {
            return .isEmpty();
        }
        @Override
        public String toString() {
            if (.get()) {
                Map<StringLongactions = .get();
                long mask = 0;
                for (String member : ) {
                    mask |= actions.get(member).longValue();
                }
                return "" + mask;
            } 
            else {
                StringBuilder sb = new StringBuilder();
                for (String member : ) {
                    if (sb.length() > 0) {
                        sb.append(',');
                    }
                    sb.append(member);
                }
                return sb.toString();
            }
        }
    }
    public PermissionSet createPermissionSet(Class<?> resourceClassString members
    {
        if (!.containsKey(resourceClass)) {
            initClassPermissions(resourceClass);
        }
        return new PermissionSet(resourceClassmembers);
    }
    @Override
    public Set<StringlistAvailablePermissions(Class<?> resourceClass) {
        if (!.containsKey(resourceClass)) {
            initClassPermissions(resourceClass);
        }
        Set<Stringpermissions = new HashSet<String>();
        for (String permission : .get(resourceClass).keySet()) {
            permissions.add(permission);
        }
        return permissions;
    }
    @Override
    public Set<StringconvertResourcePermissions(Class<?> resourceClassObject permissions) {
        return createPermissionSet(resourceClasspermissions.toString()).getPermissions();
    }
New to GrepCode? Check out our FAQ X