jeudi 3 septembre 2015

How do I protect files stored in a java object from suffering changes when the original local file is changed?

I have a java application that receives files, sends them to a database and loads them back.

Since this is a distributed application, users in different machines interact in this way: uploading and downloading files.

These are the methods I'm using:

@FXML
    private void uploadFile(ActionEvent event) throws ClassNotFoundException, IOException, SQLException {

        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Resource File");
        File file = fileChooser.showOpenDialog(mainStage).getCanonicalFile();
        File file2 = file;

        file2.setWritable(false);
        file2.setReadOnly();        

        FileUtils.copyFileToDirectory(file2, FileUtils.getFile(super.getPath(file2.getName())), true); //This is my attempt to copy the original local file to the application folder and perform changes in this file instead of the original file. Nevertheless, the application is still looking for the local file.

        shl.getCurrentTab().addFile("File-"+file2.getName(), file2);

        super.saveShell();
        super.saveLog(shl.getCurrentUser().getID() + " - " + shl.getCurrentUser().getName() +
                " - " + "UPLOADED "+file2.getName());


        System.out.println("File uploaded as "+file2.toString());
    }

public void saveShell() throws IOException, ClassNotFoundException {

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                new FileOutputStream(getPath("shell.txt")));

        objectOutputStream.writeObject(new Date());
        objectOutputStream.writeBoolean(true);
        objectOutputStream.writeFloat(1.0f);

        objectOutputStream.writeObject(shl);
        objectOutputStream.flush();
        objectOutputStream.close();
        System.out.println("Successfully saved");

        saveShellDB();  
    }

public void saveShellDB() throws ClassNotFoundException, IOException {

        Class.forName(classForName);
        Connection connection = null;

        try
        {
          // create a database connection
          connection = DriverManager.getConnection(connectionPath);
          Statement statement = connection.createStatement();
          statement.setQueryTimeout(30);  // set timeout to 30 sec.

          File file = new File("shell.txt");
          FileInputStream fis = new FileInputStream(file);

          PreparedStatement ps = connection.prepareStatement("INSERT OR REPLACE INTO shell (name,shl) VALUES (?,?)");   
          ps.setString(1, file.getName());
          ps.setBinaryStream(2, fis, (int)file.length());
          ps.executeUpdate();

          ps.close();
          fis.close();

          System.out.println("SQL save done");

        } catch(SQLException e) {

          // if the error message is "out of memory", 
          // it probably means no database file is found
          System.err.println(e.getMessage());
        }

        finally
        {
          try
          {
            if(connection != null)
              connection.close();

          } catch(SQLException e) {
            // connection close failed.
            System.err.println(e);
          }
        }

      }

When opening the file:

@FXML
    public void refreshFileList() throws ClassNotFoundException, SQLException, IOException {  

    //this is the listView object that shows the current files

        fileListView.setItems(FXCollections.observableArrayList(shl.getCurrentTab().fileMapToSet()));
        fileListView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        fileListView.setEditable(true);

        fileListView.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                currentFile = (File) fileListView.getSelectionModel().getSelectedItem();
                System.out.println("clicked on " + fileListView.getSelectionModel().getSelectedItem());

            }
        });
    }

@FXML
private void openFile(ActionEvent event) throws IOException {

    currentFile.setReadOnly();

    Desktop dt = Desktop.getDesktop();
    dt.open(currentFile);

}

private Map<String,File> _files = new HashMap<String,File>();

public Set<File> fileMapToSet() {
    this._fileSet = null;
    this._fileSet = new HashSet<File>();

    for(String key : _files.keySet()) {
        this._fileSet.add(_files.get(key));
    }

    return this._fileSet;
}

I upload a file but if afterwards I change my local file, it is somehow updated in the java object, dinamically. I need a way to protect local files from this kind of connection since files should only be updated if the user chooses to upload a new version.

Perhaps it's because de File name is usually the path of the file, like C:\Users\Test\Book.xls, using FileUtils.

Aucun commentaire:

Enregistrer un commentaire