![JAR search and dependency download from the Maven repository](/logo.png)
org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2009, 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Dave Locke - initial API and implementation and/or initial documentation
*/
package org.eclipse.paho.client.mqttv3.persist;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
import org.eclipse.paho.client.mqttv3.MqttPersistable;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.internal.FileLock;
import org.eclipse.paho.client.mqttv3.internal.MqttPersistentData;
/**
* An implementation of the {@link MqttClientPersistence} interface that provides
* file based persistence.
*
* A directory is specified when the Persistence object is created. When the persistence
* is then opened (see {@link #open(String, String)}), a sub-directory is made beneath the base
* for this client ID and connection key. This allows one persistence base directory
* to be shared by multiple clients.
*
* The sub-directory's name is created from a concatenation of the client ID and connection key
* with any instance of '/', '\\', ':' or ' ' removed.
*/
public class MqttDefaultFilePersistence implements MqttClientPersistence {
private static final String MESSAGE_FILE_EXTENSION = ".msg";
private static final String MESSAGE_BACKUP_FILE_EXTENSION = ".bup";
private static final String LOCK_FILENAME = ".lck";
private File dataDir;
private File clientDir = null;
private FileLock fileLock = null;
private static final FilenameFilter FILE_FILTER = new FilenameFilter() {
public boolean accept(File dir, String name) { return name.endsWith(MESSAGE_FILE_EXTENSION); }
};
public MqttDefaultFilePersistence() { //throws MqttPersistenceException {
this(System.getProperty("user.dir"));
}
/**
* Create an file-based persistent data store within the specified directory.
* @param directory the directory to use.
*/
public MqttDefaultFilePersistence(String directory) { //throws MqttPersistenceException {
dataDir = new File(directory);
}
public void open(String clientId, String theConnection) throws MqttPersistenceException {
if (dataDir.exists() && !dataDir.isDirectory()) {
throw new MqttPersistenceException();
} else if (!dataDir.exists() ) {
if (!dataDir.mkdirs()) {
throw new MqttPersistenceException();
}
}
if (!dataDir.canWrite()) {
throw new MqttPersistenceException();
}
StringBuffer keyBuffer = new StringBuffer();
for (int i=0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy