Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 255 lines (216 sloc) 8.651 kb
3e17890 1.1 branch is now trunk
graeme authored
1 /*
2 * Copyright 2004-2005 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
98733cf @graemerocher Partial fix for run-app in interactive mode, there seems to be another p...
graemerocher authored
16
3e17890 1.1 branch is now trunk
graeme authored
17 import grails.util.GrailsUtil
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a server ...
burtbeckwith authored
18 import grails.util.Metadata
3e17890 1.1 branch is now trunk
graeme authored
19 import grails.web.container.EmbeddableServer
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a server ...
burtbeckwith authored
20 import grails.web.container.EmbeddableServerFactory
21
22 import java.net.ServerSocket
23
24 import org.codehaus.groovy.grails.cli.interactive.InteractiveMode
3e17890 1.1 branch is now trunk
graeme authored
25
26 /**
69c8376 @graemerocher Fix for GRAILS-5055 "Server parameter to eventConfigureJetty is null in ...
graemerocher authored
27 * Gant script that executes Grails using an embedded server
3e17890 1.1 branch is now trunk
graeme authored
28 *
29 * @author Graeme Rocher
30 *
31 * @since 0.4
32 */
33
081c7a6 @graemerocher Grails 1.2 now uses Tomcat as the default server
graemerocher authored
34 includeTargets << grailsScript("_GrailsPlugins")
3e17890 1.1 branch is now trunk
graeme authored
35
835947f @burtbeckwith cleanup
burtbeckwith authored
36 SCHEME_HTTP = "http"
37 SCHEME_HTTPS = "https"
3e17890 1.1 branch is now trunk
graeme authored
38
b735e73 @pledbrook Fix for GRAILS-4364: automatic server restarts now remember whether
pledbrook authored
39 // Keep track of whether we're running in HTTPS mode in case we need
40 // to restart the server.
41 usingSecureServer = false
3e17890 1.1 branch is now trunk
graeme authored
42
43 grailsServer = null
44 grailsContext = null
45 autoRecompile = System.getProperty("disable.auto.recompile") ? !(System.getProperty("disable.auto.recompile").toBoolean()) : true
46
47 // How often should recompilation occur while the application is running (in seconds)?
48 // Defaults to 3s.
49 recompileFrequency = System.getProperty("recompile.frequency")
50 recompileFrequency = recompileFrequency ? recompileFrequency.toInteger() : 3
51
52 shouldPackageTemplates = true
53
54 // This isn't used within this script but may come in handy for scripts
55 // that depend on this one.
56 ant.path(id: "grails.runtime.classpath", runtimeClasspath)
57
58 /**
59 * Runs the application in dev mode, i.e. with class-reloading.
60 */
61 target(runApp: "Main implementation that executes a Grails application") {
62 runInline(SCHEME_HTTP, serverHost, serverPort, serverPortHttps)
63 }
64
65 /**
66 * Runs the application in dev mode over HTTPS.
67 */
68 target(runAppHttps: "Main implementation that executes a Grails application with an HTTPS listener") {
69 runInline(SCHEME_HTTPS, serverHost, serverPort, serverPortHttps)
70 }
71
72 /**
73 * Runs the application using the WAR file directly.
74 */
75 target (runWar : "Main implementation that executes a Grails application WAR") {
76 runWar(SCHEME_HTTP, serverHost, serverPort, serverPortHttps)
77 }
78
79
80 /**
81 * Runs the application over HTTPS using the WAR file directly.
82 */
83 target (runWarHttps : "Main implementation that executes a Grails application WAR") {
84 runWar(SCHEME_HTTPS, serverHost, serverPort, serverPortHttps)
85 }
86
87 private EmbeddableServerFactory loadServerFactory() {
88 def load = { name -> classLoader.loadClass(name).newInstance() }
89
7e60e71 @graemerocher Default tomcat server class name was incorrect
graemerocher authored
90 String defaultServer = "org.grails.plugins.tomcat.TomcatServerFactory"
081c7a6 @graemerocher Grails 1.2 now uses Tomcat as the default server
graemerocher authored
91 def containerClass = getPropertyValue("grails.server.factory", defaultServer)
3e17890 1.1 branch is now trunk
graeme authored
92 EmbeddableServerFactory serverFactory
93 try {
94 serverFactory = load(containerClass)
95 }
835947f @burtbeckwith cleanup
burtbeckwith authored
96 catch (ClassNotFoundException cnfe) {
97 if (containerClass == defaultServer) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it conflicte...
burtbeckwith authored
98 grailsConsole.error "WARNING: No default container found, installing Tomcat.."
6bbf23a @graemerocher fixed code causing MME
graemerocher authored
99 doInstallPlugin "tomcat", GrailsUtil.grailsVersion
0bf3e1f @graemerocher Fixed by clearing plugin settings cache (wrong method signature)
graemerocher authored
100 pluginSettings.clearCache()
081c7a6 @graemerocher Grails 1.2 now uses Tomcat as the default server
graemerocher authored
101 compilePlugins()
6447df7 @graemerocher another fix for the failing functional tests
graemerocher authored
102 loadPlugins()
835947f @burtbeckwith cleanup
burtbeckwith authored
103 serverFactory = load(containerClass)
081c7a6 @graemerocher Grails 1.2 now uses Tomcat as the default server
graemerocher authored
104 }
105 }
3e17890 1.1 branch is now trunk
graeme authored
106 catch (Throwable e) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it conflicte...
burtbeckwith authored
107 grailsConsole.error e
3e17890 1.1 branch is now trunk
graeme authored
108 event("StatusFinal", ["Failed to load container [$containerClass]: ${e.message}"])
109 exit(1)
110 }
111 return serverFactory
112 }
113
114 private runInline(scheme, host, httpPort, httpsPort) {
115 EmbeddableServerFactory serverFactory = loadServerFactory()
116 grailsServer = serverFactory.createInline("${basedir}/web-app", webXmlFile.absolutePath, serverContextPath, classLoader)
117 runServer server: grailsServer, host:host, httpPort: httpPort, httpsPort: httpsPort, scheme:scheme
118 startPluginScanner()
119 }
120
121 private runWar(scheme, host, httpPort, httpsPort) {
122 EmbeddableServerFactory serverFactory = loadServerFactory()
123 grailsServer = serverFactory.createForWAR(warName, serverContextPath)
124
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a server ...
burtbeckwith authored
125 Metadata.getCurrent().put(Metadata.WAR_DEPLOYED, "true")
3e17890 1.1 branch is now trunk
graeme authored
126 runServer server:grailsServer, host:host, httpPort:httpPort, httpsPort: httpsPort, scheme: scheme
127 }
128
129 /**
130 * Runs the Server. You can pass these named arguments:
131 *
132 * server - The server instance to use (required).
133 * port - The network port the server is running on (used to display the URL) (required).
134 * scheme - The network scheme to display in the URL (optional; defaults to "http").
135 */
136 runServer = { Map args ->
137 try {
1aa1cb0 @graemerocher Enhanced terminal output, needed to support redesigned interactive mode
graemerocher authored
138 event("StatusUpdate", ["Running Grails application"])
3e17890 1.1 branch is now trunk
graeme authored
139 def message = "Server running. Browse to http://${args.host ?: 'localhost'}:${args.httpPort}$serverContextPath"
140
141 EmbeddableServer server = args["server"]
835947f @burtbeckwith cleanup
burtbeckwith authored
142 if (server.hasProperty('eventListener')) {
0f88cc9 @graemerocher registration of event listener and grailsConfig not tied to Jetty
graemerocher authored
143 server.eventListener = this
144 }
835947f @burtbeckwith cleanup
burtbeckwith authored
145 if (server.hasProperty('grailsConfig')) {
0f88cc9 @graemerocher registration of event listener and grailsConfig not tied to Jetty
graemerocher authored
146 server.grailsConfig = config
3e17890 1.1 branch is now trunk
graeme authored
147 }
148
149 profile("start server") {
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a server ...
burtbeckwith authored
150
151 try { new ServerSocket(args.httpPort).close() }
152 catch (IOException e) {
153 event("StatusFinal", ["Server failed to start for port $args.httpPort: $e.message"])
154 exit(1)
155 }
156
835947f @burtbeckwith cleanup
burtbeckwith authored
157 if (args.scheme == 'https') {
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a server ...
burtbeckwith authored
158
159 try { new ServerSocket(args.httpsPort).close() }
160 catch (IOException e) {
161 event("StatusFinal", ["Server failed to start for port $args.httpsPort: $e.message"])
162 exit(1)
163 }
164
b735e73 @pledbrook Fix for GRAILS-4364: automatic server restarts now remember whether
pledbrook authored
165 usingSecureServer = true
3e17890 1.1 branch is now trunk
graeme authored
166 server.startSecure args.host, args.httpPort, args.httpsPort
167
168 // Update the message to reflect the fact we are running HTTPS as well.
169 message += " or https://${args.host ?: 'localhost'}:${args.httpsPort}$serverContextPath"
170 }
171 else {
172 server.start args.host, args.httpPort
173 }
174 }
175 event("StatusFinal", [message])
835947f @burtbeckwith cleanup
burtbeckwith authored
176 }
177 catch (Throwable t) {
3e17890 1.1 branch is now trunk
graeme authored
178 GrailsUtil.deepSanitize(t)
835947f @burtbeckwith cleanup
burtbeckwith authored
179 if (!(t instanceof SocketException) && !(t.cause instanceof SocketException)) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it conflicte...
burtbeckwith authored
180 grailsConsole.error t
835947f @burtbeckwith cleanup
burtbeckwith authored
181 }
3e17890 1.1 branch is now trunk
graeme authored
182 event("StatusFinal", ["Server failed to start: $t"])
183 exit(1)
184 }
185 }
186
187 /**
188 * Starts the plugin scanner. Call this after starting the server if you
189 * want changes to artifacts automatically detected and loaded.
190 */
191 target(startPluginScanner: "Starts the plugin manager's scanner that detects changes to artifacts.") {
4a69917 @burtbeckwith whitespace, STS warnings, etc.
burtbeckwith authored
192 def watcher = new org.codehaus.groovy.grails.compiler.GrailsProjectWatcher(projectCompiler, pluginManager)
193 watcher.start()
3e17890 1.1 branch is now trunk
graeme authored
194 }
195
6779eb9 @alkemist When stopping the running server, also stop the plugin change scanner.
alkemist authored
196 target(stopPluginScanner: "Stops the plugin manager's scanner that detects changes to artifacts.") {
4a69917 @burtbeckwith whitespace, STS warnings, etc.
burtbeckwith authored
197 // do nothing, here for compatibility
6779eb9 @alkemist When stopping the running server, also stop the plugin change scanner.
alkemist authored
198 }
199
3e17890 1.1 branch is now trunk
graeme authored
200 /**
201 * Keeps the server alive and checks for changes in domain classes or
202 * source files under "src". If any changes are detected, the servlet
203 * container is restarted.
204 */
205 target(watchContext: "Watches the WEB-INF/classes directory for changes and restarts the server if necessary") {
206 depends(classpath)
207
98733cf @graemerocher Partial fix for run-app in interactive mode, there seems to be another p...
graemerocher authored
208 if (InteractiveMode.current) {
f2f37f1 @burtbeckwith added port tests to avoid long stacktrace when there's already a server ...
burtbeckwith authored
209 Thread.start {
210 def im = InteractiveMode.current
211 im.grailsServer = grailsServer
212 im.run()
213 }
3e17890 1.1 branch is now trunk
graeme authored
214 }
215
4a69917 @burtbeckwith whitespace, STS warnings, etc.
burtbeckwith authored
216 keepServerAlive()
3e17890 1.1 branch is now trunk
graeme authored
217 }
218
219 target(keepServerAlive: "Idles the script, ensuring that the server stays running.") {
220 def keepRunning = true
221 def killFile = new File("${basedir}/.kill-run-app")
222 while (keepRunning) {
223 sleep(recompileFrequency * 1000)
224
225 // Check whether the kill file exists. This is a hack for the
226 // functional tests so that we can stop the servers that are
227 // started.
228 if (killFile.exists()) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it conflicte...
burtbeckwith authored
229 grailsConsole.updateStatus "Stopping server..."
3e17890 1.1 branch is now trunk
graeme authored
230 grailsServer.stop()
231 killFile.delete()
232 keepRunning = false
233 }
234 }
235 }
236
69c8376 @graemerocher Fix for GRAILS-5055 "Server parameter to eventConfigureJetty is null in ...
graemerocher authored
237 target(stopServer: "Stops the Grails servlet container") {
3e17890 1.1 branch is now trunk
graeme authored
238 if (grailsServer) {
5518da2 @graemerocher handle server stoppage errors more elegantly
graemerocher authored
239 try {
240 grailsServer.stop()
241 }
242 catch (Throwable e) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it conflicte...
burtbeckwith authored
243 grailsConsole.error "Error stopping server: ${e.message}", e
5518da2 @graemerocher handle server stoppage errors more elegantly
graemerocher authored
244 }
835947f @burtbeckwith cleanup
burtbeckwith authored
245
6779eb9 @alkemist When stopping the running server, also stop the plugin change scanner.
alkemist authored
246 try {
247 stopPluginScanner()
248 }
249 catch (Throwable e) {
e3fb07d @burtbeckwith renamed 'console' binding variable to 'grailsConsole' since it conflicte...
burtbeckwith authored
250 grailsConsole.error "Error stopping plugin change scanner: ${e.message}", e
6779eb9 @alkemist When stopping the running server, also stop the plugin change scanner.
alkemist authored
251 }
3e17890 1.1 branch is now trunk
graeme authored
252 }
253 event("StatusFinal", ["Server stopped"])
254 }
Something went wrong with that request. Please try again.