Skip to content

Instantly share code, notes, and snippets.

@jpertino
jpertino / gist:731105
Created December 6, 2010 22:30
groovy xml string generation
def referenceClosure = {
html {
body {
h1 "hello"
}
}
}
def markupBuilder(closure) {
@jpertino
jpertino / groovyws.groovy
Created December 6, 2010 22:56
groovy ws simple server and client
@Grab('org.codehaus.groovy.modules:groovyws:0.5.2')
def 'groovyws'(){}
class SyncService {
def getFoo() {
"bar. i mean, foo"
}
}
@jpertino
jpertino / missingMethodMultipleDispatch.groovy
Created December 29, 2010 23:54
groovy method dispatching with a little help
class Magic {
def methodMissing(String methodName, Object arguments) {
method(methodName, *arguments)
}
private method(String methodName, Object... arguments) {
if (!(arguments[-1] instanceof Closure))
throw new MissingMethodException(methodName, this.class, arguments)
@jpertino
jpertino / cliBuilderExample.groovy
Created December 30, 2010 14:32
groovy CliBuilder usage
def cli = new CliBuilder().with {
usage = 'program [options] <arguments>'
header = 'Options:'
footer = '-' * width
s 'simplest boolean option'
b longOpt: 'both', 'boolean option with both longop and shortop'
_ longOpt: 'no-shortop-1', 'boolean option without short version 1'
@jpertino
jpertino / uselessTrustManager.groovy
Created January 6, 2011 19:42
HttpsURLConnection DefaultSSLSocketFactory TrustManager
javax.net.ssl.SSLContext.getInstance('SSL').with {
init null, [[checkServerTrusted:{chain,authType->}]] as javax.net.ssl.X509TrustManager[], null
javax.net.ssl.HttpsURLConnection.defaultSSLSocketFactory = socketFactory
}
@jpertino
jpertino / readClipboard.groovy
Created January 8, 2011 01:42
read string from clipboard
java.awt.Toolkit.defaultToolkit.systemClipboard.getData java.awt.datatransfer.DataFlavor.stringFlavor
import static org.hamcrest.CoreMatchers.is
import static org.junit.Assert.assertArrayEquals
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertSame
import static org.junit.Assert.assertNot
import static org.junit.Assert.assertNull
import static org.junit.Assert.assertNotNull
import static org.junit.Assume.assumeNoException
@jpertino
jpertino / preparePdf.groovy
Created January 8, 2011 04:30
sort pdf pages for book-like two-page print
@GrabResolver(name='itextpdf', root='http://maven.itextpdf.com/')
@Grab('com.itextpdf:itextpdf:5.0.5')
import static com.itextpdf.text.PageSize.A4
import com.itextpdf.text.Document
import com.itextpdf.text.pdf.PdfReader
import com.itextpdf.text.pdf.PdfWriter
def cli = new CliBuilder().with {
usage = 'preparePdf [options] <source file path>'
@jpertino
jpertino / simpleJettyServer.groovy
Created January 20, 2011 04:29
a simple embedded jetty server
@Grab('org.mortbay.jetty:jetty-embedded:6.1.26')
import static org.mortbay.jetty.Handler.DEFAULT
import org.mortbay.jetty.Server
import org.mortbay.jetty.servlet.Context
import org.mortbay.jetty.servlet.DefaultServlet
import org.mortbay.servlet.MultiPartFilter
import groovy.servlet.GroovyServlet
import groovy.servlet.TemplateServlet
@jpertino
jpertino / databaseConnection.groovy
Created January 28, 2011 23:29
several database connectors and drivers
// PostgreSQL
@GrabConfig(systemClassLoader=true)
@Grab('postgresql:postgresql:9.0-801.jdbc4')
def sql = groovy.sql.Sql.newInstance(
"jdbc:postgresql://host.example.org/database",
"username", "password", "org.postgresql.Driver")
// MySQL
@GrabConfig(systemClassLoader=true)