* Allow subclasses of ConfigRepository to create the ObjectInputStream used to
read in serialized config values. * Use a transitioning flag in DatabaseConfigRegistry to indicate that it should immediately write out config values after reading them in. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4981 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -23,19 +23,17 @@ package com.threerings.admin.server;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import javax.swing.JEditorPane;
|
|
||||||
|
|
||||||
import com.samskivert.io.ByteArrayOutInputStream;
|
import com.samskivert.io.ByteArrayOutInputStream;
|
||||||
import com.samskivert.util.StringUtil;
|
import com.samskivert.util.StringUtil;
|
||||||
|
import com.threerings.admin.Log;
|
||||||
import com.threerings.io.ObjectInputStream;
|
import com.threerings.io.ObjectInputStream;
|
||||||
import com.threerings.io.ObjectOutputStream;
|
import com.threerings.io.ObjectOutputStream;
|
||||||
import com.threerings.io.Streamable;
|
import com.threerings.io.Streamable;
|
||||||
|
|
||||||
import com.threerings.presents.dobj.AccessController;
|
import com.threerings.presents.dobj.AccessController;
|
||||||
import com.threerings.presents.dobj.AttributeChangeListener;
|
import com.threerings.presents.dobj.AttributeChangeListener;
|
||||||
import com.threerings.presents.dobj.AttributeChangedEvent;
|
import com.threerings.presents.dobj.AttributeChangedEvent;
|
||||||
@@ -49,8 +47,6 @@ import com.threerings.presents.dobj.EntryUpdatedEvent;
|
|||||||
import com.threerings.presents.dobj.ObjectAccessException;
|
import com.threerings.presents.dobj.ObjectAccessException;
|
||||||
import com.threerings.presents.dobj.SetListener;
|
import com.threerings.presents.dobj.SetListener;
|
||||||
|
|
||||||
import com.threerings.admin.Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides a registry of configuration distributed objects. Using distributed object to store
|
* Provides a registry of configuration distributed objects. Using distributed object to store
|
||||||
* runtime configuration data can be exceptionally useful in that clients (with admin privileges)
|
* runtime configuration data can be exceptionally useful in that clients (with admin privileges)
|
||||||
@@ -110,6 +106,14 @@ public abstract class ConfigRegistry
|
|||||||
*/
|
*/
|
||||||
protected abstract ObjectRecord createObjectRecord (String path, DObject object);
|
protected abstract ObjectRecord createObjectRecord (String path, DObject object);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an ObjectInputStream to read serialized config entries.
|
||||||
|
*/
|
||||||
|
protected ObjectInputStream createObjectInputStream (InputStream bin)
|
||||||
|
{
|
||||||
|
return new ObjectInputStream(bin);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains all necessary info for a configuration object registration.
|
* Contains all necessary info for a configuration object registration.
|
||||||
*/
|
*/
|
||||||
@@ -281,7 +285,7 @@ public abstract class ConfigRegistry
|
|||||||
try {
|
try {
|
||||||
ByteArrayInputStream bin =
|
ByteArrayInputStream bin =
|
||||||
new ByteArrayInputStream(StringUtil.unhexlate(value));
|
new ByteArrayInputStream(StringUtil.unhexlate(value));
|
||||||
ObjectInputStream oin = new ObjectInputStream(bin);
|
ObjectInputStream oin = createObjectInputStream(bin);
|
||||||
field.set(object, oin.readObject());
|
field.set(object, oin.readObject());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.warning("Failure decoding config value [type=" + type +
|
Log.warning("Failure decoding config value [type=" + type +
|
||||||
|
|||||||
@@ -52,7 +52,21 @@ public class DatabaseConfigRegistry extends ConfigRegistry
|
|||||||
public DatabaseConfigRegistry (PersistenceContext ctx, Invoker invoker)
|
public DatabaseConfigRegistry (PersistenceContext ctx, Invoker invoker)
|
||||||
throws PersistenceException
|
throws PersistenceException
|
||||||
{
|
{
|
||||||
this(ctx, invoker, "");
|
this(ctx, invoker, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a configuration registry and prepares it for operation.
|
||||||
|
*
|
||||||
|
* @param ctx will provide access to our database.
|
||||||
|
* @param invoker this will be used to perform all database activity (except first time
|
||||||
|
* initialization) so as to avoid blocking the distributed object thread.
|
||||||
|
* @param transitioning if the values in the database need to be transitioned to a new format
|
||||||
|
*/
|
||||||
|
public DatabaseConfigRegistry (PersistenceContext ctx, Invoker invoker, boolean transitioning)
|
||||||
|
throws PersistenceException
|
||||||
|
{
|
||||||
|
this(ctx, invoker, "", transitioning);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -62,14 +76,33 @@ public class DatabaseConfigRegistry extends ConfigRegistry
|
|||||||
* @param invoker this will be used to perform all database activity (except first time
|
* @param invoker this will be used to perform all database activity (except first time
|
||||||
* initialization) so as to avoid blocking the distributed object thread.
|
* initialization) so as to avoid blocking the distributed object thread.
|
||||||
* @param node if this config registry is accessed by multiple servers which wish to maintain
|
* @param node if this config registry is accessed by multiple servers which wish to maintain
|
||||||
* seperate configs, then specificy a node for each server
|
* separate configs, then specify a node for each server
|
||||||
*/
|
*/
|
||||||
public DatabaseConfigRegistry (PersistenceContext ctx, Invoker invoker, String node)
|
public DatabaseConfigRegistry (PersistenceContext ctx, Invoker invoker, String node)
|
||||||
throws PersistenceException
|
throws PersistenceException
|
||||||
|
{
|
||||||
|
this(ctx, invoker, node, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a configuration registry and prepares it for operation.
|
||||||
|
*
|
||||||
|
* @param ctx will provide access to our database.
|
||||||
|
* @param invoker this will be used to perform all database activity (except first time
|
||||||
|
* initialization) so as to avoid blocking the distributed object thread.
|
||||||
|
* @param node if this config registry is accessed by multiple servers which wish to maintain
|
||||||
|
* separate configs, then specify a node for each server
|
||||||
|
* @param transitioning if the values in the database need to be transitioned to a new format
|
||||||
|
*/
|
||||||
|
public DatabaseConfigRegistry (PersistenceContext ctx, Invoker invoker, String node,
|
||||||
|
boolean transitioning)
|
||||||
|
throws PersistenceException
|
||||||
{
|
{
|
||||||
_repo = new ConfigRepository(ctx);
|
_repo = new ConfigRepository(ctx);
|
||||||
_invoker = invoker;
|
_invoker = invoker;
|
||||||
_node = StringUtil.isBlank(node) ? "" : node;
|
_node = StringUtil.isBlank(node) ? "" : node;
|
||||||
|
_transitioning = transitioning;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from ConfigRegistry
|
@Override // from ConfigRegistry
|
||||||
@@ -93,7 +126,7 @@ public class DatabaseConfigRegistry extends ConfigRegistry
|
|||||||
// be completely initialized when we return from this call so that subsequent systems
|
// be completely initialized when we return from this call so that subsequent systems
|
||||||
// can predictably make use of the configuration information that we load
|
// can predictably make use of the configuration information that we load
|
||||||
try {
|
try {
|
||||||
_data = _repo.loadConfig(_node, _path);
|
_data = _repo.loadConfig(_node, _path, _transitioning);
|
||||||
} catch (PersistenceException pe) {
|
} catch (PersistenceException pe) {
|
||||||
Log.warning("Failed to load object configuration [path=" + _path + "].");
|
Log.warning("Failed to load object configuration [path=" + _path + "].");
|
||||||
Log.logStackTrace(pe);
|
Log.logStackTrace(pe);
|
||||||
@@ -272,6 +305,7 @@ public class DatabaseConfigRegistry extends ConfigRegistry
|
|||||||
protected HashMap<String,String> _data;
|
protected HashMap<String,String> _data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean _transitioning;
|
||||||
protected ConfigRepository _repo;
|
protected ConfigRepository _repo;
|
||||||
protected Invoker _invoker;
|
protected Invoker _invoker;
|
||||||
protected String _node;
|
protected String _node;
|
||||||
|
|||||||
@@ -45,16 +45,22 @@ public class ConfigRepository extends DepotRepository
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads up the configuration data for the specified object.
|
* Loads up the configuration data for the specified object.
|
||||||
*
|
*
|
||||||
|
* @param transitioning - if true, the read in value will be written out again immediately to
|
||||||
|
* transition the stored data to a new format
|
||||||
|
*
|
||||||
* @return a map containing field/value pairs for all stored configuration data.
|
* @return a map containing field/value pairs for all stored configuration data.
|
||||||
*/
|
*/
|
||||||
public HashMap<String,String> loadConfig (String node, String object)
|
public HashMap<String,String> loadConfig (String node, String object, boolean transitioning)
|
||||||
throws PersistenceException
|
throws PersistenceException
|
||||||
{
|
{
|
||||||
HashMap<String,String> data = new HashMap<String,String>();
|
HashMap<String, String> data = new HashMap<String, String>();
|
||||||
Where where = new Where(ConfigRecord.OBJECT_C, object, ConfigRecord.NODE_C, node);
|
Where where = new Where(ConfigRecord.OBJECT_C, object, ConfigRecord.NODE_C, node);
|
||||||
for (ConfigRecord record : findAll(ConfigRecord.class, where)) {
|
for (ConfigRecord record : findAll(ConfigRecord.class, where)) {
|
||||||
data.put(record.field, record.value.toString());
|
data.put(record.field, record.value);
|
||||||
|
if (transitioning) {
|
||||||
|
updateConfig(node, object, record.field, record.value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@@ -73,4 +79,6 @@ public class ConfigRepository extends DepotRepository
|
|||||||
{
|
{
|
||||||
classes.add(ConfigRecord.class);
|
classes.add(ConfigRecord.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean _transitioning;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user