1   /*
2    * Created on May 9, 2004
3    * 
4    * TODO To change the template for this generated file go to Window -
5    * Preferences - Java - Code Generation - Code and Comments
6    */
7   package org.codehaus.activemq;
8   import javax.jms.Connection;
9   import javax.jms.ConnectionConsumer;
10  import javax.jms.ConnectionFactory;
11  import javax.jms.Destination;
12  import javax.jms.JMSException;
13  import javax.jms.MessageConsumer;
14  import javax.jms.MessageProducer;
15  import javax.jms.ServerSessionPool;
16  import javax.jms.Session;
17  import javax.jms.DeliveryMode;
18  import javax.jms.Topic;
19  /***
20   * @version $Revision: 1.4 $
21   */
22  public class JmsResourceProvider {
23  	
24  	private String serverUri = "vm://localhost";
25  	private boolean transacted = false;
26  	private int ackMode = Session.AUTO_ACKNOWLEDGE;
27  	private boolean isTopic;
28      private int deliveryMode;
29      private String durableName = "DummyName";
30      private String clientID = getClass().getName();
31  
32  
33  	/***
34  	 * @see org.codehaus.activemq.JmsResourceProvider#createConnectionFactory()
35  	 */
36  	public ConnectionFactory createConnectionFactory() throws JMSException {
37  		return new ActiveMQConnectionFactory(serverUri);
38  	}
39  	
40  	/***
41  	 * @see org.codehaus.activemq.JmsResourceProvider#createConnection(javax.jms.ConnectionFactory)
42  	 */
43  	public Connection createConnection(ConnectionFactory cf) throws JMSException {
44          Connection connection = cf.createConnection();
45          if (isDurableSubscriber()) {
46              connection.setClientID(getClientID());
47          }
48          return connection;
49  	}
50  	/***
51  	 * @see org.codehaus.activemq.JmsResourceProvider#createSession(javax.jms.Connection)
52  	 */
53  	public Session createSession(Connection conn) throws JMSException {
54  		return conn.createSession(transacted, ackMode);
55  	}
56  	/***
57  	 * @see org.codehaus.activemq.JmsResourceProvider#createConsumer(javax.jms.Session,
58  	 *      javax.jms.Destination)
59  	 */
60  	public MessageConsumer createConsumer(Session session,
61  			Destination destination) throws JMSException {
62          if (isDurableSubscriber()) {
63              return session.createDurableSubscriber((Topic) destination, durableName);
64          }
65  		return session.createConsumer(destination);
66  	}
67  
68  	/***
69  	 * @param ssp
70  	 * @return
71  	 */
72  	public ConnectionConsumer createConnectionConsumer(Connection connection, Destination destination, ServerSessionPool ssp) throws JMSException {
73  		return connection.createConnectionConsumer(destination,null,ssp,1);
74  	}
75  
76  	/***
77  	 * @see org.codehaus.activemq.JmsResourceProvider#createProducer(javax.jms.Session,
78  	 *      javax.jms.Destination)
79  	 */
80  	public MessageProducer createProducer(Session session,
81  			Destination destination) throws JMSException {
82          MessageProducer producer = session.createProducer(destination);
83          if (deliveryMode != 0) {
84              producer.setDeliveryMode(deliveryMode);
85          }
86          return producer;
87  	}
88  	/***
89  	 * @see org.codehaus.activemq.JmsResourceProvider#createDestination(javax.jms.Session,
90  	 *      java.lang.String)
91  	 */
92  	public Destination createDestination(Session session, String name)
93  			throws JMSException {
94  		if( isTopic )
95  			return session.createTopic("TOPIC."+name);
96  		else 
97  			return session.createQueue("QUEUE."+name);
98  	}
99  
100     public boolean isDurableSubscriber() {
101         return isTopic && deliveryMode == DeliveryMode.PERSISTENT;
102     }
103 
104 	/***
105 	 * @return Returns the ackMode.
106 	 */
107 	public int getAckMode() {
108 		return ackMode;
109 	}
110 	/***
111 	 * @param ackMode The ackMode to set.
112 	 */
113 	public void setAckMode(int ackMode) {
114 		this.ackMode = ackMode;
115 	}
116 	/***
117 	 * @return Returns the isTopic.
118 	 */
119 	public boolean isTopic() {
120 		return isTopic;
121 	}
122 	/***
123 	 * @param isTopic The isTopic to set.
124 	 */
125 	public void setTopic(boolean isTopic) {
126 		this.isTopic = isTopic;
127 	}
128 	/***
129 	 * @return Returns the serverUri.
130 	 */
131 	public String getServerUri() {
132 		return serverUri;
133 	}
134 	/***
135 	 * @param serverUri The serverUri to set.
136 	 */
137 	public void setServerUri(String serverUri) {
138 		this.serverUri = serverUri;
139 	}
140 	/***
141 	 * @return Returns the transacted.
142 	 */
143 	public boolean isTransacted() {
144 		return transacted;
145 	}
146 	/***
147 	 * @param transacted The transacted to set.
148 	 */
149 	public void setTransacted(boolean transacted) {
150 		this.transacted = transacted;
151 	}
152 
153     public int getDeliveryMode() {
154         return deliveryMode;
155     }
156 
157     public void setDeliveryMode(int deliveryMode) {
158         this.deliveryMode = deliveryMode;
159     }
160 
161     public String getClientID() {
162         return clientID;
163     }
164 
165     public void setClientID(String clientID) {
166         this.clientID = clientID;
167     }
168 
169     public String getDurableName() {
170         return durableName;
171     }
172 
173     public void setDurableName(String durableName) {
174         this.durableName = durableName;
175     }
176 }