Holy fruity beverage Batman! I'm taking the plunge and switching to Guice for

dependency resolution and management. Things are currently in a state of
transition, but the important patterns have been established and I'll aim to
sweep through all of Narya, Nenya and Vilya in the near future and do
everything properly.

Going through the other million-odd lines of code we have scattered across our
various projects that use these libraries is not something I plan to do, so
we'll be maintaining backward compatibility with the old static member method,
though I hope to strive toward eradication of that usage entirely in MSOY while
we still have a fighting chance.

Given that some of our projects will continue to use the static member method
in perpetuity, I'm not going to @Deprecate those fields because that would fill
their logs with so much spam that they'd have to turn off deprecation warnings
which would make life worse for them. So instead we'll settle for the big scary
comment warning people away from the old bits.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5153 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2008-06-07 17:44:48 +00:00
parent 443ad8e139
commit 3aefc93838
25 changed files with 1090 additions and 842 deletions
@@ -25,9 +25,14 @@ import java.util.List;
import java.util.Random;
import com.google.common.collect.Lists;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.threerings.bureau.data.AgentObject;
import com.threerings.presents.server.ShutdownManager;
import static com.threerings.bureau.Log.log;
/**
@@ -59,11 +64,12 @@ public class RegistryTester
*/
public static void main (String[] args)
{
TestServer server = new TestServer();
RegistryTester tester = new RegistryTester(server);
Injector injector = Guice.createInjector(new TestServer.Module());
TestServer server = injector.getInstance(TestServer.class);
RegistryTester tester = injector.getInstance(RegistryTester.class);
try {
server.init();
server.init(injector);
tester.start();
server.run();
@@ -75,7 +81,7 @@ public class RegistryTester
/**
* Creates a new registry tester.
*/
public RegistryTester (TestServer server)
@Inject public RegistryTester (TestServer server, ShutdownManager shutmgr)
{
_server = server;
@@ -92,7 +98,7 @@ public class RegistryTester
// stop the tests when the server shuts down
// TODO: this is not called on Ctrl-C, need a way to shut down gracefully
TestServer.registerShutdowner(new TestServer.Shutdowner() {
shutmgr.registerShutdowner(new ShutdownManager.Shutdowner() {
public void shutdown () {
log.info("Shutting down tests");
_stop = true;
@@ -21,6 +21,9 @@
package com.threerings.bureau.server;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.threerings.presents.server.PresentsServer;
import static com.threerings.bureau.Log.log;
@@ -40,9 +43,10 @@ public class TestServer extends PresentsServer
*/
public static void main (String[] args)
{
final TestServer server = new TestServer();
Injector injector = Guice.createInjector(new Module());
TestServer server = injector.getInstance(TestServer.class);
try {
server.init();
server.init(injector);
setClientTarget("bureau-runclient");
server.run();
@@ -51,20 +55,20 @@ public class TestServer extends PresentsServer
}
}
// inherit documentation - from PresentsServer
public void init ()
@Override // from PresentsServer
public void init (Injector injector)
throws Exception
{
super.init();
super.init(injector);
breg = new BureauRegistry("localhost:47624", invmgr, omgr, invoker);
}
static public void setClientTarget (String target)
public static void setClientTarget (String target)
{
breg.setCommandGenerator("test", antCommandGenerator(target));
}
static public BureauRegistry.CommandGenerator antCommandGenerator (
public static BureauRegistry.CommandGenerator antCommandGenerator (
final String target)
{
return new BureauRegistry.CommandGenerator() {
@@ -3,6 +3,9 @@
package com.threerings.crowd.server;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.threerings.crowd.data.JabberConfig;
import com.threerings.crowd.data.PlaceObject;
@@ -15,10 +18,10 @@ import static com.threerings.crowd.Log.log;
public class JabberServer extends CrowdServer
{
// documentation inherited
public void init ()
public void init (Injector injector)
throws Exception
{
super.init();
super.init(injector);
// create a single location
_pmgr = plreg.createPlace(new JabberConfig());
@@ -26,9 +29,11 @@ public class JabberServer extends CrowdServer
public static void main (String[] args)
{
JabberServer server = new JabberServer();
Injector injector = Guice.createInjector(new Module());
JabberServer server = injector.getInstance(JabberServer.class);
try {
server.init();
server.init(injector);
server.run();
} catch (Exception e) {
log.warning("Unable to initialize server.", e);
@@ -21,43 +21,29 @@
package com.threerings.presents.server;
import junit.framework.Test;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import atunit.AtUnit;
import atunit.Container;
import atunit.Unit;
import com.google.inject.Inject;
import com.threerings.presents.data.TestObject;
import com.threerings.presents.dobj.*;
import static org.junit.Assert.*;
/**
* A simple test case for the dobjmgr.
*/
public class DOMTest extends TestCase
@RunWith(AtUnit.class)
@Container(Container.Option.GUICE)
public class DOMTest
implements AttributeChangeListener, ElementUpdateListener
{
public DOMTest ()
{
super(DOMTest.class.getName());
}
public void attributeChanged (AttributeChangedEvent event)
{
assertTrue(fields[_fcount] + " == " + values[_fcount],
event.getName().equals(fields[_fcount]) &&
event.getValue().equals(values[_fcount]));
// shutdown once we receive our last update
if (++_fcount == fields.length) {
_omgr.harshShutdown();
}
}
public void elementUpdated (ElementUpdatedEvent event)
{
// Log.info("Element updated " + event);
// Log.info(StringUtil.toString(_test.ints));
// Log.info(StringUtil.toString(_test.strings));
}
public void runTest ()
@Test public void runTest ()
{
// request that a new TestObject be registered
_test = _omgr.registerObject(new TestObject());
@@ -87,27 +73,35 @@ public class DOMTest extends TestCase
_omgr.run();
}
public static Test suite ()
// from interface AttributeChangeListener
public void attributeChanged (AttributeChangedEvent event)
{
return new DOMTest();
assertTrue(fields[_fcount] + " == " + values[_fcount],
event.getName().equals(fields[_fcount]) &&
event.getValue().equals(values[_fcount]));
// shutdown once we receive our last update
if (++_fcount == fields.length) {
_omgr.harshShutdown();
}
}
public static void main (String[] args)
// from interface ElementUpdateListener
public void elementUpdated (ElementUpdatedEvent event)
{
DOMTest test = new DOMTest();
test.runTest();
// Log.info("Element updated " + event);
// Log.info(StringUtil.toString(_test.ints));
// Log.info(StringUtil.toString(_test.strings));
}
protected int _fcount = 0;
protected TestObject _test;
// the fields that will change in attribute changed events
protected Object[] fields = {
TestObject.FOO, TestObject.BAR, TestObject.FOO, TestObject.BAR };
protected Object[] fields = { TestObject.FOO, TestObject.BAR, TestObject.FOO, TestObject.BAR };
// the values we'll receive via attribute changed events
protected Object[] values = {
new Integer(99), "hoopie", new Integer(25), "howdy" };
protected Object[] values = { new Integer(99), "hoopie", new Integer(25), "howdy" };
protected static PresentsDObjectMgr _omgr = new PresentsDObjectMgr();
@Inject @Unit protected PresentsDObjectMgr _omgr;
}
@@ -21,60 +21,37 @@
package com.threerings.presents.server;
import junit.framework.Test;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import atunit.AtUnit;
import atunit.Container;
import atunit.Unit;
import com.google.inject.Inject;
import com.threerings.presents.data.TestObject;
import com.threerings.presents.dobj.*;
import static org.junit.Assert.*;
import static com.threerings.presents.Log.log;
/**
* Tests that the dobjmgr will not allow a destroyed object to be added to
* an oid list.
*/
public class DestroyedRefTest extends TestCase
implements EventListener
@RunWith(AtUnit.class)
@Container(Container.Option.GUICE)
public class DestroyedRefTest
{
public static Test suite ()
{
return new DestroyedRefTest();
}
public DestroyedRefTest ()
{
super(DestroyedRefTest.class.getName());
}
public void eventReceived (DEvent event)
{
int toid = event.getTargetOid();
// when we get the attribute change, we can exit
if (event instanceof ObjectDestroyedEvent) {
log.info("The upcoming object added event should be rejected.");
} else if (event instanceof ObjectAddedEvent &&
toid == _objtwo.getOid()) {
assertTrue("list should contain only one oid",
_objtwo.list.size() == 1);
} else if (event instanceof AttributeChangedEvent) {
// go bye bye
_omgr.harshShutdown();
} else {
fail("Got unexpected event: " + event);
}
}
public void runTest ()
@Test public void runTest ()
{
// create two test objects
_objone = _omgr.registerObject(new TestObject());
_objone.addListener(this);
_objone.addListener(_listener);
_objtwo = _omgr.registerObject(new TestObject());
_objtwo.addListener(this);
_objtwo.addListener(_listener);
// add object one to object two twice in a row to make sure repeated
// adds don't result in the object being listed twice
@@ -95,7 +72,30 @@ public class DestroyedRefTest extends TestCase
_omgr.run();
}
protected EventListener _listener = new EventListener() {
public void eventReceived (DEvent event) {
int toid = event.getTargetOid();
// when we get the attribute change, we can exit
if (event instanceof ObjectDestroyedEvent) {
log.info("The upcoming object added event should be rejected.");
} else if (event instanceof ObjectAddedEvent &&
toid == _objtwo.getOid()) {
assertTrue("list should contain only one oid",
_objtwo.list.size() == 1);
} else if (event instanceof AttributeChangedEvent) {
// go bye bye
_omgr.harshShutdown();
} else {
fail("Got unexpected event: " + event);
}
}
};
protected TestObject _objone, _objtwo;
protected static PresentsDObjectMgr _omgr = new PresentsDObjectMgr();
@Inject @Unit protected PresentsDObjectMgr _omgr;
}
@@ -21,65 +21,34 @@
package com.threerings.presents.server;
import junit.framework.Test;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import atunit.AtUnit;
import atunit.Container;
import atunit.Unit;
import com.google.inject.Inject;
import com.threerings.presents.data.TestObject;
import com.threerings.presents.dobj.*;
import static org.junit.Assert.*;
/**
* Tests the oid list reference tracking code.
*/
public class RefTest extends TestCase
implements EventListener
@RunWith(AtUnit.class)
@Container(Container.Option.GUICE)
public class RefTest
{
public static Test suite ()
{
return new RefTest();
}
public RefTest ()
{
super(RefTest.class.getName());
}
public void eventReceived (DEvent event)
{
// Log.info("Got event: " + event);
int toid = event.getTargetOid();
// once we receive the second object added we can destroy the
// target object to see if the reference is cleaned up
if (event instanceof ObjectAddedEvent &&
toid == _objtwo.getOid()) {
// Log.info("Destroying object two " + _objtwo + ".");
_objtwo.destroy();
} else if (event instanceof ObjectDestroyedEvent) {
if (toid == _objtwo.getOid()) {
// Log.info("List won't yet be empty: " + _objone.list);
assertTrue("List not empty", _objone.list.size() > 0);
} else {
// Log.info("Other object destroyed.");
// go bye bye
_omgr.harshShutdown();
}
} else if (event instanceof ObjectRemovedEvent) {
// Log.info("List should be empty: " + _objone.list);
assertTrue("List empty", _objone.list.size() == 0);
// finally destroy the other object to complete the circle
_objone.destroy();
}
}
public void runTest ()
@Test public void runTest ()
{
// create two test objects
_objone = _omgr.registerObject(new TestObject());
_objone.addListener(this);
_objone.addListener(_listener);
_objtwo = _omgr.registerObject(new TestObject());
_objtwo.addListener(this);
_objtwo.addListener(_listener);
// now that we have both objects, set up the references
_objone.addToList(_objtwo.getOid());
@@ -89,8 +58,39 @@ public class RefTest extends TestCase
_omgr.run();
}
protected EventListener _listener = new EventListener() {
public void eventReceived (DEvent event) {
// Log.info("Got event: " + event);
int toid = event.getTargetOid();
// once we receive the second object added we can destroy the
// target object to see if the reference is cleaned up
if (event instanceof ObjectAddedEvent &&
toid == _objtwo.getOid()) {
// Log.info("Destroying object two " + _objtwo + ".");
_objtwo.destroy();
} else if (event instanceof ObjectDestroyedEvent) {
if (toid == _objtwo.getOid()) {
// Log.info("List won't yet be empty: " + _objone.list);
assertTrue("List not empty", _objone.list.size() > 0);
} else {
// Log.info("Other object destroyed.");
// go bye bye
_omgr.harshShutdown();
}
} else if (event instanceof ObjectRemovedEvent) {
// Log.info("List should be empty: " + _objone.list);
assertTrue("List empty", _objone.list.size() == 0);
// finally destroy the other object to complete the circle
_objone.destroy();
}
}
};
protected TestObject _objone;
protected TestObject _objtwo;
protected static PresentsDObjectMgr _omgr = new PresentsDObjectMgr();
@Inject @Unit protected PresentsDObjectMgr _omgr;
}
@@ -21,6 +21,9 @@
package com.threerings.presents.server;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.threerings.presents.data.TestObject;
import com.threerings.presents.dobj.*;
@@ -30,13 +33,13 @@ public class TestServer extends PresentsServer
{
public static TestObject testobj;
public void init ()
public void init (Injector injector)
throws Exception
{
super.init();
super.init(injector);
// register our test provider
invmgr.registerDispatcher(new TestDispatcher(new TestManager()), "test");
_invmgr.registerDispatcher(new TestDispatcher(new TestManager()), "test");
// create a test object
testobj = omgr.registerObject(new TestObject());
@@ -48,9 +51,10 @@ public class TestServer extends PresentsServer
public static void main (String[] args)
{
TestServer server = new TestServer();
Injector injector = Guice.createInjector(new Module());
TestServer server = injector.getInstance(TestServer.class);
try {
server.init();
server.init(injector);
server.run();
} catch (Exception e) {
log.warning("Unable to initialize server.", e);