Skip to content

Commit

Permalink
Merge pull request #191 from adamschmidt/ExtensionModules
Browse files Browse the repository at this point in the history
Configuration-based Module Load
  • Loading branch information
metacret committed Feb 17, 2015
2 parents 56badfe + e9d1391 commit 31c2ec9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion suro-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ task (runServer, type: JavaExec, dependsOn: jar) {
main = "com.netflix.suro.SuroServer"
classpath = sourceSets.main.runtimeClasspath
classpath += sourceSets.main.compileClasspath
args = ['-m', 'conf/routingmap.json', '-s', 'conf/sink.json', '-i', 'conf/input.json']
args = ['-m', 'conf/routingmap.json', '-s', 'conf/sink.json', '-i', 'conf/input.json', '-x', 'conf/extensions.json']
systemProperty 'log4j.properties', 'conf/log4j.properties'
}
installApp {
Expand Down
1 change: 1 addition & 0 deletions suro-server/conf/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
50 changes: 45 additions & 5 deletions suro-server/src/main/java/com/netflix/suro/SuroServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package com.netflix.suro;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.io.Closeables;
import com.google.inject.Injector;
import com.google.inject.Module;
Expand All @@ -34,9 +38,11 @@
import org.apache.commons.cli.*;
import org.apache.commons.io.FileUtils;

import javax.annotation.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -84,7 +90,34 @@ public static void main(String[] args) throws IOException {
}
}

create(injector, properties);
List<Module> extensionModules = null;
if (line.hasOption('x')) {
String moduleFile = line.getOptionValue('x');
List<String> extensionModuleClasses = new ObjectMapper().readValue(
FileUtils.readFileToString(new File(moduleFile)),
new TypeReference<List<String>>(){});

if(extensionModuleClasses != null){
extensionModules = Lists.transform(extensionModuleClasses, new Function<String, Module>() {
@Nullable
@Override
public Module apply(String input) {
try {
return (Module)Class.forName(input).newInstance();
} catch (Throwable e) {
throw new RuntimeException(String.format("Unable to load module class %s", input), e);
}
}
});
}
}

if(extensionModules == null) { //catch-all for either no configuration or empty configuration file
extensionModules = Lists.newArrayList();
}

create(injector, properties, extensionModules.toArray(new Module[extensionModules.size()]));

injector.get().getInstance(LifecycleManager.class).start();

Runtime.getRuntime().addShutdownHook(new Thread() {
Expand Down Expand Up @@ -183,10 +216,16 @@ private static Options createOptions() {
.create('k');

Option controlPort = OptionBuilder.withArgName(OPT_CONTROL_PORT)
.hasArg()
.isRequired(false)
.withDescription("The port used to send command to this server")
.create('c');
.hasArg()
.isRequired(false)
.withDescription("The port used to send command to this server")
.create('c');

Option extensions = OptionBuilder.withArgName("extensions")
.hasArg()
.isRequired(false)
.withDescription("extension module list configuration file")
.create('x');

Options options = new Options();
options.addOption(propertyFile);
Expand All @@ -196,6 +235,7 @@ private static Options createOptions() {
options.addOption(accessKey);
options.addOption(secretKey);
options.addOption(controlPort);
options.addOption(extensions);

return options;
}
Expand Down

0 comments on commit 31c2ec9

Please sign in to comment.