/*
 * 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 org.jboss.resource.security;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.Principal;
import java.io.UnsupportedEncodingException;

import org.jboss.security.SecurityAssociation;

/** PrivilegedActions used by this package
 * 
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1.2.4 $
 */
class GetPrincipalInfoAction
{
   private static class GetPrincipalAction implements PrivilegedAction
   {
      static PrivilegedAction ACTION = new GetPrincipalAction();
      public Object run()
      {
         Principal principal = SecurityAssociation.getPrincipal();
         return principal;
      }
   }
   private static class GetCredentialAction implements PrivilegedAction
   {
      static PrivilegedAction ACTION = new GetCredentialAction();
      public Object run()
      {
         Object credential = SecurityAssociation.getCredential();
         char[] password = null;
         /* Obtain the password credential by trying char[], byte[],
          and toString()
          */
          if( credential instanceof char[] )
          {
             password = (char[]) credential;
          }
          else if( credential instanceof byte[] )
          {
             try
             {
                String tmp = new String((byte[]) credential, "UTF-8");
                password = tmp.toCharArray();
             }
             catch (UnsupportedEncodingException e)
             {
                throw new SecurityException(e.getMessage());
             }
          }
          else if( credential != null )
          {
             String tmp = credential.toString();
             password = tmp.toCharArray();
          }
         return password;
      }
   }

   static Principal getPrincipal()
   {
      Principal principal = (Principal) AccessController.doPrivileged(GetPrincipalAction.ACTION);
      return principal;
   }
   static char[] getCredential()
   {
      char[] credential = (char[]) AccessController.doPrivileged(GetCredentialAction.ACTION);
      return credential;
   }

}
