BaseSessionHandlingStrategy.java

1
package fr.sii.ogham.sms.sender.impl.cloudhopper.session;
2
3
import static fr.sii.ogham.core.retry.NamedCallable.named;
4
5
import org.slf4j.Logger;
6
7
import com.cloudhopper.smpp.SmppClient;
8
import com.cloudhopper.smpp.SmppSession;
9
10
import fr.sii.ogham.core.exception.retry.MaximumAttemptsReachedException;
11
import fr.sii.ogham.core.exception.retry.RetryException;
12
import fr.sii.ogham.core.exception.retry.RetryExecutionInterruptedException;
13
import fr.sii.ogham.core.retry.RetryExecutor;
14
import fr.sii.ogham.sms.builder.cloudhopper.SmppClientSupplier;
15
import fr.sii.ogham.sms.builder.cloudhopper.SmppSessionHandlerSupplier;
16
import fr.sii.ogham.sms.sender.impl.cloudhopper.ExtendedSmppSessionConfiguration;
17
import fr.sii.ogham.sms.sender.impl.cloudhopper.exception.ConnectionFailedException;
18
import fr.sii.ogham.sms.sender.impl.cloudhopper.exception.SmppException;
19
20
/**
21
 * Base class for different session handling strategies.
22
 * 
23
 * <p>
24
 * It provides useful methods to initialize a client and a session. It provides
25
 * a method to connect (bind) to the SMSC with retry handling. It also provides
26
 * method to cleanup. The implementations can then use this methods as they
27
 * wish.
28
 * 
29
 * @author Aurélien Baudet
30
 *
31
 */
32
public abstract class BaseSessionHandlingStrategy implements SessionHandlingStrategy {
33
	protected final Logger logger;
34
	protected final ExtendedSmppSessionConfiguration configuration;
35
	protected final SmppClientSupplier clientSupplier;
36
	protected final SmppSessionHandlerSupplier smppSessionHandlerSupplier;
37
	protected final RetryExecutor retry;
38
	protected SmppClient currentClient;
39
	protected SmppSession currentSession;
40
41
	public BaseSessionHandlingStrategy(Logger logger, ExtendedSmppSessionConfiguration configuration, SmppClientSupplier clientSupplier, SmppSessionHandlerSupplier smppSessionHandlerSupplier,
42
			RetryExecutor retry) {
43
		super();
44
		this.logger = logger;
45
		this.configuration = configuration;
46
		this.clientSupplier = clientSupplier;
47
		this.smppSessionHandlerSupplier = smppSessionHandlerSupplier;
48
		this.retry = retry;
49
	}
50
51
	/**
52
	 * Initializes a new session only if session doesn't exist (is
53
	 * {@code null}).
54
	 * 
55
	 * <p>
56
	 * A {@link SmppClient} instance must exist (either using
57
	 * {@link #initClient()} or by manually creating it). The same
58
	 * {@link SmppClient} may be used several times for different sessions.
59
	 * 
60
	 * <p>
61
	 * The creation of a session is done by calling
62
	 * {@link #connect(SmppClient)}.
63
	 * 
64
	 * @throws SmppException
65
	 *             when session couldn't be created
66
	 */
67
	protected synchronized void initSession() throws SmppException {
68 4 1. initSession : negated conditional → NO_COVERAGE
2. initSession : negated conditional → TIMED_OUT
3. initSession : negated conditional → KILLED
4. initSession : negated conditional → KILLED
		if (currentSession == null) {
69
			logger.debug("Requesting a new SMPP session");
70
			currentSession = connect(currentClient);
71
			logger.debug("SMPP session bound");
72
		}
73
	}
74
75
	/**
76
	 * Connect the client to the SMSC using a
77
	 * {@link SmppClient#bind(com.cloudhopper.smpp.SmppSessionConfiguration, com.cloudhopper.smpp.SmppSessionHandler)}
78
	 * request.
79
	 * 
80
	 * <p>
81
	 * The configured retry strategy is used to send the bind request to the
82
	 * server i.e. several attempts may be done.
83
	 * 
84
	 * @param client
85
	 *            the client used to send to bind command
86
	 * @return the created session
87
	 * @throws SmppException
88
	 *             when the session couldn't be bound
89
	 */
90
	protected synchronized SmppSession connect(final SmppClient client) throws SmppException {
91
		try {
92 8 1. connect : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::connect → NO_COVERAGE
2. lambda$connect$0 : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::lambda$connect$0 → NO_COVERAGE
3. connect : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::connect → TIMED_OUT
4. lambda$connect$0 : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::lambda$connect$0 → TIMED_OUT
5. connect : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::connect → KILLED
6. connect : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::connect → KILLED
7. lambda$connect$0 : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::lambda$connect$0 → KILLED
8. lambda$connect$0 : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::lambda$connect$0 → KILLED
			return retry.execute(named("Connection to SMPP server", () -> client.bind(configuration, smppSessionHandlerSupplier.get())));
93
		} catch (RetryExecutionInterruptedException e) {
94 1 1. connect : removed call to java/lang/Thread::interrupt → NO_COVERAGE
			Thread.currentThread().interrupt();
95
			throw new ConnectionFailedException("Failed to initialize SMPP session (interrupted)", e);
96
		} catch (MaximumAttemptsReachedException e) {
97
			throw new ConnectionFailedException("Failed to initialize SMPP session after maximum retries reached", e);
98
		} catch (RetryException e) {
99
			throw new ConnectionFailedException("Failed to initialize SMPP session", e);
100
		}
101
	}
102
103
	/**
104
	 * Create a {@link SmppClient} instance of not existing (is {@code null}).
105
	 */
106
	protected synchronized void initClient() {
107 3 1. initClient : negated conditional → NO_COVERAGE
2. initClient : negated conditional → TIMED_OUT
3. initClient : negated conditional → KILLED
		if (currentClient == null) {
108
			logger.debug("Requesting a new SmppClient instance");
109
			currentClient = clientSupplier.get();
110
		}
111
	}
112
113
	/**
114
	 * Send an unbind command to the server to properly close the session, close
115
	 * the session and cleanup everything related to the session. The current
116
	 * session is set to {@code null}.
117
	 */
118
	protected synchronized void destroySession() {
119 4 1. destroySession : negated conditional → SURVIVED
2. destroySession : negated conditional → TIMED_OUT
3. destroySession : negated conditional → KILLED
4. destroySession : negated conditional → KILLED
		if (currentSession != null) {
120
			logger.debug("Closing SMPP session");
121 4 1. destroySession : removed call to com/cloudhopper/smpp/SmppSession::unbind → SURVIVED
2. destroySession : removed call to com/cloudhopper/smpp/SmppSession::unbind → NO_COVERAGE
3. destroySession : removed call to com/cloudhopper/smpp/SmppSession::unbind → TIMED_OUT
4. destroySession : removed call to com/cloudhopper/smpp/SmppSession::unbind → KILLED
			currentSession.unbind(configuration.getUnbindTimeout());
122 4 1. destroySession : removed call to com/cloudhopper/smpp/SmppSession::destroy → SURVIVED
2. destroySession : removed call to com/cloudhopper/smpp/SmppSession::destroy → NO_COVERAGE
3. destroySession : removed call to com/cloudhopper/smpp/SmppSession::destroy → TIMED_OUT
4. destroySession : removed call to com/cloudhopper/smpp/SmppSession::destroy → KILLED
			currentSession.destroy();
123
			currentSession = null;
124
		}
125
	}
126
127
	/**
128
	 * Destroy (cleanup everything) the client. The client is set to
129
	 * {@code null}.
130
	 */
131
	protected synchronized void destroyClient() {
132 4 1. destroyClient : negated conditional → SURVIVED
2. destroyClient : negated conditional → TIMED_OUT
3. destroyClient : negated conditional → KILLED
4. destroyClient : negated conditional → KILLED
		if (currentClient != null) {
133
			logger.debug("Destroying SMPP client");
134 4 1. destroyClient : removed call to com/cloudhopper/smpp/SmppClient::destroy → SURVIVED
2. destroyClient : removed call to com/cloudhopper/smpp/SmppClient::destroy → NO_COVERAGE
3. destroyClient : removed call to com/cloudhopper/smpp/SmppClient::destroy → TIMED_OUT
4. destroyClient : removed call to com/cloudhopper/smpp/SmppClient::destroy → KILLED
			currentClient.destroy();
135
			currentClient = null;
136
		}
137
	}
138
}

Mutations

68

1.1
Location : initSession
Killed by : oghamcloudhopper.it.PartialConfigurationTest.nothingConfiguredAndLongMessageShouldSendOneLongMessageUsingDefaultEncoding(oghamcloudhopper.it.PartialConfigurationTest)
negated conditional → KILLED

2.2
Location : initSession
Killed by : oghamall.it.sms.SmsSMPPGsm7bitTest.longMessage(oghamall.it.sms.SmsSMPPGsm7bitTest)
negated conditional → KILLED

3.3
Location : initSession
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : initSession
Killed by : none
negated conditional → TIMED_OUT

92

1.1
Location : connect
Killed by : none
replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::connect → NO_COVERAGE

2.2
Location : connect
Killed by : none
replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::connect → TIMED_OUT

3.3
Location : connect
Killed by : oghamcloudhopper.it.PartialConfigurationTest.nothingConfiguredAndLongMessageShouldSendOneLongMessageUsingDefaultEncoding(oghamcloudhopper.it.PartialConfigurationTest)
replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::connect → KILLED

4.4
Location : connect
Killed by : oghamall.it.sms.SmsSMPPGsm7bitTest.longMessage(oghamall.it.sms.SmsSMPPGsm7bitTest)
replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::connect → KILLED

5.5
Location : lambda$connect$0
Killed by : oghamall.it.sms.SmsSMPPGsm7bitTest.longMessage(oghamall.it.sms.SmsSMPPGsm7bitTest)
replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::lambda$connect$0 → KILLED

6.6
Location : lambda$connect$0
Killed by : none
replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::lambda$connect$0 → TIMED_OUT

7.7
Location : lambda$connect$0
Killed by : oghamcloudhopper.it.PartialConfigurationTest.nothingConfiguredAndLongMessageShouldSendOneLongMessageUsingDefaultEncoding(oghamcloudhopper.it.PartialConfigurationTest)
replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::lambda$connect$0 → KILLED

8.8
Location : lambda$connect$0
Killed by : none
replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/BaseSessionHandlingStrategy::lambda$connect$0 → NO_COVERAGE

94

1.1
Location : connect
Killed by : none
removed call to java/lang/Thread::interrupt → NO_COVERAGE

107

1.1
Location : initClient
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : initClient
Killed by : oghamcloudhopper.it.PartialConfigurationTest.splitterEnabledAndAutoGuessEnabledAndGsm7bitEncodingConfiguredAndLongMessageWithUnsupportedCharactersShouldFailIndicatingThatMessageCantBeSplit(oghamcloudhopper.it.PartialConfigurationTest)
negated conditional → KILLED

3.3
Location : initClient
Killed by : none
negated conditional → TIMED_OUT

119

1.1
Location : destroySession
Killed by : none
negated conditional → SURVIVED

2.2
Location : destroySession
Killed by : none
negated conditional → TIMED_OUT

3.3
Location : destroySession
Killed by : oghamcloudhopper.it.PartialConfigurationTest.splitterEnabledAndAutoGuessEnabledAndGsm7bitEncodingConfiguredAndLongMessageWithUnsupportedCharactersShouldFailIndicatingThatMessageCantBeSplit(oghamcloudhopper.it.PartialConfigurationTest)
negated conditional → KILLED

4.4
Location : destroySession
Killed by : oghamall.it.sms.SmsSMPPGsm7bitTest.longMessage(oghamall.it.sms.SmsSMPPGsm7bitTest)
negated conditional → KILLED

121

1.1
Location : destroySession
Killed by : none
removed call to com/cloudhopper/smpp/SmppSession::unbind → SURVIVED

2.2
Location : destroySession
Killed by : none
removed call to com/cloudhopper/smpp/SmppSession::unbind → TIMED_OUT

3.3
Location : destroySession
Killed by : oghamcloudhopper.it.ReuseSessionStrategyTest.reuseSessionButEnquireLinkTimeout(oghamcloudhopper.it.ReuseSessionStrategyTest)
removed call to com/cloudhopper/smpp/SmppSession::unbind → KILLED

4.4
Location : destroySession
Killed by : none
removed call to com/cloudhopper/smpp/SmppSession::unbind → NO_COVERAGE

122

1.1
Location : destroySession
Killed by : none
removed call to com/cloudhopper/smpp/SmppSession::destroy → SURVIVED

2.2
Location : destroySession
Killed by : none
removed call to com/cloudhopper/smpp/SmppSession::destroy → NO_COVERAGE

3.3
Location : destroySession
Killed by : oghamcloudhopper.it.ReuseSessionStrategyTest.reuseSessionButEnquireLinkTimeout(oghamcloudhopper.it.ReuseSessionStrategyTest)
removed call to com/cloudhopper/smpp/SmppSession::destroy → KILLED

4.4
Location : destroySession
Killed by : none
removed call to com/cloudhopper/smpp/SmppSession::destroy → TIMED_OUT

132

1.1
Location : destroyClient
Killed by : oghamcloudhopper.it.PartialConfigurationTest.splitterEnabledAndAutoGuessEnabledAndGsm7bitEncodingConfiguredAndLongMessageWithUnsupportedCharactersShouldFailIndicatingThatMessageCantBeSplit(oghamcloudhopper.it.PartialConfigurationTest)
negated conditional → KILLED

2.2
Location : destroyClient
Killed by : oghamall.it.sms.SmsSMPPGsm7bitTest.longMessage(oghamall.it.sms.SmsSMPPGsm7bitTest)
negated conditional → KILLED

3.3
Location : destroyClient
Killed by : none
negated conditional → TIMED_OUT

4.4
Location : destroyClient
Killed by : none
negated conditional → SURVIVED

134

1.1
Location : destroyClient
Killed by : none
removed call to com/cloudhopper/smpp/SmppClient::destroy → SURVIVED

2.2
Location : destroyClient
Killed by : oghamcloudhopper.it.ReuseSessionStrategyTest.reuseSessionButEnquireLinkTimeout(oghamcloudhopper.it.ReuseSessionStrategyTest)
removed call to com/cloudhopper/smpp/SmppClient::destroy → KILLED

3.3
Location : destroyClient
Killed by : none
removed call to com/cloudhopper/smpp/SmppClient::destroy → NO_COVERAGE

4.4
Location : destroyClient
Killed by : none
removed call to com/cloudhopper/smpp/SmppClient::destroy → TIMED_OUT

Active mutators

Tests examined


Report generated by PIT OGHAM