1
1
package com .microsoft .azure .functions .worker .broker ;
2
2
3
+ import java .io .BufferedReader ;
4
+ import java .io .IOException ;
5
+ import java .io .InputStream ;
6
+ import java .io .InputStreamReader ;
3
7
import java .lang .reflect .*;
4
- import java .net .* ;
8
+ import java .net .URL ;
5
9
import java .util .*;
10
+ import java .util .logging .FileHandler ;
11
+ import java .util .logging .Level ;
12
+ import java .util .logging .Logger ;
13
+ import java .util .logging .SimpleFormatter ;
6
14
7
- import com .microsoft .azure .functions .annotation .* ;
15
+ import com .microsoft .azure .functions .worker . WorkerLogManager ;
8
16
import com .microsoft .azure .functions .worker .binding .*;
9
17
import com .microsoft .azure .functions .worker .description .*;
10
18
import com .microsoft .azure .functions .worker .reflect .*;
16
24
*/
17
25
public class JavaMethodExecutor {
18
26
public JavaMethodExecutor (FunctionMethodDescriptor descriptor , Map <String , BindingInfo > bindingInfos , ClassLoaderProvider classLoaderProvider )
19
- throws MalformedURLException , ClassNotFoundException , NoSuchMethodException
27
+ throws IOException , ClassNotFoundException , NoSuchMethodException
20
28
{
21
29
descriptor .validateMethodInfo ();
22
30
@@ -42,24 +50,69 @@ public JavaMethodExecutor(FunctionMethodDescriptor descriptor, Map<String, Bindi
42
50
for (Map .Entry <String , BindingInfo > entry : bindingInfos .entrySet ()) {
43
51
this .bindingDefinitions .put (entry .getKey (), new BindingDefinition (entry .getKey (), entry .getValue ()));
44
52
}
53
+ resolveFunctionFactory ();
45
54
}
46
-
55
+
56
+ // TODO remove once I figure out how to see logging in local docker
57
+ private static Logger fileLogger ;
58
+ static {
59
+ fileLogger = Logger .getAnonymousLogger ();
60
+ fileLogger .setUseParentHandlers (false );
61
+ FileHandler fh = null ;
62
+ try {
63
+ fh = new FileHandler ("java_worker.log" );
64
+ } catch (IOException e ) {
65
+ e .printStackTrace ();
66
+ }
67
+ fh .setFormatter (new SimpleFormatter ());
68
+ fh .setLevel (Level .ALL );
69
+ fileLogger .addHandler (fh );
70
+ fileLogger .info ("INITIALIZED LOGGER" );
71
+
72
+ }
73
+ private Logger getLogger () {
74
+ return fileLogger ;
75
+ }
76
+
47
77
Map <String , BindingDefinition > getBindingDefinitions () { return this .bindingDefinitions ; }
48
78
49
79
public ParameterResolver getOverloadResolver () { return this .overloadResolver ; }
50
80
51
81
void execute (BindingDataStore dataStore ) throws Exception {
52
82
Object retValue = this .overloadResolver .resolve (dataStore )
53
83
.orElseThrow (() -> new NoSuchMethodException ("Cannot locate the method signature with the given input" ))
54
- .invoke (() -> this . containingClass . newInstance ());
84
+ .invoke (() -> createFunctionInstance ());
55
85
dataStore .setDataTargetValue (BindingDataStore .RETURN_NAME , retValue );
56
86
}
57
-
87
+
88
+ private Object createFunctionInstance () throws Exception {
89
+ if (functionFactory == null ) {
90
+ return this .containingClass .newInstance ();
91
+ }
92
+ return functionFactory .invoke (null , containingClass );
93
+ }
94
+
58
95
Class <?> getContainingClass (String className , ClassLoaderProvider classLoaderProvider ) throws ClassNotFoundException {
59
96
ClassLoader classLoader = classLoaderProvider .getClassLoader ();
60
97
return Class .forName (className , true , classLoader );
61
98
}
62
99
100
+ private void resolveFunctionFactory () throws IOException , ClassNotFoundException , NoSuchMethodException {
101
+ ClassLoader cl = containingClass .getClassLoader ();
102
+ InputStream is = cl .getResourceAsStream ("META-INF/service/com.microsoft.azure.functions.FunctionFactory" );
103
+ if (is == null ) {
104
+ getLogger ().info ("resolveFunctionFactory could not find descriptor" );
105
+ return ;
106
+ }
107
+ BufferedReader reader = new BufferedReader (new InputStreamReader (is ));
108
+ String factoryName = reader .readLine ().trim ();
109
+ Class factoryClass = cl .loadClass (factoryName );
110
+ functionFactory = factoryClass .getMethod ("newInstance" , Class .class );
111
+ is .close ();
112
+ }
113
+
114
+
115
+ private Method functionFactory ;
63
116
private Class <?> containingClass ;
64
117
private final ParameterResolver overloadResolver ;
65
118
private final Map <String , BindingDefinition > bindingDefinitions ;
0 commit comments