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