3
3
import com .amazonaws .services .lambda .runtime .RequestHandler ;
4
4
import com .amazonaws .services .lambda .runtime .events .APIGatewayProxyRequestEvent ;
5
5
import com .amazonaws .services .lambda .runtime .events .APIGatewayProxyResponseEvent ;
6
+ import org .apache .log4j .LogManager ;
7
+ import org .apache .log4j .Logger ;
6
8
7
9
import java .lang .reflect .Constructor ;
8
10
9
11
public class Main {
12
+ private static final Logger LOGGER = LogManager .getLogger (Main .class );
13
+
10
14
public static void main (String [] args ) throws Exception {
11
15
LambdaRuntimeHttpClient lambdaRuntimeHttpClient = new LambdaRuntimeHttpClient ();
12
16
13
17
String handlerName = System .getenv ("_HANDLER" );
14
- System . out . println ("Invoking handler " + handlerName );
18
+ LOGGER . info ("Invoking handler " + handlerName );
15
19
16
20
RequestHandler <APIGatewayProxyRequestEvent , APIGatewayProxyResponseEvent > handler = getHandlerInstance (handlerName );
17
- if (handler == null ) {
18
- System .out .println ("Handler is null" );
19
- return ;
20
- }
21
-
22
21
LambdaRuntimeLoop lambdaRuntimeLoop = new LambdaRuntimeLoop (lambdaRuntimeHttpClient , handler );
23
22
lambdaRuntimeLoop .run ();
24
23
}
25
24
26
25
private static RequestHandler <APIGatewayProxyRequestEvent , APIGatewayProxyResponseEvent > getHandlerInstance (String handlerName ) {
27
26
Class <?> handlerClass = loadHandlerClass (handlerName );
28
- if (handlerClass == null ) {
29
- return null ;
30
- }
31
27
Constructor <?> constructor = findZeroArgConstructor (handlerClass );
32
28
if (constructor == null ) {
33
- System .out .println ("No zero-arg constructor found for " + handlerName );
34
- return null ;
29
+ throw new RuntimeException ("No zero-arg constructor" );
35
30
}
36
-
37
31
return instantiateHandler (constructor );
38
32
}
39
33
40
34
private static Class <?> loadHandlerClass (String handlerName ) {
41
35
try {
42
36
return Class .forName (handlerName );
43
37
} catch (ClassNotFoundException e ) {
44
- System . out . println ("Handler not found: " + e . getMessage () );
45
- return null ;
38
+ LOGGER . error ("Handler not found on classpath : " + handlerName );
39
+ throw new RuntimeException ( e ) ;
46
40
}
47
41
}
48
42
@@ -52,6 +46,7 @@ private static Constructor<?> findZeroArgConstructor(Class<?> clazz) {
52
46
return constructor ;
53
47
}
54
48
}
49
+ LOGGER .warn ("No zero-arg constructor found for " + clazz .getName ());
55
50
return null ;
56
51
}
57
52
@@ -60,8 +55,8 @@ private static RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyRespon
60
55
try {
61
56
return (RequestHandler <APIGatewayProxyRequestEvent , APIGatewayProxyResponseEvent >) constructor .newInstance ();
62
57
} catch (Exception e ) {
63
- System . out . println ("Failed to construct handler: " + e . getMessage () );
64
- return null ;
58
+ LOGGER . error ("Failed to construct instance of handler: " , e );
59
+ throw new RuntimeException ( e ) ;
65
60
}
66
61
}
67
62
}
0 commit comments