/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ejb3;

import java.net.URL;
import org.jboss.ejb3.dd.EjbJarDD;
import javassist.bytecode.ClassFile;

public abstract class Ejb3HandlerFactory
{
   public abstract Ejb3Handler createHandler(ClassFile cf) throws Exception;

   private static class DDFactory extends Ejb3HandlerFactory
   {
      private EjbJarDD dd;
      private Ejb3Deployment di;

      public DDFactory(Ejb3Deployment di) throws Exception
      {
         this.di = di;
         this.dd = Ejb3DescriptorHandler.parseDescriptors(di.getDeploymentUnit());
      }


      public Ejb3Handler createHandler(ClassFile cf) throws Exception
      {
         return new Ejb3DescriptorHandler(di.getDeploymentUnit(), cf, dd, di.getDefaultSLSBDomain(), di.getDefaultSFSBDomain(), di.getDefaultMDBDomain());
      }
   }

   private static class AnnotationFactory extends Ejb3HandlerFactory
   {
      private Ejb3Deployment di;

      public AnnotationFactory(Ejb3Deployment di) throws Exception
      {
         this.di = di;
      }


      public Ejb3Handler createHandler(ClassFile cf) throws Exception
      {
         return new Ejb3AnnotationHandler(di.getDeploymentUnit(), cf, di.getDefaultSLSBDomain(), di.getDefaultSFSBDomain(), di.getDefaultMDBDomain());
      }
   }

   public static Ejb3HandlerFactory getInstance(Ejb3Deployment di) throws Exception
   {
      URL ddResource = di.getDeploymentUnit().getEjbJarXml();

      if (ddResource == null)
         return new AnnotationFactory(di);
      else
         return new DDFactory(di);
   }
}
