/**
   * Intelligently merges the given RegionAttributes with the configuration setting of the
   * RegionFactory. This method is used to merge the RegionAttributes and PartitionAttributes with
   * the RegionFactory that is created when the user specified a RegionShortcut. This method gets
   * called by the createRegionFactory method depending upon the value passed to the
   * Cache.createRegionFactory() method (i.e. whether there was a RegionShortcut specified or not).
   *
   * @param <K> the Class type fo the Region key.
   * @param <V> the Class type of the Region value.
   * @param regionFactory the GemFire RegionFactory used to configure and create the Region that is
   *     the product of this RegionFactoryBean.
   * @param regionAttributes the RegionAttributes containing the Region configuration settings to
   *     merge to the RegionFactory.
   * @return the RegionFactory with the configuration settings of the RegionAttributes merged.
   * @see #isUserSpecifiedEvictionAttributes(com.gemstone.gemfire.cache.RegionAttributes)
   * @see #validateRegionAttributes(com.gemstone.gemfire.cache.RegionAttributes)
   * @see com.gemstone.gemfire.cache.RegionAttributes
   * @see com.gemstone.gemfire.cache.RegionFactory
   */
  @SuppressWarnings("unchecked")
  protected <K, V> RegionFactory<K, V> mergeRegionAttributes(
      final RegionFactory<K, V> regionFactory, final RegionAttributes<K, V> regionAttributes) {

    if (regionAttributes != null) {
      // NOTE this validation may not be strictly required depending on how the RegionAttributes
      // were "created",
      // but...
      validateRegionAttributes(regionAttributes);

      regionFactory.setCloningEnabled(regionAttributes.getCloningEnabled());
      regionFactory.setCompressor(regionAttributes.getCompressor());
      regionFactory.setConcurrencyChecksEnabled(regionAttributes.getConcurrencyChecksEnabled());
      regionFactory.setConcurrencyLevel(regionAttributes.getConcurrencyLevel());
      regionFactory.setCustomEntryIdleTimeout(regionAttributes.getCustomEntryIdleTimeout());
      regionFactory.setCustomEntryTimeToLive(regionAttributes.getCustomEntryTimeToLive());
      regionFactory.setDiskSynchronous(regionAttributes.isDiskSynchronous());
      regionFactory.setEnableAsyncConflation(regionAttributes.getEnableAsyncConflation());
      regionFactory.setEnableSubscriptionConflation(
          regionAttributes.getEnableSubscriptionConflation());
      regionFactory.setEntryIdleTimeout(regionAttributes.getEntryIdleTimeout());
      regionFactory.setEntryTimeToLive(regionAttributes.getEntryTimeToLive());

      // NOTE EvictionAttributes are created by certain RegionShortcuts; need the null check!
      if (isUserSpecifiedEvictionAttributes(regionAttributes)) {
        regionFactory.setEvictionAttributes(regionAttributes.getEvictionAttributes());
      }

      regionFactory.setIgnoreJTA(regionAttributes.getIgnoreJTA());
      regionFactory.setIndexMaintenanceSynchronous(
          regionAttributes.getIndexMaintenanceSynchronous());
      regionFactory.setInitialCapacity(regionAttributes.getInitialCapacity());
      regionFactory.setKeyConstraint(regionAttributes.getKeyConstraint());
      regionFactory.setLoadFactor(regionAttributes.getLoadFactor());
      regionFactory.setLockGrantor(regionAttributes.isLockGrantor());
      regionFactory.setMembershipAttributes(regionAttributes.getMembershipAttributes());
      regionFactory.setMulticastEnabled(regionAttributes.getMulticastEnabled());
      mergePartitionAttributes(regionFactory, regionAttributes);
      regionFactory.setPoolName(regionAttributes.getPoolName());
      regionFactory.setRegionIdleTimeout(regionAttributes.getRegionIdleTimeout());
      regionFactory.setRegionTimeToLive(regionAttributes.getRegionTimeToLive());
      regionFactory.setStatisticsEnabled(regionAttributes.getStatisticsEnabled());
      regionFactory.setSubscriptionAttributes(regionAttributes.getSubscriptionAttributes());
      regionFactory.setValueConstraint(regionAttributes.getValueConstraint());
    }

    return regionFactory;
  }
  @SuppressWarnings("rawtypes")
  @Test
  public void testReplicaWithAttributes() throws Exception {
    assertTrue(context.containsBean("replicated-with-attributes"));
    Region region = context.getBean("replicated-with-attributes", Region.class);
    RegionAttributes attrs = region.getAttributes();

    assertEquals(10, attrs.getInitialCapacity());
    assertEquals(true, attrs.getIgnoreJTA());
    assertEquals(false, attrs.getIndexMaintenanceSynchronous());
    assertEquals(String.class, attrs.getKeyConstraint());
    assertEquals(String.class, attrs.getValueConstraint());
    assertEquals(true, attrs.isDiskSynchronous());
    assertEquals(Scope.GLOBAL, attrs.getScope());
    // assertEquals(true, attrs.isLockGrantor());
    assertEquals(true, attrs.getEnableAsyncConflation());
    assertEquals(true, attrs.getEnableSubscriptionConflation());
    assertEquals(0.50, attrs.getLoadFactor(), 0.001);
    assertEquals(false, attrs.getCloningEnabled());
    assertEquals(10, attrs.getConcurrencyLevel());
    assertEquals(true, attrs.getMulticastEnabled());
  }
  @Override
  protected Region<K, V> lookupFallback(GemFireCache cache, String regionName) throws Exception {
    Assert.isTrue(cache instanceof ClientCache, "Unable to create regions from " + cache);

    ClientCache c = (ClientCache) cache;

    // first look at shortcut
    ClientRegionShortcut s = null;

    if (shortcut == null) {
      if (dataPolicy != null) {
        if (DataPolicy.EMPTY.equals(dataPolicy)) {
          s = ClientRegionShortcut.PROXY;
        } else if (DataPolicy.PERSISTENT_REPLICATE.equals(dataPolicy)) {
          s = ClientRegionShortcut.LOCAL_PERSISTENT;
        }
      }
      s = ClientRegionShortcut.LOCAL;
    } else {
      s = shortcut;
    }

    ClientRegionFactory<K, V> factory = c.createClientRegionFactory(s);

    // map the attributes onto the client
    if (attributes != null) {
      CacheListener<K, V>[] listeners = attributes.getCacheListeners();
      if (!ObjectUtils.isEmpty(listeners)) {
        for (CacheListener<K, V> listener : listeners) {
          factory.addCacheListener(listener);
        }
      }
      factory.setCloningEnabled(attributes.getCloningEnabled());
      factory.setConcurrencyLevel(attributes.getConcurrencyLevel());
      factory.setCustomEntryIdleTimeout(attributes.getCustomEntryIdleTimeout());
      factory.setCustomEntryTimeToLive(attributes.getCustomEntryTimeToLive());
      factory.setDiskStoreName(attributes.getDiskStoreName());
      factory.setDiskSynchronous(attributes.isDiskSynchronous());
      factory.setEntryIdleTimeout(attributes.getEntryIdleTimeout());
      factory.setEntryTimeToLive(attributes.getEntryTimeToLive());
      factory.setEvictionAttributes(attributes.getEvictionAttributes());
      factory.setInitialCapacity(attributes.getInitialCapacity());
      factory.setKeyConstraint(attributes.getKeyConstraint());
      factory.setLoadFactor(attributes.getLoadFactor());
      factory.setPoolName(attributes.getPoolName());
      factory.setRegionIdleTimeout(attributes.getRegionIdleTimeout());
      factory.setRegionTimeToLive(attributes.getRegionTimeToLive());
      factory.setStatisticsEnabled(attributes.getStatisticsEnabled());
      factory.setValueConstraint(attributes.getValueConstraint());
    }

    if (!ObjectUtils.isEmpty(cacheListeners)) {
      for (CacheListener<K, V> listener : cacheListeners) {
        factory.addCacheListener(listener);
      }
    }

    if (StringUtils.hasText(poolName)) {
      // try to eagerly initialize the pool name, if defined as a bean
      if (beanFactory.isTypeMatch(poolName, Pool.class)) {
        if (log.isDebugEnabled()) {
          log.debug(
              "Found bean definition for pool '" + poolName + "'. Eagerly initializing it...");
        }
        beanFactory.getBean(poolName, Pool.class);
      }

      factory.setPoolName(poolName);
    }

    Region<K, V> reg = factory.create(regionName);
    log.info("Created new cache region [" + regionName + "]");
    if (snapshot != null) {
      reg.loadSnapshot(snapshot.getInputStream());
    }

    return reg;
  }