private Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker>
      createEntityManagers(BeanContext beanContext) {
    // create the extended entity managers
    Index<EntityManagerFactory, Map> factories = beanContext.getExtendedEntityManagerFactories();
    Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> entityManagers =
        null;
    if (factories != null && factories.size() > 0) {
      entityManagers =
          new Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker>(
              new ArrayList<EntityManagerFactory>(factories.keySet()));
      for (Map.Entry<EntityManagerFactory, Map> entry : factories.entrySet()) {
        EntityManagerFactory entityManagerFactory = entry.getKey();
        Map properties = entry.getValue();

        JtaEntityManagerRegistry.EntityManagerTracker entityManagerTracker =
            entityManagerRegistry.getInheritedEntityManager(entityManagerFactory);
        EntityManager entityManager;
        if (entityManagerTracker == null) {
          if (properties != null) {
            entityManager = entityManagerFactory.createEntityManager(properties);
          } else {
            entityManager = entityManagerFactory.createEntityManager();
          }
          entityManagerTracker = new JtaEntityManagerRegistry.EntityManagerTracker(entityManager);
        } else {
          entityManagerTracker.incCounter();
        }
        entityManagers.put(entityManagerFactory, entityManagerTracker);
      }
    }
    return entityManagers;
  }
  private void registerEntityManagers(Instance instance, ThreadContext callContext)
      throws OpenEJBException {
    if (entityManagerRegistry == null) return;

    BeanContext beanContext = callContext.getBeanContext();

    // get the factories
    Index<EntityManagerFactory, Map> factories = beanContext.getExtendedEntityManagerFactories();
    if (factories == null) return;

    // get the managers for the factories
    Map<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> entityManagers =
        instance.getEntityManagers(factories);
    if (entityManagers == null) return;

    // register them
    try {
      entityManagerRegistry.addEntityManagers(
          (String) beanContext.getDeploymentID(), instance.primaryKey, entityManagers);
    } catch (EntityManagerAlreadyRegisteredException e) {
      throw new EJBException(e);
    }
  }