Search in sources :

Example 46 with BackingStoreException

use of java.util.prefs.BackingStoreException in project JMRI by JMRI.

the class GuiLafPreferencesManager method savePreferences.

@Override
public void savePreferences(Profile profile) {
    Preferences preferences = ProfileUtils.getPreferences(profile, this.getClass(), true);
    preferences.put(LOCALE, this.getLocale().toLanguageTag());
    preferences.put(LOOK_AND_FEEL, this.getLookAndFeel());
    int temp = this.getFontSize();
    if (temp == this.getDefaultFontSize()) {
        temp = 0;
    }
    if (temp != preferences.getInt(FONT_SIZE, -1)) {
        preferences.putInt(FONT_SIZE, temp);
    }
    preferences.putBoolean(NONSTANDARD_MOUSE_EVENT, this.isNonStandardMouseEvent());
    // use graphic icons in bean table state column
    preferences.putBoolean(GRAPHICTABLESTATE, this.isGraphicTableState());
    preferences.putInt(SHOW_TOOL_TIP_TIME, this.getToolTipDismissDelay());
    try {
        preferences.sync();
    } catch (BackingStoreException ex) {
        log.error("Unable to save preferences.", ex);
    }
    this.setDirty(false);
}
Also used : BackingStoreException(java.util.prefs.BackingStoreException) Preferences(java.util.prefs.Preferences)

Example 47 with BackingStoreException

use of java.util.prefs.BackingStoreException in project qi4j-sdk by Qi4j.

the class PreferencesEntityStoreMixin method entityStateOf.

@Override
public EntityState entityStateOf(EntityStoreUnitOfWork unitOfWork, EntityReference identity) {
    try {
        DefaultEntityStoreUnitOfWork desuw = (DefaultEntityStoreUnitOfWork) unitOfWork;
        Module module = desuw.module();
        if (!root.nodeExists(identity.identity())) {
            throw new NoSuchEntityException(identity, UnknownType.class);
        }
        Preferences entityPrefs = root.node(identity.identity());
        String type = entityPrefs.get("type", null);
        EntityStatus status = EntityStatus.LOADED;
        EntityDescriptor entityDescriptor = module.entityDescriptor(type);
        if (entityDescriptor == null) {
            throw new EntityTypeNotFoundException(type);
        }
        Map<QualifiedName, Object> properties = new HashMap<>();
        Preferences propsPrefs = null;
        for (PropertyDescriptor persistentPropertyDescriptor : entityDescriptor.state().properties()) {
            if (persistentPropertyDescriptor.qualifiedName().name().equals("identity")) {
                // Fake identity property
                properties.put(persistentPropertyDescriptor.qualifiedName(), identity.identity());
                continue;
            }
            if (propsPrefs == null) {
                propsPrefs = entityPrefs.node("properties");
            }
            ValueType propertyType = persistentPropertyDescriptor.valueType();
            Class<?> mainType = propertyType.mainType();
            if (Number.class.isAssignableFrom(mainType)) {
                if (mainType.equals(Long.class)) {
                    properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, LONG_PARSER));
                } else if (mainType.equals(Integer.class)) {
                    properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, INT_PARSER));
                } else if (mainType.equals(Double.class)) {
                    properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, DOUBLE_PARSER));
                } else if (mainType.equals(Float.class)) {
                    properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, FLOAT_PARSER));
                } else {
                    // Load as string even though it's a number
                    String json = propsPrefs.get(persistentPropertyDescriptor.qualifiedName().name(), null);
                    Object value;
                    if (json == null) {
                        value = null;
                    } else {
                        value = valueSerialization.deserialize(persistentPropertyDescriptor.valueType(), json);
                    }
                    properties.put(persistentPropertyDescriptor.qualifiedName(), value);
                }
            } else if (mainType.equals(Boolean.class)) {
                Boolean initialValue = (Boolean) persistentPropertyDescriptor.initialValue(module);
                properties.put(persistentPropertyDescriptor.qualifiedName(), propsPrefs.getBoolean(persistentPropertyDescriptor.qualifiedName().name(), initialValue == null ? false : initialValue));
            } else if (propertyType instanceof ValueCompositeType || propertyType instanceof MapType || propertyType instanceof CollectionType || propertyType instanceof EnumType) {
                String json = propsPrefs.get(persistentPropertyDescriptor.qualifiedName().name(), null);
                Object value;
                if (json == null) {
                    value = null;
                } else {
                    value = valueSerialization.deserialize(persistentPropertyDescriptor.valueType(), json);
                }
                properties.put(persistentPropertyDescriptor.qualifiedName(), value);
            } else {
                String json = propsPrefs.get(persistentPropertyDescriptor.qualifiedName().name(), null);
                if (json == null) {
                    if (persistentPropertyDescriptor.initialValue(module) != null) {
                        properties.put(persistentPropertyDescriptor.qualifiedName(), persistentPropertyDescriptor.initialValue(module));
                    } else {
                        properties.put(persistentPropertyDescriptor.qualifiedName(), null);
                    }
                } else {
                    Object value = valueSerialization.deserialize(propertyType, json);
                    properties.put(persistentPropertyDescriptor.qualifiedName(), value);
                }
            }
        }
        // Associations
        Map<QualifiedName, EntityReference> associations = new HashMap<>();
        Preferences assocs = null;
        for (AssociationDescriptor associationType : entityDescriptor.state().associations()) {
            if (assocs == null) {
                assocs = entityPrefs.node("associations");
            }
            String associatedEntity = assocs.get(associationType.qualifiedName().name(), null);
            EntityReference value = associatedEntity == null ? null : EntityReference.parseEntityReference(associatedEntity);
            associations.put(associationType.qualifiedName(), value);
        }
        // ManyAssociations
        Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<>();
        Preferences manyAssocs = null;
        for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
            if (manyAssocs == null) {
                manyAssocs = entityPrefs.node("manyassociations");
            }
            List<EntityReference> references = new ArrayList<>();
            String entityReferences = manyAssocs.get(manyAssociationType.qualifiedName().name(), null);
            if (entityReferences == null) {
                // ManyAssociation not found, default to empty one
                manyAssociations.put(manyAssociationType.qualifiedName(), references);
            } else {
                String[] refs = entityReferences.split("\n");
                for (String ref : refs) {
                    EntityReference value = ref == null ? null : EntityReference.parseEntityReference(ref);
                    references.add(value);
                }
                manyAssociations.put(manyAssociationType.qualifiedName(), references);
            }
        }
        // NamedAssociations
        Map<QualifiedName, Map<String, EntityReference>> namedAssociations = new HashMap<>();
        Preferences namedAssocs = null;
        for (AssociationDescriptor namedAssociationType : entityDescriptor.state().namedAssociations()) {
            if (namedAssocs == null) {
                namedAssocs = entityPrefs.node("namedassociations");
            }
            Map<String, EntityReference> references = new LinkedHashMap<>();
            String entityReferences = namedAssocs.get(namedAssociationType.qualifiedName().name(), null);
            if (entityReferences == null) {
                // NamedAssociation not found, default to empty one
                namedAssociations.put(namedAssociationType.qualifiedName(), references);
            } else {
                String[] namedRefs = entityReferences.split("\n");
                if (namedRefs.length % 2 != 0) {
                    throw new EntityStoreException("Invalid NamedAssociation storage format");
                }
                for (int idx = 0; idx < namedRefs.length; idx += 2) {
                    String name = namedRefs[idx];
                    String ref = namedRefs[idx + 1];
                    references.put(name, EntityReference.parseEntityReference(ref));
                }
                namedAssociations.put(namedAssociationType.qualifiedName(), references);
            }
        }
        return new DefaultEntityState(desuw, entityPrefs.get("version", ""), entityPrefs.getLong("modified", unitOfWork.currentTime()), identity, status, entityDescriptor, properties, associations, manyAssociations, namedAssociations);
    } catch (ValueSerializationException | BackingStoreException e) {
        throw new EntityStoreException(e);
    }
}
Also used : EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) DefaultEntityState(org.qi4j.spi.entitystore.helpers.DefaultEntityState) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) MapType(org.qi4j.api.type.MapType) LinkedHashMap(java.util.LinkedHashMap) EnumType(org.qi4j.api.type.EnumType) CollectionType(org.qi4j.api.type.CollectionType) EntityReference(org.qi4j.api.entity.EntityReference) DefaultEntityStoreUnitOfWork(org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork) EntityStatus(org.qi4j.spi.entity.EntityStatus) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Preferences(java.util.prefs.Preferences) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueType(org.qi4j.api.type.ValueType) QualifiedName(org.qi4j.api.common.QualifiedName) BackingStoreException(java.util.prefs.BackingStoreException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) Module(org.qi4j.api.structure.Module) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 48 with BackingStoreException

use of java.util.prefs.BackingStoreException in project sling by apache.

the class Util method setPreference.

static void setPreference(final String name, final Object value, final boolean flush) {
    Preferences prefs = getPreferences();
    try {
        prefs.sync();
        if (value instanceof Long) {
            prefs.putLong(name, (Long) value);
        } else if (value instanceof Integer) {
            prefs.putInt(name, (Integer) value);
        } else if (value instanceof int[]) {
            String string = null;
            for (int val : (int[]) value) {
                if (string == null) {
                    string = String.valueOf(val);
                } else {
                    string += "," + val;
                }
            }
            prefs.put(name, string);
        } else if (value != null) {
            prefs.put(name, value.toString());
        }
        if (flush) {
            prefs.flush();
        }
    } catch (BackingStoreException ioe) {
    // ignore
    }
}
Also used : BackingStoreException(java.util.prefs.BackingStoreException) Preferences(java.util.prefs.Preferences)

Example 49 with BackingStoreException

use of java.util.prefs.BackingStoreException in project processdash by dtuma.

the class MRUPreferencesList method createNewNode.

private Preferences createNewNode(Map<String, String> nodeAttrs) {
    String key = getUnusedKey();
    Preferences result = prefsBase.node(key);
    // if we are recycling a node from the past, delete and recreate it.
    if (exists(result)) {
        try {
            result.removeNode();
            result.flush();
            result = prefsBase.node(key);
        } catch (BackingStoreException bse) {
        }
    }
    for (Map.Entry<String, String> e : nodeAttrs.entrySet()) {
        String name = e.getKey();
        String val = e.getValue();
        if (val != null)
            result.put(name, val);
    }
    result.putLong(FIRST_USED, System.currentTimeMillis());
    flush(result);
    return result;
}
Also used : BackingStoreException(java.util.prefs.BackingStoreException) Preferences(java.util.prefs.Preferences) Map(java.util.Map) HashMap(java.util.HashMap)

Example 50 with BackingStoreException

use of java.util.prefs.BackingStoreException in project processdash by dtuma.

the class PreferencesUtils method putCLOB.

/**
     * Store a character large object (CLOB) under the given key in the given
     * preference node.
     * 
     * @param prefs the preference node to store data under
     * @param key the key to associate data with
     * @param clob the CLOB value to store
     */
public static void putCLOB(Preferences prefs, String key, String clob) {
    if (clob == null)
        throw new NullPointerException("CLOB value cannot be null");
    // a single preference key, just store it normally.
    if (clob.length() < Preferences.MAX_VALUE_LENGTH && !isCheckVal(clob)) {
        removeCLOB(prefs, key);
        prefs.put(key, clob);
        return;
    }
    // First, write the checksum value.  Then, if anything below fails, the
    // worst case scenario is that we will be able to detect the corrupt
    // data and revert to a future default.
    prefs.put(key, getCheckVal(clob));
    // Create a child node specifically for "clob storage" for this key
    Preferences p = getClobStorage(prefs, key);
    try {
        p.clear();
    } catch (BackingStoreException bse) {
    // it would be nice to clear all data underneath the clob storage
    // node since it might reduce our resource utilization.  But even
    // if this fails, the code below ought to cover our bases.
    }
    // Split the CLOB into smaller pieces, and put them under our clob
    // storage node with the numerically ascending keys.
    int num = 0;
    while (true) {
        int len = clob.length();
        int pieceLen = Math.min(len, Preferences.MAX_VALUE_LENGTH - 1);
        p.put(Integer.toString(num++), clob.substring(0, pieceLen));
        if (len == pieceLen)
            break;
        clob = clob.substring(pieceLen);
    }
    // Delete the key that immediately follows our numerically ascending
    // keys.  This way, if the clear() call above failed, we will still
    // be able to detect the end of the stored data.
    p.remove(Integer.toString(num));
}
Also used : BackingStoreException(java.util.prefs.BackingStoreException) Preferences(java.util.prefs.Preferences)

Aggregations

BackingStoreException (java.util.prefs.BackingStoreException)58 Preferences (java.util.prefs.Preferences)47 File (java.io.File)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 AutoCompletePreferences (org.jabref.logic.autocompleter.AutoCompletePreferences)4 FieldContentParserPreferences (org.jabref.logic.bibtex.FieldContentParserPreferences)4 LatexFieldFormatterPreferences (org.jabref.logic.bibtex.LatexFieldFormatterPreferences)4 BibtexKeyPatternPreferences (org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences)4 CleanupPreferences (org.jabref.logic.cleanup.CleanupPreferences)4 ImportFormatPreferences (org.jabref.logic.importer.ImportFormatPreferences)4 JournalAbbreviationPreferences (org.jabref.logic.journals.JournalAbbreviationPreferences)4 LayoutFormatterPreferences (org.jabref.logic.layout.LayoutFormatterPreferences)4 FileLinkPreferences (org.jabref.logic.layout.format.FileLinkPreferences)4 NameFormatterPreferences (org.jabref.logic.layout.format.NameFormatterPreferences)4 ProxyPreferences (org.jabref.logic.net.ProxyPreferences)4 OpenOfficePreferences (org.jabref.logic.openoffice.OpenOfficePreferences)4 ProtectedTermsPreferences (org.jabref.logic.protectedterms.ProtectedTermsPreferences)4