Search in sources :

Example 1 with FileLock

use of org.openide.filesystems.FileLock in project netbeans-mmd-plugin by raydac.

the class MindMapLink method writeUTF8Text.

public void writeUTF8Text(final String text) throws IOException {
    final FileObject foj = getFile();
    final FileLock flock = lock(foj);
    try {
        final OutputStream out = foj.getOutputStream(flock);
        try {
            IOUtils.write(text, out, "UTF-8");
        } finally {
            IOUtils.closeQuietly(out);
        }
    } finally {
        flock.releaseLock();
    }
    final DataObject doj = DataObject.find(foj);
    if (doj != null && doj instanceof MMDDataObject) {
        LOGGER.info("Notify about change primary file");
        ((MMDDataObject) doj).firePrimaryFileChanged();
    }
}
Also used : MMDDataObject(com.igormaznitsa.nbmindmap.nb.editor.MMDDataObject) DataObject(org.openide.loaders.DataObject) OutputStream(java.io.OutputStream) FileLock(org.openide.filesystems.FileLock) FileObject(org.openide.filesystems.FileObject) MMDDataObject(com.igormaznitsa.nbmindmap.nb.editor.MMDDataObject)

Example 2 with FileLock

use of org.openide.filesystems.FileLock in project gephi by gephi.

the class EdgeListDatabaseManager method doPersist.

private void doPersist() {
    FileLock lock = null;
    ObjectOutputStream ois = null;
    try {
        if (databaseConfigurations != null) {
            databaseConfigurations.delete();
        }
        databaseConfigurations = FileUtil.getConfigRoot().createData("EdgeListDatabase");
        lock = databaseConfigurations.lock();
        ois = new ObjectOutputStream(databaseConfigurations.getOutputStream(lock));
        ois.writeObject(edgeListDatabases);
    } catch (IOException e) {
        Exceptions.printStackTrace(e);
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
            }
        }
        if (lock != null) {
            lock.releaseLock();
        }
    }
}
Also used : FileLock(org.openide.filesystems.FileLock) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 3 with FileLock

use of org.openide.filesystems.FileLock in project gephi by gephi.

the class SaveTask method run.

@Override
public void run() {
    Progress.start(progressTicket);
    Progress.setDisplayName(progressTicket, NbBundle.getMessage(SaveTask.class, "SaveTask.name"));
    File writeFile = null;
    try {
        String tempFileName = file.getName() + "_temp" + System.currentTimeMillis();
        writeFile = new File(file.getParent(), tempFileName);
        FileOutputStream outputStream = null;
        ZipOutputStream zipOut = null;
        BufferedOutputStream bos = null;
        DataOutputStream dos = null;
        try {
            // Stream
            int zipLevel = NbPreferences.forModule(SaveTask.class).getInt(ZIP_LEVEL_PREFERENCE, 9);
            outputStream = new FileOutputStream(writeFile);
            zipOut = new ZipOutputStream(outputStream);
            zipOut.setLevel(zipLevel);
            bos = new BufferedOutputStream(zipOut);
            dos = new DataOutputStream(bos);
            // Providers and workspace
            Collection<WorkspacePersistenceProvider> providers = PersistenceProviderUtils.getPersistenceProviders();
            Workspace[] workspaces = project.getLookup().lookup(WorkspaceProviderImpl.class).getWorkspaces();
            // Setup progress
            Progress.switchToDeterminate(progressTicket, 1 + (1 + providers.size()) * workspaces.length);
            // Write Project
            writeProject(dos, zipOut);
            Progress.progress(progressTicket);
            // Write Workspace files
            for (Workspace ws : workspaces) {
                writeWorkspace(ws, dos, zipOut);
                Progress.progress(progressTicket);
                for (WorkspacePersistenceProvider provider : providers) {
                    if (provider instanceof WorkspaceXMLPersistenceProvider) {
                        writeWorkspaceChildrenXML(ws, (WorkspaceXMLPersistenceProvider) provider, dos, zipOut);
                    } else if (provider instanceof WorkspaceBytesPersistenceProvider) {
                        writeWorkspaceChildrenBytes(ws, (WorkspaceBytesPersistenceProvider) provider, dos, zipOut);
                    }
                    Progress.progress(progressTicket);
                    if (cancel) {
                        break;
                    }
                }
                if (cancel) {
                    break;
                }
            }
            Progress.switchToIndeterminate(progressTicket);
            zipOut.finish();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException ex1) {
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException ex1) {
                }
            }
            if (zipOut != null) {
                try {
                    zipOut.close();
                } catch (IOException ex1) {
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException ex1) {
                }
            }
        }
        Progress.finish(progressTicket);
        // Rename file
        if (!cancel && writeFile.exists()) {
            // Delete original file
            if (file.exists()) {
                file.delete();
            }
            FileObject tempFileObject = FileUtil.toFileObject(writeFile);
            FileLock lock = tempFileObject.lock();
            tempFileObject.rename(lock, getFileNameWithoutExt(file), getFileExtension(file));
            lock.releaseLock();
        }
    } catch (Exception ex) {
        if (ex instanceof GephiFormatException) {
            throw (GephiFormatException) ex;
        }
        throw new GephiFormatException(SaveTask.class, ex);
    } finally {
        if (writeFile != null && writeFile.exists()) {
            FileObject tempFileObject = FileUtil.toFileObject(writeFile);
            try {
                tempFileObject.delete();
            } catch (IOException ex) {
            }
        }
    }
    Progress.finish(progressTicket);
}
Also used : WorkspaceProviderImpl(org.gephi.project.impl.WorkspaceProviderImpl) DataOutputStream(java.io.DataOutputStream) WorkspacePersistenceProvider(org.gephi.project.spi.WorkspacePersistenceProvider) IOException(java.io.IOException) IOException(java.io.IOException) WorkspaceXMLPersistenceProvider(org.gephi.project.spi.WorkspaceXMLPersistenceProvider) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) WorkspaceBytesPersistenceProvider(org.gephi.project.spi.WorkspaceBytesPersistenceProvider) FileLock(org.openide.filesystems.FileLock) FileObject(org.openide.filesystems.FileObject) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Workspace(org.gephi.project.api.Workspace)

Example 4 with FileLock

use of org.openide.filesystems.FileLock in project netbeans-mmd-plugin by raydac.

the class MindMapLink method readUTF8Text.

public String readUTF8Text() throws IOException {
    final FileObject foj = getFile();
    final FileLock flock = lock(foj);
    try {
        return foj.asText("UTF-8");
    } finally {
        flock.releaseLock();
    }
}
Also used : FileLock(org.openide.filesystems.FileLock) FileObject(org.openide.filesystems.FileObject)

Example 5 with FileLock

use of org.openide.filesystems.FileLock in project netbeans-mmd-plugin by raydac.

the class AbstractElement method writeMindMap.

protected static void writeMindMap(final File file, final MindMap map) throws IOException {
    final FileObject fileObject = FileUtil.toFileObject(file);
    FileLock lock = null;
    while (true) {
        try {
            lock = fileObject.lock();
            break;
        } catch (FileAlreadyLockedException ex) {
            delay(500L);
        }
    }
    try {
        final OutputStream out = fileObject.getOutputStream(lock);
        try {
            // NOI18N
            IOUtils.write(map.packToString(), out, "UTF-8");
        } finally {
            IOUtils.closeQuietly(out);
        }
    } finally {
        if (lock != null) {
            lock.releaseLock();
        }
    }
}
Also used : OutputStream(java.io.OutputStream) FileLock(org.openide.filesystems.FileLock) FileObject(org.openide.filesystems.FileObject) FileAlreadyLockedException(org.openide.filesystems.FileAlreadyLockedException)

Aggregations

FileLock (org.openide.filesystems.FileLock)5 FileObject (org.openide.filesystems.FileObject)4 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 MMDDataObject (com.igormaznitsa.nbmindmap.nb.editor.MMDDataObject)1 BufferedOutputStream (java.io.BufferedOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 Workspace (org.gephi.project.api.Workspace)1 WorkspaceProviderImpl (org.gephi.project.impl.WorkspaceProviderImpl)1 WorkspaceBytesPersistenceProvider (org.gephi.project.spi.WorkspaceBytesPersistenceProvider)1 WorkspacePersistenceProvider (org.gephi.project.spi.WorkspacePersistenceProvider)1 WorkspaceXMLPersistenceProvider (org.gephi.project.spi.WorkspaceXMLPersistenceProvider)1 FileAlreadyLockedException (org.openide.filesystems.FileAlreadyLockedException)1 DataObject (org.openide.loaders.DataObject)1