/*
 * 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.net.axis.security;

import java.lang.reflect.Constructor;
import java.security.KeyStore;

import org.apache.log4j.Logger;
import org.apache.ws.security.components.crypto.CryptoFactory;

/**
 * <dl>
 * <dt><b>Title: </b><dd>Crypto Factory for use within JBoss.Net</dd>
 * <p>
 * <dt><b>Description: </b><dd>Crypto factory that returns JBossCrypto implementations rather than Crypto 
 * implementations</dd>
 * <p>
 * </dl>
 * @author <a href="mailto:jasone@greenrivercomputing.com">Jason Essington</a>
 * @version $Revision: 1.2.6.2 $
 */
public class JBossCryptoFactory extends CryptoFactory
{
   private static Logger log = Logger.getLogger(JBossCryptoFactory.class);

   /**
    * Gets an instance of the given Crypto class 
    * @param cryptoClassName name of the crypto class to load. This class should have a constructor that takes a 
    * keystore as a parameter
    * @param keystore this is the keystore that will be used by the crypto class.
    * @return
    */
   public static JBossCrypto getInstance(String cryptoClassName, KeyStore keystore)
   {
      if (log.isDebugEnabled())
         log.debug("Attempting to get a Crypto instance of type " + cryptoClassName);
      return loadClass(cryptoClassName, keystore);
   }

   /**
    * This is prety much the way a new crypto instance is created via the super class, only this one doesn't depend
    * upon properties, it just has the keystore fed in directly.
    * @param cryptoClassName
    * @param keystore
    * @return
    */
   private static JBossCrypto loadClass(String cryptoClassName, KeyStore keystore)
   {
      Class cryptogenClass = null;
      JBossCrypto crypto = null;
      try
      {
         // instruct the class loader to load the crypto implementation
         cryptogenClass = java.lang.Class.forName(cryptoClassName);
      }
      catch (ClassNotFoundException e)
      {
         throw new RuntimeException(cryptoClassName + " Not Found");
      }

      log.info("Using Crypto Engine [" + cryptoClassName + "]");

      try
      {
         Class[] classes = new Class[]{KeyStore.class};
         Constructor c = cryptogenClass.getConstructor(classes);
         crypto = (JBossCrypto) c.newInstance(new Object[]{keystore});
         return crypto;
      }
      catch (java.lang.Exception e)
      {
         log.debug(e);
         log.debug(cryptoClassName + " cannot create instance with KeyStore constructor");
      }

      try
      {
         // try to instantiate the Crypto subclass
         crypto = (JBossCrypto) cryptogenClass.newInstance();
         return crypto;
      }
      catch (java.lang.Exception e)
      {
         throw new RuntimeException(cryptoClassName + " cannot create instance");
      }

   }
}
