use of java.util.prefs.Preferences in project cryptomator by cryptomator.
the class SingleInstanceManager method getRemoteInstance.
/**
* Checks if there is a valid port at
* {@link Preferences#userNodeForPackage(Class)} for {@link Cryptomator} under the
* given applicationKey, tries to connect to the port at the loopback
* address and checks if the port identifies with the applicationKey.
*
* @param applicationKey
* key used to load the port and check the identity of the
* connection.
* @return
*/
public static Optional<RemoteInstance> getRemoteInstance(String applicationKey) {
Optional<Integer> port = getSavedPort(applicationKey);
if (!port.isPresent()) {
return Optional.empty();
}
SocketChannel channel = null;
boolean close = true;
try {
channel = SocketChannel.open();
channel.configureBlocking(false);
LOG.debug("connecting to instance {}", port.get());
channel.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port.get()));
SocketChannel fChannel = channel;
if (!TimeoutTask.attempt(t -> fChannel.finishConnect(), 1000, 10)) {
return Optional.empty();
}
LOG.debug("connected to instance {}", port.get());
final byte[] bytes = applicationKey.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(bytes.length);
tryFill(channel, buf, 1000);
if (buf.hasRemaining()) {
return Optional.empty();
}
buf.flip();
for (int i = 0; i < bytes.length; i++) {
if (buf.get() != bytes[i]) {
return Optional.empty();
}
}
close = false;
return Optional.of(new RemoteInstance(channel));
} catch (Exception e) {
return Optional.empty();
} finally {
if (close) {
IOUtils.closeQuietly(channel);
}
}
}
use of java.util.prefs.Preferences in project qi4j-sdk by Qi4j.
the class ListPreferencesNodes method printNode.
private static void printNode(Preferences node, String indent) throws BackingStoreException {
System.out.print(indent);
String name = node.name();
if ("".equals(name)) {
name = "/";
}
System.out.print(name);
String[] nodes = node.keys();
if (nodes.length > 0) {
System.out.print(" { ");
boolean first = true;
for (String key : nodes) {
if (!first) {
System.out.print(", ");
}
first = false;
System.out.print(key);
}
System.out.print(" }");
}
System.out.println();
for (String childName : node.childrenNames()) {
Preferences child = node.node(childName);
printNode(child, indent + " ");
}
}
use of java.util.prefs.Preferences in project qi4j-sdk by Qi4j.
the class PreferencesEntityStoreMixin method applyChanges.
@Override
public StateCommitter applyChanges(final EntityStoreUnitOfWork unitofwork, final Iterable<EntityState> state) {
return new StateCommitter() {
@Override
public void commit() {
try {
synchronized (root) {
for (EntityState entityState : state) {
DefaultEntityState state = (DefaultEntityState) entityState;
if (state.status().equals(EntityStatus.NEW)) {
Preferences entityPrefs = root.node(state.identity().identity());
writeEntityState(state, entityPrefs, unitofwork.identity(), unitofwork.currentTime());
} else if (state.status().equals(EntityStatus.UPDATED)) {
Preferences entityPrefs = root.node(state.identity().identity());
writeEntityState(state, entityPrefs, unitofwork.identity(), unitofwork.currentTime());
} else if (state.status().equals(EntityStatus.REMOVED)) {
root.node(state.identity().identity()).removeNode();
}
}
root.flush();
}
} catch (BackingStoreException e) {
throw new EntityStoreException(e);
}
}
@Override
public void cancel() {
}
};
}
use of java.util.prefs.Preferences in project qi4j-sdk by Qi4j.
the class PreferenceEntityStoreAssembler method assemble.
@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
String applicationName = module.layer().application().name();
Preferences root = Preferences.userRoot();
Preferences node = root.node(applicationName);
PreferencesEntityStoreInfo info = new PreferencesEntityStoreInfo(node);
ServiceDeclaration service = module.services(PreferencesEntityStoreService.class).setMetaInfo(info).visibleIn(visibility()).instantiateOnStartup();
if (hasIdentity()) {
service.identifiedBy(identity());
}
module.services(UuidIdentityGeneratorService.class).visibleIn(visibility());
}
use of java.util.prefs.Preferences in project jmonkeyengine by jMonkeyEngine.
the class AppSettings method load.
/**
* Loads settings previously saved in the Java preferences.
*
* @param preferencesKey The preferencesKey previously used to save the settings.
* @throws BackingStoreException If an exception occurs with the preferences
*
* @see #save(java.lang.String)
*/
public void load(String preferencesKey) throws BackingStoreException {
Preferences prefs = Preferences.userRoot().node(preferencesKey);
String[] keys = prefs.keys();
if (keys != null) {
for (String key : keys) {
if (key.charAt(1) == '_') {
// Try loading using new method
switch(key.charAt(0)) {
case 'I':
put(key.substring(2), prefs.getInt(key, (Integer) 0));
break;
case 'F':
put(key.substring(2), prefs.getFloat(key, (Float) 0f));
break;
case 'S':
put(key.substring(2), prefs.get(key, (String) null));
break;
case 'B':
put(key.substring(2), prefs.getBoolean(key, (Boolean) false));
break;
default:
throw new UnsupportedOperationException("Undefined setting type: " + key.charAt(0));
}
} else {
// Use old method for compatibility with older preferences
// TODO: Remove when no longer neccessary
Object defaultValue = defaults.get(key);
if (defaultValue instanceof Integer) {
put(key, prefs.getInt(key, (Integer) defaultValue));
} else if (defaultValue instanceof String) {
put(key, prefs.get(key, (String) defaultValue));
} else if (defaultValue instanceof Boolean) {
put(key, prefs.getBoolean(key, (Boolean) defaultValue));
}
}
}
}
}
Aggregations