-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathDestinationResource.java
More file actions
82 lines (69 loc) · 2.99 KB
/
DestinationResource.java
File metadata and controls
82 lines (69 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.amazon.sqs.javamessaging.jndi;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.naming.directory.InvalidAttributeValueException;
import com.amazon.sqs.javamessaging.SQSConnection;
/**
* Breaks the <b>description string</b> with information about the {@link ResourceType resource type}
* and {@link com.amazon.sqs.javamessaging.SQSQueueDestination#getQueueName() destination name}
* to make it an instance of {@link Destination} that encapsulates a specific provider address.
* <p><ul>
* <b>Format: </b><q><i>ResourceTypeName<b> : </b>DestinationName</i></q>.<br>
* <b>Example: </b><q><i>SA:SQS_Queue-Name_v10</i></q>.
* </ul>
*
* @author krloss
* @since 1.1.0
*/
public class DestinationResource {
private static final Pattern RESOURCE_PATTERN = Pattern.compile("^\\s*([CS][ACDU])\\s*:\\s*([-\\w]+)\\s*$");
protected final ResourceType type;
protected final String name;
/**
* Public constructor that requires <i>description</i> parameter.
* <p>
* <b>Format: </b><q><i>ResourceTypeName<b> : </b>DestinationName</i></q>.
*
* @param description - string with information about the {@link ResourceType resource type}
* and {@link com.amazon.sqs.javamessaging.SQSQueueDestination#getQueueName() destination name}.
*
* @throws InvalidAttributeValueException
*/
public DestinationResource(String description) throws InvalidAttributeValueException {
Matcher matcher;
try {
matcher = RESOURCE_PATTERN.matcher(description);
}
catch(NullPointerException npe) {
throw new InvalidAttributeValueException("DestinationResource Requires Description.");
}
if(!matcher.matches()) throw new InvalidAttributeValueException("DestinationResource Pattern Not Acceptable.");
this.name = matcher.group(2);
this.type = ResourceType.valueOf(matcher.group(1));
}
/**
* Gets the <b>connection</b> according to the <i>pooling type</i> and<br>
* creates <b>session</b> according to the <i>acknowledgment mode</i>.
*/
private Session createSession(final ConnectionsManager connectionsManager) throws JMSException {
SQSConnection connection = type.isSessionPooling ?
connectionsManager.getLazyDefaultConnection() : connectionsManager.createConnection();
return connection.createSession(false,type.acknowledgeMode);
}
/**
* Makes this object in an instance of {@link Destination}.
*
* @param connectionsManager - object that manages connections.
* @return Destination - JMS administered object that encapsulates a specific provider address.
* @throws InvalidAttributeValueException
* @throws JMSException
* @see ConnectionsManager
*/
public Destination getDestination(final ConnectionsManager connectionsManager) throws InvalidAttributeValueException, JMSException {
if(connectionsManager == null) throw new InvalidAttributeValueException("GetConnection Requires ResourceType.");
return createSession(connectionsManager).createQueue(name);
}
}