piątek, stycznia 18, 2013

HornetQ's Context Factory for Tibco BW

HornetQ 2.3 supports data replication. Detecting servers can be via JGroups UDP or explict TCP/IP list.
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NameClassPair;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;

import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.jms.HornetQJMSClient;
import org.hornetq.api.jms.JMSFactoryType;
import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory;
import org.hornetq.jms.client.HornetQConnectionFactory;


public class HQContextFactory implements InitialContextFactory, Context {
 
 private String url = null;
 private Hashtable params = null;

 @Override
 public Context getInitialContext(Hashtable params)
   throws NamingException {
  this.url = (String) params.get(PROVIDER_URL);
  this.params = params;
  return this;
 }
 
 @Override
 public Object addToEnvironment(String arg0, Object arg1)
   throws NamingException {
  throw new RuntimeException("Not implemented");
 }
 @Override
 
 @Override
 public void close() throws NamingException {}

 @Override
 public Hashtable getEnvironment() throws NamingException {
  return params;
 } 

 /**************************************/
 @Override
 public Object lookup(String name) throws NamingException {
  List servers = new LinkedList();
  for (String server : url.split("\\,")) {
   String hp[] = server.split("\\:");
   if (hp.length == 1 || hp.length == 2 && hp[1].trim().length() == 0) {
    hp = new String[] { hp[0], "5445" };
   }
   HashMap map = new HashMap();
   map.put("host", hp[0].trim());
   map.put("port", hp[1].trim());
   servers.add(new TransportConfiguration(NettyConnectorFactory.class.getName(), map));
  }
  name = name.toLowerCase();
  if (name.contains("connectionfactory")) {
   HornetQConnectionFactory cf = null;
   if (name.contains("queue")) {
    cf = HornetQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.QUEUE_XA_CF, servers.toArray(new TransportConfiguration[0]));
   }
   else if (name.contains("topic")) {
    cf = HornetQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.TOPIC_XA_CF, servers.toArray(new TransportConfiguration[0]));
   }
   else {
    cf = HornetQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.XA_CF, servers.toArray(new TransportConfiguration[0]));
   }
   return cf;
  }
  throw new NamingException("Name '"+name+"' is not supported");
 }
 /**************************************/

 @Override
 public Object lookup(Name name) throws NamingException {
  return lookup(name.toString());
 }
 @Override
 public Object lookupLink(Name name) throws NamingException {
  return lookup(name.toString());
 }
 @Override
 public Object lookupLink(String name) throws NamingException {
  return lookup(name);
 } 
}

0 komentarze: