package org.jboss.metadata;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import javax.jms.Message;
import javax.jms.Session;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.jboss.invocation.InvocationType;
import org.jboss.deployment.DeploymentException;
public class MessageDrivenMetaData
extends BeanMetaData
{
public static final int AUTO_ACKNOWLEDGE_MODE = Session.AUTO_ACKNOWLEDGE;
public static final int DUPS_OK_ACKNOWLEDGE_MODE = Session.DUPS_OK_ACKNOWLEDGE;
public static final int CLIENT_ACKNOWLEDGE_MODE = Session.CLIENT_ACKNOWLEDGE;
public static final byte DURABLE_SUBSCRIPTION = 0;
public static final byte NON_DURABLE_SUBSCRIPTION = 1;
public static final byte TX_UNSET = 9;
public static final String DEFAULT_MESSAGE_DRIVEN_BEAN_INVOKER_PROXY_BINDING = "message-driven-bean";
public static final String DEFAULT_MESSAGING_TYPE = "javax.jms.MessageListener";
private int acknowledgeMode = AUTO_ACKNOWLEDGE_MODE;
private byte subscriptionDurability = NON_DURABLE_SUBSCRIPTION;
private byte methodTransactionType = TX_UNSET;
private String messagingType;
private String destinationType;
private String destinationLink;
private String messageSelector;
private String destinationJndiName;
private String user;
private String passwd;
private String clientId;
private String subscriptionId;
private HashMap activationConfigProperties = new HashMap();
private String resourceAdapterName;
public MessageDrivenMetaData(ApplicationMetaData app)
{
super(app, BeanMetaData.MDB_TYPE);
}
public int getAcknowledgeMode()
{
if (getMethodTransactionType() == TX_REQUIRED)
{
return CLIENT_ACKNOWLEDGE_MODE;
}
else
{
return acknowledgeMode;
}
}
public String getMessagingType()
{
return messagingType;
}
public boolean isJMSMessagingType()
{
return DEFAULT_MESSAGING_TYPE.equals(messagingType);
}
public String getDestinationType()
{
return destinationType;
}
public String getDestinationLink()
{
return destinationLink;
}
public String getMessageSelector()
{
return messageSelector;
}
public String getDestinationJndiName()
{
return destinationJndiName;
}
public String getUser()
{
return user;
}
public String getPasswd()
{
return passwd;
}
public String getClientId()
{
return clientId;
}
public String getSubscriptionId()
{
return subscriptionId;
}
public byte getMethodTransactionType()
{
if (methodTransactionType == TX_UNSET)
{
Class[] sig = { Message.class };
methodTransactionType = getMethodTransactionType("onMessage", sig);
}
return methodTransactionType;
}
public byte getMethodTransactionType(String methodName, Class[] signature)
{
if (isContainerManagedTx())
{
if (super.getMethodTransactionType(methodName, signature, null) == MetaData.TX_NOT_SUPPORTED)
return TX_NOT_SUPPORTED;
else
return TX_REQUIRED;
}
else
return TX_UNKNOWN;
}
public byte getMethodTransactionType(String methodName, Class[] params, InvocationType iface)
{
if (isJMSMessagingType())
return getMethodTransactionType();
else
return getMethodTransactionType(methodName, params);
}
public byte getSubscriptionDurability()
{
return subscriptionDurability;
}
public String getDefaultConfigurationName()
{
if (isJMSMessagingType() == false)
return ConfigurationMetaData.MESSAGE_INFLOW_DRIVEN;
else
return ConfigurationMetaData.MESSAGE_DRIVEN_13;
}
public HashMap getActivationConfigProperties()
{
return activationConfigProperties;
}
public ActivationConfigPropertyMetaData getActivationConfigProperty(String name)
{
return (ActivationConfigPropertyMetaData) activationConfigProperties.get(name);
}
public String getResourceAdapterName()
{
return resourceAdapterName;
}
public void importEjbJarXml(Element element) throws DeploymentException
{
super.importEjbJarXml(element);
messageSelector = getOptionalChildContent(element, "message-selector");
if( messageSelector != null )
{
int i = -1;
while( ( i = messageSelector.indexOf( "\r\n" ) ) >= 0 )
{
messageSelector = ( i == 0 ? "" : messageSelector.substring( 0, i ) ) +
" " +
( i >= messageSelector.length() - 2 ? "" : messageSelector.substring( i + 2 ) );
}
i = -1;
while( ( i = messageSelector.indexOf( "\r" ) ) >= 0 )
{
messageSelector = ( i == 0 ? "" : messageSelector.substring( 0, i ) ) +
" " +
( i >= messageSelector.length() - 1 ? "" : messageSelector.substring( i + 1 ) );
}
i = -1;
while( ( i = messageSelector.indexOf( "\n" ) ) >= 0 )
{
messageSelector = ( i == 0 ? "" : messageSelector.substring( 0, i ) ) +
" " +
( i >= messageSelector.length() - 1 ? "" : messageSelector.substring( i + 1 ) );
}
messageSelector = messageSelector.trim();
if( "".equals( messageSelector ) )
{
messageSelector = null;
}
}
messagingType = getOptionalChildContent(element, "messaging-type", DEFAULT_MESSAGING_TYPE);
Element destination = getOptionalChild(element, "message-driven-destination");
if (destination != null)
{
destinationType = getUniqueChildContent(destination, "destination-type");
if (destinationType.equals("javax.jms.Topic"))
{
String subscr = getOptionalChildContent(destination, "subscription-durability");
if( subscr != null && subscr.equals("Durable") )
{
subscriptionDurability = DURABLE_SUBSCRIPTION;
}
else
{
subscriptionDurability = NON_DURABLE_SUBSCRIPTION; }
}
}
String ejb21DestinationType = getOptionalChildContent(element, "message-destination-type");
if (ejb21DestinationType != null)
destinationType = ejb21DestinationType;
destinationLink = getOptionalChildContent(element, "message-destination-link");
String transactionType = getUniqueChildContent(element, "transaction-type");
if (transactionType.equals("Bean"))
{
containerManagedTx = false;
String ack = getOptionalChildContent(element, "acknowledge-mode");
if( ack == null || ack.equalsIgnoreCase("Auto-acknowledge") ||
ack.equalsIgnoreCase("AUTO_ACKNOWLEDGE"))
{
acknowledgeMode = AUTO_ACKNOWLEDGE_MODE;
}
else if (ack.equalsIgnoreCase("Dups-ok-acknowledge") ||
ack.equalsIgnoreCase("DUPS_OK_ACKNOWLEDGE"))
{
acknowledgeMode = DUPS_OK_ACKNOWLEDGE_MODE;
}
else
{
throw new DeploymentException("invalid acknowledge-mode: " + ack);
}
}
else if (transactionType.equals("Container"))
{
containerManagedTx = true;
}
else
{
throw new DeploymentException
("transaction type should be 'Bean' or 'Container'");
}
Element activationConfig = getOptionalChild(element, "activation-config");
if (activationConfig != null)
{
Iterator iterator = getChildrenByTagName(activationConfig, "activation-config-property");
while (iterator.hasNext())
{
Element resourceRef = (Element) iterator.next();
ActivationConfigPropertyMetaData metaData = new ActivationConfigPropertyMetaData();
metaData.importXml(resourceRef);
if (activationConfigProperties.containsKey(metaData.getName()))
throw new DeploymentException("Duplicate activation-config-property-name: " + metaData.getName());
activationConfigProperties.put(metaData.getName(), metaData);
}
}
}
public void importJbossXml(Element element) throws DeploymentException
{
super.importJbossXml(element);
destinationJndiName = getOptionalChildContent(element, "destination-jndi-name");
user = getOptionalChildContent(element, "mdb-user");
passwd = getOptionalChildContent(element,"mdb-passwd");
clientId = getOptionalChildContent(element,"mdb-client-id");
subscriptionId = getOptionalChildContent(element,"mdb-subscription-id");
resourceAdapterName = getOptionalChildContent(element,"resource-adapter-name");
Element activationConfig = getOptionalChild(element, "activation-config");
if (activationConfig != null)
{
Iterator iterator = getChildrenByTagName(activationConfig, "activation-config-property");
while (iterator.hasNext())
{
Element resourceRef = (Element) iterator.next();
ActivationConfigPropertyMetaData metaData = new ActivationConfigPropertyMetaData();
metaData.importXml(resourceRef);
activationConfigProperties.put(metaData.getName(), metaData);
}
}
}
public void defaultInvokerBindings()
{
this.invokerBindings = new HashMap();
this.invokerBindings.put(DEFAULT_MESSAGE_DRIVEN_BEAN_INVOKER_PROXY_BINDING, getJndiName());
}
}