Search in sources :

Example 6 with Declarable

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);
    }
}
Also used : DiskWriteAttributesImpl(org.apache.geode.internal.cache.DiskWriteAttributesImpl) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Declarable(org.apache.geode.cache.Declarable) Iterator(java.util.Iterator) Map(java.util.Map)

Example 7 with Declarable

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;
}
Also used : CacheXmlException(org.apache.geode.cache.CacheXmlException) Declarable(org.apache.geode.cache.Declarable) Properties(java.util.Properties) TimeoutException(org.apache.geode.cache.TimeoutException) InternalGemFireException(org.apache.geode.InternalGemFireException) EmptyStackException(java.util.EmptyStackException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) RegionExistsException(org.apache.geode.cache.RegionExistsException) CacheXmlException(org.apache.geode.cache.CacheXmlException) SAXException(org.xml.sax.SAXException) GatewayException(org.apache.geode.cache.GatewayException) CacheWriterException(org.apache.geode.cache.CacheWriterException) SAXParseException(org.xml.sax.SAXParseException)

Example 8 with Declarable

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 }));
    }
}
Also used : Declarable(org.apache.geode.cache.Declarable) CacheXmlException(org.apache.geode.cache.CacheXmlException) CacheLoader(org.apache.geode.cache.CacheLoader) File(java.io.File)

Example 9 with Declarable

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);
}
Also used : Declarable(org.apache.geode.cache.Declarable) CacheXmlException(org.apache.geode.cache.CacheXmlException) EvictionAttributesImpl(org.apache.geode.internal.cache.EvictionAttributesImpl) ObjectSizer(org.apache.geode.cache.util.ObjectSizer)

Example 10 with Declarable

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);
}
Also used : EvictionAction(org.apache.geode.cache.EvictionAction) DiskWriteAttributesImpl(org.apache.geode.internal.cache.DiskWriteAttributesImpl) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Declarable(org.apache.geode.cache.Declarable) ObjectSizer(org.apache.geode.cache.util.ObjectSizer) SizeClassOnceObjectSizer(org.apache.geode.internal.size.SizeClassOnceObjectSizer) SizeClassOnceObjectSizer(org.apache.geode.internal.size.SizeClassOnceObjectSizer)

Aggregations

Declarable (org.apache.geode.cache.Declarable)29 CacheXmlException (org.apache.geode.cache.CacheXmlException)22 Properties (java.util.Properties)5 PartitionAttributesImpl (org.apache.geode.internal.cache.PartitionAttributesImpl)4 ObjectSizer (org.apache.geode.cache.util.ObjectSizer)3 GatewaySenderFactory (org.apache.geode.cache.wan.GatewaySenderFactory)3 File (java.io.File)2 IOException (java.io.IOException)2 EmptyStackException (java.util.EmptyStackException)2 InternalGemFireException (org.apache.geode.InternalGemFireException)2 CacheException (org.apache.geode.cache.CacheException)2 CacheWriterException (org.apache.geode.cache.CacheWriterException)2 ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)2 GatewayException (org.apache.geode.cache.GatewayException)2 RegionExistsException (org.apache.geode.cache.RegionExistsException)2 TimeoutException (org.apache.geode.cache.TimeoutException)2 Function (org.apache.geode.cache.execute.Function)2 DiskWriteAttributesImpl (org.apache.geode.internal.cache.DiskWriteAttributesImpl)2 EvictionAttributesImpl (org.apache.geode.internal.cache.EvictionAttributesImpl)2 FixedPartitionAttributesImpl (org.apache.geode.internal.cache.FixedPartitionAttributesImpl)2