use of org.apache.geode.cache.CacheXmlException in project geode by apache.
the class CacheXmlParser method endEntryTimeToLive.
/**
* When a <code>entry-time-to-live</code> element is finished, an optional Declarable (the
* custom-expiry) is followed by the {@link ExpirationAttributes} are on top of the stack followed
* by either the {@link RegionAttributesCreation} to which the expiration attributes are assigned,
* or the attributes for a {@link PartitionAttributes} to which the attributes are assigned.
*/
private void endEntryTimeToLive() {
Declarable custom = null;
if (stack.peek() instanceof Declarable) {
custom = (Declarable) stack.pop();
}
ExpirationAttributes expire = (ExpirationAttributes) stack.pop();
Object a = stack.peek();
// } else
if (a instanceof RegionAttributesCreation) {
((RegionAttributesCreation) a).setEntryTimeToLive(expire);
if (custom != null) {
((RegionAttributesCreation) a).setCustomEntryTimeToLive((CustomExpiry) custom);
}
} else {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_0_MUST_BE_DEFINED_IN_THE_CONTEXT_OF_REGIONATTRIBUTES_OR_PARTITIONATTRIBUTES.toLocalizedString(ENTRY_TIME_TO_LIVE));
}
}
use of org.apache.geode.cache.CacheXmlException in project geode by apache.
the class CacheXmlParser method endValueConstraint.
/**
* When a <code>value-constraint</code> element is finished, the name of the class should be on
* top of the stack.
*
* @throws CacheXmlException If the value constraint class cannot be loaded
*/
private void endValueConstraint() {
String className = ((StringBuffer) stack.pop()).toString().trim();
Class c;
try {
c = InternalDataSerializer.getCachedClass(className);
} catch (Exception ex) {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_COULD_NOT_LOAD_VALUECONSTRAINT_CLASS_0.toLocalizedString(className), ex);
}
// The region attributes should be on top of the stack
RegionAttributesCreation attrs = peekRegionAttributesContext("value-constraint");
attrs.setValueConstraint(c);
}
use of org.apache.geode.cache.CacheXmlException in project geode by apache.
the class CacheXmlParser method parse.
////////////////////// Static Methods //////////////////////
/**
* Parses XML data and from it creates an instance of <code>CacheXmlParser</code> that can be used
* to {@link #create}the {@link Cache}, etc.
*
* @param is the <code>InputStream</code> of XML to be parsed
*
* @return a <code>CacheXmlParser</code>, typically used to create a cache from the parsed XML
*
* @throws CacheXmlException Something went wrong while parsing the XML
*
* @since GemFire 4.0
*
*/
public static CacheXmlParser parse(InputStream is) {
/**
* The API doc http://java.sun.com/javase/6/docs/api/org/xml/sax/InputSource.html for the SAX
* InputSource says: "... standard processing of both byte and character streams is to close
* them on as part of end-of-parse cleanup, so applications should not attempt to re-use such
* streams after they have been handed to a parser."
*
* In order to block the parser from closing the stream, we wrap the InputStream in a filter,
* i.e., UnclosableInputStream, whose close() function does nothing.
*
*/
class UnclosableInputStream extends BufferedInputStream {
public UnclosableInputStream(InputStream stream) {
super(stream);
}
@Override
public void close() {
}
}
CacheXmlParser handler = new CacheXmlParser();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, true);
factory.setValidating(true);
factory.setNamespaceAware(true);
UnclosableInputStream bis = new UnclosableInputStream(is);
try {
SAXParser parser = factory.newSAXParser();
// Parser always reads one buffer plus a little extra worth before
// determining that the DTD is there. Setting mark twice the parser
// buffer size.
bis.mark((Integer) parser.getProperty(BUFFER_SIZE) * 2);
parser.setProperty(JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI);
parser.parse(bis, new DefaultHandlerDelegate(handler));
} catch (CacheXmlException e) {
if (null != e.getCause() && e.getCause().getMessage().contains(DISALLOW_DOCTYPE_DECL_FEATURE)) {
// Not schema based document, try dtd.
bis.reset();
factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, false);
SAXParser parser = factory.newSAXParser();
parser.parse(bis, new DefaultHandlerDelegate(handler));
} else {
throw e;
}
}
return handler;
} catch (Exception ex) {
if (ex instanceof CacheXmlException) {
while (true) /* ex instanceof CacheXmlException */
{
Throwable cause = ex.getCause();
if (!(cause instanceof CacheXmlException)) {
break;
} else {
ex = (CacheXmlException) cause;
}
}
throw (CacheXmlException) ex;
} else if (ex instanceof SAXException) {
// Silly JDK 1.4.2 XML parser wraps RunTime exceptions in a
// SAXException. Pshaw!
SAXException sax = (SAXException) ex;
Exception cause = sax.getException();
if (cause instanceof CacheXmlException) {
while (true) /* cause instanceof CacheXmlException */
{
Throwable cause2 = cause.getCause();
if (!(cause2 instanceof CacheXmlException)) {
break;
} else {
cause = (CacheXmlException) cause2;
}
}
throw (CacheXmlException) cause;
}
}
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_WHILE_PARSING_XML.toLocalizedString(), ex);
}
}
use of org.apache.geode.cache.CacheXmlException in project geode by apache.
the class CacheXmlParser method endKeyConstraint.
/**
* When a <code>key-constraint</code> element is finished, the name of the class should be on top
* of the stack.
*
* @throws CacheXmlException If the key constraint class cannot be loaded
*/
private void endKeyConstraint() {
String className = ((StringBuffer) stack.pop()).toString().trim();
Class c;
try {
c = InternalDataSerializer.getCachedClass(className);
} catch (Exception ex) {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_COULD_NOT_LOAD_KEYCONSTRAINT_CLASS_0.toLocalizedString(className), ex);
}
// The region attributes should be on top of the stack
RegionAttributesCreation attrs = peekRegionAttributesContext("key-constraint");
attrs.setKeyConstraint(c);
}
use of org.apache.geode.cache.CacheXmlException 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;
}
Aggregations