/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.codehaus.activemq.util.connection;
import org.apache.jmeter.util.JMeterUtils;
import org.codehaus.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
/**
* Provides static methods for creating Session and Destination objects.
*/
public class ServerConnectionFactory {
public static final String SONICMQ_SERVER = JMeterUtils.getResString("sonicmq_server");
public static final String TIBCOMQ_SERVER = JMeterUtils.getResString("tibcomq_server");
public static final String JBOSSMQ_SERVER = JMeterUtils.getResString("jbossmq_server");
public static final String SONICMQ_TOPIC = "progress.message.jclient.TopicConnectionFactory";
public static final String SONICMQ_QUEUE = "progress.message.jclient.QueueConnectionFactory";
public static final String TIBCOMQ_TOPIC = "com.tibco.tibjms.TibjmsTopicConnectionFactory";
public static final String TIBCOMQ_QUEUE = "com.tibco.tibjms.TibjmsQueueConnectionFactory";
public static final String NAMING_CONTEXT = "org.jnp.interfaces.NamingContextFactory";
public static final String JNP_INTERFACES = "org.jnp.interfaces";
/**
* Closes the connection passed through the parameter
*
* @param connection - Connection object to be closed.
* @param session - Session object to be closed.
* @throws JMSException
*/
public static void close(Connection connection, Session session) throws JMSException {
connection.close();
session.close();
}
/**
* Dynamically creates a Connection object based on the type of broker.
*
* @param url - location of the broker.
* @param mqServer - type of broker that is running.
* @param isTopic - type of message domain.
* @param embeddedBroker - specified is the broker is embedded.
* @return
* @throws JMSException
*/
public static Connection createConnectionFactory(String url,
String mqServer,
boolean isTopic,
boolean embeddedBroker) throws JMSException {
if (SONICMQ_SERVER.equals(mqServer)) {
//Creates a Connection object for a SONIC MQ server.
if (isTopic) {
return createConnectionFactory(url, SONICMQ_TOPIC);
} else {
return createConnectionFactory(url, SONICMQ_QUEUE);
}
} else if (TIBCOMQ_SERVER.equals(mqServer)) {
//Creates a Connection object for a TIBCO MQ server.
if (isTopic) {
return createConnectionFactory(url, TIBCOMQ_TOPIC);
} else {
return createConnectionFactory(url, TIBCOMQ_QUEUE);
}
} else if (JBOSSMQ_SERVER.equals(mqServer)) {
try {
InitialContext context = getInitialContext(url);
ConnectionFactory factory = (ConnectionFactory)context.lookup("ConnectionFactory");
context.close();
return factory.createConnection();
} catch (NamingException e) {
throw new JMSException("Error creating InitialContext ", e.toString());
}
} else {
//Used to create a session from the default MQ server ActiveMQConnectionFactory.
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
if (embeddedBroker) {
factory.setUseEmbeddedBroker(true);
}
factory.setTurboBoost(true);
return factory.createConnection();
}
}
/**
* Creates a Destination object through Session using subject.
*
* @param session - Session used to create the Destination.
* @param subject - the subject of the Destination to be created.
* @param mqServer - ype of broker that is running.
* @param url - location of the broker.
* @param isTopic - specified is the broker is embedded.
* @return
* @throws JMSException
*/
public static Destination createDestination(Session session,
String subject,
String mqServer,
String url,
boolean isTopic) throws JMSException {
if (JBOSSMQ_SERVER.equals(mqServer)) {
try {
if (isTopic) {
return (Topic)getInitialContext(url).lookup("topic/"+subject);
} else {
return (Queue)getInitialContext(url).lookup("queue/"+subject);
}
} catch (NamingException e) {
throw new JMSException("Error on lookup for Queue " + subject, e.toString());
}
} else {
if (isTopic) {
return session.createTopic(subject);
} else {
return session.createQueue(subject);
}
}
}
/**
* Creates a new Session object.
*
* @throws JMSException
*/
/**
* Creates a Session object.
*
* @param connection - Connection object where the session will be created from.
* @return
* @throws JMSException
*/
public static Session createSession(Connection connection, boolean isTransacted) throws JMSException {
// Creates connection and sets an ID to it.
/*IdGenerator idGenerator = new IdGenerator();
if (isDurable) {
connection.setClientID(idGenerator.generateId());
} */
// check when to use Transacted or Non-Transacted type.
if (isTransacted) {
return connection.createSession(true, Session.SESSION_TRANSACTED);
} else {
return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
}
/**
* Dynamically creates a ConnectionFactory object depending on the MQ Factory class.
*
* @param url - location of the broker.
* @param connFactoryClass - fully qualified name of connection factory to be initialized.
* @return
* @throws JMSException
*/
public static Connection createConnectionFactory(String url, String connFactoryClass) throws JMSException {
Class classObject;
Constructor constructor;
Class[] classParameter = {url.getClass()};
Object[] constArgs = {url};
try {
classObject = Class.forName(connFactoryClass);
constructor = classObject.getConstructor(classParameter);
ConnectionFactory factory = (ConnectionFactory) constructor.newInstance(constArgs);
return factory.createConnection();
} catch (ClassNotFoundException e) {
throw new JMSException("Unable to find class ", e.toString());
} catch (NoSuchMethodException e) {
throw new JMSException("No such getConstructor(Class[] clazz) method found ", e.toString());
} catch (InstantiationException e) {
throw new JMSException("Unable to instantiate class ", e.toString());
} catch (IllegalAccessException e) {
throw new JMSException("Unable to instantiate class ", e.toString());
} catch (InvocationTargetException e) {
throw new JMSException("Unable to instantiate class ", e.toString());
}
}
/**
* Creates an InitialContext object which contains the information of the broker.
* This is used if the broker uses JNDI.
*
* @param url - location of the broker.
* @return
* @throws JMSException
*/
public static InitialContext getInitialContext(String url) throws JMSException {
//Creates a Connection object from JBOSS MQ server
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, NAMING_CONTEXT);
properties.put(Context.URL_PKG_PREFIXES, JNP_INTERFACES);
properties.put(Context.PROVIDER_URL, url);
try {
return new InitialContext(properties);
} catch (NamingException e) {
throw new JMSException("Error creating InitialContext ", e.toString());
}
}
}