Groovy Groovy:日期[1]

def calendar = Calendar.instance;
calendar.with {
clear()
set MONTH, JULY
set DATE, 4
set YEAR, 1929
println time
}

will print -> Thu Jul 04 00:00:00 ICT 1929
:)

Groovy Groovy:日期[2] - Jodatime

import org.joda.time.DateTime;

new DateTime().withDayOfMonth(4).withMonthOfYear(7).withYear(1776).println('EEE, 'dd/MM/yyyy HH:mm:ss');

Groovy Groovy代表,所有者,这个引用。

class Class1 {
  def closure = {
    println this.class.name
    println delegate.class.name
    def nestedClos = {
      println owner.class.name
    }
    nestedClos()
  }
}

def clos = new Class1().closure
clos.delegate = this
clos()
/*prints:
 Class1
 Script1
 Class1$_closure1  */

Groovy 向Bootstrap.groovy添加角色

//Adding Roles
def roleAdmin = new Role(authority:'ROLE_ADMIN', description:'App admin').save()
def roleUser  = new Role(authority:'ROLE_USER', description:'App user').save()

Groovy 向Bootstrap.groovy添加权限

def secureUserEdit = new RequestMap(url: '/user/edit', configAttribute:'ROLE_ADMIN').save()
def secureUserSave = new RequestMap(url: '/user/save', configAttribute:'ROLE_ADMIN').save()
def secureUserCreate = new RequestMap(url: '/user/create', configAttribute:'ROLE_ADMIN').save()
def secureUserList = new RequestMap(url: '/user/list', configAttribute:'ROLE_ADMIN, ROLE_USER').save()

Groovy 使用Acegi AuthenticateService

def authenticateService

def debugPrincipal = {
	def userPrincipal = authenticateService.principal()
	println userPrincipal.getUsername() //shows the current logged user username
	println userPrincipal.getAuthorities() //shows the current logged user authorities
	redirect action: list, params: params
}

Groovy 实现我自己的Acegi Security事件监听器

import org.springframework.context.*
import org.springframework.security.event.authentication.*
import org.springframework.security.event.authorization.AbstractAuthorizationEvent


class AcegiEventListener implements ApplicationListener {

	void onApplicationEvent(final ApplicationEvent e) {

		if (e instanceof AbstractAuthenticationEvent) {

			if (e instanceof AuthenticationSuccessEvent) {
				
				println "** Attention: User ${e.source.principal.username} is logged!"
			}
		}
	}
}

Groovy 从GString运行外部进程

proc = "ls -al".execute()
println(proc.in.text)
println(proc.returnCode())

Groovy 寻找最长的辅音群

def words = [
	berndschiffer: 'Angstschweiss', // ß -> ss
	mittie: 'Herbstschnee',
	mittie: 'rontgst', // ö -> o
	mittie: 'Durststrecke',
	mittie: 'Kunstspringen',
	mittie: 'Borschtschschmand',
	ipreuss: 'Borschtschschussel', // ü -> u
]
println words.collect{ user, word -> 
	def cluster = word.toLowerCase().split(/[aeiou]/).sort{ it.size() }.toList().last()
	[cluster] << "@$user found the longest consonant cluster '$cluster' in the word '$word'."
}.sort{ it.first().size() }.last().last()

Groovy 使用Groovy和Apache XML-RPC访问SNIPPLR

import org.apache.xmlrpc.client.XmlRpcClient
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl

def XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl()
config.setServerURL(new URL("http://snipplr.com/xml-rpc.php"))
def XmlRpcClient client = new XmlRpcClient()
client.setConfig(config)

Object[] params = ["[yourAPIkey]"]
def result = client.execute("user.checkkey",params)
println result

params = ["[yourAPIkey]","groovy"]
result = client.execute("snippet.list",params)
println result