Skip to content

Commit 9391b77

Browse files
committed
function factory
1 parent a00b0bd commit 9391b77

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

src/main/java/com/microsoft/azure/functions/worker/broker/JavaMethodExecutor.java

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package com.microsoft.azure.functions.worker.broker;
22

3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
37
import java.lang.reflect.*;
4-
import java.net.*;
8+
import java.net.URL;
59
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;
614

7-
import com.microsoft.azure.functions.annotation.*;
15+
import com.microsoft.azure.functions.worker.WorkerLogManager;
816
import com.microsoft.azure.functions.worker.binding.*;
917
import com.microsoft.azure.functions.worker.description.*;
1018
import com.microsoft.azure.functions.worker.reflect.*;
@@ -16,7 +24,7 @@
1624
*/
1725
public class JavaMethodExecutor {
1826
public JavaMethodExecutor(FunctionMethodDescriptor descriptor, Map<String, BindingInfo> bindingInfos, ClassLoaderProvider classLoaderProvider)
19-
throws MalformedURLException, ClassNotFoundException, NoSuchMethodException
27+
throws IOException, ClassNotFoundException, NoSuchMethodException
2028
{
2129
descriptor.validateMethodInfo();
2230

@@ -42,24 +50,69 @@ public JavaMethodExecutor(FunctionMethodDescriptor descriptor, Map<String, Bindi
4250
for (Map.Entry<String, BindingInfo> entry : bindingInfos.entrySet()) {
4351
this.bindingDefinitions.put(entry.getKey(), new BindingDefinition(entry.getKey(), entry.getValue()));
4452
}
53+
resolveFunctionFactory();
4554
}
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+
4777
Map<String, BindingDefinition> getBindingDefinitions() { return this.bindingDefinitions; }
4878

4979
public ParameterResolver getOverloadResolver() { return this.overloadResolver; }
5080

5181
void execute(BindingDataStore dataStore) throws Exception {
5282
Object retValue = this.overloadResolver.resolve(dataStore)
5383
.orElseThrow(() -> new NoSuchMethodException("Cannot locate the method signature with the given input"))
54-
.invoke(() -> this.containingClass.newInstance());
84+
.invoke(() -> createFunctionInstance());
5585
dataStore.setDataTargetValue(BindingDataStore.RETURN_NAME, retValue);
5686
}
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+
5895
Class<?> getContainingClass(String className, ClassLoaderProvider classLoaderProvider) throws ClassNotFoundException {
5996
ClassLoader classLoader = classLoaderProvider.getClassLoader();
6097
return Class.forName(className, true, classLoader);
6198
}
6299

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;
63116
private Class<?> containingClass;
64117
private final ParameterResolver overloadResolver;
65118
private final Map<String, BindingDefinition> bindingDefinitions;

0 commit comments

Comments
 (0)