Search in sources :

Example 11 with Extensible

use of org.apache.geode.internal.cache.extension.Extensible in project geode by apache.

the class CacheXml81DUnitTest method testCacheExtension.

/**
   * Test extensions to <code>cache</code> element.
   * 
   * @since GemFire 8.1
   */
@Test
public void testCacheExtension() throws Exception {
    final CacheCreation cache = new CacheCreation();
    final MockCacheExtension extension = new MockCacheExtension("testCacheExtension");
    cache.getExtensionPoint().addExtension(extension);
    assertEquals(0, extension.beforeCreateCounter.get());
    assertEquals(0, extension.onCreateCounter.get());
    assertEquals(0, extension.getXmlGeneratorCounter.get());
    testXml(cache);
    assertEquals(0, extension.beforeCreateCounter.get());
    assertEquals(0, extension.onCreateCounter.get());
    assertEquals(1, extension.getXmlGeneratorCounter.get());
    @SuppressWarnings("unchecked") final Extensible<Cache> c = (Extensible<Cache>) getCache();
    assertNotNull(c);
    final MockCacheExtension m = (MockCacheExtension) c.getExtensionPoint().getExtensions().iterator().next();
    assertNotNull(m);
    assertEquals(1, m.beforeCreateCounter.get());
    assertEquals(1, m.onCreateCounter.get());
    assertEquals(0, m.getXmlGeneratorCounter.get());
}
Also used : Extensible(org.apache.geode.internal.cache.extension.Extensible) MockCacheExtension(org.apache.geode.internal.cache.extension.mock.MockCacheExtension) CacheCreation(org.apache.geode.internal.cache.xmlcache.CacheCreation) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 12 with Extensible

use of org.apache.geode.internal.cache.extension.Extensible in project geode by apache.

the class CacheXmlGenerator method generate.

/**
   * Generates XML for a given region
   */
private void generate(Region region, String elementName) throws SAXException {
    if (region == null) {
        return;
    }
    AttributesImpl atts = new AttributesImpl();
    atts.addAttribute("", "", NAME, "", region.getName());
    if (region instanceof RegionCreation) {
        RegionCreation rc = (RegionCreation) region;
        String refId = rc.getRefid();
        if (refId != null) {
            atts.addAttribute("", "", REFID, "", refId);
        }
    }
    handler.startElement("", elementName, elementName, atts);
    if (region instanceof RegionCreation) {
        RegionCreation rc = (RegionCreation) region;
        if (rc.hasAttributes()) {
            generate(null, /* unknown id */
            region.getAttributes());
        }
    } else {
        generate(null, /* unknown id */
        region.getAttributes());
    }
    // generate index data here
    Collection indexesForRegion = this.cache.getQueryService().getIndexes(region);
    if (indexesForRegion != null) {
        for (Object index : indexesForRegion) {
            generate((Index) index);
        }
    }
    if (region instanceof PartitionedRegion) {
        if (includeKeysValues) {
            if (!region.isEmpty()) {
                for (Iterator iter = region.entrySet(false).iterator(); iter.hasNext(); ) {
                    Region.Entry entry = (Region.Entry) iter.next();
                    generate(entry);
                }
            }
        }
    } else {
        if (includeKeysValues) {
            for (Iterator iter = region.entrySet(false).iterator(); iter.hasNext(); ) {
                Region.Entry entry = (Region.Entry) iter.next();
                generate(entry);
            }
        }
    }
    TreeSet rSet = new TreeSet(new RegionComparator());
    rSet.addAll(region.subregions(false));
    for (Iterator iter = rSet.iterator(); iter.hasNext(); ) {
        Region subregion = (Region) iter.next();
        generate(subregion, REGION);
    }
    if (region instanceof Extensible) {
        @SuppressWarnings({ "unchecked" }) Extensible<Region<?, ?>> extensible = (Extensible<Region<?, ?>>) region;
        generate(extensible);
    }
    handler.endElement("", elementName, elementName);
}
Also used : Extensible(org.apache.geode.internal.cache.extension.Extensible) DiskWriteAttributesImpl(org.apache.geode.internal.cache.DiskWriteAttributesImpl) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) AttributesImpl(org.xml.sax.helpers.AttributesImpl) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) TreeSet(java.util.TreeSet) Iterator(java.util.Iterator) Collection(java.util.Collection) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region)

Example 13 with Extensible

use of org.apache.geode.internal.cache.extension.Extensible in project geode by apache.

the class AlterMockCacheExtensionFunction method execute.

@Override
public void execute(FunctionContext context) {
    final Cache cache = CacheFactory.getAnyInstance();
    if (!(cache instanceof Extensible)) {
        throw new FunctionException("Not extensible cache.");
    }
    final String value = (String) ((Object[]) context.getArguments())[0];
    @SuppressWarnings("unchecked") final Extensible<Cache> extensible = (Extensible<Cache>) cache;
    for (Extension<Cache> extension : extensible.getExtensionPoint().getExtensions()) {
        if (extension instanceof MockCacheExtension) {
            ((MockCacheExtension) extension).setValue(value);
        }
    }
    final XmlEntity xmlEntity = XmlEntity.builder().withType(ELEMENT_CACHE).withNamespace(PREFIX, NAMESPACE).build();
    final ResultSender<Object> resultSender = context.getResultSender();
    final String memberNameOrId = CliUtil.getMemberNameOrId(cache.getDistributedSystem().getDistributedMember());
    resultSender.lastResult(new CliFunctionResult(memberNameOrId, xmlEntity, CliStrings.format("Mock cache extension altered on \"{0}\"", new Object[] { memberNameOrId })));
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) Extensible(org.apache.geode.internal.cache.extension.Extensible) FunctionException(org.apache.geode.cache.execute.FunctionException) Cache(org.apache.geode.cache.Cache)

Example 14 with Extensible

use of org.apache.geode.internal.cache.extension.Extensible in project geode by apache.

the class AlterMockRegionExtensionFunction method execute.

@Override
public void execute(FunctionContext context) {
    final Cache cache = CacheFactory.getAnyInstance();
    final Region<?, ?> region = cache.getRegion((String) ((Object[]) context.getArguments())[0]);
    if (!(region instanceof Extensible)) {
        throw new FunctionException("Not extensible region.");
    }
    final String value = (String) ((Object[]) context.getArguments())[1];
    @SuppressWarnings("unchecked") final Extensible<Region<?, ?>> extensible = (Extensible<Region<?, ?>>) region;
    for (Extension<Region<?, ?>> extension : extensible.getExtensionPoint().getExtensions()) {
        if (extension instanceof MockRegionExtension) {
            ((MockRegionExtension) extension).setValue(value);
        }
    }
    XmlEntity xmlEntity = new XmlEntity(CacheXml.REGION, "name", region.getName());
    final ResultSender<Object> resultSender = context.getResultSender();
    final String memberNameOrId = CliUtil.getMemberNameOrId(cache.getDistributedSystem().getDistributedMember());
    resultSender.lastResult(new CliFunctionResult(memberNameOrId, xmlEntity, CliStrings.format("Mock region extension \"{0}\" altered on \"{1}\"", new Object[] { region.getFullPath(), memberNameOrId })));
}
Also used : Extensible(org.apache.geode.internal.cache.extension.Extensible) FunctionException(org.apache.geode.cache.execute.FunctionException) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache)

Aggregations

Extensible (org.apache.geode.internal.cache.extension.Extensible)14 Cache (org.apache.geode.cache.Cache)9 Region (org.apache.geode.cache.Region)9 FunctionException (org.apache.geode.cache.execute.FunctionException)6 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)6 XmlEntity (org.apache.geode.management.internal.configuration.domain.XmlEntity)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)5 Test (org.junit.Test)5 File (java.io.File)3 Iterator (java.util.Iterator)3 InternalCache (org.apache.geode.internal.cache.InternalCache)3 LocalRegion (org.apache.geode.internal.cache.LocalRegion)3 MockRegionExtension (org.apache.geode.internal.cache.extension.mock.MockRegionExtension)3 CacheCreation (org.apache.geode.internal.cache.xmlcache.CacheCreation)3 Map (java.util.Map)2 Properties (java.util.Properties)2 TreeSet (java.util.TreeSet)2 AbstractRegion (org.apache.geode.internal.cache.AbstractRegion)2 DiskWriteAttributesImpl (org.apache.geode.internal.cache.DiskWriteAttributesImpl)2 PartitionAttributesImpl (org.apache.geode.internal.cache.PartitionAttributesImpl)2