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();
}
}
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();
}
}
}
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);
}
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();
}
}
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();
}
}
}
Aggregations