Java Code Examples for org.openide.filesystems.FileLock#releaseLock()

The following examples show how to use org.openide.filesystems.FileLock#releaseLock() . 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: ArtifactCopyOnSaveSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void copy(FileObject sourceFile, FileObject destFile) throws IOException {
    InputStream is = null;
    OutputStream os = null;
    FileLock fl = null;
    try {
        is = sourceFile.getInputStream();
        fl = destFile.lock();
        os = destFile.getOutputStream(fl);
        FileUtil.copy(is, os);
    } finally {
        if (is != null) {
            is.close();
        }
        if (os != null) {
            os.close();
        }
        if (fl != null) {
            fl.releaseLock();
        }
    }
}
 
Example 2
Source File: CordovaSampleIterator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void unZipFile(InputStream source, FileObject rootFolder) throws IOException {
    try {
        ZipInputStream str = new ZipInputStream(source);
        ZipEntry entry;
        while ((entry = str.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                FileUtil.createFolder(rootFolder, entry.getName());
                continue;
            }
            FileObject fo = FileUtil.createData(rootFolder, entry.getName());
            FileLock lock = fo.lock();
            try {
                OutputStream out = fo.getOutputStream(lock);
                try {
                    FileUtil.copy(str, out);
                } finally {
                    out.close();
                }
            } finally {
                lock.releaseLock();
            }
        }
    } finally {
        source.close();
    }
}
 
Example 3
Source File: BaseFileObjectTestHid.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testCaseSensitiveFolderRename() throws Exception {
    FileObject parent = root.getFileObject("testdir/mountdir10");
    List<FileObject> arr = Arrays.asList(parent.getChildren());
    final String up = parent.getName().toUpperCase();
    FileLock lock = parent.lock();
    try {
        parent.rename(lock, up, null);
    } finally {
        lock.releaseLock();
    }
    assertEquals("Capital name", up, parent.getNameExt());
    File real = FileUtil.toFile(parent);
    assertNotNull("Real file exists", real);
    assertEquals("It is capitalized too", up, real.getName());
    
    List<FileObject> now = Arrays.asList(parent.getChildren());
    assertEquals("Same children: ", arr, now);
}
 
Example 4
Source File: ProjectJAXWSClientSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** folder where xml client artifacts should be saved, e.g. WEB-INF/wsdl/client/SampleClient
 */
protected FileObject getWsdlFolderForClient(String name) throws IOException {
    FileObject globalWsdlFolder = getWsdlFolder(true);
    FileObject oldWsdlFolder = globalWsdlFolder.getFileObject("client/"+name); //NOI18N
    if (oldWsdlFolder!=null) {
        FileLock lock = oldWsdlFolder.lock();
        try {
            oldWsdlFolder.delete(lock);
        } finally {
            lock.releaseLock();
        }
    }
    FileObject clientWsdlFolder = globalWsdlFolder.getFileObject("client"); //NOI18N
    if (clientWsdlFolder==null) clientWsdlFolder = globalWsdlFolder.createFolder("client"); //NOI18N
    return clientWsdlFolder.createFolder(name);
}
 
Example 5
Source File: AbstractSourceFileObject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public final boolean delete() {
    if (isDirty() != null) {
        //If the file is modified in editor do not delete it
        return false;
    } else {
        final FileObject file = handle.resolveFileObject(false);
        if (file == null) {
            return false;
        }
        try {
            FileLock lock = file.lock();
            try {
                file.delete (lock);
                return true;
            }
            finally {
                lock.releaseLock();
            }
        } catch (IOException e) {
            return false;
        }
    }
}
 
Example 6
Source File: RemoveWritablesTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testRenamedFile() throws Exception {
    FileObject existingFile = FileUtil.getConfigFile( "foo/test1" );
    
    assertNotNull( existingFile );
    
    FileLock lock = existingFile.lock();
    existingFile.rename( lock, "newName", "newExt" );
    lock.releaseLock();
    
    assertNotNull( FileUtil.getConfigFile( "foo/newName.newExt" ) );
    
    File maskFile = new File( new File( configDir, "foo"), "test1_hidden" );
    assertTrue( maskFile.exists() );
    
    FileObject newFile = FileUtil.getConfigFile("foo");
    assertTrue(newFile.canRevert());
    newFile.revert();
    
    assertFalse( "local file removed", maskFile.exists() );
    assertNotNull( "FileObject exists again", FileUtil.getConfigFile( "foo/test1" ) );
    assertNull( "renamed file is gone", FileUtil.getConfigFile( "foo/newName.newExt" ) );
}
 
Example 7
Source File: J2SEProjectFactory.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new application manifest file with minimal initial contents.
 * @param dir the directory to create it in
 * @throws IOException in case of problems
 */
private static void createManifest(final FileObject dir, final String manifestFile) throws IOException {

    FileObject manifest = dir.createData(manifestFile);
    FileLock lock = manifest.lock();
    try {
        OutputStream os = manifest.getOutputStream(lock);
        try {
            PrintWriter pw = new PrintWriter(os);
            pw.println("Manifest-Version: 1.0"); // NOI18N
            pw.println("X-COMMENT: Main-Class will be added automatically by build"); // NOI18N
            pw.println(); // safest to end in \n\n due to JRE parsing bug
            pw.flush();
        } finally {
            os.close();
        }
    } finally {
        lock.releaseLock();
    }

}
 
Example 8
Source File: PersistenceManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Deletes specified file object */
public static void deleteOneFO (FileObject fo) {
    FileLock lock = null;
    if (fo.isValid()) {
        try {
            lock = fo.lock();
            fo.delete(lock);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            if (lock != null) {
                lock.releaseLock();
            }
        }
    }
}
 
Example 9
Source File: MindMapLink.java    From netbeans-mmd-plugin with Apache License 2.0 6 votes vote down vote up
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 instanceof MMDDataObject) {
    LOGGER.info("Notify about change primary file");
    ((MMDDataObject) doj).firePrimaryFileChanged();
  }
}
 
Example 10
Source File: Deadlock73332Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testDeadLock() throws Exception {
    assertNotNull(folder);
    assertTrue(folder instanceof BaseFileObj);
    FileObject data = FileUtil.createData(folder, "/a/b/c/data.txt");
    assertNotNull(data);
    FileLock lock = data.lock();
    try {
        data.move(lock,folder, data.getName(), data.getExt());
    } finally {
        lock.releaseLock();
    }
}
 
Example 11
Source File: MasterDetailGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the content of the file.
 *
 * @param file file whose content should be written.
 * @param content new content of the file.
 * @throws IOException when the writing fails.
 */
private static void write(FileObject file, String content, String encoding) throws IOException {
    FileLock lock = file.lock();
    try {
        OutputStream os = file.getOutputStream(lock);
        os.write(content.getBytes(encoding));
        os.close();
    } finally {
        lock.releaseLock();
    }
}
 
Example 12
Source File: ToolbarPoolDeadlockTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Object writeInstance (final FileObject folder, final String name, final Object inst) throws IOException {
    class W implements FileSystem.AtomicAction {
        public Object create;
        
        public void run () throws IOException {
            FileObject fo = FileUtil.createData (folder, name);
            FileLock lock = fo.lock ();
            ObjectOutputStream oos = new ObjectOutputStream (fo.getOutputStream (lock));
            oos.writeObject (inst);
            oos.close ();
            lock.releaseLock ();
            
            DataObject obj = DataObject.find (fo);
            InstanceCookie ic = obj.getCookie(InstanceCookie.class);
            
            assertNotNull ("Cookie created", ic);
            try {
                create = ic.instanceCreate ();
                assertEquals ("The same instance class", inst.getClass(), create.getClass ());
            } catch (ClassNotFoundException ex) {
                fail (ex.getMessage ());
            }
        }
    }
    W w = new W ();
    folder.getFileSystem ().runAtomicAction (w);
    return w.create;
}
 
Example 13
Source File: TestUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void storeProjectProperties(FileObject projectDir, EditableProperties props) throws IOException {
    FileObject propsFO = projectDir.getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH);
    FileLock lock = propsFO.lock();
    try {
        OutputStream os = propsFO.getOutputStream(lock);
        try {
            props.store(os);
        } finally {
            os.close();
        }
    } finally {
        lock.releaseLock();
    }
}
 
Example 14
Source File: OQLSupport.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public static void saveModel(OQLTreeModel model) {
    FileLock lock = null;
    try {
        Properties p = modelToProperties(model);
        if (!p.isEmpty()) ProfilerStorage.saveGlobalProperties(p, SAVED_OQL_QUERIES_FILENAME);
    } catch (Exception e) {
        ProfilerLogger.log(e);
    } finally {
        if (lock != null) lock.releaseLock();
    }
}
 
Example 15
Source File: JavadocAndSourceRootDetectionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static FileObject createZipFile (
        @NonNull final FileObject folder,
        @NonNull final String name,
        @NonNull final String path,
        @NonNull final String content) throws IOException {
    final FileObject zipRoot1 = folder.createData(name);
    final FileLock lock = zipRoot1.lock();
    try {
        final ZipOutputStream zout = new ZipOutputStream(zipRoot1.getOutputStream(lock));  //NOI18N            
        try {
            final String[] pathElements = path.split("/");  //NOI18N
            final StringBuilder currentPath = new StringBuilder();
            for (String element : pathElements) {
                if (currentPath.length() > 0) {
                    currentPath.append('/');                //NOI18N
                }
                currentPath.append(element);
                zout.putNextEntry(new ZipEntry(currentPath.toString()));
            }
            zout.write(content.getBytes(Charset.defaultCharset()));
        } finally {
            zout.close();
        }
    } finally {
        lock.releaseLock();
    }
    return FileUtil.getArchiveRoot(zipRoot1);
}
 
Example 16
Source File: FolderLookupTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testFindInstanceNotCreatedByYouIssue24986 () throws Exception {
    String fsstruct [] = new String [] {
        "AA/",
    };
    TestUtilHid.destroyLocalFileSystem (getName());
    FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);

    FileObject bb = lfs.findResource("/AA");

    String inst = "My instnace";

    DataFolder folder = DataFolder.findFolder (bb);
    FileObject fo = FileUtil.createData (folder.getPrimaryFile (), "test.ser");
    FileLock lock = fo.lock ();
    ObjectOutputStream oss = new ObjectOutputStream (fo.getOutputStream (lock));
    oss.writeObject (inst);
    oss.close ();
    lock.releaseLock ();
    DataObject o = DataObject.find (fo);
    assertTrue ("Is IDO: " + o, o instanceof InstanceDataObject);
    InstanceDataObject obj = (InstanceDataObject)o;

    assertEquals ("The instance is created", inst, obj.instanceCreate ());
    assertNotSame ("But is not the same", inst, obj.instanceCreate ());
    inst = (String)obj.instanceCreate ();

    Lookup lookup = new org.openide.loaders.FolderLookup (folder).getLookup ();

    Lookup.Template t = new Lookup.Template (null, null, inst);
    Collection found = lookup.lookup (t).allInstances ();

    assertEquals ("Lookup finds it as well", 1, found.size ());
    assertEquals ("Lookup finds it as well", inst, found.iterator ().next());
}
 
Example 17
Source File: WSUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void addJaxWsEntries(FileObject ddFolder, JAXWSLightSupport jaxWsSupport)
        throws IOException {

    generateSunJaxwsFile(ddFolder);
    FileObject sunjaxwsFile = ddFolder.getFileObject("sun-jaxws.xml"); //NOI18N
    Endpoints endpoints = EndpointsProvider.getDefault().getEndpoints(sunjaxwsFile);
    for (JaxWsService service: jaxWsSupport.getServices()) {
        if (service.isServiceProvider()) {
            addService(endpoints, service);
        }
    }
    FileLock lock = null;
    OutputStream os = null;
    synchronized (sunjaxwsFile) {
        try {
            lock = sunjaxwsFile.lock();
            os = sunjaxwsFile.getOutputStream(lock);
            endpoints.write(os);
        } finally{
            if (lock != null)
                lock.releaseLock();

            if(os != null)
                os.close();
        }
    }
}
 
Example 18
Source File: JaxWsChildren.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private FileObject getWsdlFolderForService(JAXWSSupport support, String name) throws IOException {
    FileObject globalWsdlFolder = support.getWsdlFolder(true);
    FileObject oldWsdlFolder = globalWsdlFolder.getFileObject(name);
    if (oldWsdlFolder!=null) {
        FileLock lock = oldWsdlFolder.lock();
        try {
            oldWsdlFolder.delete(lock);
        } finally {
            lock.releaseLock();
        }
    }
    return globalWsdlFolder.createFolder(name);
}
 
Example 19
Source File: WebProjectJAXWSSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Removes the servlet entry from web.xml and
 * the endpoint entry from the sun-jaxws.xml file
 */
@Override
public void removeNonJsr109Entries(String serviceName) throws IOException {
    //delete web.xml entry
    removeServiceEntriesFromDD(serviceName);
    //delete entry in sun-jaxws.xml file.
    FileObject ddFolder = getDeploymentDescriptorFolder();
    if(ddFolder != null){
        FileObject sunjaxwsFile = ddFolder.getFileObject("sun-jaxws.xml");
        if(sunjaxwsFile != null){
            FileLock lock = null;
            //if there are no more services, delete the file
            JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class);
            if(jaxWsModel.getServices().length == 0) {
                synchronized(this) {
                    try{
                        lock = sunjaxwsFile.lock();
                        sunjaxwsFile.delete(lock);
                    } finally{
                        if(lock != null){
                            lock.releaseLock();
                        }
                    }
                }
            } else{
                //remove the entry from the sunjaxwsFile
                Endpoints endpoints = EndpointsProvider.getDefault().getEndpoints(sunjaxwsFile);
                Endpoint endpoint = endpoints.findEndpointByName(serviceName);
                if(endpoint != null){
                    endpoints.removeEndpoint(endpoint);
                    OutputStream os = null;
                    synchronized(this) {
                        try{
                            lock = sunjaxwsFile.lock();
                            os = sunjaxwsFile.getOutputStream(lock);
                            endpoints.write(os);
                        }finally{
                            if(lock != null){
                                lock.releaseLock();
                            }
                            if(os != null){
                                os.close();
                            }
                        }
                    }
                }
            }
        }
    }else{
        String mes = NbBundle.getMessage(WebProjectJAXWSSupport.class, "MSG_CannotFindDDDirectory"); // NOI18N
        NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
        DialogDisplayer.getDefault().notify(desc);
    }
    
    //TODO if no more web services, remove the jaxws21 library
}
 
Example 20
Source File: FormEditorSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean notifyModified () {
    boolean alreadyModified = isModified();
    boolean retVal = super.notifyModified();
    
    if (retVal) { // java source modification
        addSaveCookie();
    }
    
    if (!alreadyModified) {
        FileObject formFile = formDataObject.getFormFile();
        if (!formFile.canWrite()) { // Issue 74092
            FileLock lock = null;
            try {
                lock = formFile.lock();
            } catch (UserQuestionException uqex) {
                NotifyDescriptor nd = new NotifyDescriptor.Confirmation(
                        uqex.getLocalizedMessage(),
                        FormUtils.getBundleString("TITLE_UserQuestion"), // NOI18N
                        NotifyDescriptor.YES_NO_OPTION);
                DialogDisplayer.getDefault().notify(nd);
                if (NotifyDescriptor.YES_OPTION.equals(nd.getValue())) {
                    try {
                        uqex.confirmed();
                        EventQueue.invokeLater(new Runnable() {
                            @Override
                            public void run()  {
                                reloadForm();
                            }
                        });
                    } catch (IOException ioex) {
                        ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioex);
                    }
                }
            } catch (IOException ex) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
            } finally {
                if (lock != null) {
                    lock.releaseLock();
                }
            }
        }
        updateMVTCDisplayName();
    }
    return retVal;
}