/*
 * Copyright 2014 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package monica.files.socket;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import monica.configuration.context.ConfigurationContext;
import monica.framework.Storage;
import monica.framework.storage.AbstractStorageFactory;
import monica.framework.transport.TransportFile;
import io.netty.channel.ChannelHandler.Sharable;


/**
 * 
 * @author lucy@polarcoral.com
 *
 *         2017-08-29
 */
@Sharable
public class FileServerHandler extends SimpleChannelInboundHandler<Object> {
	private String STORAGE_CONFIG_KEY = "storage";
  

	@Override
	public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
		  String storageFactoryName = String.valueOf(ConfigurationContext.propMap.get(STORAGE_CONFIG_KEY));		   
		  Object o = this.getClass().getClassLoader().loadClass(storageFactoryName).newInstance();
		  Storage storage = ((AbstractStorageFactory)o).newStorageInstance();
		  storage.storage((TransportFile)msg);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		cause.printStackTrace();

		if (ctx.channel().isActive()) {
			ctx.writeAndFlush("ERR: " + cause.getClass().getSimpleName() + ": " + cause.getMessage() + '\n')
					.addListener(ChannelFutureListener.CLOSE);
		}
	}
}

@@ -659,16 +659,101 @@
 ocket;%0A%0A
+import java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0A%0A
 import i
@@ -787,24 +787,24 @@
 reListener;%0A
-
 import io.ne
@@ -1343,16 +1343,48 @@
 age%22;%0A  
+  private final int poolSize=20;
 %0A%0A%09@Over
@@ -1472,16 +1472,97 @@
 ption %7B%0A
+%09%09  ExecutorService storageService = Executors.newFixedThreadPool(poolSize);%09 %09%09%0A
 %09%09  Stri
@@ -1820,24 +1820,26 @@
 eInstance();
+%09%09
 %0A%09%09  storage
@@ -1842,37 +1842,148 @@
 rage
-.storage((TransportFile)msg);
+Service.execute(new Runnable()%7B%0A%09%09%09%09@Override%0A%09%09%09%09public void run() %7B%0A%09%09%09%09%09storage.storage((TransportFile)msg);%09%09%09%09%0A%09%09%09%09%7D%09%09%09%09  %0A%09%09%09  %7D);%09%09  
 %0A%09%7D%0A
@@ -2069,16 +2069,16 @@
 ause) %7B%0A
+
 %09%09cause.
@@ -2096,17 +2096,16 @@
 race();%0A
-%0A
 %09%09if (ct

Create the thread pool for storage operation