org.openide.filesystems.FileAlreadyLockedException Java Examples

The following examples show how to use org.openide.filesystems.FileAlreadyLockedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LockForFile.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static LockForFile registerLock(LockForFile result) throws IOException, FileAlreadyLockedException {
    File file = result.getFile();
    Namesakes namesakes = new Namesakes();
    Namesakes oldNamesakes = name2Namesakes.putIfAbsent(file.getName(), namesakes);
    if (oldNamesakes != null) { 
        namesakes = oldNamesakes;
    }
    if (namesakes.putInstance(file, result) == null) {
        FileAlreadyLockedException alreadyLockedException = new FileAlreadyLockedException(file.getAbsolutePath());
        LockForFile previousLock = namesakes.getInstance(file);
        // #151576 - check for null although it should not happen
        if (previousLock != null) {
            alreadyLockedException.initCause(previousLock.lockedBy);
        }
        throw alreadyLockedException;
    }
    result.valid = true;
    return result;
}
 
Example #2
Source File: TemplatesPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void revertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_revertButtonActionPerformed
    final Node [] nodes = manager.getSelectedNodes ();
    if (nodes != null && confirmRevert(nodes)) {
        // Are you sure DLG
        rp.post(new Runnable() {
            @Override
            public void run() {
                for (Node node : nodes) {
                    FileObject fo = node.getLookup().lookup(FileObject.class);
                    if (fo != null) {
                        try {
                            fo.revert();
                        } catch (FileAlreadyLockedException falex) {
                            notifyFileLocked(fo);
                            revertButton.setEnabled(true);
                        } catch (IOException ex) {
                            Logger.getLogger(TemplatesPanel.class.getName()).log(Level.WARNING, null, ex);
                        }
                    }
                }
            }
        });
        revertButton.setEnabled(false);
    }
}
 
Example #3
Source File: DataEditorSupportTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void test68015 () throws Exception {
    DES edSupport = support();
    edSupport.open();
    
    waitEQ();
    
    edSupport.desEnv().markModified();
    
    assertTrue(edSupport.messageName().indexOf('*') != -1);
    assertTrue(edSupport.messageHtmlName().indexOf('*') != -1);

    try {
        assertLockFree(fileObject);
        fail("File shall be locked already");
    } catch (FileAlreadyLockedException ex) {
        // OK
    }

}
 
Example #4
Source File: AppClientProxy.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void write(FileObject fo) throws java.io.IOException {
    if (app != null) {
        try {
            org.openide.filesystems.FileLock lock = fo.lock();
            try {
                java.io.OutputStream os = fo.getOutputStream(lock);
                try {
                    writing=true;
                    write(os);
                } finally {
                    os.close();
                }
            } finally {
                lock.releaseLock();
            }
        } catch (FileAlreadyLockedException ex) {
            // trying to use OutputProvider for writing changes
            org.openide.loaders.DataObject dobj = org.openide.loaders.DataObject.find(fo);
            if (dobj!=null && dobj instanceof AppClientProxy.OutputProvider)
                ((AppClientProxy.OutputProvider)dobj).write(this);
            else throw ex;
        }
    }
}
 
Example #5
Source File: RemoteClient.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private FileLock lockFile(FileObject fo) throws IOException, DownloadSkipException {
    try {
        return fo.lock();
    } catch (FileAlreadyLockedException lockedException) {
        if (warnChangedFile(fo)) {
            FileUtils.saveFile(fo);
            // XXX remove once #213141 is fixed
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
            return fo.lock();
        } else {
            throw new DownloadSkipException();
        }
    }
}
 
Example #6
Source File: AssetData.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void saveProperties() throws FileAlreadyLockedException, IOException {
    OutputStream out = null;
    FileLock lock = null;
    try {
        FileObject pFile = file.getPrimaryFile();
        FileObject myFile = FileUtil.findBrother(pFile, extension);
        if (myFile == null) {
            myFile = FileUtil.createData(pFile.getParent(), pFile.getName() + "." + extension);
        }
        lock = myFile.lock();
        out = myFile.getOutputStream(lock);
        store(out, "");
    } finally {
        if (out != null) {
            out.close();
        }
        if (lock != null) {
            lock.releaseLock();
        }
    }
}
 
Example #7
Source File: PHPSamplesWizardIterator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void writeLines(final FileObject readme, final String... lines) throws FileAlreadyLockedException, IOException {
    PrintWriter readmeW = new PrintWriter(FileUtil.toFile(readme), "UTF-8"); // NOI18N
    for (String line : lines) {
        readmeW.println(line);
    }
    readmeW.close();
}
 
Example #8
Source File: XmlMultiViewDataObject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public FileLock lock() throws IOException {
    synchronized (this) {
        FileLock current = getLock();
        if (current != null) {
            throw new FileAlreadyLockedException("File is already locked by [" + current + "]."); // NO18N
        }
        FileLock l = new FileLock();
        lockReference = new WeakReference(l);
        return l;
    }
}
 
Example #9
Source File: Utils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static OutputStream getOutputStream(FileObject fo) throws FileAlreadyLockedException, IOException, InterruptedException {
    int retry = 0;
    while (true) {
        try {
            return fo.getOutputStream();
        } catch (IOException ioe) {
            retry++;
            if (retry > 7) {
                throw ioe;
            }
            Thread.sleep(retry * 30);
        }
    }
}
 
Example #10
Source File: RevertDeletedAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static OutputStream getOutputStream(FileObject fo) throws FileAlreadyLockedException, IOException, InterruptedException {
    int retry = 0;
    while (true) {
        try {
            return fo.getOutputStream();                
        } catch (IOException ioe) {            
            retry++;
            if (retry > 7) {
                throw ioe;
            }
            Thread.sleep(retry * 30);
        } 
    }                    
}
 
Example #11
Source File: AbstractElement.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
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 {
      IOUtils.write(map.packToString(), out, "UTF-8"); //NOI18N
    }
    finally {
      IOUtils.closeQuietly(out);
    }
  }
  finally {
    if (lock != null) {
      lock.releaseLock();
    }
  }
}
 
Example #12
Source File: Controller.java    From netbeans with Apache License 2.0 4 votes vote down vote up
void saveTransaction(Node[] nodes) {

	if(!haveDirectories()) {
	    // PENDING - report the error properly
	    // This should not happen
	    log("Couldn't get the directory"); //NOI18N
	    return;
	}

	Node[] newNodes = new Node[nodes.length];
	TransactionNode mvNode; 
	String id;
	 
	boolean error = false; 

	for(int i=0; i < nodes.length; ++i) {
	    
	    mvNode = (TransactionNode)nodes[i];
	    id = mvNode.getID();
	    
	    if(debug) log(" Saving " + id); //NOI18N 

	    if(currBeans.containsKey(id)) 
		saveBeans.put(id, currBeans.remove(id)); 
	    
	    // Note I didn't load the bean here yet. Will only do that 
	    // if the data is displayed. 
		
	    FileLock lock = null; 
	    try {
		FileObject fold = 
		    currDir.getFileObject(id, "xml"); //NOI18N
		lock = fold.lock();
		fold.copy(saveDir, id, "xml"); //NOI18N
		if(debug) log(fold.getName());
		fold.delete(lock);
		mvNode.setCurrent(false);
		newNodes[i] = mvNode;
	    }
	    catch(FileAlreadyLockedException falex) {
		error = true;
		// PENDING report properly
	    }
	    catch(IOException ioex) {
		error = true;
		// PENDING report properly
	    }
	    catch(Exception ex) {
		error = true; 
		// PENDING report properly
	    }
	    finally { 
		if(lock != null) lock.releaseLock(); 
	    }
	    
	}
	if(!error) currTrans.remove(nodes);
	savedTrans.add(newNodes);
    }
 
Example #13
Source File: Controller.java    From netbeans with Apache License 2.0 4 votes vote down vote up
void deleteDirectory(String dir) {

	if(!haveDirectories()) {
	    // PENDING - report the error property
	    // This should not happen
	    log("Couldn't get the directory"); //NOI18N 
	    return;
	}

	final FileObject directory;
	if(dir.equals(saveDirStr)) {
	    directory = saveDir;
	    savedTrans.remove(savedTrans.getNodes());
	    saveBeans.clear();
	}
	
	else {   
	    directory = currDir;
	    currTrans.remove(currTrans.getNodes());
	    currBeans.clear();
	}
        if (directory.getChildren().length == 0) {
            return;
        }
        final ProgressMonitor progressMonitor = new ProgressMonitor();
        
        RequestProcessor.getDefault().post(new Runnable () {
            public void run() {
                Thread.yield();

                int number = directory.getChildren().length;
                int oldValue = -1;
                int i = 0;
                
                for(Enumeration e = directory.getData(false); e.hasMoreElements(); ++i) {
                    FileObject fo = (FileObject) e.nextElement();
                    FileLock lock = null;
                    try {
                        lock = fo.lock();
                        fo.delete(lock);
                    } catch(FileAlreadyLockedException falex) {
                        Logger.getLogger("global").log(Level.INFO, null, falex);
                    } catch(IOException exception) {
                        Logger.getLogger("global").log(Level.INFO, null, exception);
                    } finally { 
                        if(lock != null) {
                            lock.releaseLock();
                        }
                    }
                    // update the progress monitor if needed
                    final int newValue = 100 * i/number;
                    if (newValue > oldValue) {
                        oldValue = newValue;
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                progressMonitor.setValue(newValue);
                            }
                        });
                    }
                }
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        progressMonitor.close();
                    }
                });
            }
        });
        progressMonitor.setVisible(true);
    }