/*
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt 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.jms.server.recovery;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.xa.XAResource;
import org.jboss.jms.client.ClientAOPStackLoader;
import org.jboss.jms.client.JBossConnectionFactory;
import org.jboss.jms.client.delegate.ClientClusteredConnectionFactoryDelegate;
import org.jboss.jms.client.delegate.ClientConnectionDelegate;
import org.jboss.jms.client.delegate.ClientConnectionFactoryDelegate;
import org.jboss.jms.delegate.ConnectionFactoryDelegate;
import org.jboss.jms.delegate.CreateConnectionResult;
import org.jboss.jms.jndi.JMSProviderAdapter;
import org.jboss.logging.Logger;
import org.jboss.util.naming.Util;
import com.arjuna.ats.jta.recovery.XAResourceRecovery;
/**
*
* A XAResourceRecovery instance that can be used to recover a JBM cluster.
*
* This class needs a clustered connection factory to create a connection to JBM cluster.
* It keeps track of all living nodes in the cluster. On requesting for recovery,
* it can returns all XAResources for all the live nodes,
* therefore the recovery manager can use it alone to recover all prepared transactions
* in the cluster.
*
*
* It also can be used in a non clustered JBM node.
*
*/
public class ClusteredMessagingXAResourceRecovery implements XAResourceRecovery
{
private boolean trace = log.isTraceEnabled();
private static final Logger log = Logger.getLogger(ClusteredMessagingXAResourceRecovery.class);
private String providerAdaptorName;
private String username;
private String password;
private Queue<XAResource> resources = null;
private Iterator<XAResource> iterator = null;
private ConnectionFactoryDelegate factoryDelegate;
public ClusteredMessagingXAResourceRecovery()
{
if(trace) log.trace("Constructing BridgeXAResourceRecovery2");
}
public boolean initialise(String config)
{
if (log.isTraceEnabled()) { log.trace(this + " intialise: " + config); }
StringTokenizer tok = new StringTokenizer(config, ",");
//First (mandatory) param is the provider adaptor name
if (!tok.hasMoreTokens())
{
throw new IllegalArgumentException("Must specify provider adaptor name in config");
}
providerAdaptorName = tok.nextToken();
//Next two (optional) parameters are the username and password to use for creating the connection
//for recovery
if (tok.hasMoreTokens())
{
username = tok.nextToken();
if (!tok.hasMoreTokens())
{
throw new IllegalArgumentException("If username is specified, password must be specified too");
}
password = tok.nextToken();
}
if (log.isTraceEnabled()) { log.trace(this + " initialised"); }
return true;
}
/*
* Iterates the resources. Note we can't clean up the resources right after
* we find no more resources because the recovery manager will still use it.
* It is safe to do clean up of resources at next scan
*/
public boolean hasMoreResources()
{
if (log.isTraceEnabled()) { log.trace(this + " hasMoreResources"); }
if (iterator == null)
{
shutdownResources();
updateResources();
}
if (iterator.hasNext())
{
return true;
}
iterator = null;
return false;
}
public XAResource getXAResource()
{
if (log.isTraceEnabled()) { log.trace(this + " getXAResource"); }
return iterator.next();
}
protected void finalize()
{
shutdownResources();
}
private void shutdownResources()
{
if (resources == null) return;
for (XAResource res : resources)
{
((MessagingXAResourceWrapper2)res).close();
}
resources = null;
iterator = null;
}
/*
* Get XAResources for each node. If it is a non-clustered factory,
* only one XAresource is returned.
*/
private void updateResources()
{
if (log.isTraceEnabled()) { log.trace(this + " updating Resources"); }
resources = new LinkedList<XAResource>();
try
{
if (factoryDelegate == null)
{
JBossConnectionFactory factory = getConnectionFactory();
factoryDelegate = factory.getDelegate();
try
{
ClientAOPStackLoader.getInstance().load(factoryDelegate);
}
catch(Exception e)
{
// Need to log message since no guarantee that client will log it
final String msg = "Failed to download and/or install client side AOP stack";
log.error(msg, e);
throw new RuntimeException(msg, e);
}
}
if (factoryDelegate instanceof ClientClusteredConnectionFactoryDelegate)
{
if (log.isTraceEnabled()) { log.trace(this + " getting XAResources from the cluster"); }
ClientClusteredConnectionFactoryDelegate clusteredFactoryDelegate = (ClientClusteredConnectionFactoryDelegate)factoryDelegate;
ClientConnectionFactoryDelegate[] delegates = clusteredFactoryDelegate.getDelegates();
for (ClientConnectionFactoryDelegate del : delegates)
{
try
{
CreateConnectionResult result = del.createConnectionDelegate(username, password, -1);
MessagingXAResourceWrapper2 res = new MessagingXAResourceWrapper2(result.getDelegate(),
del.getServerID());
resources.add(res);
}
catch (Throwable e)
{
log.error("Failed to create resource for node " + del.getServerID(), e);
}
}
}
else
{
if (log.isTraceEnabled()) { log.trace(this + " getting XAResources from single node"); }
ClientConnectionDelegate delegate = null;
try
{
CreateConnectionResult result = factoryDelegate.createConnectionDelegate(username, password, -1);
delegate = result.getDelegate();
MessagingXAResourceWrapper2 res = new MessagingXAResourceWrapper2(delegate, delegate.getServerID());
resources.add(res);
}
catch (Throwable e)
{
log.error("Failed to create resource using factory " + factoryDelegate, e);
}
}
}
catch (Exception e)
{
log.error("Failed to get connection factory.", e);
}
iterator = resources.iterator();
if (resources.isEmpty())
{
//if no resources, this could mean all nodes shut down for the moment
//so we set the delegate to null to enable a fresh jndi lookup
//next time.
this.factoryDelegate = null;
}
}
//the provider adaptor should use HA-JNDI
private JBossConnectionFactory getConnectionFactory() throws Exception
{
if (log.isTraceEnabled()) { log.trace(this + " look up CF via provider " + providerAdaptorName); }
Context ctx = new InitialContext();
JMSProviderAdapter adapter = (JMSProviderAdapter) ctx.lookup(providerAdaptorName);
String connectionFactoryRef = adapter.getFactoryRef();
if (connectionFactoryRef == null)
throw new IllegalStateException("Provider '" + providerAdaptorName + "' has no FactoryRef");
// Lookup the connection factory
ctx = adapter.getInitialContext();
try
{
JBossConnectionFactory factory = (JBossConnectionFactory)Util.lookup(ctx, connectionFactoryRef, JBossConnectionFactory.class);
return factory;
}
finally
{
ctx.close();
}
}
}