BootStrap.groovy
BootStrap.groovy
Grails allows to simulate example data (this is called bootstrapping).
To create example data, you can use the class BootStrap.groovy from the
directory ./grails-app/conf with some data
This class is automatically executed whenever the server is started and can be
used to create some example data for testing.
class User {
String username
String name
static hasMany = [roles: Role]
static constraints = {
username(nullable: false, blank: false, unique: true)
name(nullable: false, blank: false, unique: true)
}
}
class Role {
String name
static constraints = {
name(nullable: false, blank: false, unique: true)
}
}
Domain
BootStrap.groovy
class BootStrap {
def init = { servletContext ->
User user1 = new User(username: 'user1', name: 'Regular')
User user2 = new User(username: 'user2', name: 'Super')
User user3 = new User(username: 'user3', name: 'Über')
User user4 = new User(username: 'user4', name: 'Admin')
Role role1 = new Role(name: 'Observer')
Role role2 = new Role(name: 'Executor')
Role role3 = new Role(name: 'Collector')
Role role4 = new Role(name: 'Administrator')
user1.addToRoles(role1)
user2.addToRoles(role1)
user2.addToRoles(role2)
user3.addToRoles(role1)
user3.addToRoles(role2)
user3.addToRoles(role3)
user4.addToRoles(role4)
role1.save() role2.save() role3.save() role4.save() role1.save() user2.save() user3.save()
user4.save()
}
}
grails {
mail {
host = "smtpserver"
port = 123
username = 'username'
password = 'password'
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
grails.mail.default.from = "abc@xyz.com"
grails.mail.disabled=true
app.startup.mail.to = "abc@xyz.com"
Config.groovy
def init = { servletContext ->
println "Application starting up... "
def toMail = ConfigurationHolder.config.app.startup.mail.to
def mailSubject = "Application starting up on
${InetAddress.localHost.hostName}";
String mailBody = '''
Application started at ''' + new SimpleDateFormat("dd-MMM-yyy hh:mm:ss").format(new
Date()) + '''<br/>''' +
'''<a href=' ''' + ConfigurationHolder.config.grails.serverURL + ''' '> ''' +
ConfigurationHolder.config.grails.serverURL + '''</a><br/><br/><br/>
@Note : This is an automatically generated email.
'''
sendNotification(toMail, mailSubject, mailBody )
}
def destroy = {
println "Application shutting down... "
def toMail = ConfigurationHolder.config.app.startup.mail.to
def mailSubject = "Application starting up on
${InetAddress.localHost.hostName}";
String mailBody = '''
Application shutting down at ''' + new SimpleDateFormat("dd-MMM-yyy
hh:mm:ss").format(new Date()) + '''<br/>''' +
'''<a href=' ''' + ConfigurationHolder.config.grails.serverURL + ''' '> ''' +
ConfigurationHolder.config.grails.serverURL + '''</a><br/><br/><br/>
@Note : This is an automatically generated email.
'''
sendNotification(toMail, mailSubject, mailBody )
}
def sendNotification(String toMail, String mailSubject, String mailBody)
{
println "### sending email ###"
mailService.sendMail {
to toMail
subject mailSubject
body mailBody
}
}

Boot strap.groovy

  • 1.
  • 2.
    BootStrap.groovy Grails allows tosimulate example data (this is called bootstrapping). To create example data, you can use the class BootStrap.groovy from the directory ./grails-app/conf with some data This class is automatically executed whenever the server is started and can be used to create some example data for testing.
  • 3.
    class User { Stringusername String name static hasMany = [roles: Role] static constraints = { username(nullable: false, blank: false, unique: true) name(nullable: false, blank: false, unique: true) } } class Role { String name static constraints = { name(nullable: false, blank: false, unique: true) } } Domain
  • 4.
    BootStrap.groovy class BootStrap { definit = { servletContext -> User user1 = new User(username: 'user1', name: 'Regular') User user2 = new User(username: 'user2', name: 'Super') User user3 = new User(username: 'user3', name: 'Über') User user4 = new User(username: 'user4', name: 'Admin') Role role1 = new Role(name: 'Observer') Role role2 = new Role(name: 'Executor') Role role3 = new Role(name: 'Collector') Role role4 = new Role(name: 'Administrator') user1.addToRoles(role1) user2.addToRoles(role1) user2.addToRoles(role2) user3.addToRoles(role1) user3.addToRoles(role2) user3.addToRoles(role3) user4.addToRoles(role4) role1.save() role2.save() role3.save() role4.save() role1.save() user2.save() user3.save() user4.save() } }
  • 5.
    grails { mail { host= "smtpserver" port = 123 username = 'username' password = 'password' props = ["mail.smtp.auth":"true", "mail.smtp.socketFactory.port":"465", "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory", "mail.smtp.socketFactory.fallback":"false"] } } grails.mail.default.from = "abc@xyz.com" grails.mail.disabled=true app.startup.mail.to = "abc@xyz.com" Config.groovy
  • 6.
    def init ={ servletContext -> println "Application starting up... " def toMail = ConfigurationHolder.config.app.startup.mail.to def mailSubject = "Application starting up on ${InetAddress.localHost.hostName}"; String mailBody = ''' Application started at ''' + new SimpleDateFormat("dd-MMM-yyy hh:mm:ss").format(new Date()) + '''<br/>''' + '''<a href=' ''' + ConfigurationHolder.config.grails.serverURL + ''' '> ''' + ConfigurationHolder.config.grails.serverURL + '''</a><br/><br/><br/> @Note : This is an automatically generated email. ''' sendNotification(toMail, mailSubject, mailBody ) }
  • 7.
    def destroy ={ println "Application shutting down... " def toMail = ConfigurationHolder.config.app.startup.mail.to def mailSubject = "Application starting up on ${InetAddress.localHost.hostName}"; String mailBody = ''' Application shutting down at ''' + new SimpleDateFormat("dd-MMM-yyy hh:mm:ss").format(new Date()) + '''<br/>''' + '''<a href=' ''' + ConfigurationHolder.config.grails.serverURL + ''' '> ''' + ConfigurationHolder.config.grails.serverURL + '''</a><br/><br/><br/> @Note : This is an automatically generated email. ''' sendNotification(toMail, mailSubject, mailBody ) }
  • 8.
    def sendNotification(String toMail,String mailSubject, String mailBody) { println "### sending email ###" mailService.sendMail { to toMail subject mailSubject body mailBody } }