1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: AbstractConnectionFactory.java,v 1.5 2005/05/03 13:45:58 tanderson Exp $
44 */
45 package org.exolab.jms.net.connector;
46
47 import java.util.Map;
48
49 import java.security.Principal;
50
51 import org.exolab.jms.net.uri.URI;
52 import org.exolab.jms.net.uri.URIHelper;
53 import org.exolab.jms.net.util.Properties;
54
55
56 /***
57 * Abstract implementation of the {@link ConnectionFactory} interface.
58 *
59 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
60 * @version $Revision: 1.5 $ $Date: 2005/05/03 13:45:58 $
61 */
62 public abstract class AbstractConnectionFactory implements ConnectionFactory {
63
64 /***
65 * Connection property prefix. Connection properties are prefixed with this,
66 * followed by the connection scheme.
67 */
68 public static final String PROPERTY_PREFIX = "org.exolab.jms.net.";
69
70 /***
71 * The connect scheme that this factory supports.
72 */
73 private final String _connectScheme;
74
75 /***
76 * The accept scheme that this factory supports.
77 */
78 private final String _acceptScheme;
79
80 /***
81 * The managed connection factory.
82 */
83 private final ManagedConnectionFactory _factory;
84
85 /***
86 * The connection manager.
87 */
88 private final ConnectionManager _manager;
89
90
91 /***
92 * Construct a new <code>AbstractConnectionFactory</code>.
93 *
94 * @param scheme the scheme that this factory supports, for both connect
95 * and accept
96 * @param factory the managed connection factory
97 * @param manager the connection manager
98 */
99 public AbstractConnectionFactory(String scheme,
100 ManagedConnectionFactory factory,
101 ConnectionManager manager) {
102 this(scheme, scheme, factory, manager);
103 }
104
105 /***
106 * Construct a new <code>AbstractConnectionFactory</code>.
107 *
108 * @param connectScheme the connect scheme that this factory supports
109 * @param acceptScheme the accept scheme that this factory supports
110 * @param factory the managed connection factory
111 * @param manager the connection manager
112 */
113 public AbstractConnectionFactory(String connectScheme,
114 String acceptScheme,
115 ManagedConnectionFactory factory,
116 ConnectionManager manager) {
117 _connectScheme = connectScheme;
118 _acceptScheme = acceptScheme;
119 _factory = factory;
120 _manager = manager;
121 }
122
123 /***
124 * Determines if this factory supports connections to the specified URI.
125 *
126 * @param uri the connection address
127 * @return <code>true</code> if this factory supports the URI;
128 * <code>false</code> otherwise
129 */
130 public boolean canConnect(URI uri) {
131 return _connectScheme.equals(uri.getScheme());
132 }
133
134 /***
135 * Returns a connection to the specified URI, using the default connection
136 * properties.
137 *
138 * @param principal the security principal. May be <code>null</code>
139 * @param uri the connection address
140 * @return a connection to <code>uri</code>
141 * @throws ResourceException if a connection cannot be established
142 */
143 public Connection getConnection(Principal principal, URI uri)
144 throws ResourceException {
145 return getConnection(principal, uri, null);
146 }
147
148 /***
149 * Returns a connection to the specified URI, using the specified connection
150 * properties.
151 *
152 * @param principal the security principal. May be <code>null</code>
153 * @param uri the connection address
154 * @param properties connection properties. If <code>null</code>, use the
155 * default connection properties
156 * @return a connection to <code>uri</code>
157 * @throws ResourceException if a connection cannot be established
158 */
159 public Connection getConnection(Principal principal, URI uri,
160 Map properties)
161 throws ResourceException {
162 ConnectionRequestInfo info =
163 getConnectionRequestInfo(uri, properties);
164 if (principal == null) {
165 principal = URIHelper.getPrincipal(uri);
166 }
167 return _manager.allocateConnection(_factory, principal, info);
168 }
169
170 /***
171 * Determines if this factory supports listening for new connections on the
172 * specified URI.
173 *
174 * @param uri the connection address
175 * @return <code>true</code> if this factory supports the URI;
176 * <code>false</code> otherwise
177 */
178 public boolean canAccept(URI uri) {
179 return _acceptScheme.equals(uri.getScheme());
180 }
181
182 /***
183 * Listen for new connections on the specified URI, using the default
184 * connection acceptor properties.
185 *
186 * @param uri the connection address
187 * @throws ResourceException if connections can't be accepted on the
188 * specified URI
189 */
190 public void accept(URI uri) throws ResourceException {
191 accept(uri, null);
192 }
193
194 /***
195 * Listen for new connections on the specified URI, using the specified
196 * acceptor properties.
197 *
198 * @param uri the connection address
199 * @param properties acceptor properties. May be <code>null</code>
200 * @throws ResourceException if connections can't be accepted on the
201 * specified URI
202 */
203 public void accept(URI uri, Map properties) throws ResourceException {
204 ConnectionRequestInfo info = getAcceptorRequestInfo(uri, properties);
205 _manager.accept(_factory, info);
206 }
207
208 /***
209 * Returns connection request info for the specified URI and connection
210 * properties.
211 *
212 * @param uri the connection address
213 * @param properties connection properties. If <code>null</code>, use the
214 * default connection properties
215 * @return connection request info corresponding to <code>uri</code> and
216 * <code>properties</code>
217 * @throws ResourceException for any error
218 */
219 protected abstract ConnectionRequestInfo getConnectionRequestInfo(
220 URI uri, Map properties)
221 throws ResourceException;
222
223 /***
224 * Returns connection request info for the specified URI and connection
225 * acceptor properties.
226 * <p/>
227 * This implementation returns {@link #getConnectionRequestInfo(URI, Map)}.
228 *
229 * @param uri the connection address
230 * @param properties acceptor properties. May be <code>null</code>
231 * @return connection request info corresponding to <code>uri</code> and
232 * <code>properties</code>
233 * @throws ResourceException for any error
234 */
235 protected ConnectionRequestInfo getAcceptorRequestInfo(
236 URI uri, Map properties)
237 throws ResourceException {
238 return getConnectionRequestInfo(uri, properties);
239 }
240
241 /***
242 * Returns the managed connection factory.
243 *
244 * @return the managed connection factory
245 */
246 protected ManagedConnectionFactory getManagedConnectionFactory() {
247 return _factory;
248 }
249
250 /***
251 * Returns the connection manager.
252 *
253 * @return the connection manager
254 */
255 protected ConnectionManager getConnectionManager() {
256 return _manager;
257 }
258
259 /***
260 * Helper to return a {@link Properties} instance for the supplied
261 * map.
262 * <p/>
263 * All searches will be performed with properties prefixed by
264 * "org.exolab.jms.net.<scheme>." .
265 */
266 protected Properties getProperties(Map properties) {
267 String prefix = PROPERTY_PREFIX + _connectScheme + ".";
268 return new Properties(properties, prefix);
269 }
270
271 }