Skip to content

Instantly share code, notes, and snippets.

View nickman's full-sized avatar

Nicholas Whitehead nickman

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / SimpleBCR.groovy
Created January 13, 2012 19:55
Simple Working Exampe of BCR
import org.helios.gmx.classloading.*;
foo = {message -> println message};
bytes = ByteCodeRepository.getInstance().getByteCode(foo.getClass());
println "Class:${foo.getClass().getName()} ByteCode:${bytes.length} bytes";
@nickman
nickman / GroovyClassByteCodeRetrieval.groovy
Created January 13, 2012 15:20
Example of Getting ByteCode From A Compiled Groovy Class
import org.codehaus.groovy.control.*
// Compile a closure producing class
cu = new CompilationUnit();
cu.addSource("ClosureFactory.groovy", "public class ClosureFactory { public Closure getClosure() { return {message -> println message} }; }");
cu.compile();
// Get the closure, the closure class bytes and invoke the closure.
clazz = Class.forName("ClosureFactory");
clozure = clazz.newInstance().getClosure();
clozureClass = clozure.getClass();