This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
GaeRunTask.groovy
163 lines (142 loc) · 5.68 KB
/
GaeRunTask.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.plugins.gae.task
import org.gradle.api.GradleException
import org.gradle.api.InvalidUserDataException
import org.gradle.api.plugins.gae.task.internal.*
/**
* Google App Engine task starting up a local development server.
*
* @see <a href="http://code.google.com/appengine/docs/java/tools/devserver.html#Running_the_Development_Web_Server">Documentation</a>
* @author Benjamin Muschko
*/
class GaeRunTask extends AbstractGaeTask implements Explodable {
String httpAddress
Integer httpPort
Integer stopPort
String stopKey
File explodedWarDirectory
Boolean daemon
Boolean disableUpdateCheck
List<String> jvmFlags
final KickStartSynchronizer kickStartSynchronizer = new KickStartSynchronizer()
@Override
void validateConfiguration() {
super.validateConfiguration()
validatePort(getHttpPort(), 'HTTP')
validatePort(getStopPort(), 'Stop')
}
private void validatePort(Integer port, String type) {
if(!PortUtility.isValidPortNumber(port)) {
throw new InvalidUserDataException("Invalid $type port number: $port")
}
if(!PortUtility.isAvailable(port)) {
throw new InvalidUserDataException("$type port number already in use: $port")
}
logger.info "$type port = $port"
}
@Override
void executeTask() {
startLocalDevelopmentServer()
}
private void startLocalDevelopmentServer() {
try {
logger.info 'Starting local development server...'
if(!getDaemon()) {
startShutdownMonitor(new SystemExitShutdownCallback())
runKickStart()
}
else {
Thread kickStartThread = new Thread(new KickStartRunnable())
startShutdownMonitor(new ThreadShutdownCallback(kickStartThread))
kickStartThread.start()
// Pause current thread until local development server is fully started
kickStartSynchronizer.pause()
}
}
catch(Exception e) {
throw new GradleException('An error occurred starting the local development server.', e)
}
finally {
if(!getDaemon()) {
logger.info 'Local development server exiting.'
}
}
}
private void startShutdownMonitor(ShutdownCallback shutdownCallback) {
Thread shutdownMonitor = new ShutdownMonitor(getStopPort(), getStopKey(), shutdownCallback)
shutdownMonitor.start()
}
private void runKickStart() {
KickStartParams kickStartParams = new KickStartParams()
kickStartParams.httpPort = getHttpPort()
kickStartParams.httpAddress = getHttpAddress()
kickStartParams.disableUpdateCheck = getDisableUpdateCheck()
kickStartParams.jvmFlags = getJvmFlags()
kickStartParams.explodedWarDirectory = getExplodedWarDirectory()
List<String> params = KickStartParamsBuilder.instance.buildCommandLineParams(kickStartParams)
logger.info "Using params = $params"
ClassLoader classLoader = Thread.currentThread().contextClassLoader
Class kickStart = Class.forName('com.google.appengine.tools.KickStart', true, classLoader)
kickStart.main(params as String[])
}
private class KickStartRunnable implements Runnable {
@Override
void run() {
InputStream systemInOriginal = System.in
PrintStream systemOutOriginal = System.out
PrintStream systemErrOriginal = System.err
PipedOutputStream pipeOut = new PipedOutputStream()
PipedInputStream pipeIn = new PipedInputStream(pipeOut)
PrintStream out = new PrintStream(pipeOut, true)
System.setIn(pipeIn)
System.setOut(out)
System.setErr(out)
Thread outputCommandLineConsumer = new CommandLineStreamConsumer(pipeIn, new OutStreamHandler(), GaeRunTask.this.kickStartSynchronizer)
outputCommandLineConsumer.start()
try {
runKickStart()
}
catch(Exception e) {
throw new GradleException('An error occurred starting the local development server.', e)
}
finally {
System.setIn(systemInOriginal)
System.setOut(systemOutOriginal)
System.setErr(systemErrOriginal)
}
}
private class OutStreamHandler implements StreamOutputHandler {
@Override
void handleLine(String line) {
logger.info line
checkServerStartupProgress(line)
}
}
private class ErrStreamHandler implements StreamOutputHandler {
@Override
void handleLine(String line) {
logger.error line
checkServerStartupProgress(line)
}
}
private void checkServerStartupProgress(final String line) {
if(line.contains('The server is running')) {
GaeRunTask.this.kickStartSynchronizer.resume()
}
}
}
}