Skip to content

Commit b62f929

Browse files
committed
Issue #522 - If MemoryPersistence has not been opened, do not throw an exception when closing.
Signed-off-by: James Sutton <james.sutton@uk.ibm.com>
1 parent fd1a501 commit b62f929

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/persist/MemoryPersistence.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,68 +26,86 @@
2626
* Persistence that uses memory
2727
*
2828
* In cases where reliability is not required across client or device
29-
* restarts memory this memory peristence can be used. In cases where
29+
* restarts memory this memory persistence can be used. In cases where
3030
* reliability is required like when clean session is set to false
3131
* then a non-volatile form of persistence should be used.
3232
*
3333
*/
3434
public class MemoryPersistence implements MqttClientPersistence {
3535

36-
private Hashtable data;
36+
private Hashtable<String, MqttPersistable> data;
3737

3838
/* (non-Javadoc)
3939
* @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#close()
4040
*/
4141
public void close() throws MqttPersistenceException {
42-
data.clear();
42+
if (data != null) {
43+
data.clear();
44+
}
4345
}
4446

4547
/* (non-Javadoc)
4648
* @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#keys()
4749
*/
48-
public Enumeration keys() throws MqttPersistenceException {
50+
public Enumeration<String> keys() throws MqttPersistenceException {
51+
checkIsOpen();
4952
return data.keys();
5053
}
5154

5255
/* (non-Javadoc)
5356
* @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#get(java.lang.String)
5457
*/
5558
public MqttPersistable get(String key) throws MqttPersistenceException {
59+
checkIsOpen();
5660
return (MqttPersistable)data.get(key);
5761
}
5862

5963
/* (non-Javadoc)
6064
* @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#open(java.lang.String, java.lang.String)
6165
*/
6266
public void open(String clientId, String serverURI) throws MqttPersistenceException {
63-
this.data = new Hashtable();
67+
this.data = new Hashtable<String, MqttPersistable>();
6468
}
6569

6670
/* (non-Javadoc)
67-
* @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#put(java.lang.String, org.eclipse.paho.client.mqttv3.MqttPersistable)
71+
* @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#put(java.lang.String, org.eclipse.paho.mqttv5.client.MqttPersistable)
6872
*/
6973
public void put(String key, MqttPersistable persistable) throws MqttPersistenceException {
74+
checkIsOpen();
7075
data.put(key, persistable);
7176
}
7277

7378
/* (non-Javadoc)
7479
* @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#remove(java.lang.String)
7580
*/
7681
public void remove(String key) throws MqttPersistenceException {
82+
checkIsOpen();
7783
data.remove(key);
7884
}
7985

8086
/* (non-Javadoc)
8187
* @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#clear()
8288
*/
8389
public void clear() throws MqttPersistenceException {
90+
checkIsOpen();
8491
data.clear();
8592
}
8693

8794
/* (non-Javadoc)
8895
* @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#containsKey(java.lang.String)
8996
*/
9097
public boolean containsKey(String key) throws MqttPersistenceException {
98+
checkIsOpen();
9199
return data.containsKey(key);
92100
}
101+
102+
/**
103+
* Checks whether the persistence has been opened.
104+
* @throws MqttPersistenceException if the persistence has not been opened.
105+
*/
106+
private void checkIsOpen() throws MqttPersistenceException {
107+
if (data == null) {
108+
throw new MqttPersistenceException();
109+
}
110+
}
93111
}

org.eclipse.paho.mqttv5.client/src/main/java/org/eclipse/paho/mqttv5/client/persist/MemoryPersistence.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* Persistence that uses memory
2727
*
2828
* In cases where reliability is not required across client or device
29-
* restarts memory this memory peristence can be used. In cases where
29+
* restarts memory this memory persistence can be used. In cases where
3030
* reliability is required like when clean session is set to false
3131
* then a non-volatile form of persistence should be used.
3232
*
@@ -39,8 +39,9 @@ public class MemoryPersistence implements MqttClientPersistence {
3939
* @see org.eclipse.paho.mqttv5.client.MqttClientPersistence#close()
4040
*/
4141
public void close() throws MqttPersistenceException {
42-
checkIsOpen();
43-
data.clear();
42+
if (data != null) {
43+
data.clear();
44+
}
4445
}
4546

4647
/* (non-Javadoc)

0 commit comments

Comments
 (0)