Java Code Examples for org.openide.filesystems.FileSystem.AtomicAction

The following examples show how to use org.openide.filesystems.FileSystem.AtomicAction. These examples are extracted from open source projects. 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 Project: netbeans   Source File: DataEditorSupport.java    License: Apache License 2.0 6 votes vote down vote up
/** The time when the data has been modified
*/
public Date getTime() {
    // #32777 - refresh file object and return always the actual time
    action = new FileSystem.AtomicAction() {
        public void run() throws IOException {
            getFileImpl().refresh(false);
        }
    };
    try {
        getFileImpl().getFileSystem().runAtomicAction(action);
    } catch (IOException ex) {
        //Nothing to do here
    }
    
    return getFileImpl ().lastModified ();
}
 
Example 2
Source Project: netbeans   Source File: EarProjectGenerator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new empty Enterprise Application project.
 *
 * @param prjDir the top-level directory (need not yet exist but if it does it must be empty)
 * @param name the code name for the project
 * @return the helper object permitting it to be further customized
 * @throws IOException in case something went wrong
 */
public static AntProjectHelper createProject(File prjDir, String name, Profile j2eeProfile,
        String serverInstanceId, String sourceLevel, String librariesDefinition) throws IOException {
    FileObject projectDir = FileUtil.createFolder(prjDir);
    final EarProjectGenerator earGen = new EarProjectGenerator(prjDir, projectDir, name, j2eeProfile,
            serverInstanceId, sourceLevel, librariesDefinition);
    final AntProjectHelper[] h = new AntProjectHelper[1];
    
    // create project in one FS atomic action:
    FileSystem fs = projectDir.getFileSystem();
    fs.runAtomicAction(new FileSystem.AtomicAction() {
        public void run() throws IOException {
            AntProjectHelper helper = earGen.doCreateProject();
            h[0] = helper;
        }});
    return h[0];
}
 
Example 3
Source Project: netbeans   Source File: EarProjectGenerator.java    License: Apache License 2.0 6 votes vote down vote up
public static AntProjectHelper importProject(File pDir, final File sDir, String name,
        Profile j2eeProfile, String serverInstanceID, final String platformName,
        String sourceLevel, final Map<FileObject, ModuleType> userModules,
        String librariesDefinition)
        throws IOException {
    FileObject projectDir = FileUtil.createFolder(pDir);
    final EarProjectGenerator earGen = new EarProjectGenerator(pDir, projectDir, name,
            j2eeProfile, serverInstanceID, sourceLevel, librariesDefinition);
    final AntProjectHelper[] h = new AntProjectHelper[1];
    
    // create project in one FS atomic action:
    FileSystem fs = projectDir.getFileSystem();
    fs.runAtomicAction(new FileSystem.AtomicAction() {
        public void run() throws IOException {
            AntProjectHelper helper = earGen.doImportProject(sDir, userModules, platformName);
            h[0] = helper;
        }});
    return h[0];
}
 
Example 4
Source Project: netbeans   Source File: WSUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void removeImplClass(Project project, String implClass) {
    Sources sources = project.getLookup().lookup(Sources.class);
    String resource = implClass.replace('.','/')+".java"; //NOI18N
    if (sources!=null) {
        SourceGroup[] srcGroup = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
        for (int i=0;i<srcGroup.length;i++) {
            final FileObject srcRoot = srcGroup[i].getRootFolder();
            final FileObject implClassFo = srcRoot.getFileObject(resource);
            if (implClassFo!=null) {
                try {
                    FileSystem fs = implClassFo.getFileSystem();
                    fs.runAtomicAction(new AtomicAction() {
                        @Override
                        public void run() {
                            deleteFile(implClassFo);
                        }
                    });
                } catch (IOException ex) {
                    ErrorManager.getDefault().notify(ex);
                }
                return;
            }
        }
    }
}
 
Example 5
Source Project: netbeans   Source File: WSUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void removeImplClass(Project project, String implClass) {
    Sources sources = project.getLookup().lookup(Sources.class);
    String resource = implClass.replace('.','/')+".java"; //NOI18N
    if (sources!=null) {
        SourceGroup[] srcGroup = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
        for (int i=0;i<srcGroup.length;i++) {
            final FileObject srcRoot = srcGroup[i].getRootFolder();
            final FileObject implClassFo = srcRoot.getFileObject(resource);
            if (implClassFo!=null) {
                try {
                    FileSystem fs = implClassFo.getFileSystem();
                    fs.runAtomicAction(new AtomicAction() {
                        public void run() {
                            deleteFile(implClassFo);
                        }
                    });
                } catch (IOException ex) {
                    ErrorManager.getDefault().notify(ex);
                }
                return;
            }
        }
    }
}
 
Example 6
Source Project: netbeans   Source File: FilesystemInterceptorTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testDeleteA_CreateA_RunAtomic() throws Exception {
    // init
    final File fileA = new File(repositoryLocation, "A");
    h.setFilesToRefresh(Collections.singleton(fileA));
    fileA.createNewFile();
    add();
    commit();
    assertEquals(EnumSet.of(Status.UPTODATE), getCache().getStatus(fileA).getStatus());
    assertTrue(h.waitForFilesToRefresh());

    final FileObject fo = FileUtil.toFileObject(fileA);
    AtomicAction a = new AtomicAction() {
        @Override
        public void run() throws IOException {
            fo.delete();
            fo.getParent().createData(fo.getName());
        }
    };
    h.setFilesToRefresh(Collections.singleton(fileA));
    fo.getFileSystem().runAtomicAction(a);
    assertTrue(h.waitForFilesToRefresh());

    // test
    assertTrue(fileA.exists());
    assertEquals(EnumSet.of(Status.UPTODATE), getCache().getStatus(fileA).getStatus());
}
 
Example 7
Source Project: netbeans   Source File: InteceptorTest.java    License: Apache License 2.0 6 votes vote down vote up
public void deleteA_CreateA_RunAtomic() throws IOException, SVNClientException {
    // init
    final File fileA = new File(wc, "A");
    fileA.createNewFile();        
    commit(wc);        
    assertEquals(SVNStatusKind.NORMAL, getSVNStatus(fileA).getTextStatus());        
    
    final FileObject fo = FileUtil.toFileObject(fileA);
    AtomicAction a = new AtomicAction() {
        public void run() throws IOException {             
            fo.delete();
            fo.getParent().createData(fo.getName());
        }
    };
    fo.getFileSystem().runAtomicAction(a);        
    
    waitALittleBit(500); // after create 
    
    // test 
    assertTrue(fileA.exists());        
    assertEquals(SVNStatusKind.NORMAL, getSVNStatus(fileA).getTextStatus());        
    assertEquals(FileInformation.STATUS_VERSIONED_UPTODATE, getStatus(fileA));                
}
 
Example 8
Source Project: netbeans   Source File: ProjectProperties.java    License: Apache License 2.0 6 votes vote down vote up
private void diskChange(FileEvent fe) {
    boolean writing = false;
    if (fe != null) {
        synchronized (saveActions) {
            for (AtomicAction a : saveActions) {
                if (fe.firedFrom(a)) {
                    writing = true;
                    break;
                }
            }
        }
    }
    if (!writing) {
        loaded = false;
        reloadedStackTrace = new Throwable("noticed disk change here");
    }
    fireChange();
    if (!writing) {
        helper.fireExternalChange(path);
    }
}
 
Example 9
Source Project: netbeans   Source File: PackageRename.java    License: Apache License 2.0 5 votes vote down vote up
private void atomicSetName(final String name) {
    try {
        folder.getFileSystem().runAtomicAction(new AtomicAction() {
            @Override
            public void run() throws IOException {
                setName(name);
            }
        });
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 10
Source Project: netbeans   Source File: AntProjectSupportTest.java    License: Apache License 2.0 5 votes vote down vote up
@RandomlyFails // NB-Core-Build #3638
public void testInitiallyInvalidScript() throws Exception {
    final FileObject fo = scratch.createData("x.ant");
    assertEquals("it is an APDO", AntProjectDataObject.class, DataObject.find(fo).getClass());
    AntProjectCookie apc = new AntProjectSupport(fo);
    assertNull("invalid", apc.getDocument());
    assertNotNull("invalid", apc.getParseException());
    MockChangeListener l = new MockChangeListener();
    apc.addChangeListener(l);
    //l.expectNoEvents(5000);
    fo.getFileSystem().runAtomicAction(new AtomicAction() {
        public void run() throws IOException {
            OutputStream os = fo.getOutputStream();
            try {
                os.write("<project default='x'><target name='x'/></project>".getBytes("UTF-8"));
            } finally {
                os.close();
            }
        }
    });
    l.expectEvent(5000);
    /* XXX parseException usually turns out to be non-null here still; too many threads running around, who knows what is going on
    Throwable x = apc.getParseException();
    if (x != null) {
        throw (AssertionFailedError) new AssertionFailedError("now valid (no exc)").initCause(x);
    }
    Document doc = apc.getDocument();
    assertNotNull("now valid (have doc)", doc);
    assertEquals("one target", 1, doc.getElementsByTagName("target").getLength());
     */
}
 
Example 11
Source Project: netbeans   Source File: PropertiesStorage.java    License: Apache License 2.0 5 votes vote down vote up
public void runAtomic(final Runnable run) {
    try {
        configRoot.getFileSystem().runAtomicAction(new AtomicAction() {
            public void run() throws IOException {
                run.run();
            }
        });
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 12
Source Project: netbeans   Source File: ModuleList.java    License: Apache License 2.0 5 votes vote down vote up
/** Delete a module from disk.
 */
private void deleteFromDisk(final Module m, final DiskStatus status) throws IOException {
    final String nameDashes = m.getCodeNameBase().replace('.', '-'); // NOI18N
    //final long expectedTime = status.lastApprovedChange;
    FileSystem.AtomicAction aa = new FileSystem.AtomicAction() {
        public void run() throws IOException {
            FileObject xml = folder.getFileObject(nameDashes, "xml"); // NOI18N
            if (xml == null) {
                // Could be that the XML was already deleted externally, etc.
                LOG.fine("ModuleList: " + m + "'s XML already gone from disk");
                return;
            }
            //if (xml == null) throw new IOException("No such XML file: " + nameDashes + ".xml"); // NOI18N
            if (status.dirty) {
                // Someone wrote to the file since we did. Don't delete it blindly!
                // XXX should this throw an exception, or just warn??
                throw new IOException("Unapproved external change to " + xml); // NOI18N
            }
            LOG.fine("ModuleList: deleting " + xml);
            /*
            if (xml.lastModified().getTime() != expectedTime) {
                // Someone wrote to the file since we did. Don't delete it blindly!
                throw new IOException("Unapproved external change to " + xml); // NOI18N
            }
             */
            xml.delete();
            FileObject ser = folder.getFileObject(nameDashes, "ser"); // NOI18N
            if (ser != null) {
                LOG.fine("(and also " + ser + ")");
                ser.delete();
            }
        }
    };
    myAtomicActions.add(aa);
    folder.getFileSystem().runAtomicAction(aa);
}
 
Example 13
Source Project: netbeans   Source File: ModuleList.java    License: Apache License 2.0 5 votes vote down vote up
/** Check if a given file event in the Modules/ folder was a result
 * of our own manipulations, as opposed to some other code (or polled
 * refresh) manipulating one of these XML files. See #15573.
 */
private boolean isOurs(FileEvent ev) {
    for (FileSystem.AtomicAction action : myAtomicActions) {
        if (ev.firedFrom(action)) {
            return true;
        }
    }
    return false;
}
 
Example 14
Source Project: netbeans   Source File: FsEventFromAtomicActionTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testFiredFromOneAtomicAction() throws Exception {
    final File workDir = getWorkDir();
    final FileObject workDirFo = FileUtil.toFileObject(workDir);

    final AtomicAction myAtomicAction = new AtomicAction() {
        @Override
        public void run() throws IOException {
            try {
                for (int i = 0; i < RUNS; ++i) {
                    FileUtil.createData(workDirFo, FILE_PREFIX + i);
                }
            } catch (IOException ex) {
                // checked later
            }
        }
    };
    MyFileChangeListener myChangeListener = new MyFileChangeListener(myAtomicAction);
    FileUtil.addRecursiveListener(myChangeListener, workDir);

    assertEquals("files before", 0, workDir.list().length);

    FileUtil.runAtomicAction(myAtomicAction);

    assertEquals("files after", RUNS, workDir.list().length);
    assertEquals(printEvents(myChangeListener.notFromAtomicAction), 0, myChangeListener.notFromAtomicAction.size());
    assertEquals("events", RUNS, myChangeListener.events.get());
}
 
Example 15
Source Project: netbeans   Source File: FolderOrder.java    License: Apache License 2.0 5 votes vote down vote up
/** Changes the order of data objects.
 */
public void setOrder (final DataObject[] arr) throws IOException {
    // #251398: setOrder is internally synchronized(this) and fires FS events
    // while holding the lock over this FolderOrder, although the Order is
    // already updated. FSAA will defer event dispatch until after the FolderOrder
    // lock is released. Also helps Listeners to see the oder consistent, not
    // partially set as individual FObj attributes are changed.
    folder.getFileSystem().runAtomicAction(new AtomicAction() {
        public void run() throws IOException {
            doSetOrder(arr);
        }
    });
}
 
Example 16
Source Project: netbeans   Source File: FileUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Executes atomic action. For more info see {@link FileSystem#runAtomicAction}. 
 * <p>
 * All events about filesystem changes (related to events on all affected instances of <code>FileSystem</code>)
 * are postponed after the whole <code>atomicCode</code> 
 * is executed.
 * </p>
 * @param atomicCode code that is supposed to be run as atomic action. See {@link FileSystem#runAtomicAction}
 * @since 7.5
 */
public static final void runAtomicAction(final Runnable atomicCode) {
    final AtomicAction action = new FileSystem.AtomicAction() {
        public void run() throws IOException {
            atomicCode.run();
        }
    };
    try {
        FileUtil.runAtomicAction(action);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 17
Source Project: netbeans   Source File: FileUtil.java    License: Apache License 2.0 5 votes vote down vote up
/** Extract jar file into folder represented by file object. If the JAR contains
* files with name filesystem.attributes, it is assumed that these files
* has been created by DefaultAttributes implementation and the content
* of these files is treated as attributes and added to extracted files.
* <p><code>META-INF/</code> directories are skipped over.
*
* @param fo file object of destination folder
* @param is input stream of jar file
* @exception IOException if the extraction fails
* @deprecated Use of XML filesystem layers generally obsoletes this method.
*             For tests, use {@link org.openide.util.test.TestFileUtils#unpackZipFile}.
*/
@Deprecated
public static void extractJar(final FileObject fo, final InputStream is)
throws IOException {
    FileSystem fs = fo.getFileSystem();

    fs.runAtomicAction(
        new FileSystem.AtomicAction() {
            public void run() throws IOException {
                extractJarImpl(fo, is);
            }
        }
    );
}
 
Example 18
Source Project: netbeans   Source File: EjbJarProjectGenerator.java    License: Apache License 2.0 5 votes vote down vote up
public static AntProjectHelper createProject(final EjbJarProjectCreateData createData) throws IOException {
    File dir = createData.getProjectDir();

    final FileObject projectDir = FileUtil.createFolder(dir);
    final AntProjectHelper[] h = new AntProjectHelper[1];

    // create project in one FS atomic action:
    FileSystem fs = projectDir.getFileSystem();
    fs.runAtomicAction(new FileSystem.AtomicAction() {
        public void run() throws IOException {
            AntProjectHelper helper = createProjectImpl(createData, projectDir);
            h[0] = helper;
        }});
    return h[0];
}
 
Example 19
Source Project: netbeans   Source File: AppClientProjectGenerator.java    License: Apache License 2.0 5 votes vote down vote up
public static AntProjectHelper createProject(final AppClientProjectCreateData createData) throws IOException {
    File dir = createData.getProjectDir();

    final AntProjectHelper[] h = new AntProjectHelper[1];
    final FileObject projectDir = FileUtil.createFolder(dir);

    // create project in one FS atomic action:
    FileSystem fs = projectDir.getFileSystem();
    fs.runAtomicAction(new FileSystem.AtomicAction() {
        public void run() throws IOException {
            AntProjectHelper helper = createProjectImpl(createData, projectDir);
            h[0] = helper;
        }});
    return h[0];
}
 
Example 20
Source Project: netbeans   Source File: AppClientProjectGenerator.java    License: Apache License 2.0 5 votes vote down vote up
public static AntProjectHelper importProject(final AppClientProjectCreateData createData) throws IOException {
    File dir = createData.getProjectDir();
    
    final AntProjectHelper[] h = new AntProjectHelper[1];
    final FileObject projectDir = FileUtil.createFolder(dir);

    // create project in one FS atomic action:
    FileSystem fs = projectDir.getFileSystem();
    fs.runAtomicAction(new FileSystem.AtomicAction() {
        public void run() throws IOException {
            AntProjectHelper helper = importProjectImpl(createData, projectDir);
            h[0] = helper;
        }});
    return h[0];
}
 
Example 21
Source Project: netbeans   Source File: AntProjectHelper.java    License: Apache License 2.0 5 votes vote down vote up
private void change(FileEvent fe) {
    synchronized (saveActions) {
        for (AtomicAction a : saveActions) {
            if (fe.firedFrom(a)) {
                return;
            }
        }
    }
    String path;
    File f = FileUtil.toFile(fe.getFile());
    synchronized (modifiedMetadataPaths) {
        if (f.equals(resolveFile(PROJECT_XML_PATH))) {
            if (modifiedMetadataPaths.contains(PROJECT_XML_PATH)) {
                //#68872: don't do anything if the given file has non-saved changes:
                return ;
            }
            path = PROJECT_XML_PATH;
            projectXmlValid = false;
        } else if (f.equals(resolveFile(PRIVATE_XML_PATH))) {
            if (modifiedMetadataPaths.contains(PRIVATE_XML_PATH)) {
                //#68872: don't do anything if the given file has non-saved changes:
                return ;
            }
            path = PRIVATE_XML_PATH;
            privateXmlValid = false;
        } else {
            LOG.log(Level.WARNING, "#184132: unexpected file change in {0}; possibly deleted project?", f);
            return;
        }
    }
    fireExternalChange(path);
}
 
Example 22
Source Project: netbeans   Source File: PropertiesEditorSupport.java    License: Apache License 2.0 5 votes vote down vote up
/** Helper method. Saves this entry. */
    private void saveThisEntry() throws IOException {
        FileSystem.AtomicAction aa = new SaveImpl(this);
        FileUtil.runAtomicAction(aa);
//        super.saveDocument();
        // #32777 - it can happen that save operation was interrupted
        // and file is still modified. Mark it unmodified only when it is really
        // not modified.
        if (!env.isModified()) {
            myEntry.setModified(false);
        }
    }
 
Example 23
Source Project: netbeans   Source File: AbstractTestUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates new Data Object
 */
public static DataObject createDataObject(DataFolder folder, final String name, final String extension, final String content) throws IOException {
    final FileObject targetFolder = folder.getPrimaryFile();
    FileSystem filesystem = targetFolder.getFileSystem();
    
    final FileObject[] fileObject = new FileObject[1];
    AtomicAction fsAction = new AtomicAction() {
        public void run() throws IOException {
            FileObject fo = targetFolder.createData(name, extension);
            FileLock lock = null;
            try {
                lock = fo.lock();
                OutputStream out = fo.getOutputStream(lock);
                out = new BufferedOutputStream(out, 999);
                Writer writer = new OutputStreamWriter(out, "UTF8");        // NOI18N
                writer.write(content + '\n');  // NOI18N                    
                writer.flush();
                writer.close();
                
                // return DataObject
                lock.releaseLock();
                lock = null;
                
                fileObject[0] = fo;
                
            } finally {
                if (lock != null) lock.releaseLock();
            }
        }
    };
    
    filesystem.runAtomicAction(fsAction);
    return DataObject.find(fileObject[0]);
}
 
Example 24
Source Project: editorconfig-netbeans   Source File: EditorConfigProcessor.java    License: MIT License 5 votes vote down vote up
private void updateChangesInEditorWindow(final FileInfo info) {
  LOG.log(Level.INFO, "Update changes in Editor window for: {0}", info.getPath());

      try {
        FileUtil.runAtomicAction((AtomicAction) new WriteEditorAction(info));
      } catch (IOException ex) {
        LOG.log(Level.SEVERE, ex.getMessage());
      }
}
 
Example 25
Source Project: editorconfig-netbeans   Source File: EditorConfigProcessor.java    License: MIT License 5 votes vote down vote up
/**
 * TODO: Make sure that a Reformat is done to write correct indentions.
 *
 * @param info
 */
private void updateChangesInFile(FileInfo info) {
  LOG.log(Level.INFO, "Write content (with all rules applied) to file: {0}",
          info.getFileObject().getPath());

  try {
    FileUtil.runAtomicAction((AtomicAction) new WriteStringToFileAction(info));
  } catch (IOException ex) {
    LOG.log(Level.SEVERE, ex.getMessage());
  }
}
 
Example 26
Source Project: netbeans   Source File: ModuleList.java    License: Apache License 2.0 4 votes vote down vote up
/** Write information about a module out to disk.
 * If the old status is given as null, this is a newly
 * added module; create an appropriate status and return it.
 * Else update the existing status and return it (it is
 * assumed properties are already updated).
 * Should write the XML and create/rewrite/delete the serialized
 * installer file as needed.
 */
private DiskStatus writeOut(Module m, DiskStatus old) throws IOException {
    final DiskStatus nue;
    if (old == null) {
        nue = new DiskStatus();
        nue.module = m;
        nue.setDiskProps(computeProperties(m));
    } else {
        nue = old;
    }
    FileSystem.AtomicAction aa = new FileSystem.AtomicAction() {
        public void run() throws IOException {
            if (nue.file == null) {
                nue.file = FileUtil.createData(folder, ((String)nue.diskProps.get("name")).replace('.', '-') + ".xml"); // NOI18N
            } else {
                // Just verify that no one else touched it since we last did.
                if (/*nue.lastApprovedChange != nue.file.lastModified().getTime()*/nue.dirty) {
                    // Oops, something is wrong. #156764 - log at lower level.
                    LOG.log(Level.INFO, null, new IOException("Will not clobber external changes in " + nue.file));
                    return;
                }
            }
            LOG.fine("ModuleList: (re)writing " + nue.file);
            FileLock lock = nue.file.lock();
            try {
                OutputStream os = nue.file.getOutputStream(lock);
                try {
                    writeStatus(nue.diskProps, os);
                } finally {
                    os.close();
                }
            } finally {
                lock.releaseLock();
            }
            //nue.lastApprovedChange = nue.file.lastModified().getTime();
        }
    };
    myAtomicActions.add(aa);
    folder.getFileSystem().runAtomicAction(aa);
    return nue;
}
 
Example 27
Source Project: netbeans   Source File: ModuleList.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    LOG.fine("ModuleList: will process outstanding external XML changes");
    mgr.mutexPrivileged().enterWriteAccess();
    try {
        folder.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
            public void run() throws IOException {
                // 1. For any dirty XML for which status exists but reloadable differs from XML: change.
                // 2. For any XML for which we have no status: create & create status, as disabled.
                // 3. For all dirty XML which says enabled but status says disabled: batch-enable as possible.
                //    (Where not possible, mark disabled in XML??)
                // 4. For all dirty XML which says disabled but status says enabled: batch-disable plus others.
                // 5. For all status for which no XML exists: batch-disable plus others, then delete.
                // 6. For any dirty XML for which jar/autoload/eager/release/specversion differs from
                //    actual state of module: warn but do nothing.
                // 7. For now, ignore any changes in *.ser.
                // 8. For any dirty XML for which status now exists: replace diskProps with contents of XML.
                // 9. Mark all statuses clean.
                // Code name to module XMLs found on disk:
                Map<String,FileObject> xmlfiles = prepareXMLFiles();
                // Code name to properties for dirty XML or XML sans status only.
                Map<String,Map<String,Object>> dirtyprops = prepareDirtyProps(xmlfiles);
                // #27106: do not listen to changes we ourselves produce.
                // It only matters if statuses has not been updated before
                // the changes are fired.
                listening = false;
                try {
                    stepCheckReloadable(dirtyprops);
                    stepCreate(xmlfiles, dirtyprops);
                    stepEnable(dirtyprops);
                    stepDisable(dirtyprops);
                    stepDelete(xmlfiles);
                    stepCheckMisc(dirtyprops);
                    stepCheckSer(xmlfiles, dirtyprops);
                } finally {
                    listening = true;
                    stepUpdateProps(dirtyprops);
                    stepMarkClean();
                }
            }
        });
        LOG.fine("ModuleList: finished processing outstanding external XML changes");
    } catch (IOException ioe) {
        LOG.log(Level.WARNING, null, ioe);
    } finally {
        mgr.mutexPrivileged().exitWriteAccess();
    }
}
 
Example 28
Source Project: netbeans   Source File: FsEventFromAtomicActionTest.java    License: Apache License 2.0 4 votes vote down vote up
public MyFileChangeListener(AtomicAction atomicAction) {
    this.atomicAction = atomicAction;
}
 
Example 29
Source Project: netbeans   Source File: DataEditorSupport.java    License: Apache License 2.0 4 votes vote down vote up
/** Saves document. Overrides superclass method, adds checking
 * for read-only property of saving file and warns user in that case. */
@Override
public void saveDocument() throws IOException {
    FileSystem.AtomicAction aa = new SaveImpl(this);
    FileUtil.runAtomicAction(aa);
}
 
Example 30
Source Project: netbeans   Source File: FolderContext.java    License: Apache License 2.0 4 votes vote down vote up
/** Creates Data Object.
 * @param spec Data Object's relative path separated by '/'
 * @param content Data Object's content
 * @throws IOException if the Data Object cannot be created
 * @return DataObject
 */
public DataObject createDataObject(String spec, final String content) throws IOException {
    File file = new File(spec);
    final String path = file.getParent();
    final String name = file.getName();
    
    final FileObject parent = getFileObject(path);
    if (parent == null) {
        throw new IllegalStateException("Cannot find folder: " + makeURL(path));
    }
    FileSystem filesystem = parent.getFileSystem();
    
    final FileObject[] fileObject = new FileObject[1];
    AtomicAction fsAction = new AtomicAction() {
        public void run() throws IOException {
            if (content == null) {
                // create folder
                fileObject[0] = parent.createFolder(name);
            } else {
                // create file
                FileObject fo = parent.createData(name);
                FileLock lock = null;
                try {
                    lock = fo.lock();
                    OutputStream out = fo.getOutputStream(lock);
                    out = new BufferedOutputStream(out);
                    Writer writer = new OutputStreamWriter(out, "UTF8");
                    writer.write(content);
                    writer.close();
                    // return DataObject
                    lock.releaseLock();
                    lock = null;
                    fileObject[0] = fo;
                } finally {
                    if (lock != null) lock.releaseLock();
                }
            }
        }
    };
    
    filesystem.runAtomicAction(fsAction);
    return DataObject.find(fileObject[0]);
}