From 675bd291b84bff0dbe1c012bab52ac568ed6e7f8 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 14 Sep 2017 19:56:02 +0300 Subject: [PATCH] Replace File by Path --- .../inr/numass/data/SetDirectionUtility.java | 76 ------------------- .../actions/SubstractSpectrumAction.java | 4 +- .../java/inr/numass/utils/OldDataReader.java | 20 ++--- .../java/inr/numass/server/ServerRunner.java | 7 +- 4 files changed, 16 insertions(+), 91 deletions(-) delete mode 100644 numass-core/src/main/java/inr/numass/data/SetDirectionUtility.java diff --git a/numass-core/src/main/java/inr/numass/data/SetDirectionUtility.java b/numass-core/src/main/java/inr/numass/data/SetDirectionUtility.java deleted file mode 100644 index a3de48cd..00000000 --- a/numass-core/src/main/java/inr/numass/data/SetDirectionUtility.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package inr.numass.data; - -import hep.dataforge.context.Context; -import hep.dataforge.context.Global; - -import java.io.*; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; - -/** - * A temporary utility to store set directions to avoid multiple file reading - * - * @author Alexander Nozik - */ -@Deprecated -public class SetDirectionUtility { - - private static final String FILE_NAME = "numass_set_direction.map"; - - private static final Map directionMap = new HashMap<>(); - - private static boolean isLoaded = false; - - static synchronized boolean isReversed(String setName, Function provider) { - if (!isLoaded) { - load(Global.instance()); - } - return directionMap.computeIfAbsent(setName, provider); - } - - public static File cacheFile(Context context) { - return new File(context.io().getTmpDirectory(), FILE_NAME); - } - - @SuppressWarnings("unchecked") - public static synchronized void load(Context context) { - context.getLogger().info("Loading set direction utility"); - File file = cacheFile(context); - if (file.exists()) { - directionMap.clear(); - try (FileInputStream fst = new FileInputStream(file)) { - try (ObjectInputStream st = new ObjectInputStream(fst)) { - directionMap.putAll((Map) st.readObject()); - context.getLogger().info("Set directions successfully loaded from file"); - } catch (ClassNotFoundException | IOException ex) { - context.getLogger().error("Failed to load numass direction mapping", ex); - } - } catch (IOException ex) { - context.getLogger().error("Failed to load numass direction mapping", ex); - } - } - - isLoaded = true; - } - - public static synchronized void save(Context context) { - try { - File file = cacheFile(context); - if (!file.exists()) { - file.createNewFile(); - } - try (ObjectOutputStream st = new ObjectOutputStream(new FileOutputStream(file))) { - st.writeObject(directionMap); - context.getLogger().info("Set directions successfully saved to file"); - } - } catch (IOException ex) { - context.getLogger().error("Failed to save numass direction mapping", ex); - } - } -} diff --git a/numass-main/src/main/java/inr/numass/actions/SubstractSpectrumAction.java b/numass-main/src/main/java/inr/numass/actions/SubstractSpectrumAction.java index dfb04c47..799c8dc4 100644 --- a/numass-main/src/main/java/inr/numass/actions/SubstractSpectrumAction.java +++ b/numass-main/src/main/java/inr/numass/actions/SubstractSpectrumAction.java @@ -16,8 +16,8 @@ import hep.dataforge.tables.ValueMap; import hep.dataforge.values.Values; import inr.numass.utils.NumassUtils; -import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.Optional; /** @@ -30,7 +30,7 @@ public class SubstractSpectrumAction extends OneToOneAction { protected Table execute(Context context, String name, Table input, Laminate inputMeta) { try { String referencePath = inputMeta. getString("file", "empty.dat"); - File referenceFile = context.io().getFile(referencePath); + Path referenceFile = context.io().getFile(referencePath); Table referenceTable = new ColumnedDataReader(referenceFile).toTable(); ListTable.Builder builder = new ListTable.Builder(input.getFormat()); input.getRows().forEach(point -> { diff --git a/numass-main/src/main/java/inr/numass/utils/OldDataReader.java b/numass-main/src/main/java/inr/numass/utils/OldDataReader.java index 73899ced..e62ce393 100644 --- a/numass-main/src/main/java/inr/numass/utils/OldDataReader.java +++ b/numass-main/src/main/java/inr/numass/utils/OldDataReader.java @@ -22,8 +22,8 @@ import hep.dataforge.tables.ValueMap; import hep.dataforge.values.Values; import inr.numass.data.SpectrumDataAdapter; -import java.io.File; -import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Path; import java.util.Locale; import java.util.Scanner; @@ -35,10 +35,10 @@ import static java.util.Locale.setDefault; */ public class OldDataReader { - public static Table readConfig(String path) throws FileNotFoundException { + public static Table readConfig(String path) throws IOException { String[] list = {"X", "time", "ushift"}; ListTable.Builder res = new ListTable.Builder(list); - File file = Global.instance().io().getFile(path); + Path file = Global.instance().io().getFile(path); Scanner sc = new Scanner(file); sc.nextLine(); @@ -60,7 +60,7 @@ public class OldDataReader { public static Table readData(String path, double Elow) { SpectrumDataAdapter factory = new SpectrumDataAdapter(); ListTable.Builder res = new ListTable.Builder(factory.getFormat()); - File file = Global.instance().io().getFile(path); + Path file = Global.instance().io().getFile(path); double x; int count; int time; @@ -70,7 +70,7 @@ public class OldDataReader { Scanner sc; try { sc = new Scanner(file); - } catch (FileNotFoundException ex) { + } catch (IOException ex) { throw new RuntimeException(ex.getMessage()); } double dummy; @@ -112,7 +112,7 @@ public class OldDataReader { public static Table readDataAsGun(String path, double Elow) { SpectrumDataAdapter factory = new SpectrumDataAdapter(); ListTable.Builder res = new ListTable.Builder(factory.getFormat()); - File file = Global.instance().io().getFile(path); + Path file = Global.instance().io().getFile(path); double x; long count; int time; @@ -122,7 +122,7 @@ public class OldDataReader { Scanner sc; try { sc = new Scanner(file); - } catch (FileNotFoundException ex) { + } catch (IOException ex) { throw new RuntimeException(ex.getMessage()); } double dummy; @@ -145,7 +145,7 @@ public class OldDataReader { public static Table readSpectrumData(String path) { SpectrumDataAdapter factory = new SpectrumDataAdapter(); ListTable.Builder res = new ListTable.Builder(factory.getFormat()); - File file = Global.instance().io().getFile(path); + Path file = Global.instance().io().getFile(path); double x; double count; double time; @@ -158,7 +158,7 @@ public class OldDataReader { Scanner sc; try { sc = new Scanner(file); - } catch (FileNotFoundException ex) { + } catch (IOException ex) { throw new RuntimeException(ex.getMessage()); } diff --git a/numass-server/src/main/java/inr/numass/server/ServerRunner.java b/numass-server/src/main/java/inr/numass/server/ServerRunner.java index bfe47e45..4f95811f 100644 --- a/numass-server/src/main/java/inr/numass/server/ServerRunner.java +++ b/numass-server/src/main/java/inr/numass/server/ServerRunner.java @@ -8,8 +8,9 @@ import hep.dataforge.meta.SimpleConfigurable; import inr.numass.data.storage.NumassStorage; import org.slf4j.LoggerFactory; -import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.text.ParseException; /** @@ -27,8 +28,8 @@ public class ServerRunner extends SimpleConfigurable implements AutoCloseable { public ServerRunner() throws IOException, ParseException { // Global.instance().pluginManager().load(StorageManager.class); - File configFile = new File(SERVER_CONFIG_PATH); - if (configFile.exists()) { + Path configFile = context.io().getFile(SERVER_CONFIG_PATH); + if (Files.exists(configFile)) { context.getLogger().info("Trying to read server configuration from {}", SERVER_CONFIG_PATH); configure(MetaFileReader.read(configFile)); }