/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.jca.common.metadata.common.v10;
import org.jboss.jca.common.CommonBundle;
import org.jboss.jca.common.api.metadata.common.CommonAdminObject;
import org.jboss.jca.common.api.metadata.common.CommonPool;
import org.jboss.jca.common.api.metadata.common.CommonSecurity;
import org.jboss.jca.common.api.metadata.common.CommonTimeOut;
import org.jboss.jca.common.api.metadata.common.CommonValidation;
import org.jboss.jca.common.api.metadata.common.Recovery;
import org.jboss.jca.common.api.metadata.common.v10.CommonConnDef;
import org.jboss.jca.common.api.metadata.resourceadapter.v10.ResourceAdapter;
import org.jboss.jca.common.api.validator.ValidateException;
import org.jboss.jca.common.metadata.AbstractParser;
import org.jboss.jca.common.metadata.ParserException;
import org.jboss.jca.common.metadata.common.CommonAdminObjectImpl;
import org.jboss.jca.common.metadata.common.CommonTimeOutImpl;
import org.jboss.jca.common.metadata.common.CommonValidationImpl;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import org.jboss.logging.Messages;
/**
*
* A CommonIronJacamarParser.
*
* @author <a href="stefano.maestri@jboss.com">Stefano Maestri</a>
*
*/
public abstract class CommonIronJacamarParser extends AbstractParser
{
/** The bundle */
private static CommonBundle bundle = Messages.getBundle(CommonBundle.class);
/**
*
* parse a single connection-definition tag
*
* @param reader the reader
* @param isXA Is XA capable
* @return the parse {@link CommonConnDef} object
* @throws XMLStreamException XMLStreamException
* @throws ParserException ParserException
* @throws ValidateException ValidateException
*/
protected CommonConnDef parseConnectionDefinitions(XMLStreamReader reader, Boolean isXA) throws XMLStreamException,
ParserException, ValidateException
{
Map<String, String> configProperties = new HashMap<String, String>();
CommonSecurity security = null;
CommonTimeOut timeOut = null;
CommonValidation validation = null;
CommonPool pool = null;
Recovery recovery = null;
//attributes reading
Boolean useJavaContext = Boolean.TRUE;
String className = null;
Boolean enabled = Boolean.TRUE;
String jndiName = null;
String poolName = null;
Boolean useCcm = Boolean.TRUE;
int attributeSize = reader.getAttributeCount();
if (isXA == null)
isXA = Boolean.FALSE;
for (int i = 0; i < attributeSize; i++)
{
CommonConnDef.Attribute attribute = CommonConnDef.Attribute.forName(reader.getAttributeLocalName(i));
switch (attribute)
{
case ENABLED : {
enabled = attributeAsBoolean(reader, attribute.getLocalName(), true);
break;
}
case JNDI_NAME : {
jndiName = attributeAsString(reader, attribute.getLocalName());
break;
}
case CLASS_NAME : {
className = attributeAsString(reader, attribute.getLocalName());
break;
}
case POOL_NAME : {
poolName = attributeAsString(reader, attribute.getLocalName());
break;
}
case USE_JAVA_CONTEXT : {
useJavaContext = attributeAsBoolean(reader, attribute.getLocalName(), true);
break;
}
case USE_CCM : {
useCcm = attributeAsBoolean(reader, attribute.getLocalName(), true);
break;
}
default :
throw new ParserException(bundle.unexpectedAttribute(attribute.getLocalName(), reader.getLocalName()));
}
}
if (jndiName == null || jndiName.trim().equals(""))
throw new ParserException(bundle.missingJndiName(reader.getLocalName()));
while (reader.hasNext())
{
switch (reader.nextTag())
{
case END_ELEMENT : {
if (ResourceAdapter.Tag.forName(reader.getLocalName()) == ResourceAdapter.Tag.CONNECTION_DEFINITION)
{
return new CommonConnDefImpl(configProperties, className, jndiName, poolName, enabled,
useJavaContext, useCcm, pool, timeOut, validation,
security, recovery, isXA);
}
else
{
if (CommonConnDef.Tag.forName(reader.getLocalName()) == CommonConnDef.Tag.UNKNOWN)
{
throw new ParserException(bundle.unexpectedEndTag(reader.getLocalName()));
}
}
break;
}
case START_ELEMENT : {
switch (CommonConnDef.Tag.forName(reader.getLocalName()))
{
case CONFIG_PROPERTY : {
parseConfigProperty(configProperties, reader);
break;
}
case SECURITY : {
security = parseSecuritySettings(reader);
break;
}
case TIMEOUT : {
timeOut = parseTimeOut(reader, isXA);
break;
}
case VALIDATION : {
validation = parseValidation(reader);
break;
}
case XA_POOL : {
if (pool != null)
throw new ParserException(bundle.multiplePools());
pool = parseXaPool(reader);
isXA = Boolean.TRUE;
break;
}
case POOL : {
if (pool != null)
throw new ParserException(bundle.multiplePools());
pool = parsePool(reader);
break;
}
case RECOVERY : {
recovery = parseRecovery(reader);
break;
}
default :
throw new ParserException(bundle.unexpectedElement(reader.getLocalName()));
}
break;
}
}
}
throw new ParserException(bundle.unexpectedEndOfDocument());
}
/**
* Parse validation
* @param reader The reader
* @return The result
* @exception XMLStreamException XMLStreamException
* @exception ParserException ParserException
* @exception ValidateException ValidateException
*/
protected CommonValidation parseValidation(XMLStreamReader reader) throws XMLStreamException,
ParserException,
ValidateException
{
Boolean useFastFail = Boolean.FALSE;
Boolean backgroundValidation = Boolean.FALSE;
Long backgroundValidationMillis = null;
while (reader.hasNext())
{
switch (reader.nextTag())
{
case END_ELEMENT : {
if (CommonConnDef.Tag.forName(reader.getLocalName()) == CommonConnDef.Tag.VALIDATION)
{
return new CommonValidationImpl(backgroundValidation, backgroundValidationMillis, useFastFail);
}
else
{
if (CommonValidation.Tag.forName(reader.getLocalName()) == CommonValidation.Tag.UNKNOWN)
{
throw new ParserException(bundle.unexpectedEndTag(reader.getLocalName()));
}
}
break;
}
case START_ELEMENT : {
switch (CommonValidation.Tag.forName(reader.getLocalName()))
{
case BACKGROUND_VALIDATION_MILLIS : {
backgroundValidationMillis = elementAsLong(reader);
break;
}
case BACKGROUND_VALIDATION : {
backgroundValidation = elementAsBoolean(reader);
break;
}
case USE_FAST_FAIL : {
useFastFail = elementAsBoolean(reader);
break;
}
default :
throw new ParserException(bundle.unexpectedElement(reader.getLocalName()));
}
break;
}
}
}
throw new ParserException(bundle.unexpectedEndOfDocument());
}
/**
* Parse timeout
* @param reader The reader
* @param isXa XA flag
* @return The result
* @exception XMLStreamException XMLStreamException
* @exception ParserException ParserException
* @exception ValidateException ValidateException
*/
protected CommonTimeOut parseTimeOut(XMLStreamReader reader, Boolean isXa) throws XMLStreamException,
ParserException, ValidateException
{
Long blockingTimeoutMillis = null;
Long allocationRetryWaitMillis = null;
Long idleTimeoutMinutes = null;
Integer allocationRetry = null;
Integer xaResourceTimeout = null;
while (reader.hasNext())
{
switch (reader.nextTag())
{
case END_ELEMENT : {
if (CommonConnDef.Tag.forName(reader.getLocalName()) == CommonConnDef.Tag.TIMEOUT)
{
return new CommonTimeOutImpl(blockingTimeoutMillis, idleTimeoutMinutes, allocationRetry,
allocationRetryWaitMillis, xaResourceTimeout);
}
else
{
if (CommonTimeOut.Tag.forName(reader.getLocalName()) == CommonTimeOut.Tag.UNKNOWN)
{
throw new ParserException(bundle.unexpectedEndTag(reader.getLocalName()));
}
}
break;
}
case START_ELEMENT : {
switch (CommonTimeOut.Tag.forName(reader.getLocalName()))
{
case ALLOCATION_RETRY_WAIT_MILLIS : {
allocationRetryWaitMillis = elementAsLong(reader);
break;
}
case ALLOCATION_RETRY : {
allocationRetry = elementAsInteger(reader);
break;
}
case BLOCKING_TIMEOUT_MILLIS : {
blockingTimeoutMillis = elementAsLong(reader);
break;
}
case IDLE_TIMEOUT_MINUTES : {
idleTimeoutMinutes = elementAsLong(reader);
break;
}
case XA_RESOURCE_TIMEOUT : {
if (isXa != null && Boolean.FALSE.equals(isXa))
throw new ParserException(bundle.unsupportedElement(reader.getLocalName()));
xaResourceTimeout = elementAsInteger(reader);
break;
}
default :
throw new ParserException(bundle.unexpectedElement(reader.getLocalName()));
}
break;
}
}
}
throw new ParserException(bundle.unexpectedEndOfDocument());
}
/**
*
* parse a single admin-oject tag
*
* @param reader the reader
* @return the parsed {@link CommonAdminObject}
* @throws XMLStreamException XMLStreamException
* @throws ParserException ParserException
*/
protected CommonAdminObject parseAdminObjects(XMLStreamReader reader) throws XMLStreamException,
ParserException
{
Map<String, String> configProperties = new HashMap<String, String>();
//attributes reading
Boolean useJavaContext = Boolean.TRUE;
String className = null;
Boolean enabled = Boolean.TRUE;
String jndiName = null;
String poolName = null;
int attributeSize = reader.getAttributeCount();
for (int i = 0; i < attributeSize; i++)
{
CommonAdminObject.Attribute attribute = CommonAdminObject.Attribute.forName(reader
.getAttributeLocalName(i));
switch (attribute)
{
case ENABLED : {
enabled = attributeAsBoolean(reader, attribute.getLocalName(), true);
break;
}
case JNDI_NAME : {
jndiName = attributeAsString(reader, attribute.getLocalName());
break;
}
case CLASS_NAME : {
className = attributeAsString(reader, attribute.getLocalName());
break;
}
case USE_JAVA_CONTEXT : {
useJavaContext = attributeAsBoolean(reader, attribute.getLocalName(), true);
break;
}
case POOL_NAME : {
poolName = attributeAsString(reader, attribute.getLocalName());
break;
}
default :
throw new ParserException(bundle.unexpectedAttribute(attribute.getLocalName(), reader.getLocalName()));
}
}
if (jndiName == null || jndiName.trim().equals(""))
throw new ParserException(bundle.missingJndiName(reader.getLocalName()));
while (reader.hasNext())
{
switch (reader.nextTag())
{
case END_ELEMENT : {
if (ResourceAdapter.Tag.forName(reader.getLocalName()) == ResourceAdapter.Tag.ADMIN_OBJECT)
{
return new CommonAdminObjectImpl(configProperties, className, jndiName, poolName, enabled,
useJavaContext);
}
else
{
if (CommonAdminObject.Tag.forName(reader.getLocalName()) == CommonAdminObject.Tag.UNKNOWN)
{
throw new ParserException(bundle.unexpectedEndTag(reader.getLocalName()));
}
}
break;
}
case START_ELEMENT : {
switch (CommonAdminObject.Tag.forName(reader.getLocalName()))
{
case CONFIG_PROPERTY : {
parseConfigProperty(configProperties, reader);
break;
}
default :
throw new ParserException(bundle.unexpectedElement(reader.getLocalName()));
}
break;
}
}
}
throw new ParserException(bundle.unexpectedEndOfDocument());
}
}