use of org.apache.geode.cache.Declarable in project geode by apache.
the class CacheXmlGenerator method generate.
private void generate(final Properties props, String elementName) throws SAXException {
if (props == null || props.isEmpty()) {
return;
}
if (elementName != null) {
handler.startElement("", elementName, elementName, EMPTY);
}
for (Iterator iter = props.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry) iter.next();
String name = (String) entry.getKey();
Object value = entry.getValue();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "", NAME, "", name);
handler.startElement("", PARAMETER, PARAMETER, atts);
if (value instanceof String) {
generate((String) value);
} else if (value instanceof Declarable) {
generate((Declarable) value);
} else {
// Ignore it
}
handler.endElement("", PARAMETER, PARAMETER);
}
if (elementName != null) {
handler.endElement("", elementName, elementName);
}
}
use of org.apache.geode.cache.Declarable in project geode by apache.
the class CacheXmlParser method createDeclarable.
/**
* Creates and initializes an instance of {@link Declarable} from the contents of the stack.
*
* @throws CacheXmlException Something goes wrong while instantiating or initializing the
* declarable
*/
private Declarable createDeclarable() {
Properties props = new Properties();
Object top = stack.pop();
while (top instanceof Parameter) {
Parameter param = (Parameter) top;
props.put(param.getName(), param.getValue());
top = stack.pop();
}
logger.trace(LogMarker.CACHE_XML_PARSER, LocalizedMessage.create(LocalizedStrings.CacheXmlParser_XML_PARSER_CREATEDECLARABLE_PROPERTIES__0, props));
Assert.assertTrue(top instanceof String);
String className = (String) top;
logger.trace(LogMarker.CACHE_XML_PARSER, LocalizedMessage.create(LocalizedStrings.CacheXmlParser_XML_PARSER_CREATEDECLARABLE_CLASS_NAME_0, className));
Object o;
try {
Class c = InternalDataSerializer.getCachedClass(className);
o = c.newInstance();
} catch (Exception ex) {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_WHILE_INSTANTIATING_A_0.toLocalizedString(className), ex);
}
if (!(o instanceof Declarable)) {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_CLASS_0_IS_NOT_AN_INSTANCE_OF_DECLARABLE.toLocalizedString(className));
}
Declarable d = (Declarable) o;
d.init(props);
this.cache.addDeclarableProperties(d, props);
return d;
}
use of org.apache.geode.cache.Declarable in project geode by apache.
the class CacheXmlParser method endCacheLoader.
/**
* When a <code>cache-loader</code> element is finished, the {@link Parameter}s and class names
* are popped off the stack. The cache loader is instantiated and initialized with the parameters,
* if appropriate. When the loader is being created in a dynamic-region-factory, there may be a
* disk-dir element on the stack, represented by a File object. Otherwise, dynamic-region-factory
* uses a RegionAttributesCreation, just like a region, and is treated the same.<p) The loader may
* also be created in the context of partition-attributes.
*/
private void endCacheLoader() {
Declarable d = createDeclarable();
if (!(d instanceof CacheLoader)) {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_0_IS_NOT_AN_INSTANCE_OF_A_CACHELOADER.toLocalizedString(d.getClass().getName()));
}
// Two peeks required to handle dynamic region context
Object a = stack.peek();
// check for disk-dir
if ((a instanceof File)) {
Object sav = stack.pop();
a = stack.peek();
if (!(a instanceof RegionAttributesCreation)) {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_CACHELOADER_MUST_BE_DEFINED_IN_THE_CONTEXT_OF_REGIONATTRIBUTES.toLocalizedString());
}
stack.push(sav);
RegionAttributesCreation attrs = (RegionAttributesCreation) a;
attrs.setCacheLoader((CacheLoader) d);
} else // check for normal region-attributes
if (a instanceof RegionAttributesCreation) {
RegionAttributesCreation attrs = (RegionAttributesCreation) a;
attrs.setCacheLoader((CacheLoader) d);
} else {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_0_MUST_BE_DEFINED_IN_THE_CONTEXT_OF_REGIONATTRIBUTES_OR_1.toLocalizedString(new Object[] { CACHE_LOADER, DYNAMIC_REGION_FACTORY }));
}
}
use of org.apache.geode.cache.Declarable in project geode by apache.
the class CacheXmlParser method endLRUHeapPercentage.
/**
* Complete the configuration of a <code>lru-heap-percentage</code> eviction controller. Check for
* the declaration of an {@link ObjectSizer}. Assign the attributes to the enclosed
* <code>region-attributes</code>
*/
private void endLRUHeapPercentage() {
Object declCheck = stack.peek();
Declarable d = null;
if (declCheck instanceof String || declCheck instanceof Parameter) {
d = createDeclarable();
if (!(d instanceof ObjectSizer)) {
String s = "A " + d.getClass().getName() + " is not an instance of a ObjectSizer";
throw new CacheXmlException(s);
}
}
EvictionAttributesImpl eai = (EvictionAttributesImpl) stack.pop();
if (d != null) {
eai.setObjectSizer((ObjectSizer) d);
}
RegionAttributesCreation regAttrs = peekRegionAttributesContext(LRU_HEAP_PERCENTAGE);
regAttrs.setEvictionAttributes(eai);
}
use of org.apache.geode.cache.Declarable in project geode by apache.
the class CacheXmlGenerator method generate.
private void generate(EvictionAttributes ea) throws SAXException {
EvictionAction eAction = ea.getAction();
if (eAction.isNone()) {
return;
}
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "", ACTION, "", eAction.toString());
handler.startElement("", EVICTION_ATTRIBUTES, EVICTION_ATTRIBUTES, EMPTY);
if (ea.getAlgorithm() == EvictionAlgorithm.LRU_ENTRY) {
atts.addAttribute("", "", MAXIMUM, "", String.valueOf(ea.getMaximum()));
handler.startElement("", LRU_ENTRY_COUNT, LRU_ENTRY_COUNT, atts);
handler.endElement("", LRU_ENTRY_COUNT, LRU_ENTRY_COUNT);
} else if (ea.getAlgorithm() == EvictionAlgorithm.LRU_MEMORY) {
atts.addAttribute("", "", MAXIMUM, "", String.valueOf(ea.getMaximum()));
handler.startElement("", LRU_MEMORY_SIZE, LRU_MEMORY_SIZE, atts);
ObjectSizer os = ea.getObjectSizer();
if (os != null && os != ObjectSizer.DEFAULT) {
generate((Declarable) os, false);
}
handler.endElement("", LRU_MEMORY_SIZE, LRU_MEMORY_SIZE);
} else if (ea.getAlgorithm() == EvictionAlgorithm.LRU_HEAP) {
handler.startElement("", LRU_HEAP_PERCENTAGE, LRU_HEAP_PERCENTAGE, atts);
if (this.version.compareTo(CacheXmlVersion.GEMFIRE_6_0) >= 0) {
ObjectSizer os = ea.getObjectSizer();
if (!(os instanceof SizeClassOnceObjectSizer)) {
if (os != null) {
generate((Declarable) os, false);
}
}
}
handler.endElement("", LRU_HEAP_PERCENTAGE, LRU_HEAP_PERCENTAGE);
} else {
// all other algos are ignored
}
handler.endElement("", EVICTION_ATTRIBUTES, EVICTION_ATTRIBUTES);
}
Aggregations