/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package javax.security.auth;

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

/** An alternate implementation of the JAAS 1.0 Configuration class that deals
 * with ClassLoader shortcomings that were fixed in the JAAS included with
 * JDK1.4 and latter. This version allows LoginModules to be loaded from the
 * Thread context ClassLoader and uses an XML based configuration by default.
 *
 * This permissions is used to restrict access to a Subject's private
 * credentials and the name component of the permission is of the form:
 *  "CredentialClass * *"
 * where CredentialClass is the fully qualified class name of the type of
 * credential the permission applies to. This may be "*" to indicate that all
 * credential types are included.
 *
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1.2.3 $
 */
public class PrivateCredentialPermission extends Permission
{
   /** */
   private String credentialClassName;
   /** The type of principals the permission applies. Currently always "*" */
   private String principalClass;
   /** The name of principals the permission applies. Currently always "*" */
   private String principalName;

   public PrivateCredentialPermission(String name, String actions)
   {
      super(name);
      if( actions == null || actions.equalsIgnoreCase("read") == false )
         throw new IllegalArgumentException("The only supported action is 'read'");

      // Parse CredentialClass {PrincipalClass "PrincipalName"}*
      int space = name.indexOf(' ');
      if( space > 0 )
         name = name.substring(0, space);
      credentialClassName = name;
      principalClass = "*";
      principalName = "*";
   }

   public boolean equals(Object obj)
   {
      boolean equals = false;
      if( obj instanceof PrivateCredentialPermission )
      {
         PrivateCredentialPermission pcp = (PrivateCredentialPermission) obj;
         equals = credentialClassName.equals(pcp.credentialClassName);
      }
      return equals;
   }
   public int hashCode()
   {
      return credentialClassName.hashCode();
   }
   public String getActions()
   {
      return "read";
   }
   public String getCredentialClass()
   {
      return credentialClassName;
   }
   public String[][] getPrincipals()
   {
      String[][] principals = {{"*", "*"}};
      return principals;
   }
   public boolean implies(Permission p)
   {
      if( (p instanceof PrivateCredentialPermission) == false )
         return false;
      PrivateCredentialPermission pcp = (PrivateCredentialPermission) p;
      boolean implies = credentialClassName.equals("*");
      if( implies == false )
      {
         implies = credentialClassName.equals(pcp.credentialClassName);
      }
      return implies;
   }
   public PermissionCollection newPermissionCollection()
   {
      return null;
   }
}
