Search in sources :

Example 1 with BackingStoreException

use of org.osgi.service.prefs.BackingStoreException in project che by eclipse.

the class JavaProject method setOptions.

@Override
public void setOptions(Map newOptions) {
    IEclipsePreferences projectPreferences = getEclipsePreferences();
    if (projectPreferences == null)
        return;
    try {
        if (newOptions == null) {
            projectPreferences.clear();
        } else {
            Iterator entries = newOptions.entrySet().iterator();
            JavaModelManager javaModelManager = getJavaModelManager();
            while (entries.hasNext()) {
                Map.Entry entry = (Map.Entry) entries.next();
                String key = (String) entry.getKey();
                String value = (String) entry.getValue();
                javaModelManager.storePreference(key, value, projectPreferences, newOptions);
            }
            // reset to default all options not in new map
            // @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=26255
            // @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=49691
            String[] pNames = projectPreferences.keys();
            int ln = pNames.length;
            for (int i = 0; i < ln; i++) {
                String key = pNames[i];
                if (!newOptions.containsKey(key)) {
                    // old preferences => remove from preferences table
                    projectPreferences.remove(key);
                }
            }
        }
        // persist options
        projectPreferences.flush();
        // flush cache immediately
        try {
            getPerProjectInfo().options = null;
        } catch (JavaModelException e) {
        // do nothing
        }
    } catch (BackingStoreException e) {
    // problem with pref store - quietly ignore
    }
}
Also used : JavaModelManager.getJavaModelManager(org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) JavaModelException(org.eclipse.jdt.core.JavaModelException) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) Iterator(java.util.Iterator) BackingStoreException(org.osgi.service.prefs.BackingStoreException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with BackingStoreException

use of org.osgi.service.prefs.BackingStoreException in project che by eclipse.

the class ChePreferences method loadProperties.

protected static Properties loadProperties(String location) throws BackingStoreException {
    //        if (DEBUG_PREFERENCE_GENERAL)
    //            PrefsMessages.message("Loading preferences from file: " + location); //$NON-NLS-1$
    InputStream input = null;
    Properties result = new Properties();
    try {
        input = new SafeFileInputStream(new File(location));
        result.load(input);
    } catch (FileNotFoundException e) {
        //                PrefsMessages.message("Preference file does not exist: " + location); //$NON-NLS-1$
        return result;
    } catch (IOException e) {
        //            log(new Status(IStatus.INFO, PrefsMessages.OWNER_NAME, IStatus.INFO, message, e));
        throw new BackingStoreException(e.getMessage(), e);
    } finally {
        if (input != null)
            try {
                input.close();
            } catch (IOException e) {
            // ignore
            }
    }
    return result;
}
Also used : SafeFileInputStream(org.eclipse.core.internal.preferences.SafeFileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) BackingStoreException(org.osgi.service.prefs.BackingStoreException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) SafeFileInputStream(org.eclipse.core.internal.preferences.SafeFileInputStream)

Example 3 with BackingStoreException

use of org.osgi.service.prefs.BackingStoreException in project che by eclipse.

the class ChePreferences method write.

/*
 * Helper method to persist a Properties object to the filesystem. We use this
 * helper so we can remove the date/timestamp that Properties#store always
 * puts in the file.
 */
protected static void write(Properties properties, String location) throws BackingStoreException {
    // create the parent directories if they don't exist
    //        File parentFile = new File(location);
    //        if (parentFile == null)
    //            return;
    //        parentFile.mkdirs();
    OutputStream output = null;
    try {
        File file = new File(location);
        if (!file.exists()) {
            File parentFile = file.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            file.createNewFile();
        }
        output = new SafeFileOutputStream(file);
        //$NON-NLS-1$
        output.write(removeTimestampFromTable(properties).getBytes("UTF-8"));
        output.flush();
    } catch (IOException e) {
        //            String message = NLS.bind(PrefsMessages.preferences_saveException, location);
        ResourcesPlugin.log(new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, "preferences_saveException", e));
        throw new BackingStoreException("preferences_saveException");
    } finally {
        if (output != null)
            try {
                output.close();
            } catch (IOException e) {
            // ignore
            }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SafeFileOutputStream(org.eclipse.core.internal.preferences.SafeFileOutputStream) OutputStream(java.io.OutputStream) BackingStoreException(org.osgi.service.prefs.BackingStoreException) IOException(java.io.IOException) SafeFileOutputStream(org.eclipse.core.internal.preferences.SafeFileOutputStream) File(java.io.File)

Example 4 with BackingStoreException

use of org.osgi.service.prefs.BackingStoreException in project flux by eclipse.

the class Activator method removeConnectedProjectPreference.

private void removeConnectedProjectPreference(String projectName) {
    IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(PLUGIN_ID);
    String currentPreferences = preferences.get(CONNECTED_PROJECTS_ID, "");
    String[] projects = StringUtils.split(currentPreferences, ";");
    Collection<String> retainedProjects = new HashSet<String>();
    for (String existingProjectName : projects) {
        if (!existingProjectName.equals(projectName)) {
            retainedProjects.add(existingProjectName);
        }
    }
    String newPreferences = StringUtils.join(retainedProjects, ";");
    preferences.put(CONNECTED_PROJECTS_ID, newPreferences);
    try {
        preferences.flush();
    } catch (BackingStoreException e) {
    // We really don't care that much..
    }
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) BackingStoreException(org.osgi.service.prefs.BackingStoreException) HashSet(java.util.HashSet)

Example 5 with BackingStoreException

use of org.osgi.service.prefs.BackingStoreException in project azure-tools-for-java by Microsoft.

the class ApplicationInsightsPreferences method savePreferences.

/**
	 * Stores application insights resources list
	 * in preference file in the form of byte array. 
	 */
private void savePreferences() {
    try {
        Preferences prefs = PluginUtil.getPrefs(PREF_FILE);
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput output = new ObjectOutputStream(buffer);
        List<ApplicationInsightsResource> data = ApplicationInsightsResourceRegistry.getAppInsightsResrcList();
        ApplicationInsightsResource[] dataArray = data.stream().filter(a -> !a.isImported()).sorted().toArray(ApplicationInsightsResource[]::new);
        /*
			 * Sort list according to application insights resource name.
			 * Save only manually added resources
			 */
        try {
            output.writeObject(dataArray);
        } finally {
            output.close();
        }
        prefs.putByteArray(PREF_KEY, buffer.toByteArray());
        prefs.flush();
    } catch (BackingStoreException e) {
        Activator.getDefault().log(e.getMessage(), e);
    } catch (IOException e) {
        Activator.getDefault().log(e.getMessage(), e);
    }
}
Also used : ApplicationInsightsResource(com.microsoft.applicationinsights.preference.ApplicationInsightsResource) ObjectOutput(java.io.ObjectOutput) BackingStoreException(org.osgi.service.prefs.BackingStoreException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Preferences(org.osgi.service.prefs.Preferences) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

BackingStoreException (org.osgi.service.prefs.BackingStoreException)23 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)10 Preferences (org.osgi.service.prefs.Preferences)8 IOException (java.io.IOException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 ProjectScope (org.eclipse.core.resources.ProjectScope)2 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 JavaModelManager.getJavaModelManager (org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager)2 ApplicationInsightsResource (com.microsoft.applicationinsights.preference.ApplicationInsightsResource)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 ObjectOutput (java.io.ObjectOutput)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1