1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activespace.jms;
19
20 import org.codehaus.activespace.Space;
21 import org.codehaus.activespace.SpaceException;
22 import org.codehaus.activespace.SpaceFactory;
23
24 import javax.jms.Connection;
25 import javax.jms.ConnectionFactory;
26 import javax.jms.JMSException;
27
28 /***
29 * A JMS implemenation of {@link SpaceFactory} which needs to be configured with a
30 * JMS connection to be usable.
31 *
32 * @version $Revision: 1.1.1.1 $
33 */
34 public class JmsSpaceFactory implements SpaceFactory {
35 private Marshaller marshaller = JmsSpace.DEFAULT_MARSHALLER;
36 private boolean transacted = JmsSpace.DEFAULT_TRANSACTED;
37 private int deliveryMode = JmsSpace.DEFAULT_DELIVERY_MODE;
38 private int priority = JmsSpace.DEFAULT_PRIORITY;
39 private int acknowledgeMode = JmsSpace.DEFAULT_ACKNOWLEGE_MODE;
40 private ConnectionFactory connectionFactory;
41 private Connection connection;
42 private int count;
43 private String user;
44 private String password;
45
46 public JmsSpaceFactory() {
47 }
48
49 public JmsSpaceFactory(ConnectionFactory connectionFactory) {
50 this.connectionFactory = connectionFactory;
51 }
52
53 public Space createSpace(String destination, int deliveryMode, String selector) throws SpaceException {
54 try {
55 JmsSpace answer = new JmsSpace(this, getConnection(), destination, deliveryMode, selector, transacted, getMarshaller(), deliveryMode, priority, acknowledgeMode);
56 return answer;
57 }
58 catch (JMSException e) {
59 throw new SpaceException("Could not create a JMS connection: " + e, e);
60 }
61 }
62
63
64
65
66 public boolean isTransacted() {
67 return transacted;
68 }
69
70 public void setTransacted(boolean transacted) {
71 this.transacted = transacted;
72 }
73
74 public int getDeliveryMode() {
75 return deliveryMode;
76 }
77
78 public void setDeliveryMode(int deliveryMode) {
79 this.deliveryMode = deliveryMode;
80 }
81
82 public int getPriority() {
83 return priority;
84 }
85
86 public void setPriority(int priority) {
87 this.priority = priority;
88 }
89
90 public int getAcknowledgeMode() {
91 return acknowledgeMode;
92 }
93
94 public void setAcknowledgeMode(int acknowledgeMode) {
95 this.acknowledgeMode = acknowledgeMode;
96 }
97
98 public Marshaller getMarshaller() {
99 return marshaller;
100 }
101
102 public Connection getConnection() throws JMSException, SpaceException {
103 if (connection == null) {
104 connection = createConnection();
105 connection.start();
106 }
107 return connection;
108 }
109
110 public void setMarshaller(Marshaller marshaller) {
111 this.marshaller = marshaller;
112 }
113
114 public void setConnection(Connection connection) {
115 this.connection = connection;
116 }
117
118 public ConnectionFactory getConnectionFactory() throws SpaceException {
119 if (connectionFactory == null) {
120 try {
121 connectionFactory = createConnectionFactory();
122 }
123 catch (JMSException e) {
124 throw new SpaceException("Could not create a JMS ConnectionFactory: " + e, e);
125 }
126 }
127 return connectionFactory;
128 }
129
130 public void setConnectionFactory(ConnectionFactory connectionFactory) {
131 this.connectionFactory = connectionFactory;
132 }
133
134 public String getUser() {
135 return user;
136 }
137
138 /***
139 * Sets the user name used to create new JMS connections or null to not use one
140 *
141 * @param user
142 */
143 public void setUser(String user) {
144 this.user = user;
145 }
146
147 public String getPassword() {
148 return password;
149 }
150
151 /***
152 * Sets the password to use when creating new JMS connections if a user name is specified
153 * otherwise this property is ignored
154 *
155 * @param password
156 */
157 public void setPassword(String password) {
158 this.password = password;
159 }
160
161
162
163 protected ConnectionFactory createConnectionFactory() throws JMSException, SpaceException {
164 throw new SpaceException("No JMS ConnectionFactory is configured for this SpaceFactory so cannot create a new Space");
165 }
166
167 protected Connection createConnection() throws SpaceException, JMSException {
168 if (user != null) {
169 return getConnectionFactory().createConnection(user, password);
170 }
171 else {
172 return getConnectionFactory().createConnection();
173 }
174 }
175
176 /***
177 * Hook so that the factory knows that a space is starting
178 *
179 * @param space
180 */
181 synchronized void onSpaceStart(JmsSpace space) {
182 ++count;
183 }
184
185 /***
186 * Hook so that the factory can close down any resources
187 *
188 * @param space
189 * @throws JMSException
190 */
191 synchronized void onSpaceClose(JmsSpace space) throws JMSException {
192 if (--count <= 0 && connection != null) {
193 try {
194 connection.close();
195 }
196 finally {
197 connection = null;
198 }
199 }
200 }
201
202 }