Skip to content

Instantly share code, notes, and snippets.

View bdelacretaz's full-sized avatar

Bertrand Delacretaz bdelacretaz

View GitHub Profile
@bdelacretaz
bdelacretaz / j.groovy
Created June 30, 2016 15:07
Groovy JsonBuilder experiments
// Demonstrate various features of the Groovy StreamingJsonBuilder
// run this with groovy <filename>
// See below for typical output
// A Groovy class
class Resource { String path; String resourceType }
// Groovy list of resources
resources = [
new Resource(path:"/tmp/one", resourceType:"my/example"),
@bdelacretaz
bdelacretaz / jsonwrapped.groovy
Last active July 1, 2016 13:17
Groovy JsonBuilder with simulated Sling resource wrapper
// Experimenting with Groovy JsonBuilder as a way to implement
// a JSON builder script engine for Sling
// The Sling .jsonbuild script would only contain the part
// between // SCRIPT markers below, the rest is glue that
// this new scripting engine would provide
// We'll probably need a Groovy-friendly wrapper
// with convenience methods for the current resource,
// where the tree navigation starts from
@bdelacretaz
bdelacretaz / slingGroovyJsonRendering.groovy
Last active July 5, 2016 12:40
Sling .groovy rendering script that uses Groovy's JsonBuilder
// Sling .groovy rendering script that uses Groovy's JsonBuilder
// Requires the groovy, groovy-json, groovy-templates and groovy-jsr233 bundles, so
// that /system/console/status-slingscripting lists the Groovy engine.
// Also requires adding "groovy" as an additional extension to the
// org.apache.sling.scripting.core.impl.ScriptCacheImpl config
import groovy.json.StreamingJsonBuilder
def builder = new StreamingJsonBuilder(out)
def getChildren(r) {
@bdelacretaz
bdelacretaz / metrics.yaml
Created July 13, 2016 12:09
metrics.yaml for SLING-4849 which produces "Unused Config" for all 4 specified methods
global:
monitor:true
reporters:
jmx: true
console:
period: 30
org.apache.sling.jcr.api.SlingRepository:
getDefaultWorkspace:
()Ljava/lang/String;: timer
loginAdministrative:
// Read a JSON message from ActiveMQ, get a filename from it,
// use exiftool to get file metadata and encode in JSON
import groovy.json.StreamingJsonBuilder
def writer = new java.io.OutputStreamWriter(System.out)
def builder = new StreamingJsonBuilder(writer)
// Get JSON message from queue
// TODO should not ACK it if this script fails
package main
// I'm a total beginner in Go, please have mercy ;-)
// Reads a JSON input object with an "url" field that points to an image file
// Uses exiftool to extract file metadata
// Outputs a JSON document with that metadata + request information
import (
"bufio"
"encoding/json"
@bdelacretaz
bdelacretaz / Lambdastuff.java
Created June 8, 2017 15:34
Example with Java 8 Lamdbam executors and futures
package lamdbastuff;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
@bdelacretaz
bdelacretaz / fileopen.d
Created June 22, 2017 05:22
dtrace script to dump all file open calls for a specific process
#!/usr/sbin/dtrace -s
/* dump all "open" syscalls */
/* $1 is the PID to observe */
syscall::open:entry
/pid == $1/
{
printf("'%s' (PID: %d): syscall:open %s", execname, pid, copyinstr(arg0));
}
@bdelacretaz
bdelacretaz / template.groovy
Created June 27, 2017 11:47
Inject Groovy expressions in a plain Java string
// Demonstrate how to inject Groovy expressions
// in a plain Java string
def now = "${new Date()}"
def data = [ value : "The date is ${now}" ]
// Inject Groovy variables from inputData
// into the supplied string
def interpolate(inputData, str) {
// TODO is there a way to have another name than "x"
// for the injected variable?
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<h1>Some Java</h1>
<pre><code>
public class Result implements Iterable <ResultLog.Entry> {