Made changes to the StorageSystem (IOExceptions, default serialization
manager, etc). Minor refactoring.
This commit is contained in:
parent
80aa58b590
commit
bd82695d2d
|
@ -15,10 +15,10 @@
|
|||
*/
|
||||
package dorkbox.network;
|
||||
|
||||
import dorkbox.network.util.store.SettingsStore;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import dorkbox.network.store.SettingsStore;
|
||||
|
||||
public
|
||||
class Configuration {
|
||||
/**
|
||||
|
|
|
@ -43,9 +43,9 @@ import dorkbox.network.connection.wrapper.ChannelWrapper;
|
|||
import dorkbox.network.pipeline.KryoEncoder;
|
||||
import dorkbox.network.pipeline.KryoEncoderCrypto;
|
||||
import dorkbox.network.rmi.RmiBridge;
|
||||
import dorkbox.network.store.NullSettingsStore;
|
||||
import dorkbox.network.store.SettingsStore;
|
||||
import dorkbox.network.util.CryptoSerializationManager;
|
||||
import dorkbox.network.util.store.NullSettingsStore;
|
||||
import dorkbox.network.util.store.SettingsStore;
|
||||
import dorkbox.util.OS;
|
||||
import dorkbox.util.Property;
|
||||
import dorkbox.util.crypto.CryptoECC;
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
package dorkbox.network.connection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
@ -23,11 +22,10 @@ import java.util.Map;
|
|||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
|
||||
import dorkbox.network.util.store.SettingsStore;
|
||||
import dorkbox.network.store.DB_Server;
|
||||
import dorkbox.network.store.SettingsStore;
|
||||
import dorkbox.util.SerializationManager;
|
||||
import dorkbox.util.bytes.ByteArrayWrapper;
|
||||
import dorkbox.util.database.DB_Server;
|
||||
import dorkbox.util.database.DatabaseStorage;
|
||||
import dorkbox.util.exceptions.SecurityException;
|
||||
import dorkbox.util.storage.Storage;
|
||||
import dorkbox.util.storage.StorageSystem;
|
||||
|
@ -38,7 +36,6 @@ import dorkbox.util.storage.StorageSystem;
|
|||
*/
|
||||
final
|
||||
class PropertyStore extends SettingsStore {
|
||||
|
||||
private Storage storage;
|
||||
private Map<ByteArrayWrapper, DB_Server> servers;
|
||||
|
||||
|
@ -52,7 +49,7 @@ class PropertyStore extends SettingsStore {
|
|||
*/
|
||||
@Override
|
||||
public
|
||||
void init(final SerializationManager serializationManager, final Storage ignored) throws IOException {
|
||||
void init(final SerializationManager serializationManager, final Storage ignored) {
|
||||
// make sure our custom types are registered
|
||||
// only register if not ALREADY initialized, since we can initialize in the server and in the client. This creates problems if
|
||||
// running inside the same JVM (we don't permit it)
|
||||
|
@ -63,9 +60,9 @@ class PropertyStore extends SettingsStore {
|
|||
}
|
||||
|
||||
this.storage = StorageSystem.Memory()
|
||||
.make();
|
||||
.build();
|
||||
|
||||
servers = this.storage.getAndPut(DatabaseStorage.SERVERS, new HashMap<ByteArrayWrapper, DB_Server>(16));
|
||||
servers = this.storage.getAndPut(DB_Server.STORAGE_KEY, new HashMap<ByteArrayWrapper, DB_Server>(16));
|
||||
|
||||
DB_Server localServer = servers.get(DB_Server.IP_SELF); // this will always be null and is here to help people that copy/paste code
|
||||
if (localServer == null) {
|
||||
|
@ -73,7 +70,7 @@ class PropertyStore extends SettingsStore {
|
|||
servers.put(DB_Server.IP_SELF, localServer);
|
||||
|
||||
// have to always specify what we are saving
|
||||
this.storage.putAndSave(DatabaseStorage.SERVERS, servers);
|
||||
this.storage.putAndSave(DB_Server.STORAGE_KEY, servers);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,7 +98,7 @@ class PropertyStore extends SettingsStore {
|
|||
.setPrivateKey(serverPrivateKey);
|
||||
|
||||
// have to always specify what we are saving
|
||||
storage.putAndSave(DatabaseStorage.SERVERS, servers);
|
||||
storage.putAndSave(DB_Server.STORAGE_KEY, servers);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -128,7 +125,7 @@ class PropertyStore extends SettingsStore {
|
|||
.setPublicKey(serverPublicKey);
|
||||
|
||||
// have to always specify what we are saving
|
||||
storage.putAndSave(DatabaseStorage.SERVERS, servers);
|
||||
storage.putAndSave(DB_Server.STORAGE_KEY, servers);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -153,7 +150,7 @@ class PropertyStore extends SettingsStore {
|
|||
localServer.setSalt(bytes);
|
||||
|
||||
// have to always specify what we are saving
|
||||
storage.putAndSave(DatabaseStorage.SERVERS, servers);
|
||||
storage.putAndSave(DB_Server.STORAGE_KEY, servers);
|
||||
}
|
||||
|
||||
return salt;
|
||||
|
|
138
src/dorkbox/network/store/DB_Server.java
Normal file
138
src/dorkbox/network/store/DB_Server.java
Normal file
|
@ -0,0 +1,138 @@
|
|||
/*
|
||||
* Copyright 2014 dorkbox, llc
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package dorkbox.network.store;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
|
||||
import dorkbox.util.bytes.ByteArrayWrapper;
|
||||
import dorkbox.util.storage.StorageKey;
|
||||
|
||||
public
|
||||
class DB_Server {
|
||||
/**
|
||||
* The storage key used to save all server connections
|
||||
*/
|
||||
public static final StorageKey STORAGE_KEY = new StorageKey("servers");
|
||||
|
||||
/**
|
||||
* Address 0.0.0.0/32 may be used as a source address for this host on this network.
|
||||
*/
|
||||
public static final ByteArrayWrapper IP_SELF = ByteArrayWrapper.wrap(new byte[] {0, 0, 0, 0});
|
||||
|
||||
// salt + IP address is used for equals!
|
||||
private byte[] ipAddress;
|
||||
private byte[] salt;
|
||||
|
||||
private ECPrivateKeyParameters privateKey;
|
||||
private ECPublicKeyParameters publicKey;
|
||||
|
||||
// must have empty constructor
|
||||
public
|
||||
DB_Server() {
|
||||
}
|
||||
|
||||
public
|
||||
byte[] getAddress() {
|
||||
if (this.ipAddress == null) {
|
||||
return null;
|
||||
}
|
||||
return this.ipAddress;
|
||||
}
|
||||
|
||||
public
|
||||
void setAddress(byte[] ipAddress) {
|
||||
this.ipAddress = ipAddress;
|
||||
}
|
||||
|
||||
public
|
||||
byte[] getSalt() {
|
||||
return this.salt;
|
||||
}
|
||||
|
||||
public
|
||||
void setSalt(byte[] salt) {
|
||||
this.salt = salt;
|
||||
}
|
||||
|
||||
|
||||
public
|
||||
ECPrivateKeyParameters getPrivateKey() {
|
||||
return this.privateKey;
|
||||
}
|
||||
|
||||
public
|
||||
void setPrivateKey(ECPrivateKeyParameters privateKey) {
|
||||
this.privateKey = privateKey;
|
||||
}
|
||||
|
||||
|
||||
public
|
||||
ECPublicKeyParameters getPublicKey() {
|
||||
return this.publicKey;
|
||||
}
|
||||
|
||||
public
|
||||
void setPublicKey(ECPublicKeyParameters publicKey) {
|
||||
this.publicKey = publicKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + (this.ipAddress == null ? 0 : Arrays.hashCode(this.ipAddress));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
DB_Server other = (DB_Server) obj;
|
||||
if (this.ipAddress == null) {
|
||||
if (other.ipAddress != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (!Arrays.equals(this.ipAddress, other.ipAddress)) {
|
||||
return false;
|
||||
}
|
||||
return Arrays.equals(this.salt, other.salt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
String toString() {
|
||||
byte[] bytes = this.ipAddress;
|
||||
if (bytes != null) {
|
||||
return "DB_Server " + Arrays.toString(bytes);
|
||||
}
|
||||
|
||||
return "DB_Server [no-ip-set]";
|
||||
}
|
||||
}
|
|
@ -13,16 +13,16 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package dorkbox.network.util.store;
|
||||
package dorkbox.network.store;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
|
||||
import dorkbox.util.SerializationManager;
|
||||
import dorkbox.util.exceptions.SecurityException;
|
||||
import dorkbox.util.storage.Storage;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public
|
||||
class NullSettingsStore extends SettingsStore {
|
||||
|
@ -31,7 +31,7 @@ class NullSettingsStore extends SettingsStore {
|
|||
|
||||
@Override
|
||||
public
|
||||
void init(final SerializationManager serializationManager, final Storage storage) throws IOException {
|
||||
void init(final SerializationManager serializationManager, final Storage storage) {
|
||||
|
||||
}
|
||||
|
|
@ -13,18 +13,17 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package dorkbox.network.util.store;
|
||||
package dorkbox.network.store;
|
||||
|
||||
import dorkbox.util.OS;
|
||||
import dorkbox.util.SerializationManager;
|
||||
import dorkbox.util.exceptions.SecurityException;
|
||||
import dorkbox.util.storage.Storage;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import dorkbox.util.OS;
|
||||
import dorkbox.util.SerializationManager;
|
||||
import dorkbox.util.exceptions.SecurityException;
|
||||
import dorkbox.util.storage.Storage;
|
||||
|
||||
/**
|
||||
* This class provides a way for the network stack to use the server's database, instead of a property file (which it uses when stand-alone)
|
||||
|
@ -39,7 +38,7 @@ class SettingsStore {
|
|||
* Initialize the settingsStore with the provided serialization manager.
|
||||
*/
|
||||
public abstract
|
||||
void init(SerializationManager serializationManager, Storage storage) throws IOException;
|
||||
void init(SerializationManager serializationManager, Storage storage);
|
||||
|
||||
|
||||
private static String getCallingClass() {
|
|
@ -16,13 +16,12 @@
|
|||
package dorkbox.network.connection;
|
||||
|
||||
|
||||
import dorkbox.network.BaseTest;
|
||||
import dorkbox.util.exceptions.SecurityException;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
import dorkbox.network.BaseTest;
|
||||
import dorkbox.util.exceptions.SecurityException;
|
||||
|
||||
|
||||
public
|
||||
|
@ -32,11 +31,7 @@ class PropertyStoreAccessTest extends BaseTest {
|
|||
public
|
||||
void testAccess() throws SecurityException {
|
||||
PropertyStore store = new PropertyStore();
|
||||
try {
|
||||
store.init(null, null);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
store.init(null, null);
|
||||
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
|
|
Loading…
Reference in New Issue
Block a user