Permalink
Browse files

Allow Rack servers to announce

  • Loading branch information...
1 parent fadb66b commit b1ead67b0ffd465a13e0ef78e388dfffe7cd936d @electrum electrum committed Apr 14, 2012
@@ -0,0 +1,34 @@
+package com.proofpoint.rack;
+
+import com.google.inject.Inject;
+import com.proofpoint.discovery.client.AnnouncementHttpServerInfo;
+import com.proofpoint.discovery.client.Announcer;
+import com.proofpoint.discovery.client.ServiceAnnouncement;
+
+import static com.proofpoint.discovery.client.ServiceAnnouncement.ServiceAnnouncementBuilder;
+import static com.proofpoint.discovery.client.ServiceAnnouncement.serviceAnnouncement;
+
+public class RackAnnouncer
+{
+ @Inject
+ public RackAnnouncer(RackServletConfig config, Announcer announcer, AnnouncementHttpServerInfo httpServerInfo)
+ {
+ if (config.getServiceAnnouncement() != null) {
+ announcer.addServiceAnnouncement(createHttpAnnouncement(httpServerInfo, config.getServiceAnnouncement()));
+ }
+ }
+
+ private static ServiceAnnouncement createHttpAnnouncement(AnnouncementHttpServerInfo httpServerInfo, String announcement)
+ {
+ ServiceAnnouncementBuilder builder = serviceAnnouncement(announcement);
+ if (httpServerInfo.getHttpUri() != null) {
+ builder.addProperty("http", httpServerInfo.getHttpUri().toString());
+ builder.addProperty("http-external", httpServerInfo.getHttpExternalUri().toString());
+ }
+ if (httpServerInfo.getHttpsUri() != null) {
+ builder.addProperty("https", httpServerInfo.getHttpsUri().toString());
+ builder.addProperty("https-external", httpServerInfo.getHttpsExternalUri().toString());
+ }
+ return builder.build();
+ }
+}
@@ -35,7 +35,10 @@ public void configure(Binder binder)
binder.disableCircularProxies();
binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(RackServlet.class).in(Scopes.SINGLETON);
+
ConfigurationModule.bindConfig(binder).to(RackServletConfig.class);
+
+ binder.bind(RackAnnouncer.class).asEagerSingleton();
}
/**
@@ -19,11 +19,13 @@
import com.proofpoint.configuration.ConfigDescription;
import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
public class RackServletConfig
{
//Defaults to assuming config.ru is in the root.
private String rackConfigPath = "config.ru";
+ private String serviceAnnouncement;
@NotNull
public String getRackConfigPath()
@@ -38,4 +40,18 @@ public RackServletConfig setRackConfigPath(String rackConfigPath)
this.rackConfigPath = rackConfigPath;
return this;
}
+
+ @Pattern(regexp = "[a-z][a-z0-9]{0,14}")
+ public String getServiceAnnouncement()
+ {
+ return serviceAnnouncement;
+ }
+
+ @Config("rackserver.announcement")
+ @ConfigDescription("name of service to announce")
+ public RackServletConfig setServiceAnnouncement(String serviceAnnouncement)
+ {
+ this.serviceAnnouncement = serviceAnnouncement;
+ return this;
+ }
}
@@ -0,0 +1,76 @@
+package com.proofpoint.rack;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+import com.google.common.io.Resources;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.proofpoint.configuration.ConfigurationFactory;
+import com.proofpoint.configuration.ConfigurationModule;
+import com.proofpoint.discovery.client.AnnouncementHttpServerInfo;
+import com.proofpoint.discovery.client.Announcer;
+import com.proofpoint.discovery.client.DiscoveryLookupClient;
+import com.proofpoint.discovery.client.ServiceAnnouncement;
+import com.proofpoint.discovery.client.ServiceDescriptor;
+import com.proofpoint.discovery.client.testing.InMemoryDiscoveryClient;
+import com.proofpoint.discovery.client.testing.TestingDiscoveryModule;
+import com.proofpoint.http.server.testing.TestingHttpServer;
+import com.proofpoint.http.server.testing.TestingHttpServerModule;
+import com.proofpoint.node.testing.TestingNodeModule;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
+
+import java.util.List;
+
+import static com.proofpoint.discovery.client.ServiceAnnouncement.serviceAnnouncement;
+import static org.testng.Assert.assertEquals;
+
+public class TestRackAnnounce
+{
+ private TestingHttpServer server;
+
+ @AfterMethod
+ public void tearDown()
+ throws Exception
+ {
+ if (server != null) {
+ server.stop();
+ }
+ }
+
+ @Test
+ public void testAnnouncement()
+ throws Exception
+ {
+ Injector injector = Guice.createInjector(
+ new TestingHttpServerModule(),
+ new RackModule(),
+ new TestingNodeModule(),
+ new TestingDiscoveryModule(),
+ new ConfigurationModule(new ConfigurationFactory(
+ ImmutableMap.<String, String>builder()
+ .put("rackserver.rack-config-path", Resources.getResource("test/raw/config.ru").getFile())
+ .put("rackserver.announcement", "racktest")
+ .build()
+ )));
+
+ server = injector.getInstance(TestingHttpServer.class);
+ server.start();
+
+ injector.getInstance(Announcer.class).start();
+
+ AnnouncementHttpServerInfo httpServerInfo = injector.getInstance(AnnouncementHttpServerInfo.class);
+ ServiceAnnouncement announcement = serviceAnnouncement("racktest")
+ .addProperty("http", httpServerInfo.getHttpUri().toASCIIString())
+ .addProperty("http-external", httpServerInfo.getHttpExternalUri().toASCIIString())
+ .build();
+
+ DiscoveryLookupClient lookupClient = injector.getInstance(InMemoryDiscoveryClient.class);
+ List<ServiceDescriptor> descriptors = lookupClient.getServices("racktest").get().getServiceDescriptors();
+
+ assertEquals(descriptors.size(), 1);
+ ServiceDescriptor descriptor = Iterables.getOnlyElement(descriptors);
+
+ assertEquals(descriptor.getProperties(), announcement.getProperties());
+ }
+}
@@ -23,6 +23,7 @@
import com.ning.http.client.Response;
import com.proofpoint.configuration.ConfigurationFactory;
import com.proofpoint.configuration.ConfigurationModule;
+import com.proofpoint.discovery.client.testing.TestingDiscoveryModule;
import com.proofpoint.http.server.testing.TestingHttpServer;
import com.proofpoint.http.server.testing.TestingHttpServerModule;
import com.proofpoint.node.testing.TestingNodeModule;
@@ -49,6 +50,7 @@ public void setup()
new TestingHttpServerModule(),
new RackModule(),
new TestingNodeModule(),
+ new TestingDiscoveryModule(),
new ConfigurationModule(new ConfigurationFactory(
ImmutableMap.<String, String>builder()
.put("rackserver.rack-config-path", Resources.getResource("test/raw/config.ru").getFile())
@@ -23,6 +23,7 @@
import com.ning.http.client.Response;
import com.proofpoint.configuration.ConfigurationFactory;
import com.proofpoint.configuration.ConfigurationModule;
+import com.proofpoint.discovery.client.testing.TestingDiscoveryModule;
import com.proofpoint.http.server.testing.TestingHttpServer;
import com.proofpoint.http.server.testing.TestingHttpServerModule;
import com.proofpoint.node.testing.TestingNodeModule;
@@ -45,6 +46,7 @@ public void setup()
new TestingHttpServerModule(),
new RackModule(),
new TestingNodeModule(),
+ new TestingDiscoveryModule(),
new ConfigurationModule(new ConfigurationFactory(
ImmutableMap.<String, String>builder()
.put("rackserver.rack-config-path", Resources.getResource("test/sinatra/config.ru").getFile())
@@ -38,15 +38,22 @@
public void testDefaults()
{
ConfigAssertions.assertRecordedDefaults(ConfigAssertions.recordDefaults(RackServletConfig.class)
- .setRackConfigPath("config.ru"));
+ .setRackConfigPath("config.ru")
+ .setServiceAnnouncement(null)
+ );
}
@Test
public void testExplicitPropertyMappings()
{
- Map<String, String> properties = ImmutableMap.of("rackserver.rack-config-path", "rack-configuration.ru");
+ Map<String, String> properties = ImmutableMap.<String, String>builder()
+ .put("rackserver.rack-config-path", "rack-configuration.ru")
+ .put("rackserver.announcement", "test")
+ .build();
- RackServletConfig expected = new RackServletConfig().setRackConfigPath("rack-configuration.ru");
+ RackServletConfig expected = new RackServletConfig()
+ .setRackConfigPath("rack-configuration.ru")
+ .setServiceAnnouncement("test");
ConfigAssertions.assertFullMapping(properties, expected);
}

0 comments on commit b1ead67

Please sign in to comment.