/*
 * Copyright 2002 Tryllian BV and Otto Moerbeek
 * http://www.tryllian.com
 * otto@tryllian.com
 */

package net.drijf.javaone;

import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;

/**
 * This class implements a simple role assignment. Normally, you would
 * like to define roles based on property files or read them from a
 * database.
 *
 * @author Otto Moerbeek
 */
public class RoleImpl implements Role
{
    /** The role that disallows anything. */
    public static final Role NOTHING 
        = new RoleImpl("Nothing", new Permission[] { 
    });
    
    /** The role that allows anything */
    public static final Role ALL = new RoleImpl("All", new Permission[] { 
        new java.security.AllPermission()
    });
    
    /** A role that allows classloading and reading of files, */
    public static final Role READER = new RoleImpl("Reader", new Permission[] {
        new net.drijf.javaone.ClassLoadPermission("*"),
        new java.io.FilePermission("-", "read"),
    });

    /** A role that allows clas loading of a specific class */
    public static final Role ENTER = new RoleImpl("Enter", new Permission[] {
        new net.drijf.javaone.ClassLoadPermission("test.Test"),
    });
    
    /** The name of this role */
    private String name;

    /** The permmissions associate with this role */
    private Permissions permissions = new Permissions();

    /**
     * Construct a new role.
     * @param name the name of the role.
     * @param perms the permissions to associate with this role.
     */
    public RoleImpl(String name, Permission[] perms) {

        this.name = name;

        for (int i = 0; i < perms.length; i++) {
            permissions.add(perms[i]);
        }

        permissions.setReadOnly();
    }

    /** Return the permissions assocated with this role. */
    public PermissionCollection getPermissions() {
        return permissions;
    }

    /** Return the name of this role. */
    public String getName() {
        return name;
    }
}
