From d9c986e97f28e33b4e83beb4bf93028e274386cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesu=CC=81s=20A=2E=20A=CC=81lvarez?= Date: Wed, 6 Dec 2017 13:47:39 +0100 Subject: [PATCH] pass handler name to java --- lib/plugins/aws/invokeLocal/index.js | 21 ++++++++++++++------- lib/plugins/aws/invokeLocal/index.test.js | 6 ++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/plugins/aws/invokeLocal/index.js b/lib/plugins/aws/invokeLocal/index.js index f73c5cb10..49cd79b34 100644 --- a/lib/plugins/aws/invokeLocal/index.js +++ b/lib/plugins/aws/invokeLocal/index.js @@ -118,10 +118,10 @@ class AwsInvokeLocal { || this.serverless.service.provider.runtime || 'nodejs4.3'; const handler = this.options.functionObj.handler; - const handlerPath = handler.split('.')[0]; - const handlerName = handler.split('.')[1]; if (runtime.startsWith('nodejs')) { + const handlerPath = handler.split('.')[0]; + const handlerName = handler.split('.')[1]; return this.invokeLocalNodeJs( handlerPath, handlerName, @@ -130,6 +130,8 @@ class AwsInvokeLocal { } if (runtime === 'python2.7' || runtime === 'python3.6') { + const handlerPath = handler.split('.')[0]; + const handlerName = handler.split('.')[1]; return this.invokeLocalPython( process.platform === 'win32' ? 'python.exe' : runtime, handlerPath, @@ -139,9 +141,12 @@ class AwsInvokeLocal { } if (runtime === 'java8') { + const className = handler.split('::')[0]; + const handlerName = handler.split('::')[1] || 'handleRequest'; return this.invokeLocalJava( 'java', - handler, + className, + handlerName, this.serverless.service.package.artifact, this.options.data, this.options.context); @@ -177,11 +182,12 @@ class AwsInvokeLocal { }); } - callJavaBridge(artifactPath, className, input) { + callJavaBridge(artifactPath, className, handlerName, input) { return new BbPromise((resolve) => fs.statAsync(artifactPath).then(() => { const java = spawn('java', [ `-DartifactPath=${artifactPath}`, `-DclassName=${className}`, + `-DhandlerName=${handlerName}`, '-jar', path.join(__dirname, 'java', 'target', 'invoke-bridge-1.0.jar'), ]); @@ -201,7 +207,7 @@ class AwsInvokeLocal { })); } - invokeLocalJava(runtime, className, artifactPath, event, customContext) { + invokeLocalJava(runtime, className, handlerName, artifactPath, event, customContext) { const timeout = Number(this.options.functionObj.timeout) || Number(this.serverless.service.provider.timeout) || 6; @@ -220,7 +226,7 @@ class AwsInvokeLocal { const executablePath = path.join(javaBridgePath, 'target'); return new BbPromise(resolve => fs.statAsync(executablePath) - .then(() => this.callJavaBridge(artifactPath, className, input)) + .then(() => this.callJavaBridge(artifactPath, className, handlerName, input)) .then(resolve) .catch(() => { const mvn = spawn('mvn', [ @@ -235,7 +241,8 @@ class AwsInvokeLocal { mvn.stderr.on('data', (buf) => this.serverless.cli.consoleLog(`mvn - ${buf.toString()}`)); mvn.stdin.end(); - mvn.on('close', () => this.callJavaBridge(artifactPath, className, input).then(resolve)); + mvn.on('close', () => this.callJavaBridge(artifactPath, className, handlerName, input) + .then(resolve)); })); } diff --git a/lib/plugins/aws/invokeLocal/index.test.js b/lib/plugins/aws/invokeLocal/index.test.js index d2542acae..c2e837435 100644 --- a/lib/plugins/aws/invokeLocal/index.test.js +++ b/lib/plugins/aws/invokeLocal/index.test.js @@ -379,6 +379,7 @@ describe('AwsInvokeLocal', () => { expect(invokeLocalJavaStub.calledWithExactly( 'java', 'handler.hello', + 'handleRequest', undefined, {}, undefined @@ -588,6 +589,7 @@ describe('AwsInvokeLocal', () => { awsInvokeLocalMocked.callJavaBridge( __dirname, 'com.serverless.Handler', + 'handleRequest', '{}' ).then(() => { expect(writeChildStub.calledOnce).to.be.equal(true); @@ -625,6 +627,7 @@ describe('AwsInvokeLocal', () => { awsInvokeLocal.invokeLocalJava( 'java', 'com.serverless.Handler', + 'handleRequest', __dirname, {} ).then(() => { @@ -632,6 +635,7 @@ describe('AwsInvokeLocal', () => { expect(callJavaBridgeStub.calledWithExactly( __dirname, 'com.serverless.Handler', + 'handleRequest', JSON.stringify({ event: {}, context: { @@ -694,6 +698,7 @@ describe('AwsInvokeLocal', () => { awsInvokeLocalMocked.invokeLocalJava( 'java', 'com.serverless.Handler', + 'handleRequest', __dirname, {} ).then(() => { @@ -701,6 +706,7 @@ describe('AwsInvokeLocal', () => { expect(callJavaBridgeMockedStub.calledWithExactly( __dirname, 'com.serverless.Handler', + 'handleRequest', JSON.stringify({ event: {}, context: {