Skip to content

Instantly share code, notes, and snippets.

View nickman's full-sized avatar

Nicholas Whitehead nickman

View GitHub Profile
@nickman
nickman / GeneratedClosureClassFileTransformer.java
Created January 13, 2012 20:03
ClassFileTransformer for capturing GeneratedClosure byte code
/**
* {@inheritDoc}
* @see java.lang.instrument.ClassFileTransformer#transform(java.lang.ClassLoader, java.lang.String, java.lang.Class, java.security.ProtectionDomain, byte[])
*/
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
byte[] bytecode = classfileBuffer;
if(classBeingRedefined==null) {
if(loader instanceof GroovyClassLoader && isGeneratedClosure(bytecode)) {
@nickman
nickman / OnDemandCFT.groovy
Created January 13, 2012 20:58
On Demand ClassFileTransformer
import org.codehaus.groovy.control.*
import org.codehaus.groovy.ast.ClassHelper;
import org.helios.gmx.classloading.*;
// Acquire the instrumentation instance
def instrumentation = ByteCodeRepository.getInstance().agentInstrumentation.getInstrumentation();
// Define a closre
foo = {message -> println message};
// Define a simple class file transformer
class ByteCodeNet implements java.lang.instrument.ClassFileTransformer {
def clazzName;
@nickman
nickman / InnerClosures.groovy
Created January 13, 2012 21:46
Dumping Inner Closures
import org.helios.gmx.classloading.*;
import org.codehaus.groovy.control.*
def instrumentation = ByteCodeRepository.getInstance().agentInstrumentation.getInstrumentation();
class ByteCodeNet implements java.lang.instrument.ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, java.security.ProtectionDomain protectionDomain, byte[] classfileBuffer) {
if(ByteCodeRepository.getInstance().isGeneratedClosure(classfileBuffer)) {
println "Generated Closure: $className";
}
@nickman
nickman / HeliosMBeanServerBuilder.java
Created April 12, 2012 12:14
An example of an MBeanServer interceptor
package org.helios.jmx;
import java.io.ObjectInputStream;
import java.lang.management.ManagementFactory;
import java.util.Set;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceAlreadyExistsException;
@nickman
nickman / SimpleChannelFactory.java
Created May 18, 2012 00:41
Simple Channel Factory
Executor bossPool = Executors.newCachedThreadPool();
Executor workerPool = Executors.newCachedThreadPool();
ChannelFactory channelFactory = new NioClientSocketChannelFactory(boosPool, workerPool);
ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(
new ObjectEncoder()
}
};
Bootstrap boostrap = new ClientBootstrap(channelFactory);
@nickman
nickman / NettyChannelHandlers.html
Created June 6, 2012 19:49
Netty Channel Handlers
<table border='1'><tr><th>Direction</th><th>Sharable</th><th>Exclusive</th></tr><tr><td>Upstream</td><td valign='top'>
<ul>
<li>org.jboss.netty.handler.timeout.ReadTimeoutHandler</li>
<li>org.jboss.netty.handler.codec.base64.Base64Decoder</li>
<li>org.jboss.netty.handler.timeout.IdleStateHandler</li>
<li>org.jboss.netty.handler.codec.protobuf.ProtobufDecoder</li>
<li>org.jboss.netty.handler.codec.string.StringDecoder</li>
</ul></td><td valign='top'>
<ul>
<li>org.jboss.netty.handler.timeout.IdleStateAwareChannelUpstreamHandler</li>
@nickman
nickman / ExclusiveMyStringServerFactory.java
Created June 12, 2012 13:44
MyStringServerFactory - All Exclusive
public class MyStringServerFactory implements ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline p = Channels.pipeline();
// Decoders
p.addLast("frameDecoder", new DelimiterBasedFrameDecoder(Delimiters.lineDelimiter()));
p.ddLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
// Encoder
p.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
return p;
}
@nickman
nickman / SharedMyStringServerFactory.java
Created June 12, 2012 13:51
MyStringServerFactory - Shared
public class MyStringServerFactory implements ChannelPipelineFactory {
private final StringEncoder stringEncoder = new StringEncoder(CharsetUtil.UTF_8);
private final StringDecoder stringDecoder = new StringDecoder(CharsetUtil.UTF_8);
public ChannelPipeline getPipeline() throws Exception { ChannelPipeline p = Channels.pipeline();
// Decoders
p.addLast("frameDecoder", new DelimiterBasedFrameDecoder(Delimiters.lineDelimiter()));
p.ddLast("stringDecoder", stringDecoder);
// Encoder
p.addLast("stringEncoder", stringEncoder);
return p;
@nickman
nickman / MyPipelineFactory.java
Created June 12, 2012 13:54
MyPipelineFactory
public class MyPipelineFactory implements ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
// Create and configure a new pipeline for a new channel.
ChannelPipeline p = Channels.pipeline();
p.addLast("encoder", new EncodingHandler());
p.addLast("decoder", new DecodingHandler());
p.addLast("logic", new LogicHandler());
return p;
}
}
@nickman
nickman / WebSocketInvoker.js
Created April 27, 2013 21:22
Signature & Options for Generalized WebSocket Service Invoker
/**
* Generalized websocket service invoker
* @param command A (mandatory) JSON subscription request
* @param options:<ul>
* <li><b>timeout</b>: The timeout in ms. on the request invocation confirm. (i.e. not a subscriber timeout) Default is 2000 ms.</li>
* <li><b>onresponse</b>: A callback invoked when the immediate response of the command invocation is received.</li>
* <li><b>ontimeout</b>: A callback invoked when the request times out</li>
* <li><b>onevent</b>: A callback invoked when an asynchronous event is received associated to the original invocation.</li>
* <li><b>oncancel</b>: A callback invoked the asynchronous event subscription associated to the original invocation is cancelled</li>
* </ul>