/**
 * Copyright 2015 the original author or authors.
 *
 * Licensed 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 org.nanoframework.core.plugins.defaults.plugin;

import java.io.File;
import java.net.URI;
import java.net.URL;

import javax.servlet.ServletConfig;

import org.apache.commons.lang3.StringUtils;
import org.nanoframework.commons.util.ResourceUtils;
import org.nanoframework.core.plugins.Plugin;
import org.nanoframework.core.plugins.PluginLoaderException;

/**
 * @author yanghe
 * @date 2015年11月17日 上午9:06:59
 */
public class Log4j2Plugin implements Plugin {
	public static final String DEFAULT_LOG4J2_PARAMETER_NAME = "log4j2";
	private String log4j2;
	
	@Override
	public boolean load() throws Throwable {
		if(StringUtils.isNotBlank(log4j2)) {
		    final File file = ResourceUtils.getFile(log4j2);
			if (file != null) {
				try {
					Class<?> LogManager = Class.forName("org.apache.logging.log4j.LogManager");
					Object context = LogManager.getMethod("getContext", boolean.class).invoke(LogManager, false);
					Class<?> LoggerContext = Class.forName("org.apache.logging.log4j.core.LoggerContext");
					LoggerContext.getMethod("setConfigLocation", URI.class).invoke(context, file.toURI());
					LoggerContext.getMethod("reconfigure").invoke(context);
				} catch(Exception e) {
					if(!(e instanceof ClassNotFoundException)) {
						throw new PluginLoaderException(e.getMessage(), e);
					}
					
					return false;
				}
			}
		} else {
			return false;
		}
		
		return true;
	}

	@Override
	public void config(ServletConfig config) throws Throwable {
		log4j2 = config.getInitParameter(DEFAULT_LOG4J2_PARAMETER_NAME);
	}

}

@@ -1,15 +1,14 @@
 /*
-*
 %0A * Copyrigh
@@ -13,16 +13,21 @@
 ght 2015
+-2016
  the ori
@@ -238,19 +238,20 @@
 t%0A *%0A * 
-%09%09%09
+    
 http://w
@@ -715,29 +715,8 @@
 URI;
-%0Aimport java.net.URL;
 %0A%0Aim
@@ -1059,17 +1059,20 @@
 lugin %7B%0A
-%09
+    
 public s
@@ -1132,17 +1132,20 @@
 og4j2%22;%0A
-%09
+    
 private 
@@ -1159,19 +1159,21 @@
 log4j2;%0A
-%09%0A%09
+%0A    
 @Overrid
@@ -1170,25 +1170,28 @@
   @Override%0A
-%09
+    
 public boole
@@ -1219,20 +1219,27 @@
 wable %7B%0A
-%09%09
+        
 if
+ 
 (StringU
@@ -1265,18 +1265,24 @@
 4j2)) %7B%0A
-%09%09
+        
     fina
@@ -1326,19 +1326,28 @@
 og4j2);%0A
-%09%09%09
+            
 if (file
@@ -1362,23 +1362,50 @@
 ) %7B%0A
-%09%09%09%09try %7B%0A%09%09%09%09%09
+                try %7B%0A                    
 Clas
@@ -1476,21 +1476,36 @@
 ager%22);%0A
-%09%09%09%09%09
+                    
 Object c
@@ -1590,21 +1590,36 @@
 false);%0A
-%09%09%09%09%09
+                    
 Class%3C?%3E
@@ -1697,21 +1697,36 @@
 text%22);%0A
-%09%09%09%09%09
+                    
 LoggerCo
@@ -1804,21 +1804,36 @@
 URI());%0A
-%09%09%09%09%09
+                    
 LoggerCo
@@ -1884,19 +1884,32 @@
 t);%0A
-%09%09%09%09
+                
 %7D catch
+ 
 (Exc
@@ -1924,15 +1924,31 @@
 ) %7B%0A
-%09%09%09%09%09
+                    
 if
+ 
 (!(e
@@ -1990,14 +1990,32 @@
 ) %7B%0A
-%09%09%09%09%09%09
+                        
 thro
@@ -2066,88 +2066,159 @@
 e);%0A
-%09%09%09%09%09%7D%0A%09%09%09%09%09%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09%0A%09%09
+                    %7D%0A%0A                    return false;%0A                %7D%0A            %7D%0A        %7D else %7B%0A            return false;%0A        %7D%0A%0A        
 retu
@@ -2230,13 +2230,19 @@
 ue;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 @Ove
@@ -2247,17 +2247,20 @@
 verride%0A
-%09
+    
 public v
@@ -2315,10 +2315,16 @@
 e %7B%0A
-%09%09
+        
 log4
@@ -2388,9 +2388,12 @@
 E);%0A
-%09
+    
 %7D%0A%0A%7D

update Log4jPlugin.java