11package com .google .cloud .functions .invoker .runner ;
22
3+ import com .beust .jcommander .JCommander ;
4+ import com .beust .jcommander .Parameter ;
5+ import com .beust .jcommander .ParameterException ;
36import com .google .cloud .functions .invoker .BackgroundCloudFunction ;
47import com .google .cloud .functions .invoker .BackgroundFunctionExecutor ;
58import com .google .cloud .functions .invoker .BackgroundFunctionSignatureMatcher ;
2427import java .util .logging .LogManager ;
2528import java .util .logging .Logger ;
2629import javax .servlet .http .HttpServlet ;
27- import org .apache .commons .cli .CommandLine ;
28- import org .apache .commons .cli .CommandLineParser ;
29- import org .apache .commons .cli .DefaultParser ;
30- import org .apache .commons .cli .HelpFormatter ;
31- import org .apache .commons .cli .Options ;
32- import org .apache .commons .cli .ParseException ;
3330import org .eclipse .jetty .server .Server ;
3431import org .eclipse .jetty .servlet .ServletContextHandler ;
3532import org .eclipse .jetty .servlet .ServletHolder ;
@@ -62,38 +59,69 @@ public class Invoker {
6259 logger = Logger .getLogger (Invoker .class .getName ());
6360 }
6461
65- public Invoker (
66- Integer port ,
67- String functionTarget ,
68- String functionSignatureType ,
69- Optional <String > functionJarPath ) {
70- this .port = port ;
71- this .functionTarget = functionTarget ;
72- this .functionSignatureType = functionSignatureType ;
73- this .functionJarPath = functionJarPath ;
62+ private static class Options {
63+ @ Parameter (
64+ description = "Port on which to listen for HTTP requests." ,
65+ names = "--port"
66+ )
67+ private String port = System .getenv ().getOrDefault ("PORT" , "8080" );
68+
69+ // TODO(emcmanus): the default value here no longer makes sense and should be changed to a
70+ // class name once we have finished retiring the java8 runtime.
71+ @ Parameter (
72+ description = "Name of function class to execute when servicing incoming requests." ,
73+ names = "--target"
74+ )
75+ private String target =
76+ System .getenv ().getOrDefault ("FUNCTION_TARGET" , "TestFunction.function" );
77+
78+ @ Parameter (
79+ description = "Name of a jar file that contains the function to execute. This must be"
80+ + " self-contained: either it must be a \" fat jar\" which bundles the dependencies"
81+ + " of all of the function code, or it must use the Class-Path attribute in the jar"
82+ + " manifest to point to those dependencies." ,
83+ names = "--jar"
84+ )
85+ private String jar = null ;
86+
87+ @ Parameter (
88+ names = "--help" , help = true
89+ )
90+ private boolean help = false ;
7491 }
7592
7693 public static void main (String [] args ) throws Exception {
94+ Options options = new Options ();
95+ JCommander jCommander = JCommander .newBuilder ()
96+ .addObject (options )
97+ .build ();
98+ try {
99+ jCommander .parse (args );
100+ } catch (ParameterException e ) {
101+ jCommander .usage ();
102+ throw e ;
103+ }
77104
78- CommandLine line = parseCommandLineOptions (args );
105+ if (options .help ) {
106+ jCommander .usage ();
107+ return ;
108+ }
79109
80- int port =
81- Arrays .asList (line .getOptionValue ("port" ), System .getenv ("PORT" )).stream ()
82- .filter (Objects ::nonNull )
83- .findFirst ()
84- .map (Integer ::parseInt )
85- .orElse (8080 );
86- String functionTarget =
87- Arrays .asList (line .getOptionValue ("target" ), System .getenv ("FUNCTION_TARGET" )).stream ()
88- .filter (Objects ::nonNull )
89- .findFirst ()
90- .orElse ("TestFunction.function" );
110+ int port ;
111+ try {
112+ port = Integer .parseInt (options .port );
113+ } catch (NumberFormatException e ) {
114+ System .err .println ("--port value should be an integer: " + options .port );
115+ jCommander .usage ();
116+ throw e ;
117+ }
118+ String functionTarget = options .target ;
91119 Path standardFunctionJarPath = Paths .get ("function/function.jar" );
92120 Optional <String > functionJarPath =
93121 Arrays .asList (
94- line . getOptionValue ( " jar" ) ,
95- System .getenv ("FUNCTION_JAR" ),
96- Files .exists (standardFunctionJarPath ) ? standardFunctionJarPath .toString () : null )
122+ options . jar ,
123+ System .getenv ("FUNCTION_JAR" ),
124+ Files .exists (standardFunctionJarPath ) ? standardFunctionJarPath .toString () : null )
97125 .stream ()
98126 .filter (Objects ::nonNull )
99127 .findFirst ();
@@ -111,30 +139,22 @@ private static boolean isLocalRun() {
111139 return System .getenv ("K_SERVICE" ) == null ;
112140 }
113141
114- private static CommandLine parseCommandLineOptions (String [] args ) {
115- CommandLineParser parser = new DefaultParser ();
116- Options options = new Options ();
117- options .addOption ("port" , true , "the port on which server listens to HTTP requests" );
118- options .addOption ("target" , true , "fully qualified name of the target method to execute" );
119- options .addOption ("jar" , true , "path to function jar" );
120-
121- try {
122- CommandLine line = parser .parse (options , args );
123- return line ;
124- } catch (ParseException e ) {
125- logger .log (Level .SEVERE , "Failed to parse command line options" , e );
126- HelpFormatter formatter = new HelpFormatter ();
127- formatter .printHelp ("Invoker" , options );
128- System .exit (1 );
129- }
130- return null ;
131- }
132-
133142 private final Integer port ;
134143 private final String functionTarget ;
135144 private final String functionSignatureType ;
136145 private final Optional <String > functionJarPath ;
137146
147+ public Invoker (
148+ Integer port ,
149+ String functionTarget ,
150+ String functionSignatureType ,
151+ Optional <String > functionJarPath ) {
152+ this .port = port ;
153+ this .functionTarget = functionTarget ;
154+ this .functionSignatureType = functionSignatureType ;
155+ this .functionJarPath = functionJarPath ;
156+ }
157+
138158 public void startServer () throws Exception {
139159 Server server = new Server (port );
140160
0 commit comments