1 /*
   2  * Copyright (c) 1997, 2007, 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 
  27 package com.sun.jmx.snmp.IPAcl;
  28 
  29 
  30 
  31 import java.util.Vector;
  32 import java.io.Serializable;
  33 
  34 import java.security.Principal;
  35 import java.security.acl.Owner;
  36 import java.security.acl.LastOwnerException;
  37 import java.security.acl.NotOwnerException;
  38 
  39 
  40 /**
  41  * Owner of Access Control Lists (ACLs).
  42  * The initial owner Principal should be specified as an
  43  * argument to the constructor of the class AclImpl.
  44  *
  45  * @see java.security.acl.Owner
  46  */
  47 
  48 class OwnerImpl implements Owner, Serializable {
  49   private static final long serialVersionUID = -576066072046319874L;
  50 
  51   private Vector<Principal> ownerList = null;
  52 
  53   /**
  54    * Constructs an empty list of owner.
  55    */
  56   public OwnerImpl (){
  57         ownerList = new Vector<Principal>();
  58   }
  59 
  60   /**
  61    * Constructs a list of owner with the specified principal as first element.
  62    *
  63    * @param owner the principal added to the owner list.
  64    */
  65   public OwnerImpl (PrincipalImpl owner){
  66         ownerList = new Vector<Principal>();
  67         ownerList.addElement(owner);
  68   }
  69 
  70   /**
  71    * Adds an owner. Only owners can modify ACL contents. The caller principal
  72    * must be an owner of the ACL in order to invoke this method. That is, only
  73    * an owner can add another owner. The initial owner is configured at
  74    * ACL construction time.
  75    *
  76    * @param caller the principal invoking this method.
  77    *        It must be an owner of the ACL.
  78    * @param owner the owner that should be added to the list of owners.
  79    * @return true if successful, false if owner is already an owner.
  80    * @exception NotOwnerException if the caller principal is not an owner
  81    *    of the ACL.
  82    */
  83   public boolean addOwner(Principal caller, Principal owner)
  84         throws NotOwnerException {
  85         if (!ownerList.contains(caller))
  86           throw new NotOwnerException();
  87 
  88         if (ownerList.contains(owner)) {
  89           return false;
  90         } else {
  91           ownerList.addElement(owner);
  92           return true;
  93         }
  94   }
  95 
  96   /**
  97    * Deletes an owner. If this is the last owner in the ACL, an exception is raised.
  98    *<P>
  99    * The caller principal must be an owner of the ACL in order to invoke this method.
 100    *
 101    * @param caller the principal invoking this method. It must be an owner
 102    *   of the ACL.
 103    * @param owner the owner to be removed from the list of owners.
 104    * @return true if successful, false if owner is already an owner.
 105    * @exception NotOwnerException if the caller principal is not an owner
 106    *   of the ACL.
 107    * @exception LastOwnerException if there is only one owner left, so that
 108    *   deleteOwner would leave the ACL owner-less.
 109    */
 110   public boolean deleteOwner(Principal caller, Principal owner)
 111                 throws NotOwnerException,LastOwnerException {
 112 
 113         if (!ownerList.contains(caller))
 114           throw new NotOwnerException();
 115 
 116         if (!ownerList.contains(owner)){
 117           return false;
 118         } else {
 119           if (ownerList.size() == 1)
 120                 throw new LastOwnerException();
 121 
 122           ownerList.removeElement(owner);
 123           return true;
 124         }
 125   }
 126 
 127   /**
 128    * Returns true if the given principal is an owner of the ACL.
 129    *
 130    * @param owner the principal to be checked to determine whether or
 131    *        not it is an owner.
 132    * @return true if the given principal is an owner of the ACL.
 133    */
 134   public boolean isOwner(Principal owner){
 135         return ownerList.contains(owner);
 136   }
 137 }