Package com.evasion.common.flex.factory

Source Code of com.evasion.common.flex.factory.EJBFactory

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;
   }
}
TOP

Related Classes of com.evasion.common.flex.factory.EJBFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.