GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / scripts / RunAppHttps.groovy
blob7087c084c4acf3c7499fffe6c779803a0fb3bfdc
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.
17 /**
18 * Gant script that executes Grails using an embedded Jetty server with
19 * an HTTPS listener
21 * @author Graeme Rocher
23 * @since 0.4
26 import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
27 import org.mortbay.jetty.*
28 import org.mortbay.jetty.nio.*
29 import org.mortbay.jetty.handler.*
30 import org.mortbay.jetty.webapp.*
31 import org.mortbay.jetty.security.*
32 import sun.security.tools.*
34 Ant.property(environment:"env")
35 grailsHome = Ant.antProject.properties."env.GRAILS_HOME"
36 userHome = Ant.antProject.properties."user.home"
37 Ant.property(file:"${grailsHome}/build.properties")
38 grailsVersion = Ant.antProject.properties.'grails.version'
39 grailsServer = null
40 grailsContext = null
41 keystore = "${userHome}/.grails/${grailsVersion}/ssl/keystore"
42 keystoreFile = new File("${keystore}")
43 keyPassword = "123456"
45 includeTargets << new File ( "${grailsHome}/scripts/Init.groovy" )
46 includeTargets << new File ( "${grailsHome}/scripts/Package.groovy" )
47 includeTargets << new File ( "${grailsHome}/scripts/RunApp.groovy" )
49 target ('default': "Run's a Grails application in Jetty with HTTPS listener") {
50 depends( checkVersion, configureProxy, packageApp, generateWebXml )
51 runAppHttps()
52 watchContext()
54 target ( runAppHttps : "Main implementation that executes a Grails application with ans HTTPS listener") {
55 System.setProperty('org.mortbay.xml.XmlParser.NotValidating', 'true')
56 def server = configureHttpServer()
57 try {
58 if (!(keystoreFile.exists())) {
59 createCert()
61 def secureListener = new SslSocketConnector()
62 secureListener.setPort(serverPortHttps)
63 secureListener.setMaxIdleTime(50000)
64 secureListener.setPassword("${keyPassword}")
65 secureListener.setKeyPassword("${keyPassword}")
66 secureListener.setKeystore("${keystore}")
67 secureListener.setNeedClientAuth(false)
68 secureListener.setWantClientAuth(true)
69 def connectors = server.getConnectors().toList()
70 connectors.add(secureListener)
71 server.setConnectors(connectors.toArray(new Connector[0]))
72 server.start()
73 event("StatusFinal", ["Server running. Browse to https://localhost:$serverPortHttps$serverContextPath"])
74 } catch(Throwable t) {
75 t.printStackTrace()
76 event("StatusFinal", ["Server failed to start: $t"])
80 target(createCert:"Creates a keystore and SSL cert for use with HTTPS"){
81 println 'Creating SSL Cert...'
82 if(!keystoreFile.getParentFile().exists() &&
83 !keystoreFile.getParentFile().mkdir()){
84 def msg = "Unable to create keystore folder: " + keystoreFile.getParentFile().getCanonicalPath()
85 event("StatusFinal", [msg])
86 throw new RuntimeException(msg)
88 String[] keytoolArgs = ["-genkey", "-alias", "localhost", "-dname",
89 "CN=localhost,OU=Test,O=Test,C=US", "-keyalg", "RSA",
90 "-validity", "365", "-storepass", "key", "-keystore",
91 "${keystore}", "-storepass", "${keyPassword}",
92 "-keypass", "${keyPassword}"]
93 KeyTool.main(keytoolArgs)
94 println 'Created SSL Cert'