Skip to content

Commit

Permalink
Fixes issue of saving configuration files if dir does not exists.
Browse files Browse the repository at this point in the history
* Now the dir of configuration files is created if it does not exist
  • Loading branch information
adrianromero committed Aug 3, 2019
1 parent b33b40d commit 1090a7d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
9 changes: 5 additions & 4 deletions src/android/java/com/adr/helloiot/HelloPlatformAndroid.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// HelloIoT is a dashboard creator for MQTT
// Copyright (C) 2017-2018 Adrián Romero Corchado.
// Copyright (C) 2017-2019 Adrián Romero Corchado.
//
// This file is part of HelloIot.
//
Expand All @@ -23,6 +23,7 @@
import javafxports.android.FXActivity;
import com.adr.helloiot.scripting.Rhino;
import com.adr.helloiot.scripting.Script;
import java.io.IOException;
import java.io.File;
/**
*
Expand All @@ -32,7 +33,7 @@ public class HelloPlatformAndroid extends HelloPlatform {

private final FXActivity context;

public HelloPlatformAndroid(){
public HelloPlatformAndroid() {
context = FXActivity.getInstance();
}

Expand All @@ -42,7 +43,7 @@ public String getHome() {
}

@Override
public File getFile(String fileName){
public File getFile(String fileName) throws IOException {
return new File(context.getFilesDir(), fileName);
}

Expand All @@ -62,5 +63,5 @@ public void keepON() {
Window window = context.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
});
}
}
}
6 changes: 1 addition & 5 deletions src/main/java/com/adr/helloiot/HelloPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;

/**
*
* @author adrian
*/
public abstract class HelloPlatform {

private final static String APP_PROPERTIES = "app.properties";
Expand Down Expand Up @@ -120,7 +116,7 @@ public void setProperty(String key, String value) {

public abstract String getHome();

public abstract File getFile(String file);
public abstract File getFile(String file) throws IOException;

public abstract boolean isFullScreen();

Expand Down
26 changes: 15 additions & 11 deletions src/main/java/com/adr/helloiot/HelloPlatformDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,29 @@
import com.adr.helloiot.scripting.Nashorn;
import com.adr.helloiot.scripting.Script;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
*
* @author adrian
*/
public class HelloPlatformDesktop extends HelloPlatform {

@Override
public String getHome() {
return System.getProperty("HELLOIOT_HOME");
}

@Override
public File getFile(String file) {
public File getFile(String file) throws IOException {
String home = getHome();
if (home == null || home.isEmpty()) {
home = System.getProperty("user.home") + "/.helloiot";
}
return new File(home, file);
String homepath = (home == null || home.isEmpty())
? System.getProperty("user.home")
: home;
Path root = Paths.get(homepath, ".helloiot");

Files.createDirectories(root);

return root.resolve(file).toFile();
}

@Override
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/adr/helloiot/MainManagerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public class MainManagerClient implements MainManager {
private ClientLoginNode clientlogin = null;
private ConnectUI[] connectuis = null;

private File configfile;
private StackPane root = null;

public MainManagerClient() {
Expand All @@ -76,9 +75,9 @@ private void showLogin() {

ConfigProperties configprops = new ConfigProperties();
try {
configprops.load(() -> new FileInputStream(configfile));
configprops.load(() -> new FileInputStream(getPropsFile()));
} catch (IOException ex) {
LOGGER.log(Level.WARNING, String.format("Using defaults. Properties file not found: %s.", configfile));
LOGGER.log(Level.WARNING, String.format("Using defaults. Properties file not found: %s.", CONFIG_PROPERTIES));
}

connectuis = new ConnectUI[bridgeconfigs.length];
Expand Down Expand Up @@ -163,7 +162,7 @@ private void hideLogin() {
}

try {
configprops.save(() -> new FileOutputStream(configfile));
configprops.save(() -> new FileOutputStream(getPropsFile()));
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Cannot save configuration properties.", ex);
}
Expand All @@ -180,10 +179,10 @@ private void showApplication() throws HelloIoTException {

ConfigProperties configprops = new ConfigProperties();
try {
configprops.load(() -> new FileInputStream(configfile));
configprops.load(() -> new FileInputStream(getPropsFile()));
} catch (IOException ex) {
// No properties file found, then use defaults and continue
LOGGER.log(Level.WARNING, () -> String.format("Using defaults. Properties file not found: %s.", configfile));
LOGGER.log(Level.WARNING, () -> String.format("Using defaults. Properties file not found: %s.", CONFIG_PROPERTIES));
}

VarProperties config = new VarProperties();
Expand Down Expand Up @@ -257,8 +256,6 @@ private void hideApplication() {
@Override
public void construct(StackPane root, Parameters params) {
this.root = root;

configfile = HelloPlatform.getInstance().getFile(CONFIG_PROPERTIES);

boolean status = Boolean.parseBoolean(HelloPlatform.getInstance().getProperty("window.status", "false"));
if (status) {
Expand All @@ -279,4 +276,8 @@ public void destroy() {
hideLogin();
hideApplication();
}

private File getPropsFile() throws IOException {
return HelloPlatform.getInstance().getFile(CONFIG_PROPERTIES);
}
}
22 changes: 12 additions & 10 deletions src/main/java/com/adr/helloiot/local/ManagerLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author adrian
*/
public class ManagerLocal implements ManagerProtocol {

private final static Logger logger = Logger.getLogger(ManagerLocal.class.getName());
Expand Down Expand Up @@ -122,11 +118,10 @@ public void publish(EventMessage message) {
@SuppressWarnings("unchecked")
private void readMapClient() {
mapClient = null;
File dbfile = HelloPlatform.getInstance().getFile("localmsg-" + fileid + ".map");
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(dbfile))) {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(getDBFile()))) {
mapClient = (ConcurrentMap<String, byte[]>) in.readObject();
} catch (IOException | ClassNotFoundException ex) {
logger.log(Level.WARNING, String.format("Creating map. Local map file not found: %s.", dbfile));
logger.log(Level.WARNING, String.format("Creating map. Local map file not found: %s.", getDBFileName()));
}

if (mapClient == null) {
Expand All @@ -143,9 +138,16 @@ private void readMapClient() {
}

private void writeMapClient() throws IOException {
File dbfile = HelloPlatform.getInstance().getFile("localmsg-" + fileid + ".map");
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dbfile))) {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(getDBFile()))) {
out.writeObject(mapClient);
}
}
}

private String getDBFileName() {
return "localmsg-" + fileid + ".map";
}

private File getDBFile() throws IOException{
return HelloPlatform.getInstance().getFile(getDBFileName());
}
}

0 comments on commit 1090a7d

Please sign in to comment.