All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evasion.common.flex.factory.EJBFactory Maven / Gradle / Ivy

There is a newer version: 2.0.0.2
Show newest version
package com.evasion.common.flex.factory;

import java.lang.reflect.Method;
import java.text.MessageFormat;

import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;

public class EJBFactory implements FlexFactory
{
   private static final String SOURCE = "source";
   private static final String ERROR_CODE = "EJB.Invocation";

   final IResourceLocator resourceLocator = new LocalCachingJNDIResourceLocator();

   /**
    * Initializes the component with configuration information.
    */
   public void initialize(
         final String id, final ConfigMap configMap )
   {
   }

   /**
    * This method is called when the definition of an instance that this factory
    * looks up is initialized.
    */
   public FactoryInstance createFactoryInstance(
         final String id, final ConfigMap properties )
   {
      final FactoryInstance instance = new FactoryInstance( this, id,
            properties );
      instance.setSource( properties.getPropertyAsString(
            SOURCE, instance.getId() ) );

      return instance;
   }

   /**
    * Returns the instance specified by the source and properties arguments.
    */
   public Object lookup(
         final FactoryInstance instanceInfo )
   {
      Object ejb;

      final String jndiName = instanceInfo.getSource();

      try
      {
         final Object ejbHome = resourceLocator.locate( jndiName );

         final Method method = ejbHome.getClass().getMethod(
               "create", new Class[]
               {} );

         ejb = method.invoke(
               ejbHome, new Object[]
               {} );
      }
      catch ( ResourceException e )
      {
         throw createServiceException(
               MessageFormat.format(
                     "EJB not found {0}", new Object[]
                     { jndiName } ), e );
      }
      catch ( Exception e )
      {
         throw createServiceException(
               MessageFormat.format(
                     "error creating EJB {0}", new Object[]
                     { jndiName } ), e );
      }

      return ejb;
   }

   protected ServiceException createServiceException(
         final String msg, final Throwable cause )
   {
      final ServiceException e = new ServiceException();
      e.setMessage( msg );
      e.setRootCause( cause );
      e.setDetails( msg );
      e.setCode( ERROR_CODE );

      return e;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy