/**
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.apache.axis.attachments;

// $Id: AttachmentSupport.java,v 1.1.1.1 2004/08/20 18:42:18 tdiesler Exp $

import org.apache.axis.AxisEngine;
import org.apache.axis.MessageContext;
import org.apache.axis.components.logger.LogFactory;
import org.apache.axis.utils.ClassUtils;
import org.apache.axis.utils.Messages;
import org.apache.commons.logging.Log;

/**
 * Provide access to the attechment support implementation.
 * <p/>
 * The default implementation is {@link AttachmentsImpl}
 * You can overwrite the implementation through the engine option {@link AxisEngine.PROP_ATTACHMENT_IMPLEMENTATION}
 *
 * @author Thomas Diesler (thomas.diesler@jboss.org)
 * @since 31-May-2004
 */
public final class AttachmentSupport
{

   /**
    * Provides logging
    */
   private static Log log = LogFactory.getLog(AttachmentSupport.class.getName());

   /**
    * Default Attachments Implementation class.
    */
   public static final String DEFAULT_ATTACHMNET_IMPL = AxisEngine.DEFAULT_ATTACHMENT_IMPL;

   /**
    * Current Attachment implementation.
    */
   private static String attachImplClassName = DEFAULT_ATTACHMNET_IMPL;

   private static boolean checkForAttachmentSupport = true;
   private static boolean attachmentSupportEnabled = false;
   private static Class attachImplClass;

   /**
    * Check if we have attachments enabled.
    * <p/>
    * The check is only done once.
    */
   public static synchronized boolean isAttachmentSupportEnabled()
   {
      return isAttachmentSupportEnabled(null);
   }

   /**
    * Check if we have attachments enabled.
    * <p/>
    * The check is only done once.
    */
   public static synchronized boolean isAttachmentSupportEnabled(MessageContext msgCtx)
   {

      if (checkForAttachmentSupport)
      {
         checkForAttachmentSupport = false;

         try
         {
            if (msgCtx != null)
            {
               AxisEngine engine = msgCtx.getAxisEngine();
               if (engine != null)
               {
                  attachImplClassName = (String)engine.getOption(AxisEngine.PROP_ATTACHMENT_IMPLEMENTATION);
               }
            }

            if (attachImplClassName == null)
            {
               attachImplClassName = AxisEngine.DEFAULT_ATTACHMENT_IMPL;
            }

            /**
             * Attempt to resolve class name, verify that these are present...
             */
            ClassUtils.forName("javax.activation.DataHandler");
            ClassUtils.forName("javax.mail.internet.MimeMultipart");

            attachImplClass = ClassUtils.forName(attachImplClassName);
            attachmentSupportEnabled = true;
         }
         catch (ClassNotFoundException ex)
         {
            // no support for it, leave mAttachments null.
         }
         catch (NoClassDefFoundError ex)
         {
            // no support for it, leave mAttachments null.
         }
         log.debug(Messages.getMessage("attachEnabled") + "  " + attachmentSupportEnabled);
      }

      return attachmentSupportEnabled;
   }

   /**
    * Get the attachment implementation class
    */
   public static Class getImplementationClass()
   {
      return attachImplClass;
   }
}
