curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [2/3] git commit: temp checkin
Date Tue, 27 May 2014 22:17:53 GMT
temp checkin


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/65b1ff41
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/65b1ff41
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/65b1ff41

Branch: refs/heads/curator-rpc
Commit: 65b1ff417c369e6494522b703f6d938ef4d5895d
Parents: 3b57798
Author: randgalt <randgalt@apache.org>
Authored: Tue May 27 17:01:23 2014 -0500
Committer: randgalt <randgalt@apache.org>
Committed: Tue May 27 17:01:23 2014 -0500

----------------------------------------------------------------------
 curator-x-rpc/pom.xml                           |   5 +
 .../org/apache/curator/x/rpc/Configuration.java |  13 ++
 .../curator/x/rpc/CuratorProjectionServer.java  | 153 ++++++++++++++++++-
 3 files changed, 167 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/65b1ff41/curator-x-rpc/pom.xml
----------------------------------------------------------------------
diff --git a/curator-x-rpc/pom.xml b/curator-x-rpc/pom.xml
index cf2442c..ff6bc52 100644
--- a/curator-x-rpc/pom.xml
+++ b/curator-x-rpc/pom.xml
@@ -22,6 +22,11 @@
         </dependency>
 
         <dependency>
+            <groupId>org.codehaus.jackson</groupId>
+            <artifactId>jackson-mapper-asl</artifactId>
+        </dependency>
+
+        <dependency>
             <groupId>org.apache.curator</groupId>
             <artifactId>curator-test</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/curator/blob/65b1ff41/curator-x-rpc/src/main/java/org/apache/curator/x/rpc/Configuration.java
----------------------------------------------------------------------
diff --git a/curator-x-rpc/src/main/java/org/apache/curator/x/rpc/Configuration.java b/curator-x-rpc/src/main/java/org/apache/curator/x/rpc/Configuration.java
new file mode 100644
index 0000000..a89016c
--- /dev/null
+++ b/curator-x-rpc/src/main/java/org/apache/curator/x/rpc/Configuration.java
@@ -0,0 +1,13 @@
+package org.apache.curator.x.rpc;
+
+import com.facebook.swift.service.ThriftServerConfig;
+import io.airlift.configuration.Config;
+
+public class Configuration extends ThriftServerConfig
+{
+    @Config("hey")
+    public void setHey(int hey)
+    {
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/curator/blob/65b1ff41/curator-x-rpc/src/main/java/org/apache/curator/x/rpc/CuratorProjectionServer.java
----------------------------------------------------------------------
diff --git a/curator-x-rpc/src/main/java/org/apache/curator/x/rpc/CuratorProjectionServer.java
b/curator-x-rpc/src/main/java/org/apache/curator/x/rpc/CuratorProjectionServer.java
index 75c304b..4606177 100644
--- a/curator-x-rpc/src/main/java/org/apache/curator/x/rpc/CuratorProjectionServer.java
+++ b/curator-x-rpc/src/main/java/org/apache/curator/x/rpc/CuratorProjectionServer.java
@@ -21,22 +21,167 @@ package org.apache.curator.x.rpc;
 import com.facebook.swift.codec.ThriftCodecManager;
 import com.facebook.swift.service.ThriftEventHandler;
 import com.facebook.swift.service.ThriftServer;
-import com.facebook.swift.service.ThriftServerConfig;
 import com.facebook.swift.service.ThriftServiceProcessor;
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
+import com.google.common.io.Files;
+import io.airlift.configuration.Config;
+import io.airlift.units.DataSize;
+import io.airlift.units.Duration;
 import org.apache.curator.x.rpc.idl.event.EventService;
 import org.apache.curator.x.rpc.idl.projection.CuratorProjectionService;
+import org.codehaus.jackson.map.AnnotationIntrospector;
+import org.codehaus.jackson.map.DeserializationConfig;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.introspect.AnnotatedMethod;
+import org.codehaus.jackson.map.introspect.NopAnnotationIntrospector;
+import org.codehaus.jackson.node.ObjectNode;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.nio.charset.Charset;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
 
 public class CuratorProjectionServer
 {
-    public static void main(String[] args)
+    private final RpcManager rpcManager;
+    private final ThriftServer server;
+    private final AtomicReference<State> state = new AtomicReference<State>(State.LATENT);
+
+    private enum State
+    {
+        LATENT,
+        STARTED,
+        STOPPED
+    }
+
+    public static void main(String[] args) throws IOException
+    {
+        if ( (args.length == 0) || args[0].equalsIgnoreCase("?") || args[0].equalsIgnoreCase("-h")
|| args[0].equalsIgnoreCase("--help") )
+        {
+            printHelp();
+            return;
+        }
+
+        ObjectMapper objectMapper = new ObjectMapper();
+        String options;
+        File f = new File(args[0]);
+        if ( f.exists() )
+        {
+            options = Files.toString(f, Charset.defaultCharset());
+        }
+        else
+        {
+            System.out.println("First argument is not a file. Treating the command line as
a list of field/values");
+            options = buildOptions(objectMapper, args);
+        }
+
+        AnnotationIntrospector introspector = new NopAnnotationIntrospector()
+        {
+            @Override
+            public String findSettablePropertyName(AnnotatedMethod am)
+            {
+                Config config = am.getAnnotated().getAnnotation(Config.class);
+                return (config != null) ? config.value() : super.findSettablePropertyName(am);
+            }
+        };
+        DeserializationConfig deserializationConfig = objectMapper.getDeserializationConfig().withAnnotationIntrospector(introspector);
+        objectMapper.setDeserializationConfig(deserializationConfig);
+        Configuration configuration = objectMapper.reader().withType(Configuration.class).readValue(options);
+
+        final CuratorProjectionServer server = new CuratorProjectionServer(configuration);
+        server.start();
+
+        Runnable shutdown = new Runnable()
+        {
+            @Override
+            public void run()
+            {
+                server.stop();
+            }
+        };
+        Thread hook = new Thread(shutdown);
+        Runtime.getRuntime().addShutdownHook(hook);
+    }
+
+    public CuratorProjectionServer(Configuration thriftServerConfig)
     {
-        RpcManager rpcManager = new RpcManager(TimeUnit.SECONDS.toMillis(10));   // TODO
+        rpcManager = new RpcManager(TimeUnit.SECONDS.toMillis(10));
         EventService eventService = new EventService(rpcManager, 5000); // TODO
         CuratorProjectionService projectionService = new CuratorProjectionService(rpcManager);
         ThriftServiceProcessor processor = new ThriftServiceProcessor(new ThriftCodecManager(),
Lists.<ThriftEventHandler>newArrayList(), projectionService, eventService);
-        ThriftServer server = new ThriftServer(processor, new ThriftServerConfig().setPort(8899));
 // TODO
+        server = new ThriftServer(processor, thriftServerConfig);
+    }
+
+    public void start()
+    {
+        Preconditions.checkState(state.compareAndSet(State.LATENT, State.STARTED), "Already
started");
+
         server.start();
     }
+
+    public void stop()
+    {
+        if ( state.compareAndSet(State.STARTED, State.STOPPED) )
+        {
+            rpcManager.close();
+            server.close();
+        }
+    }
+
+    private static void printHelp()
+    {
+        System.out.println("Curator RPC - an RPC server for using Apache Curator APIs and
recipes from non JVM languages.");
+        System.out.println();
+        System.out.println("Arguments:");
+        System.out.println("\t<none>              show this help");
+        System.out.println("\t<path>              path to a JSON configuration file");
+        System.out.println("\t<field value> ...   list of values that would be in the
JSON configuration file");
+        System.out.println();
+        System.out.println("Values:");
+
+        for ( Method method : Configuration.class.getMethods() )
+        {
+            Config config = method.getAnnotation(Config.class);
+            if ( (config != null) && (method.getParameterTypes().length == 1) )
+            {
+                System.out.println("\t" + config.value() + ": " + getType(method));
+            }
+        }
+    }
+
+    private static String getType(Method method)
+    {
+        Class<?> type = method.getParameterTypes()[0];
+        String result = type.getSimpleName();
+        if ( type.equals(Duration.class) )
+        {
+            result += example(new Duration(10, TimeUnit.MINUTES));
+        }
+        else if ( type.equals(DataSize.class) )
+        {
+            result += example(new DataSize(1.5, DataSize.Unit.GIGABYTE));
+        }
+        return result;
+    }
+
+    private static String example(Object s)
+    {
+        return " (e.g. \"" + s + "\")";
+    }
+
+    private static String buildOptions(ObjectMapper objectMapper, String[] args) throws IOException
+    {
+        ObjectNode node = objectMapper.createObjectNode();
+        for ( int i = 0; i < args.length; i += 2 )
+        {
+            if ( (i + 1) >= args.length )
+            {
+                throw new IOException("Bad command line. Must be list of fields and values
of the form: \"field1 value1 ... fieldN valueN\"");
+            }
+            node.put(args[i], args[i + 1]);
+        }
+        return objectMapper.writeValueAsString(node);
+    }
 }


Mime
View raw message