001 /*
002 @license.text@
003 */
004 package biz.hammurapi.jms;
005
006 import javax.jms.JMSException;
007 import javax.jms.TopicConnection;
008 import javax.jms.TopicConnectionFactory;
009 import javax.xml.transform.TransformerException;
010
011 import org.apache.xpath.CachedXPathAPI;
012 import org.w3c.dom.Element;
013 import org.w3c.dom.Node;
014
015 import biz.hammurapi.config.ConfigurationException;
016 import biz.hammurapi.config.Context;
017 import biz.hammurapi.config.JndiWrapper;
018 import biz.hammurapi.xml.dom.AbstractDomObject;
019
020
021 /**
022 * This class reads configuration from XML, looks up JMS Topic connection factory in JNDI and obtains connection from the factory.
023 * Configuration:<BR/>
024 * Attribute: <code>jndi-name</code><BR>
025 * Nested elements: <code>environment-property</code> with attribute <code>name</code>, <code>user</code> and <code>password</code> elements for authentication.
026 * Example:<PRE><topicConnection type="biz.hammurapi.jms.TopicConnectionWrapper" jndi-name="TopicConnectionFactory">
027 <environment-property name="java.naming.factory.initial"><I>factory class</I></environment-property>
028 <environment-property name="java.naming.provider.url"><I>provider url</I></environment-property>
029 <user>usr</user>
030 <password>pwd</password>
031 </topicConnection>
032 </PRE>
033 * Environment properties are passed to the constructor of initial JNDI context.
034 * @author Pavel Vlasov
035 * @revision $Revision$
036 */
037 public class TopicConnectionWrapper extends JndiWrapper {
038
039 private TopicConnection connection;
040
041 /**
042 * Returns topic connection.
043 */
044 public Object getMaster() {
045 return connection;
046 }
047
048 /**
049 * Obstains connection factory from JNDI, gets connection and starts it.
050 */
051 public void start() throws ConfigurationException {
052 super.start();
053 try {
054 TopicConnectionFactory connectionFactory = (TopicConnectionFactory) super.getMaster();
055
056 if (user==null) {
057 connection = connectionFactory.createTopicConnection();
058 } else {
059 connection = connectionFactory.createTopicConnection(user, password);
060 }
061
062 connection.start();
063 } catch (JMSException e) {
064 throw new ConfigurationException("Could not obtain or start connection: "+e, e);
065 }
066 }
067
068 /**
069 * Stops connection.
070 */
071 public void stop() throws ConfigurationException {
072 if (connection!=null) {
073 try {
074 connection.stop();
075 connection=null;
076 } catch (JMSException e) {
077 throw new ConfigurationException("Could not stop connection: "+e, e);
078 }
079 }
080 }
081
082 public void setOwner(Object owner) {
083 // Nothing
084 }
085
086 private String user;
087 private String password;
088
089 public void configure(Node configNode, Context context) throws ConfigurationException {
090 super.configure(configNode, context);
091 CachedXPathAPI cxpa=new CachedXPathAPI();
092
093 try {
094 user = AbstractDomObject.getElementText((Element) configNode, "user", cxpa);
095 if (user!=null) {
096 password=AbstractDomObject.getElementText((Element) configNode, "password", cxpa);
097 }
098 } catch (TransformerException e) {
099 throw new ConfigurationException("Could not read user name and password: "+e, e);
100 }
101 }
102
103 }