Java Code Examples for org.openide.filesystems.FileSystem#runAtomicAction()

The following examples show how to use org.openide.filesystems.FileSystem#runAtomicAction() . 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: DeployOnSaveManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void resumeListening(final J2eeModuleProvider provider) {
    boolean resume = false;
    synchronized (this) {
        resume = suspended.containsKey(provider);
    }

    // don't do resume unless it is really needed
    if (resume) {
        FileObject fo = ((ConfigSupportImpl) provider.getConfigSupport()).getProjectDirectory();
        FileUtil.refreshAll();

        try {
            FileSystem fs = (fo != null) ? fo.getFileSystem() : FileUtil.getConfigRoot().getFileSystem();
            fs.runAtomicAction(new FileSystem.AtomicAction() {

                @Override
                public void run() throws IOException {
                    clearSuspended(provider);
                }
            });
        } catch (IOException ex) {
            LOGGER.log(Level.INFO, null, ex);
            clearSuspended(provider);
        }
    }        
}
 
Example 2
Source File: WSUtils.java    From netbeans with 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 3
Source File: SceneSerializer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static synchronized void writeToFile(final Document document, final FileObject file) {
    try {
        FileSystem fs = file.getFileSystem();
        fs.runAtomicAction(new FileSystem.AtomicAction() {

            @Override
            public void run() throws IOException {
                final FileLock lock = file.lock();
                try {
                    OutputStream fos = file.getOutputStream(lock);
                    try {
                        XMLUtil.write(document, fos, "UTF-8"); // NOI18N
                    } finally {
                        fos.close();
                    }
                } finally {
                    lock.releaseLock();
                }
            }
        });
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 4
Source File: JSFClientGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static void createFile(final FileObject jsfRoot, final String content, final String name, final Charset encoding) throws IOException{
    FileSystem fs = jsfRoot.getFileSystem();

    fs.runAtomicAction(new FileSystem.AtomicAction() {
        public void run() throws IOException {
            FileObject detailForm = FileUtil.createData(jsfRoot, name);//NOI18N
            FileLock lock = detailForm.lock();
            try {
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(detailForm.getOutputStream(lock), encoding));
                bw.write(content);
                bw.close();
            }
            finally {
                lock.releaseLock();
            }
        }
    });
}
 
Example 5
Source File: ProjectLibraryProviderTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testCreateLibraryUnderFSAtomicAction() throws Exception {
    final LibraryManager mgr = LibraryManager.forLocation(new URL(base, "libraries.properties"));
    final Map<String,List<URI>> content = new HashMap<String,List<URI>>();
    content.put("classpath", Arrays.asList(new URI("jh.jar!/"), new URI("jh-search.jar!/")));
    content.put("javadoc", Arrays.asList(new URI("jh-api/")));

    FileSystem fs = projdir.getFileSystem();
    fs.runAtomicAction(new FileSystem.AtomicAction() {
        public void run() throws IOException {
            Library lib = mgr.createURILibrary("j2se", "javahelp", content);
            assertEquals("j2se", lib.getType());
            assertEquals("javahelp", lib.getName());
            assertEquals(content.get("classpath"), lib.getURIContent("classpath"));
            assertEquals(content.get("javadoc"), lib.getURIContent("javadoc"));
            try {
                setLibraryContent(lib, "src", new URL(base, "separate/jgraph-src/").toURI(), new URL(base, "jgraph-other-src/").toURI());
            } catch (Exception e) {
                throw new IOException(e.toString());
            }
        }});
}
 
Example 6
Source File: EarProjectGenerator.java    From netbeans with 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 7
Source File: EarProjectGenerator.java    From netbeans with 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 8
Source File: GsfUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void extractZip(final FileObject fo, final InputStream is)
throws IOException {
    FileSystem fs = fo.getFileSystem();

    fs.runAtomicAction(
        new FileSystem.AtomicAction() {
            public @Override void run() throws IOException {
                extractZipImpl(fo, is);
            }
        }
    );
}
 
Example 9
Source File: AppClientProjectGenerator.java    From netbeans with 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 10
Source File: PayaraConfiguration.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void createDefaultSunDD(File sunDDFile) throws IOException {
    FileObject sunDDTemplate = Utils.getSunDDFromProjectsModuleVersion(module, sunDDFile.getName()); //FileUtil.getConfigFile(resource);
    if (sunDDTemplate != null) {
        FileObject configFolder = FileUtil.createFolder(sunDDFile.getParentFile());
        FileSystem fs = configFolder.getFileSystem();
        XmlFileCreator creator = new XmlFileCreator(sunDDTemplate, configFolder, sunDDTemplate.getName(), sunDDTemplate.getExt());
        fs.runAtomicAction(creator);
    }
}
 
Example 11
Source File: GlassfishConfiguration.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void createDefaultSunDD(File sunDDFile) throws IOException {
    FileObject sunDDTemplate = Utils.getSunDDFromProjectsModuleVersion(module, sunDDFile.getName()); //FileUtil.getConfigFile(resource);
    if (sunDDTemplate != null) {
        FileObject configFolder = FileUtil.createFolder(sunDDFile.getParentFile());
        FileSystem fs = configFolder.getFileSystem();
        XmlFileCreator creator = new XmlFileCreator(sunDDTemplate, configFolder, sunDDTemplate.getName(), sunDDTemplate.getExt());
        fs.runAtomicAction(creator);
    }
}
 
Example 12
Source File: FastDeploy.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addDescriptorToDeployedDirectory(J2eeModule module, File sunDDFile) {
    FileObject sunDDTemplate = Utils.getSunDDFromProjectsModuleVersion(module, sunDDFile.getName()); //FileUtil.getConfigFile(resource);
    if (sunDDTemplate != null) {
        try {
            FileObject configFolder = FileUtil.createFolder(sunDDFile.getParentFile());
            FileSystem fs = configFolder.getFileSystem();
            XmlFileCreator creator = new XmlFileCreator(sunDDTemplate, configFolder, sunDDTemplate.getName(), sunDDTemplate.getExt());
            fs.runAtomicAction(creator);
        } catch (IOException ioe) {
            Logger.getLogger("glassfish").log(Level.WARNING, "could not create {0}", sunDDTemplate.getPath());
        }
    }
}
 
Example 13
Source File: Hk2DatasourceManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void writeXmlResourceToFile(final File sunResourcesXml, final Document doc) throws IOException {

        FileObject parentFolder = FileUtil.createFolder(sunResourcesXml.getParentFile());
        FileSystem fs = parentFolder.getFileSystem();

         fs.runAtomicAction(new FileSystem.AtomicAction() {
            @Override
            public void run() throws IOException {
                FileLock lock = null;
                OutputStream os = null;
                try {
                    FileObject sunResourcesFO = FileUtil.createData(sunResourcesXml);
                    lock = sunResourcesFO.lock();
                    os = sunResourcesFO.getOutputStream(lock);

                    XMLUtil.write(doc, os, doc.getXmlEncoding());
                } finally {
                    if(os !=null ){
                        os.close();
                    }
                    if(lock != null) {
                        lock.releaseLock();
                    }
                }
            }
        });
    }
 
Example 14
Source File: WildflyDatasourceManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void writeFile(final File file, final BaseBean bean) throws ConfigurationException {
    try {

        FileSystem fs = deployDir.getFileSystem();
        fs.runAtomicAction(new FileSystem.AtomicAction() {
            public void run() throws IOException {
                OutputStream os = null;
                FileLock lock = null;
                try {
                    String name = file.getName();
                    FileObject configFO = deployDir.getFileObject(name);
                    if (configFO == null) {
                        configFO = deployDir.createData(name);
                    }
                    lock = configFO.lock();
                    os = new BufferedOutputStream(configFO.getOutputStream(lock), 4096);
                    // TODO notification needed
                    if (bean != null) {
                        bean.write(os);
                    }
                } finally {
                    if (os != null) {
                        try {
                            os.close();
                        } catch (IOException ioe) {
                        }
                    }
                    if (lock != null) {
                        lock.releaseLock();
                    }
                }
            }
        });
    } catch (IOException e) {
        throw new ConfigurationException(e.getLocalizedMessage());
    }
}
 
Example 15
Source File: EjbJarProjectGenerator.java    From netbeans with 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 16
Source File: SpringWebModuleExtender.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Set<FileObject> extend(WebModule webModule) {
    CreateSpringConfig createSpringConfig = new CreateSpringConfig(webModule);
    FileObject webInf = webModule.getWebInf();
    if (webInf == null) {
        try {
            FileObject documentBase = webModule.getDocumentBase();
            if (documentBase == null) {
                LOGGER.log(Level.INFO, "WebModule does not have valid documentBase");
                return Collections.<FileObject>emptySet();
            }
            webInf = FileUtil.createFolder(documentBase, "WEB-INF"); //NOI18N
        } catch (IOException ex) {
            LOGGER.log(Level.WARNING, "Exception during creating WEB-INF directory", ex);
        }
    }
    if (webInf != null) {
        try {
            FileSystem fs = webInf.getFileSystem();
            fs.runAtomicAction(createSpringConfig);
        } catch (IOException e) {
            Logger.getLogger("global").log(Level.INFO, null, e);
            return Collections.<FileObject>emptySet();
        }
    }
    return createSpringConfig.getFilesToOpen();
}
 
Example 17
Source File: FastDeploy.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addDescriptorToDeployedDirectory(J2eeModule module, File sunDDFile) {
    FileObject sunDDTemplate = Utils.getSunDDFromProjectsModuleVersion(module, sunDDFile.getName()); //FileUtil.getConfigFile(resource);
    if (sunDDTemplate != null) {
        try {
            FileObject configFolder = FileUtil.createFolder(sunDDFile.getParentFile());
            FileSystem fs = configFolder.getFileSystem();
            XmlFileCreator creator = new XmlFileCreator(sunDDTemplate, configFolder, sunDDTemplate.getName(), sunDDTemplate.getExt());
            fs.runAtomicAction(creator);
        } catch (IOException ioe) {
            Logger.getLogger("payara").log(Level.WARNING, "could not create {0}", sunDDTemplate.getPath());
        }
    }
}
 
Example 18
Source File: JBossDatasourceManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void writeFile(final File file, final BaseBean bean) throws ConfigurationException {
    try {

        FileSystem fs = deployDir.getFileSystem();
        fs.runAtomicAction(new FileSystem.AtomicAction() {
            public void run() throws IOException {
                OutputStream os = null;
                FileLock lock = null;
                try {
                    String name = file.getName();
                    FileObject configFO = deployDir.getFileObject(name);
                    if (configFO == null) {
                        configFO = deployDir.createData(name);
                    }
                    lock = configFO.lock();
                    os = new BufferedOutputStream (configFO.getOutputStream(lock), 4096);
                    // TODO notification needed
                    if (bean != null) {
                        bean.write(os);
                    }
                } finally {
                    if (os != null) {
                        try { os.close(); } catch(IOException ioe) {}
                    }
                    if (lock != null) 
                        lock.releaseLock();
                }
            }
        });
    } catch (IOException e) {
        throw new ConfigurationException (e.getLocalizedMessage ());
    }
}
 
Example 19
Source File: JSFConfigModelUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * attempts to save the document model to disk.
 * if model is in transaction, the transaction is ended first,
 * then dataobject's SaveCookie is called.
 *
 * @param model
 * @throws java.io.IOException if saving fails.
 */
public static void saveChanges(DocumentModel<?> model) throws IOException {
    if (model.isIntransaction()) {
        try {
            model.endTransaction();
        } catch (IllegalStateException ex) {
            IOException io = new IOException("Cannot save faces config", ex);
            throw Exceptions.attachLocalizedMessage(io,
                    NbBundle.getMessage(JSFConfigModelUtilities.class, "ERR_Save_FacesConfig",
                    Exceptions.findLocalizedMessage(ex)));
        }
    }
    model.sync();
    DataObject dobj = model.getModelSource().getLookup().lookup(DataObject.class);
    if (dobj == null) {
        final Document doc = model.getModelSource().getLookup().lookup(Document.class);
        final File file = model.getModelSource().getLookup().lookup(File.class);
        LOGGER.log(Level.FINE, "saving changes in {0}", file);
        File parent = file.getParentFile();
        FileObject parentFo = FileUtil.toFileObject(parent);
        if (parentFo == null) {
            parent.mkdirs();
            FileUtil.refreshFor(parent);
            parentFo = FileUtil.toFileObject(parent);
        }
        final FileObject fParentFo = parentFo;
        if (fParentFo != null) {
            FileSystem fs = parentFo.getFileSystem();
            fs.runAtomicAction(new FileSystem.AtomicAction() {

                @Override
                public void run() throws IOException {
                    String text;
                    try {
                        text = doc.getText(0, doc.getLength());
                    } catch (BadLocationException x) {
                        throw new IOException(x);
                    }
                    FileObject fo = fParentFo.getFileObject(file.getName());
                    if (fo == null) {
                        fo = fParentFo.createData(file.getName());
                    }
                    OutputStream os = fo.getOutputStream();
                    try {
                        os.write(text.getBytes(FileEncodingQuery.getEncoding(fo)));
                    } finally {
                        os.close();
                    }
                }
            });
        }
    } else {
        SaveCookie save = dobj.getLookup().lookup(SaveCookie.class);
        if (save != null) {
            LOGGER.log(Level.FINE, "saving changes in {0}", dobj);
            save.save();
        } else {
            LOGGER.log(Level.FINE, "no changes in {0}", dobj);
        }
    }
}
 
Example 20
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * attempts to save the document model to disk.
 * if model is in transaction, the transaction is ended first,
 * then dataobject's SaveCookie is called.
 *
 * @param model
 * @throws java.io.IOException if saving fails.
 */
public static void saveChanges(AbstractDocumentModel<?> model) throws IOException {
    if (model.isIntransaction()) {
        // the ISE thrown from endTransction is handled in performPOMModelOperations.
        model.endTransaction();
    }
    model.sync();
    DataObject dobj = model.getModelSource().getLookup().lookup(DataObject.class);
    if (dobj == null) {
        final Document doc = model.getModelSource().getLookup().lookup(Document.class);
        final File file = model.getModelSource().getLookup().lookup(File.class);
        logger.log(Level.FINE, "saving changes in {0}", file);
        File parent = file.getParentFile();
        FileObject parentFo = FileUtil.toFileObject(parent);
        if (parentFo == null) {
            parent.mkdirs();
            FileUtil.refreshFor(parent);
            parentFo = FileUtil.toFileObject(parent);
        }
        final FileObject fParentFo = parentFo;
        if (fParentFo != null) {
            FileSystem fs = parentFo.getFileSystem();
            fs.runAtomicAction(new FileSystem.AtomicAction() {
                public @Override void run() throws IOException {
                    String text;
                    try {
                        text = doc.getText(0, doc.getLength());
                    } catch (BadLocationException x) {
                        throw new IOException(x);
                    }
                    FileObject fo = fParentFo.getFileObject(file.getName());
                    if (fo == null) {
                        fo = fParentFo.createData(file.getName());
                    }
                    OutputStream os = fo.getOutputStream();
                    try {
                        os.write(text.getBytes("UTF-8"));
                    } finally {
                        os.close();
                    }
                }
            });
        } else {
            //TODO report
        }
    } else {
        SaveCookie save = dobj.getLookup().lookup(SaveCookie.class);
        if (save != null) {
            logger.log(Level.FINE, "saving changes in {0}", dobj);
            save.save();
        } else {
            logger.log(Level.FINE, "no changes in {0} where modified={1}", new Object[] {dobj, dobj.isModified()});
        }
    }
}