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());
}
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);
}
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 })));
}
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 })));
}
Aggregations