Java Code Examples for org.openide.util.MutexException#getException()

The following examples show how to use org.openide.util.MutexException#getException() . 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: CopySupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Transferable paste() throws IOException {
    if (java.awt.EventQueue.isDispatchThread()) {
        return doPaste();
    }
    else { // reinvoke synchronously in AWT thread
        try {
            return Mutex.EVENT.readAccess(this);
        }
        catch (MutexException ex) {
            Exception e = ex.getException();
            if (e instanceof IOException)
                throw (IOException) e;
            else { // should not happen, ignore
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
                return transferable;
            }
        }
    }
}
 
Example 2
Source File: NbProjectManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Save all modified projects.
 * <p>Acquires write access.
 * @throws IOException if any of them cannot be saved
 * @see ProjectFactory#saveProject
 */
public void saveAllProjects() throws IOException {
    try {
        getMutex().writeAccess(new Mutex.ExceptionAction<Void>() {
            @Override
            public Void run() throws IOException {
                Iterator<Project> it = modifiedProjects.iterator();
                while (it.hasNext()) {
                    Project p = it.next();
                    ProjectFactory f = proj2Factory.get(p);
                    if (f != null) {
                        f.saveProject(p);
                        LOG.log(Level.FINE, "saveProject({0})", p.getProjectDirectory());
                    } else {
                        LOG.log(Level.WARNING, "Project {0} was already deleted", p);
                    }
                    it.remove();
                }
                return null;
            }
        });
    } catch (MutexException e) {
        throw (IOException)e.getException();
    }
}
 
Example 3
Source File: UnboundTargetAlert.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Show the alert.
 * If accepted, generate a binding for the command (and add a context menu item for the project).
 * @return true if the alert was accepted and there is now a binding, false if cancelled
 * @throws IOException if there is a problem writing bindings
 */
public boolean accepted() throws IOException {
    String projectDisplayName = ProjectUtils.getInformation(project).getDisplayName();
    if (displayAlert(projectDisplayName)) {
        try {
            ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
                public Void run() throws IOException {
                    generateBindingAndAddContextMenuItem();
                    ProjectManager.getDefault().saveProject(project);
                    return null;
                }
            });
        } catch (MutexException e) {
            throw (IOException) e.getException();
        }
        return true;
    } else {
        return false;
    }
}
 
Example 4
Source File: CopySupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Transferable paste() throws IOException {
    if (java.awt.EventQueue.isDispatchThread())
        return doPaste();
    else { // reinvoke synchronously in AWT thread
        try {
            return Mutex.EVENT.readAccess(this);
        }
        catch (MutexException ex) {
            Exception e = ex.getException();
            if (e instanceof IOException)
                throw (IOException) e;
            else { // should not happen, ignore
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
                return ExTransferable.EMPTY;
            }
        }
    }
}
 
Example 5
Source File: NbPlatform.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static void removePlatform(final NbPlatform plaf) throws IOException {
    try {
        ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
            @Override public Void run() throws IOException {
                EditableProperties props = PropertyUtils.getGlobalProperties();
                props.remove(PLATFORM_PREFIX + plaf.getID() + PLATFORM_DEST_DIR_SUFFIX);
                props.remove(PLATFORM_PREFIX + plaf.getID() + PLATFORM_HARNESS_DIR_SUFFIX);
                props.remove(PLATFORM_PREFIX + plaf.getID() + PLATFORM_LABEL_SUFFIX);
                props.remove(PLATFORM_PREFIX + plaf.getID() + PLATFORM_SOURCES_SUFFIX);
                props.remove(PLATFORM_PREFIX + plaf.getID() + PLATFORM_JAVADOC_SUFFIX);
                PropertyUtils.putGlobalProperties(props);
                return null;
            }
        });
    } catch (MutexException e) {
        throw (IOException) e.getException();
    }
    getPlatformsInternal().remove(plaf);
    ModuleList.refresh(); // #97262
    LOG.log(Level.FINE, "NbPlatform removed: {0}", plaf);
}
 
Example 6
Source File: InstanceDataObjectModuleTestHid.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected void twiddle(final Module m, final int action) throws Exception {
    try {
        mgr.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
            public Void run() throws Exception {
                switch (action) {
                case TWIDDLE_ENABLE:
                    mgr.enable(m);
                    break;
                case TWIDDLE_DISABLE:
                    mgr.disable(m);
                    break;
                case TWIDDLE_RELOAD:
                    mgr.disable(m);
                    mgr.enable(m);
                    break;
                default:
                    throw new IllegalArgumentException("bad action: " + action);
                }
                return null;
            }
        });
    } catch (MutexException me) {
        throw me.getException();
    }
}
 
Example 7
Source File: InstanceDataObjectModuleTestHid.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected @Override void setUp() throws Exception {
    ERR = ErrManager.getDefault().getInstance("TEST-" + getName());
    
    mgr = org.netbeans.core.startup.Main.getModuleSystem().getManager();
    File data = new File(getDataDir(), "lookup");
    File jars = getWorkDir();
    final File jar1 = SetupHid.createTestJAR(data, jars, "test1", null);
    final File jar2 = SetupHid.createTestJAR(data, jars, "test2", null);
    try {
        mgr.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
            public Void run() throws Exception {
                m1 = mgr.create(jar1, new ModuleHistory(jar1.getAbsolutePath()), false, false, false);
                if (!m1.getProblems().isEmpty()) throw new IllegalStateException("m1 is uninstallable: " + m1.getProblems());
                m2 = mgr.create(jar2, new ModuleHistory(jar2.getAbsolutePath()), false, false, false);
                return null;
            }
        });
    } catch (MutexException me) {
        throw me.getException();
    }
    
    ERR.log("setup finished");
}
 
Example 8
Source File: NbModuleProjectGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Generates standalone NetBeans Module. */
public static void createStandAloneModule(final File projectDir, final String cnb,
        final String name, final String bundlePath,
        final String layerPath, final String platformID, final boolean osgi, final boolean tests) throws IOException {
    try {
        logUsage("StandAloneModule"); // NOI18N
        ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
            public Void run() throws IOException {
                final FileObject dirFO = FileUtil.createFolder(projectDir);
                if (ProjectManager.getDefault().findProject(dirFO) != null) {
                    throw new IllegalArgumentException("Already a project in " + dirFO); // NOI18N
                }
                createProjectXML(dirFO, cnb, NbModuleType.STANDALONE, osgi);
                createPlatformProperties(dirFO, platformID);
                createManifest(dirFO, cnb, bundlePath, layerPath, osgi);
                if (bundlePath != null) {
                    createBundle(dirFO, bundlePath, name);
                }
                if (layerPath != null) {
                    createLayerInSrc(dirFO, layerPath);
                }
                createEmptyTestDir(dirFO, tests);
                createInitialProperties(dirFO);
                ModuleList.refresh();
                ProjectManager.getDefault().clearNonProjectCache();
                return null;
            }
        });
    } catch (MutexException e) {
        throw (IOException) e.getException();
    }
}
 
Example 9
Source File: NbModuleProjectGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Generates NetBeans Module within the netbeans.org source tree.
 */
public static void createNetBeansOrgModule(final File projectDir, final String cnb,
        final String name, final String bundlePath, final String layerPath, final boolean osgi) throws IOException {
    try {
        logUsage("NetBeansOrgModule"); // NOI18N
        ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
            public Void run() throws IOException {
                File nborg = ModuleList.findNetBeansOrg(projectDir);
                if (nborg == null) {
                    throw new IllegalArgumentException(projectDir + " doesn't " + // NOI18N
                            "point to a top-level directory within the netbeans.org main or contrib repositories"); // NOI18N
                }
                final FileObject dirFO = FileUtil.createFolder(projectDir);
                if (ProjectManager.getDefault().findProject(dirFO) != null) {
                    throw new IllegalArgumentException("Already a project in " + dirFO); // NOI18N
                }
                createNetBeansOrgBuildXML(dirFO, cnb, nborg);
                createProjectXML(dirFO, cnb, NbModuleType.NETBEANS_ORG, osgi);
                createManifest(dirFO, cnb, bundlePath, layerPath, osgi);
                createBundle(dirFO, bundlePath, name);
                if (layerPath != null) {
                    createLayerInSrc(dirFO, layerPath);
                }
                createEmptyTestDir(dirFO, false);
                createInitialProperties(dirFO);
                ModuleList.refresh();
                ProjectManager.getDefault().clearNonProjectCache();
                return null;
            }
        });
    } catch (MutexException e) {
        throw (IOException) e.getException();
    }
}
 
Example 10
Source File: AntFilesHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void initRestBuildExtension() throws IOException {
    boolean restBuildScriptRefreshed = refreshRestBuildXml();
    boolean saveProjectXml = false;
    boolean changed = false;
    FileObject restBuildScript = project.getProjectDirectory().getFileObject(REST_BUILD_XML_PATH);
    if (restBuildScript != null) {
        Extension extension =  extender.getExtension(REST_ANT_EXT_NAME);
        if (extension == null) {
            extension = extender.addExtension(REST_ANT_EXT_NAME, restBuildScript);
            changed = true;
            saveProjectXml = true;
        }
    }

    // check for cleanup of last version
    if (cleanupLastExtensionVersions()) {
        saveProjectXml = true;
    }
    if (saveProjectXml) {
        ProjectManager.getDefault().saveProject(project);
    }

    if (changed && !restBuildScriptRefreshed) {
        // generate build script
        try {
            final GeneratedFilesHelper helper = new GeneratedFilesHelper(projectHelper);
            ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean>() {
                public Boolean run() throws IOException {
                    URL xslURL = this.getClass().getClassLoader().getResource(REST_BUILD_XSL);
                    helper.generateBuildScriptFromStylesheet(REST_BUILD_XML_PATH,  xslURL);
                    return true;
                }
            });
        } catch (MutexException e) {
            throw (IOException)e.getException();
        }
    }
}
 
Example 11
Source File: AuxiliaryConfigBasedPreferencesProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() throws BackingStoreException {
    try {
        ProjectManager.mutex(false, project).writeAccess(new ExceptionAction<Void>() {
            public Void run() throws BackingStoreException {
                AuxiliaryConfigBasedPreferences.super.clear();
                return null;
            }
        });
    } catch (MutexException ex) {
        throw (BackingStoreException) ex.getException();
    }
}
 
Example 12
Source File: SuitePropertiesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void saveProperties(final SuiteProperties props) throws IOException {
    try {
        // Store properties
        ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
            public Void run() throws IOException {
                props.storeProperties();
                return null;
            }
        });
        ProjectManager.getDefault().saveProject(props.getProject());
    } catch (MutexException e) {
        throw (IOException) e.getException();
    }
}
 
Example 13
Source File: AuxiliaryConfigBasedPreferencesProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public String[] childrenNames() throws BackingStoreException {
    try {
        return ProjectManager.mutex(false, project).readAccess(new ExceptionAction<String[]>() {
            public String[] run() throws BackingStoreException {
                return AuxiliaryConfigBasedPreferences.super.childrenNames();
            }
        });
    } catch (MutexException ex) {
        throw (BackingStoreException) ex.getException();
    }
}
 
Example 14
Source File: NbModuleProjectGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Generates suite component NetBeans Module. */
public static void createSuiteComponentModule(final File projectDir, final String cnb,
        final String name, final String bundlePath,
        final String layerPath, final File suiteDir, final boolean osgi, final boolean tests) throws IOException {
    try {
        logUsage("SuiteComponentModule"); // NOI18N
        ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
            public Void run() throws IOException {
                final FileObject dirFO = FileUtil.createFolder(projectDir);
                if (ProjectManager.getDefault().findProject(dirFO) != null) {
                    throw new IllegalArgumentException("Already a project in " + dirFO); // NOI18N
                }
                createProjectXML(dirFO, cnb, NbModuleType.SUITE_COMPONENT, osgi);
                createSuiteProperties(dirFO, suiteDir);
                createManifest(dirFO, cnb, bundlePath, layerPath, osgi);
                if (bundlePath != null) {
                    createBundle(dirFO, bundlePath, name);
                }
                if (layerPath != null) {
                    createLayerInSrc(dirFO, layerPath);
                }
                createEmptyTestDir(dirFO, tests);
                createInitialProperties(dirFO);
                ModuleList.refresh();
                ProjectManager.getDefault().clearNonProjectCache();
                appendToSuite(cnb, dirFO, suiteDir);
                return null;
            }
        });
    } catch (MutexException e) {
        throw (IOException) e.getException();
    }
}
 
Example 15
Source File: ModuleProperties.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static void storeJavaPlatform(AntProjectHelper helper, PropertyEvaluator eval, JavaPlatform platform, boolean isNetBeansOrg) throws IOException {
    if (isNetBeansOrg) {
        final boolean isDefault = platform == null || platform == JavaPlatform.getDefault();
        final File home = isDefault ? null : getPlatformLocation(platform);
        if (home != null || isDefault) {
            final FileObject nbbuild = helper.resolveFileObject(eval.evaluate("${nb_all}/nbbuild")); // NOI18N
            if (nbbuild != null) {
                try {
                    ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
                        public Void run() throws IOException {
                            FileObject userBuildProperties = nbbuild.getFileObject("user.build.properties"); // NOI18N
                            if (userBuildProperties == null) {
                                userBuildProperties = nbbuild.createData("user.build.properties"); // NOI18N
                            }
                            EditableProperties ep = Util.loadProperties(userBuildProperties);
                            if (isDefault) {
                                // Have to remove it; no default value.
                                ep.remove("nbjdk.home");
                            } else {
                                ep.setProperty("nbjdk.home", home.getAbsolutePath());
                            }
                            Util.storeProperties(userBuildProperties, ep);
                            return null;
                        }
                    });
                } catch (MutexException e) {
                    throw (IOException) e.getException();
                }
            }
        }
    } else {
        EditableProperties props = helper.getProperties(NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH);
        if (platform == null || platform == JavaPlatform.getDefault()) {
            if (props.containsKey(JAVA_PLATFORM_PROPERTY)) {
                // Could also just remove it, but probably nicer to set it explicitly to 'default'.
                props.put(JAVA_PLATFORM_PROPERTY, "default"); // NOI18N
            }
        } else {
            props.put(JAVA_PLATFORM_PROPERTY, getPlatformID(platform));
        }
        helper.putProperties(NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH, props);
    }
}
 
Example 16
Source File: JFXProjectProperties.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void storeFX() throws IOException {
    updatePreloaderDependencies(CONFIGS);
    CONFIGS.storeActive();
    final EditableProperties ep = new EditableProperties(true);
    final FileObject projPropsFO = project.getProjectDirectory().getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH);
    final EditableProperties pep = new EditableProperties(true);
    final FileObject privPropsFO = project.getProjectDirectory().getFileObject(AntProjectHelper.PRIVATE_PROPERTIES_PATH);        
    try {
        ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
            @Override
            public Void run() throws Exception {
                final InputStream is = projPropsFO.getInputStream();
                final InputStream pis = privPropsFO.getInputStream();
                try {
                    ep.load(is);
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
                try {
                    pep.load(pis);
                } finally {
                    if (pis != null) {
                        pis.close();
                    }
                }
                
                fxPropGroup.store(ep);
                storeRest(ep, pep);
                CONFIGS.store(ep, pep);
                updatePreloaderComment(ep);
                //JFXProjectUtils.updateClassPathExtensionProperties(ep);
                logProps(ep);

                OutputStream os = null;
                FileLock lock = null;
                try {
                    lock = projPropsFO.lock();
                    os = projPropsFO.getOutputStream(lock);
                    ep.store(os);
                } finally {
                    if (lock != null) {
                        lock.releaseLock();
                    }
                    if (os != null) {
                        os.close();
                    }
                }
                try {
                    lock = privPropsFO.lock();
                    os = privPropsFO.getOutputStream(lock);
                    pep.store(os);
                } finally {
                    if (lock != null) {
                        lock.releaseLock();
                    }
                    if (os != null) {
                        os.close();
                    }
                }
                return null;
            }
        });
    } catch (MutexException mux) {
        throw (IOException) mux.getException();
    }
}
 
Example 17
Source File: JFXProjectUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Updates project.properties so that if JFXRT artifacts are explicitly added to
 * compile classpath if they are not on classpath by default. This is a workaround
 * of the fact that JDK1.7 does contain FX RT but does not provide it on classpath.
 * JDK1.8 has FX RT on classpath, but still may not include all relevant artifacts by default
 * and may need this extension.
 * Note that this extension is relevant not only for FX Application projects, but also
 * for SE projects that have the property "keep.javafx.runtime.on.classpath" set 
 * (see SE Deployment category in Project Properties dialog).
 * 
 * @param prj the project to update
 */
public static void updateClassPathExtension(@NonNull final Project project) throws IOException {
    final boolean hasDefaultJavaFXPlatform[] = new boolean[1];
    final EditableProperties ep = new EditableProperties(true);
    final FileObject projPropsFO = project.getProjectDirectory().getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH);
    if(projPropsFO != null) {
        try {
            ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
                @Override
                public Void run() throws Exception {
                    final Lookup lookup = project.getLookup();
                    final J2SEPropertyEvaluator eval = lookup.lookup(J2SEPropertyEvaluator.class);
                    if (eval != null) {
                        // if project has Default_JavaFX_Platform, change it to default Java Platform
                        String platformName = eval.evaluator().getProperty(JFXProjectProperties.PLATFORM_ACTIVE);
                        hasDefaultJavaFXPlatform[0] = JFXProjectProperties.isEqual(platformName, JavaFXPlatformUtils.DEFAULT_JAVAFX_PLATFORM);
                    }
                    if(hasDefaultJavaFXPlatform[0]) {
                        final J2SEProjectPlatform platformSetter = lookup.lookup(J2SEProjectPlatform.class);
                        if(platformSetter != null) {
                            platformSetter.setProjectPlatform(JavaPlatformManager.getDefault().getDefaultPlatform());
                        }
                    }
                    try (InputStream is = projPropsFO.getInputStream()) {
                        ep.load(is);
                    }
                    boolean cpExtUpdated = updateClassPathExtensionProperties(ep);
                    if (cpExtUpdated) {
                        final FileLock lock = projPropsFO.lock();
                        try (OutputStream os =projPropsFO.getOutputStream(lock)) {
                            ep.store(os);
                        } finally {                                                                
                            lock.releaseLock();
                        }
                    }
                    return null;
                }
            });
        } catch (MutexException mux) {
            throw (IOException) mux.getException();
        }
        if(hasDefaultJavaFXPlatform[0]) {
            final String headerTemplate = NbBundle.getMessage(JFXProjectUtils.class, "TXT_UPDATED_DEFAULT_PLATFORM_HEADER"); //NOI18N
            final String header = MessageFormat.format(headerTemplate, new Object[] {ProjectUtils.getInformation(project).getDisplayName()});
            final String content = NbBundle.getMessage(JFXProjectUtils.class, "TXT_UPDATED_DEFAULT_PLATFORM_CONTENT"); //NOI18N
            Notification notePlatformChange = NotificationDisplayer.getDefault().notify(
                    header, 
                    ImageUtilities.loadImageIcon("org/netbeans/modules/javafx2/project/ui/resources/jfx_project.png", true), //NOI18N
                    content, 
                    null, 
                    NotificationDisplayer.Priority.LOW, 
                    NotificationDisplayer.Category.INFO);
            JFXProjectOpenedHook.addNotification(project, notePlatformChange);
        }
    } else {
        LOGGER.warning("Project metafiles inaccessible - classpath extension could not be verified and updated if needed."); //NOI18N
    }
}
 
Example 18
Source File: ProjectConvertorFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NonNull
private Project createProject() throws IOException {
    try {
        return ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Project>() {
            @Override
            public Project run() throws Exception {
                excluded.add(projectDirectory);
                try {
                    projectState.notifyDeleted();
                    final Project prj = result.createProject();
                    if (prj == null) {
                        throw new IllegalStateException(String.format(
                            "The convertor %s created null project.",   //NOI18N
                            result));
                    }
                    //Set the Lookup to the created project lookup
                    //Remove OpenHook as the OpenProjectList calls all POH in
                    //the project's Lookup even there the non merged, so it's safer
                    //to remove it.
                    //Also remove ProjectInfo as it's overriden by project's own
                    //no need for it anymore
                    final DynamicLookup dynLkp = getLookup();
                    final Lookup[] baseLkps = dynLkp.getBaseLookups();
                    dynLkp.setBaseLookups(
                        baseLkps[1],
                        prj.getLookup());
                    return prj;
                } finally {
                    excluded.remove(projectDirectory);
                }
            }
        });
    } catch (final MutexException e) {
        final Exception root = e.getException();
        if (root instanceof RuntimeException) {
            throw (RuntimeException) root;
        } else if (root instanceof IOException) {
            throw (IOException) root;
        } else {
            throw new RuntimeException(root);
        }
    }
}
 
Example 19
Source File: ProjectExtensionProperties.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void store() throws IOException {
    final FileObject projPropsFO = project.getProjectDirectory().getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH);
    if (projPropsFO == null) {
        return;
    }
    final EditableProperties ep = new EditableProperties(true);

    try {
        final InputStream is = projPropsFO.getInputStream();
        ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {

            @Override
            public Void run() throws Exception {
                try {
                    ep.load(is);
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
                putProperties(properties, ep);
                OutputStream os = null;
                FileLock lock = null;
                try {
                    lock = projPropsFO.lock();
                    os = projPropsFO.getOutputStream(lock);
                    ep.store(os);
                } finally {
                    if (lock != null) {
                        lock.releaseLock();
                    }
                    if (os != null) {
                        os.close();
                    }
                }
                return null;
            }
        });
    } catch (MutexException mux) {
        throw (IOException) mux.getException();
    }

}
 
Example 20
Source File: AntBuildExtender.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void setValueOfProperty(final String propName, final String value, final boolean add) throws IOException {
    try {
        final FileObject projPropsFO = implementation.getOwningProject().getProjectDirectory().getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH);
        final InputStream is = projPropsFO.getInputStream();
        ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
            public @Override Void run() throws Exception {
                EditableProperties editableProps = new EditableProperties(true);
                
                try {
                    editableProps.load(is);
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
                
                String libIDs[] = new String[0];
                String savedPropVal = editableProps.getProperty(propName);
                if (savedPropVal != null) {
                    libIDs = savedPropVal.split(",");
                }
                Set<String> libIDSet = new TreeSet<String>(Arrays.asList(libIDs));
                if (add) {
                    libIDSet.add(value);
                } else {
                    libIDSet.remove(value);
                }
                String newLibIDs[] = libIDSet.toArray(new String[libIDSet.size()]);
                StringBuilder propValue = new StringBuilder();
                for (String newLibID : newLibIDs) {
                    propValue.append(newLibID);
                    propValue.append(",");
                }
                propValue.delete(propValue.length() - 1, propValue.length());
                
                editableProps.setProperty(propName, propValue.toString());
                
                OutputStream os = projPropsFO.getOutputStream();
                try {
                    editableProps.store(os);
                } finally {
                    os.close();
                }
                return null;
            }
        });
    } catch (MutexException mux) {
        throw (IOException) mux.getException();
    }
}