Browse Source

tidy up whitespace

Luis Neves 9 years ago
parent
commit
6e49d62cfd

+ 2 - 3
frameworks/Java/netty/pom.xml

@@ -1,5 +1,4 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
 	<modelVersion>4.0.0</modelVersion>
 
@@ -8,7 +7,7 @@
 	<version>0.1</version>
 
 	<packaging>jar</packaging>
-	
+
 	<dependencies>
 		<dependency>
 			<groupId>io.netty</groupId>

+ 88 - 89
frameworks/Java/netty/src/main/java/hello/HelloServerHandler.java

@@ -1,5 +1,14 @@
 package hello;
 
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
+
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelFutureListener;
@@ -14,100 +23,90 @@ import io.netty.handler.codec.http.HttpVersion;
 import io.netty.util.CharsetUtil;
 import io.netty.util.concurrent.FastThreadLocal;
 
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
+public class HelloServerHandler extends SimpleChannelInboundHandler<Object> {
+	private static final FastThreadLocal<DateFormat> FORMAT = new FastThreadLocal<DateFormat>() {
+		@Override
+		protected DateFormat initialValue() {
+			return new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
+		}
+	};
+
+	private static final ByteBuf CONTENT_BUFFER = Unpooled.unreleasableBuffer(Unpooled.directBuffer().writeBytes("Hello, World!".getBytes(CharsetUtil.UTF_8)));
+	private static final CharSequence contentLength = HttpHeaders.newEntity(String.valueOf(CONTENT_BUFFER.readableBytes()));
+
+	private static final CharSequence TYPE_PLAIN = HttpHeaders.newEntity("text/plain; charset=UTF-8");
+	private static final CharSequence TYPE_JSON = HttpHeaders.newEntity("application/json; charset=UTF-8");
+	private static final CharSequence SERVER_NAME = HttpHeaders.newEntity("Netty");
+	private static final CharSequence CONTENT_TYPE_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.CONTENT_TYPE);
+	private static final CharSequence DATE_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.DATE);
+	private static final CharSequence CONTENT_LENGTH_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.CONTENT_LENGTH);
+	private static final CharSequence SERVER_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.SERVER);
+	private static final ObjectMapper MAPPER;
+
+	static {
+		MAPPER = new ObjectMapper();
+		MAPPER.registerModule(new AfterburnerModule());
+	}
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
+	private volatile CharSequence date = HttpHeaders.newEntity(FORMAT.get().format(new Date()));
+
+	HelloServerHandler(ScheduledExecutorService service) {
+		service.scheduleWithFixedDelay(new Runnable() {
+			private final DateFormat format = FORMAT.get();
+
+			@Override
+			public void run() {
+				date = HttpHeaders.newEntity(format.format(new Date()));
+			}
+		}, 1000, 1000, TimeUnit.MILLISECONDS);
 
-public class HelloServerHandler extends SimpleChannelInboundHandler<Object> {
-    private static final FastThreadLocal<DateFormat> FORMAT = new FastThreadLocal<DateFormat>() {
-	@Override
-	protected DateFormat initialValue() {
-	    return new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
 	}
-    };
-
-    private static final ByteBuf CONTENT_BUFFER = Unpooled.unreleasableBuffer(Unpooled.directBuffer().writeBytes("Hello, World!".getBytes(CharsetUtil.UTF_8)));
-    private static final CharSequence contentLength = HttpHeaders.newEntity(String.valueOf(CONTENT_BUFFER.readableBytes()));
-
-    private static final CharSequence TYPE_PLAIN = HttpHeaders.newEntity("text/plain; charset=UTF-8");
-    private static final CharSequence TYPE_JSON = HttpHeaders.newEntity("application/json; charset=UTF-8");
-    private static final CharSequence SERVER_NAME = HttpHeaders.newEntity("Netty");
-    private static final CharSequence CONTENT_TYPE_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.CONTENT_TYPE);
-    private static final CharSequence DATE_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.DATE);
-    private static final CharSequence CONTENT_LENGTH_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.CONTENT_LENGTH);
-    private static final CharSequence SERVER_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.SERVER);
-    private static final ObjectMapper MAPPER;
-
-    static {
-	MAPPER = new ObjectMapper();
-	MAPPER.registerModule(new AfterburnerModule());
-    }
-
-    private volatile CharSequence date = HttpHeaders.newEntity(FORMAT.get().format(new Date()));
-
-    HelloServerHandler(ScheduledExecutorService service) {
-	service.scheduleWithFixedDelay(new Runnable() {
-	    private final DateFormat format = FORMAT.get();
-
-	    @Override
-	    public void run() {
-		date = HttpHeaders.newEntity(format.format(new Date()));
-	    }
-	}, 1000, 1000, TimeUnit.MILLISECONDS);
-
-    }
-
-    @Override
-    public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
-	if (msg instanceof HttpRequest) {
-	    HttpRequest request = (HttpRequest) msg;
-	    String uri = request.getUri();
-	    switch (uri) {
-	    case "/plaintext":
-		writeResponse(ctx, request, CONTENT_BUFFER.duplicate(), TYPE_PLAIN, contentLength);
-		return;
-	    case "/json":
-		byte[] json = MAPPER.writeValueAsBytes(new Message("Hello, World!"));
-		writeResponse(ctx, request, Unpooled.wrappedBuffer(json), TYPE_JSON, String.valueOf(json.length));
-		return;
-	    }
-	    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, Unpooled.EMPTY_BUFFER, false);
-	    ctx.write(response).addListener(ChannelFutureListener.CLOSE);
+
+	@Override
+	public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
+		if (msg instanceof HttpRequest) {
+			HttpRequest request = (HttpRequest) msg;
+			String uri = request.getUri();
+			switch (uri) {
+			case "/plaintext":
+				writeResponse(ctx, request, CONTENT_BUFFER.duplicate(), TYPE_PLAIN, contentLength);
+				return;
+			case "/json":
+				byte[] json = MAPPER.writeValueAsBytes(new Message("Hello, World!"));
+				writeResponse(ctx, request, Unpooled.wrappedBuffer(json), TYPE_JSON, String.valueOf(json.length));
+				return;
+			}
+			FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, Unpooled.EMPTY_BUFFER, false);
+			ctx.write(response).addListener(ChannelFutureListener.CLOSE);
+		}
 	}
-    }
-
-    private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf, CharSequence contentType, CharSequence contentLength) {
-	// Decide whether to close the connection or not.
-	boolean keepAlive = HttpHeaders.isKeepAlive(request);
-	// Build the response object.
-	FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf, false);
-	HttpHeaders headers = response.headers();
-	headers.set(CONTENT_TYPE_ENTITY, contentType);
-	headers.set(SERVER_ENTITY, SERVER_NAME);
-	headers.set(DATE_ENTITY, date);
-	headers.set(CONTENT_LENGTH_ENTITY, contentLength);
-
-	// Close the non-keep-alive connection after the write operation is
-	// done.
-	if (!keepAlive) {
-	    ctx.write(response).addListener(ChannelFutureListener.CLOSE);
-	} else {
-	    ctx.write(response, ctx.voidPromise());
+
+	private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf, CharSequence contentType, CharSequence contentLength) {
+		// Decide whether to close the connection or not.
+		boolean keepAlive = HttpHeaders.isKeepAlive(request);
+		// Build the response object.
+		FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf, false);
+		HttpHeaders headers = response.headers();
+		headers.set(CONTENT_TYPE_ENTITY, contentType);
+		headers.set(SERVER_ENTITY, SERVER_NAME);
+		headers.set(DATE_ENTITY, date);
+		headers.set(CONTENT_LENGTH_ENTITY, contentLength);
+
+		// Close the non-keep-alive connection after the write operation is done.
+		if (!keepAlive) {
+			ctx.write(response).addListener(ChannelFutureListener.CLOSE);
+		} else {
+			ctx.write(response, ctx.voidPromise());
+		}
 	}
-    }
 
-    @Override
-    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
-	ctx.close();
-    }
+	@Override
+	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+		ctx.close();
+	}
 
-    @Override
-    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
-	ctx.flush();
-    }
+	@Override
+	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
+		ctx.flush();
+	}
 }

+ 13 - 13
frameworks/Java/netty/src/main/java/hello/HelloServerInitializer.java

@@ -1,26 +1,26 @@
 package hello;
 
+import java.util.concurrent.ScheduledExecutorService;
+
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.ChannelPipeline;
 import io.netty.channel.socket.SocketChannel;
 import io.netty.handler.codec.http.HttpRequestDecoder;
 import io.netty.handler.codec.http.HttpResponseEncoder;
 
-import java.util.concurrent.ScheduledExecutorService;
-
 public class HelloServerInitializer extends ChannelInitializer<SocketChannel> {
 
-    private ScheduledExecutorService service;
+	private ScheduledExecutorService service;
 
-    public HelloServerInitializer(ScheduledExecutorService service) {
-	this.service = service;
-    }
+	public HelloServerInitializer(ScheduledExecutorService service) {
+		this.service = service;
+	}
 
-    @Override
-    public void initChannel(SocketChannel ch) throws Exception {
-	ChannelPipeline p = ch.pipeline();
-	p.addLast("encoder", new HttpResponseEncoder());
-	p.addLast("decoder", new HttpRequestDecoder(4096, 8192, 8192, false));
-	p.addLast("handler", new HelloServerHandler(service));
-    }
+	@Override
+	public void initChannel(SocketChannel ch) throws Exception {
+		ChannelPipeline p = ch.pipeline();
+		p.addLast("encoder", new HttpResponseEncoder());
+		p.addLast("decoder", new HttpRequestDecoder(4096, 8192, 8192, false));
+		p.addLast("handler", new HelloServerHandler(service));
+	}
 }

+ 42 - 42
frameworks/Java/netty/src/main/java/hello/HelloWebServer.java

@@ -18,56 +18,56 @@ import io.netty.util.ResourceLeakDetector.Level;
 
 public class HelloWebServer {
 
-    static {
-	ResourceLeakDetector.setLevel(Level.DISABLED);
-    }
+	static {
+		ResourceLeakDetector.setLevel(Level.DISABLED);
+	}
 
-    private final int port;
+	private final int port;
 
-    public HelloWebServer(int port) {
-	this.port = port;
-    }
+	public HelloWebServer(int port) {
+		this.port = port;
+	}
 
-    public void run() throws Exception {
-	// Configure the server.
+	public void run() throws Exception {
+		// Configure the server.
 
-	if (Epoll.isAvailable()) {
-	    doRun(new EpollEventLoopGroup(), EpollServerSocketChannel.class);
-	} else {
-	    doRun(new NioEventLoopGroup(), NioServerSocketChannel.class);
+		if (Epoll.isAvailable()) {
+			doRun(new EpollEventLoopGroup(), EpollServerSocketChannel.class);
+		} else {
+			doRun(new NioEventLoopGroup(), NioServerSocketChannel.class);
+		}
 	}
-    }
 
-    private void doRun(EventLoopGroup loupGroup, Class<? extends ServerChannel> serverChannelClass) throws InterruptedException {
-	try {
-		InetSocketAddress inet = new InetSocketAddress(port);
-		
-	    ServerBootstrap b = new ServerBootstrap();
-	    b.option(ChannelOption.SO_BACKLOG, 1024);
-	    b.option(ChannelOption.SO_REUSEADDR, true);
-	    b.group(loupGroup).channel(serverChannelClass).childHandler(new HelloServerInitializer(loupGroup.next()));
-	    b.option(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE);
-	    b.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true));
-	    b.childOption(ChannelOption.SO_REUSEADDR, true);
-	    b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE);
+	private void doRun(EventLoopGroup loupGroup, Class<? extends ServerChannel> serverChannelClass) throws InterruptedException {
+		try {
+			InetSocketAddress inet = new InetSocketAddress(port);
+
+			ServerBootstrap b = new ServerBootstrap();
+			b.option(ChannelOption.SO_BACKLOG, 1024);
+			b.option(ChannelOption.SO_REUSEADDR, true);
+			b.group(loupGroup).channel(serverChannelClass).childHandler(new HelloServerInitializer(loupGroup.next()));
+			b.option(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE);
+			b.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true));
+			b.childOption(ChannelOption.SO_REUSEADDR, true);
+			b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE);
+
+			Channel ch = b.bind(inet).sync().channel();
+
+			System.out.printf("Httpd started. Listening on: %s%n", inet.toString());
 
-	    Channel ch = b.bind(inet).sync().channel();
-	    
-	    System.out.printf("Httpd started. Listening on: %s%n", inet.toString());	    
-	    
-	    ch.closeFuture().sync();
-	} finally {
-	    loupGroup.shutdownGracefully().sync();
+			ch.closeFuture().sync();
+		} finally {
+			loupGroup.shutdownGracefully().sync();
+		}
 	}
-    }
 
-    public static void main(String[] args) throws Exception {
-	int port;
-	if (args.length > 0) {
-	    port = Integer.parseInt(args[0]);
-	} else {
-	    port = 8080;
+	public static void main(String[] args) throws Exception {
+		int port;
+		if (args.length > 0) {
+			port = Integer.parseInt(args[0]);
+		} else {
+			port = 8080;
+		}
+		new HelloWebServer(port).run();
 	}
-	new HelloWebServer(port).run();
-    }
 }

+ 8 - 8
frameworks/Java/netty/src/main/java/hello/Message.java

@@ -2,14 +2,14 @@ package hello;
 
 public class Message {
 
-    private final String message;
+	private final String message;
 
-    public Message(String message) {
-	super();
-	this.message = message;
-    }
+	public Message(String message) {
+		super();
+		this.message = message;
+	}
 
-    public String getMessage() {
-	return message;
-    }
+	public String getMessage() {
+		return message;
+	}
 }