Skip to content

Instantly share code, notes, and snippets.

@danveloper
danveloper / AppConfig.groovy
Created May 22, 2013 16:31
Accessing Grails Configuration with Spring Property Placeholders
package com.objectpartners.application.config
config {
social {
twitter {
consumerKey = "ABCD"
consumerSecret = "EFGH"
accessToken = "IJKL"
accessTokenSecret = "MNOP"
}
@danveloper
danveloper / ApplicationConfiguration.groovy
Last active December 17, 2015 14:19
Groovy Configuration DSL for environment properties in Spring
package com.danveloper.reactor.config
reactor {
social {
twitter {
consumerKey = "ABCD"
consumerSecret = "EFGH"
accessToken = "IJKL"
accessTokenSecret = "MNOP"
}
@danveloper
danveloper / ImprovedNamingDescriptorCustomizer.java
Created May 6, 2013 05:53
EclipseLink class descriptor customizer that mimics Hibernate's ImprovedNamingStrategy for table and field naming
public class ImprovedNamingDescriptorCustomizer implements DescriptorCustomizer {
@Override
public void customize(ClassDescriptor classDescriptor) throws Exception {
String className = classDescriptor.getJavaClassName();
String shortName = Helper.getShortClassName(className);
String improvedName = toImprovedName(shortName);
classDescriptor.setTableName(improvedName);
updateMappings(classDescriptor, improvedName);
@danveloper
danveloper / TrampolineLambda.java
Created April 18, 2013 18:27
Trampoline in Java 8 with Lambdas and interface defaults
public class TrampolineLambda {
interface TrampolineFunction<T, R> {
R apply(T...obj);
public default Object trampoline(T...objs) {
Object result = apply(objs);
if (!(result instanceof TrampolineFunction)) {
return result;
} else {
return this;
@danveloper
danveloper / MemoizedLambdaFib.java
Last active December 16, 2015 08:19
Memoized Fibonacci calculation using lambdas in Java 8
public class MemoizedLambdaFib {
interface MemoizedFunction<T, R> {
enum Cache {
_;
Map<Object, Object> vals = new HashMap<>();
}
R calc(T t);
@danveloper
danveloper / Promises.java
Created April 16, 2013 19:40
Java 8 CompletableFuture (Promise) and Lambda example
public class Promises {
private static class PageMetrics {
Integer visits;
Long avgMsOnPage;
@Override
public String toString() {
return String.format("{ avgMsOnPage=%d, visits=%d }", avgMsOnPage, visits);
}
@danveloper
danveloper / convert.sh
Last active December 5, 2018 18:51
Java to Groovy converter in bash!!
#!/bin/sh
find . -iname "*.java" -exec sed s/\;$// {} \;
@danveloper
danveloper / LoggingService.groovy
Last active December 15, 2015 06:09
LoggingService.log - Real Time Logging Project
class LoggingService {
/**
* These two fields represent the atmosphere "config".
*/
static final MAPPING_URI = ApplicationHolder.application.config.grails.atmosphere.mappingUri
static atmosphere = [mapping: MAPPING_URI]
/**
* The {@link LoggingServiceActivator} delegates its messages to this method.
* This method, in turn, broadcasts them to the UI via the atmosphere broadcaster.
@danveloper
danveloper / index.xml
Created March 20, 2013 21:25
UI Atmosphere Subscription (new logging instance) -- Real Time Logging Project
<!-- ... some rendered code, divs and shit, removed for brevity ... -->
<script>
(function() {
var url = '${createLink(uri: ApplicationHolder.application.config.grails.atmosphere.mappingUri)}/logchannel/${id}/${ip}/${hostname}';
$.atmosphere.subscribe(url, Logging.router, $.atmosphere.request = {transport:'websocket', fallbackTransport:'streaming'});
...
})();
</script>
@danveloper
danveloper / gist:5208601
Last active December 15, 2015 05:20
UI Atmosphere Subscription -- Real Time Logging
<!-- ... resources & jquery snipped for brevity ... -->
<atmosphere:resources/>
<r:script>
$(document).ready(function() {
var url = '${createLink(uri:ApplicationHolder.application.config.grails.atmosphere.mappingUri)}';
$.atmosphere.subscribe(url, Logging.router, $.atmosphere.request = {transport:'websocket', fallbackTransport:'streaming'});
...
});
</r:script>