Skip to content

Instantly share code, notes, and snippets.

View dtanner's full-sized avatar

Dan Tanner dtanner

  • Target
  • Minneapolis MN US
View GitHub Profile
@dtanner
dtanner / Test.groovy
Created August 24, 2012 17:00
Examples of overriding groovy instance and static methods
class Test {
void method1() {
println 'In method1'
}
void method1(String foo) {
println "In method1 with String param of $foo"
}
static method2(String foo) {
@dtanner
dtanner / TestGen.groovy
Created October 30, 2012 18:03
Groovy script to generate addition table quizzes for kids.
def getRandomNumber() {
randomInt = new Random().nextInt(11)
}
def buildCell() {
"""${getRandomNumber()} <br>
+ ${getRandomNumber()} <br>
<hr width=40px>
"""
@dtanner
dtanner / Bootstrap.groovy
Created December 16, 2012 22:54
Example of a grails project using mybatis migrations in the Bootstrap.groovy, automatically nuking and paving the test databases.
def init = { servletContext ->
if (["test", "ci", "functional"].contains(Environment.current.name)) {
// bring some environments down to bare database. 20120609160926 is the base migration script's ID
// limitation: if we add development to this list and run the app in IDEA, it orphans the java processes
executeCommand ("${getMigrationCommand()} --env=${Environment.current.name} version 20120609160926", new File("./db"))
executeCommand ("${getMigrationCommand()} --env=${Environment.current.name} up", new File("./db"))
}
// other init operations...
}
@dtanner
dtanner / UsageTrackingFilters.groovy
Created February 3, 2013 17:00
Sample Grails usage tracking filter. Optimized for splunk logging with its key=value separation.
package myapp
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.apache.log4j.MDC
class UsageTrackingFilters {
private static final Log LOG = LogFactory.getLog('usagetracking')
private static final String REQUEST_ID = "requestId"
@dtanner
dtanner / _field.gsp
Created September 27, 2013 14:43
Allow class attribute on a custom Grails Fields template
<%@ page defaultCodec="none" %>
<div class="form-group ${formGroupClass ?: ''} <g:if test="${invalid}">has-error</g:if>">
<label class="control-label ${labelClass ?: ''}" for="${property}">${label} <%=required ? "*" : ""%></label>
<input type="<%=type ?: "text" %>" name="${prefix ?: ''}${property}" id="${prefix ?: ''}${property}" value="${value}" <%=constraints.maxSize ? "maxlength=${constraints.maxSize}" : ""%> <%=readonly ? "readonly=readonly" : ""%> class="form-control <%= pageScope["class"] ?: '' %>" <%=placeholder ? "placeholder='$placeholder'" : ""%>/>
<g:if test="${helpText}"><span class="help-block">${helpText}</span></g:if>
<g:if test="${invalid}"><span class="help-block">${errors.join('<br>')}</span></g:if>
</div>
@dtanner
dtanner / groovy-spread-populated.groovy
Last active August 29, 2015 14:09
Groovy spread operators on a populated collection
def things = [ [a: 1], [a: 2] ]
things.collect { it.a } // returns [1, 2]
things*.a // returns [1, 2]
things.a // returns [1, 2]
@dtanner
dtanner / groovy-spread-null-collection.groovy
Last active August 29, 2015 14:09
Groovy spread operators on a null collection
def things = null
things.collect { it.a } // returns []
things*.a // returns null
things.a // throws a NullPointerException
@dtanner
dtanner / TomcatDatasourceUtil.groovy
Last active August 29, 2015 14:20
Grails Tomcat Datasource Utility
package com.foo.util
import groovy.util.logging.Log4j
import org.apache.tomcat.jdbc.pool.ConnectionPool
import org.codehaus.groovy.grails.commons.GrailsApplication
@Log4j
class TomcatDatasourceUtil {
static void ensureCurrentDatasources(GrailsApplication application, List datasourceNames) {
@dtanner
dtanner / Config.groovy
Created April 30, 2015 05:32
Example of loading a Grails configuration value from etcd
import groovy.json.JsonSlurper
dataSource {
// most properties are directly set
pooled = true
// ...
// the url is retrieved from etcd...make sure the etcd resource is properly protected
def jsonSlurper = new JsonSlurper()
def catalogUrlConfig = jsonSlurper.parseText(new URL("http://etcdlocation:2379/v2/keys/dataSource/url").text)
@dtanner
dtanner / RemoteConfigService.groovy
Created April 30, 2015 05:39
Example of a basic utility class to interact with etcd
package com.foo.config
import com.foog.util.TomcatDatasourceUtil
import grails.plugins.rest.client.RestBuilder
import groovy.json.JsonSlurper
/**
* Application service to get and set values from a centralized remote configuration service.
*/
class RemoteConfigService {