chickenlj closed pull request #899: Hessian support attachments and add log4j2 
support.
URL: https://github.com/apache/incubator-dubbo/pull/899
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/dubbo-common/pom.xml b/dubbo-common/pom.xml
index 6ab4d4d5e1..20f219403b 100644
--- a/dubbo-common/pom.xml
+++ b/dubbo-common/pom.xml
@@ -43,6 +43,16 @@
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+            <scope>provided</scope>
+        </dependency>
         <dependency>
             <groupId>org.javassist</groupId>
             <artifactId>javassist</artifactId>
diff --git 
a/dubbo-common/src/main/java/com/alibaba/dubbo/common/logger/LoggerFactory.java 
b/dubbo-common/src/main/java/com/alibaba/dubbo/common/logger/LoggerFactory.java
index bef474731d..d24ee154e8 100644
--- 
a/dubbo-common/src/main/java/com/alibaba/dubbo/common/logger/LoggerFactory.java
+++ 
b/dubbo-common/src/main/java/com/alibaba/dubbo/common/logger/LoggerFactory.java
@@ -19,6 +19,7 @@
 import com.alibaba.dubbo.common.logger.jcl.JclLoggerAdapter;
 import com.alibaba.dubbo.common.logger.jdk.JdkLoggerAdapter;
 import com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter;
+import com.alibaba.dubbo.common.logger.log4j2.Log4j2LoggerAdapter;
 import com.alibaba.dubbo.common.logger.slf4j.Slf4jLoggerAdapter;
 import com.alibaba.dubbo.common.logger.support.FailsafeLogger;
 
@@ -46,6 +47,8 @@
             setLoggerAdapter(new JclLoggerAdapter());
         } else if ("log4j".equals(logger)) {
             setLoggerAdapter(new Log4jLoggerAdapter());
+        } else if ("log4j2".equals(logger)) {
+            setLoggerAdapter(new Log4j2LoggerAdapter());
         } else if ("jdk".equals(logger)) {
             setLoggerAdapter(new JdkLoggerAdapter());
         } else {
diff --git 
a/dubbo-common/src/main/java/com/alibaba/dubbo/common/logger/log4j2/Log4j2Logger.java
 
b/dubbo-common/src/main/java/com/alibaba/dubbo/common/logger/log4j2/Log4j2Logger.java
new file mode 100644
index 0000000000..2c5fd68f5f
--- /dev/null
+++ 
b/dubbo-common/src/main/java/com/alibaba/dubbo/common/logger/log4j2/Log4j2Logger.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 1999-2011 Alibaba Group.
+ *  
+ * 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 com.alibaba.dubbo.common.logger.log4j2;
+
+import com.alibaba.dubbo.common.logger.Logger;
+import com.alibaba.dubbo.common.logger.support.FailsafeLogger;
+import org.apache.logging.log4j.LogManager;
+
+public class Log4j2Logger implements Logger {
+
+    private static final String FQCN = FailsafeLogger.class.getName();
+
+    private final org.apache.logging.log4j.Logger logger;
+
+    public Log4j2Logger(String name) {
+        this.logger = LogManager.getFactory().getContext(FQCN, null, null, 
false).getLogger(name);
+    }
+
+    public Log4j2Logger(Class<?> name) {
+        this.logger = LogManager.getFactory().getContext(FQCN, 
name.getClassLoader(), null, false).getLogger(name.getName());
+    }
+
+    public void trace(String msg) {
+        logger.trace(msg);
+    }
+
+    public void trace(Throwable e) {
+        logger.trace(e);
+    }
+
+    public void trace(String msg, Throwable e) {
+        logger.trace(msg, e);
+    }
+
+    public void debug(String msg) {
+        logger.debug(msg);
+    }
+
+    public void debug(Throwable e) {
+        logger.debug(e);
+    }
+
+    public void debug(String msg, Throwable e) {
+        logger.debug(msg, e);
+    }
+
+    public void info(String msg) {
+        logger.info(msg);
+    }
+
+    public void info(Throwable e) {
+        logger.info(e);
+    }
+
+    public void info(String msg, Throwable e) {
+        logger.info(msg, e);
+    }
+
+    public void warn(String msg) {
+        logger.warn(msg);
+    }
+
+    public void warn(Throwable e) {
+        logger.warn(e);
+    }
+
+    public void warn(String msg, Throwable e) {
+        logger.warn(msg, e);
+    }
+
+    public void error(String msg) {
+        logger.error(msg);
+    }
+
+    public void error(Throwable e) {
+        logger.error(e);
+    }
+
+    public void error(String msg, Throwable e) {
+        logger.error(msg, e);
+    }
+
+    public boolean isTraceEnabled() {
+        return logger.isTraceEnabled();
+    }
+
+    public boolean isDebugEnabled() {
+        return logger.isDebugEnabled();
+    }
+
+    public boolean isInfoEnabled() {
+        return logger.isInfoEnabled();
+    }
+
+    public boolean isWarnEnabled() {
+        return logger.isWarnEnabled();
+    }
+
+    public boolean isErrorEnabled() {
+        return logger.isErrorEnabled();
+    }
+}
\ No newline at end of file
diff --git 
a/dubbo-common/src/main/java/com/alibaba/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java
 
b/dubbo-common/src/main/java/com/alibaba/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java
new file mode 100644
index 0000000000..6c08cb25f2
--- /dev/null
+++ 
b/dubbo-common/src/main/java/com/alibaba/dubbo/common/logger/log4j2/Log4j2LoggerAdapter.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright 1999-2011 Alibaba Group.
+ *  
+ * 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 com.alibaba.dubbo.common.logger.log4j2;
+
+import com.alibaba.dubbo.common.logger.Level;
+import com.alibaba.dubbo.common.logger.Logger;
+import com.alibaba.dubbo.common.logger.LoggerAdapter;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.FileAppender;
+import org.apache.logging.log4j.core.appender.RollingFileAppender;
+import org.apache.logging.log4j.core.config.Configuration;
+
+import java.io.File;
+import java.util.Map;
+
+public class Log4j2LoggerAdapter implements LoggerAdapter {
+
+    private File file;
+
+    /**
+     * The Unix separator character.
+     */
+    private static final char UNIX_SEPARATOR = '/';
+
+    /**
+     * The Windows separator character.
+     */
+    private static final char WINDOWS_SEPARATOR = '\\';
+
+    @SuppressWarnings("unchecked")
+    public Log4j2LoggerAdapter() {
+
+        try {
+            LoggerContext lcx = (LoggerContext) LogManager.getContext(false);
+
+            Map<String, Appender> appenders = 
lcx.getConfiguration().getAppenders();
+            if (appenders != null) {
+                for (String key : appenders.keySet()) {
+                    Appender appender = appenders.get(key);
+                    if (appender instanceof FileAppender) {
+                        FileAppender fileAppender = (FileAppender) appender;
+                        String filename = fileAppender.getFileName();
+                        this.file = new File(filename);
+                        break;
+                    } else if (appender instanceof RollingFileAppender) {
+                        RollingFileAppender fileAppender = 
(RollingFileAppender) appender;
+                        String filename = fileAppender.getFileName();
+                        this.file = new File(filename);
+                        break;
+                    }
+                }
+            }
+        } catch (Throwable ignored) {
+        }
+    }
+
+    private String getDefaultLoggingRoot() {
+        return new File(System.getProperty("user.home") + 
"/logs").getAbsolutePath();
+    }
+
+    public Logger getLogger(Class<?> key) {
+        return new Log4j2Logger(key);
+    }
+
+    public Logger getLogger(String key) {
+        return new Log4j2Logger(key);
+    }
+
+    public void setLevel(Level level) {
+        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        final Configuration config = ctx.getConfiguration();
+
+        config.getRootLogger().setLevel(toLog4jLevel(level));
+        ctx.updateLoggers();
+    }
+
+    public Level getLevel() {
+        return fromLog4jLevel(LogManager.getRootLogger().getLevel());
+    }
+
+    public File getFile() {
+        return file;
+    }
+
+    private static org.apache.logging.log4j.Level toLog4jLevel(Level level) {
+        if (level == Level.ALL)
+            return org.apache.logging.log4j.Level.ALL;
+        if (level == Level.TRACE)
+            return org.apache.logging.log4j.Level.TRACE;
+        if (level == Level.DEBUG)
+            return org.apache.logging.log4j.Level.DEBUG;
+        if (level == Level.INFO)
+            return org.apache.logging.log4j.Level.INFO;
+        if (level == Level.WARN)
+            return org.apache.logging.log4j.Level.WARN;
+        if (level == Level.ERROR)
+            return org.apache.logging.log4j.Level.ERROR;
+        // if (level == Level.OFF)
+        return org.apache.logging.log4j.Level.OFF;
+    }
+
+    private static Level fromLog4jLevel(org.apache.logging.log4j.Level level) {
+        if (level == org.apache.logging.log4j.Level.ALL)
+            return Level.ALL;
+        if (level == org.apache.logging.log4j.Level.TRACE)
+            return Level.TRACE;
+        if (level == org.apache.logging.log4j.Level.DEBUG)
+            return Level.DEBUG;
+        if (level == org.apache.logging.log4j.Level.INFO)
+            return Level.INFO;
+        if (level == org.apache.logging.log4j.Level.WARN)
+            return Level.WARN;
+        if (level == org.apache.logging.log4j.Level.ERROR)
+            return Level.ERROR;
+        // if (level == org.apache.log4j.Level.OFF)
+        return Level.OFF;
+    }
+
+    public void setFile(File file) {
+
+    }
+
+}
+
+
+//            System.setProperty("logFileName", getDefaultLoggingRoot());
+//
+//                    if (lcx != null) {
+//                    Map<String, Appender> appenders = 
lcx.getConfiguration().getAppenders();
+//        if (appenders != null) {
+//        for (String key : appenders.keySet()) {
+//        Appender appender = appenders.get(key);
+//        if (appender instanceof FileAppender) {
+//        FileAppender fileAppender = (FileAppender) appender;
+//        String filename = fileAppender.getFileName();
+//        file = new File(filename);
+//        break;
+//        } else if (appender instanceof RollingFileAppender) {
+//        RollingFileAppender fileAppender = (RollingFileAppender) appender;
+//        String filename = fileAppender.getFileName();
+//        file = new File(filename);
+//        break;
+//        }
+//        }
+//        }
+//        }
\ No newline at end of file
diff --git a/dubbo-container/dubbo-container-log4j2/pom.xml 
b/dubbo-container/dubbo-container-log4j2/pom.xml
new file mode 100644
index 0000000000..27548b7f0f
--- /dev/null
+++ b/dubbo-container/dubbo-container-log4j2/pom.xml
@@ -0,0 +1,53 @@
+<!--
+ - Copyright 1999-2011 Alibaba Group.
+ -  
+ - 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.
+-->
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xmlns="http://maven.apache.org/POM/4.0.0";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>com.alibaba</groupId>
+        <artifactId>dubbo-container</artifactId>
+        <version>2.5.7</version>
+    </parent>
+    <artifactId>dubbo-container-log4j2</artifactId>
+    <packaging>jar</packaging>
+    <name>${project.artifactId}</name>
+    <description>The log4j container module of dubbo project</description>
+    <properties>
+        <skip_maven_deploy>true</skip_maven_deploy>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>dubbo-container-api</artifactId>
+            <version>${project.parent.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-1.2-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+</project>
\ No newline at end of file
diff --git 
a/dubbo-container/dubbo-container-log4j2/src/main/java/com/alibaba/dubbo/container/log4j2/Log4j2Container.java
 
b/dubbo-container/dubbo-container-log4j2/src/main/java/com/alibaba/dubbo/container/log4j2/Log4j2Container.java
new file mode 100644
index 0000000000..ad703bde37
--- /dev/null
+++ 
b/dubbo-container/dubbo-container-log4j2/src/main/java/com/alibaba/dubbo/container/log4j2/Log4j2Container.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright 1999-2011 Alibaba Group.
+ *  
+ * 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 com.alibaba.dubbo.container.log4j2;
+
+import com.alibaba.dubbo.common.utils.ConfigUtils;
+import com.alibaba.dubbo.container.Container;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.RollingFileAppender;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.apache.logging.log4j.core.config.properties.PropertiesConfiguration;
+import 
org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Log4j2Container. (SPI, Singleton, ThreadSafe)
+ *
+ * @author william.liangf
+ */
+public class Log4j2Container implements Container {
+
+    public static final String LOG4J_FILE = "dubbo.log4j.file";
+
+    public static final String LOG4J_LEVEL = "dubbo.log4j.level";
+
+    public static final String LOG4J_IS_CONSOLE = "dubbo.log4j.console";
+
+    public static final String DEFAULT_LOG4J_LEVEL = "ERROR";
+
+    /**
+     * The Unix separator character.
+     */
+    private static final char UNIX_SEPARATOR = '/';
+
+    /**
+     * The Windows separator character.
+     */
+    private static final char WINDOWS_SEPARATOR = '\\';
+
+    @SuppressWarnings("unchecked")
+    public void start() {
+        String file = ConfigUtils.getProperty(LOG4J_FILE);
+        if (file != null && file.length() > 0) {
+            Level level = Level.toLevel(ConfigUtils.getProperty(LOG4J_LEVEL), 
Level.ERROR);
+
+            final LoggerContext ctx = (LoggerContext) 
LogManager.getContext(false);
+
+            try {
+                String basePath;
+                String filename;
+                int index = -1;
+
+                if (file.lastIndexOf(UNIX_SEPARATOR) > -1) {
+                    index = file.lastIndexOf(UNIX_SEPARATOR);
+                } else if (file.lastIndexOf(WINDOWS_SEPARATOR) > -1) {
+                    index = file.lastIndexOf(WINDOWS_SEPARATOR);
+                }
+
+                if (index > -1) {
+                    basePath = file.substring(0, index);
+                    filename = file.substring(index + 1);
+                } else {
+                    basePath = "./";
+                    filename = file;
+                }
+
+                if (filename.lastIndexOf(".") > -1) {
+                    filename = filename.substring(0, 
filename.lastIndexOf("."));
+                }
+
+                Map<String, Appender> appenderMap = 
ctx.getConfiguration().getAppenders();
+
+                System.setProperty("log4j2_file", file);
+                System.setProperty("log4j2_basePath", basePath);
+                System.setProperty("log4j2_filename", filename);
+                URL url = getClass().getResource("/log4j2-default.properties");
+                ConfigurationSource source = new 
ConfigurationSource(url.openStream(), url);
+
+                PropertiesConfigurationFactory propertiesConfigurationFactory 
= new PropertiesConfigurationFactory();
+
+                PropertiesConfiguration configuration = 
propertiesConfigurationFactory.getConfiguration(ctx, source);
+                configuration.start();
+
+                LoggerConfig loggerConfig = 
ctx.getConfiguration().getRootLogger();
+
+                //清除默认的Appender
+                for (String name : loggerConfig.getAppenders().keySet()) {
+                    if (name != null && name.contains("DefaultConsole")) {
+                        loggerConfig.removeAppender(name);
+                    }
+                }
+
+                
loggerConfig.addAppender(configuration.getAppender("DubboRollingFileName"), 
level, null);
+
+                if (Boolean.valueOf(ConfigUtils.getProperty(LOG4J_IS_CONSOLE, 
"true"))) {
+                    
loggerConfig.addAppender(configuration.getAppender("DubboConsoleName"), level, 
null);
+                }
+
+                loggerConfig.setLevel(level);
+                ctx.updateLoggers();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public void stop() {
+    }
+
+}
\ No newline at end of file
diff --git 
a/dubbo-container/dubbo-container-log4j2/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.container.Container
 
b/dubbo-container/dubbo-container-log4j2/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.container.Container
new file mode 100644
index 0000000000..ccc9a29765
--- /dev/null
+++ 
b/dubbo-container/dubbo-container-log4j2/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.container.Container
@@ -0,0 +1 @@
+log4j2=com.alibaba.dubbo.container.log4j2.Log4j2Container
\ No newline at end of file
diff --git 
a/dubbo-container/dubbo-container-log4j2/src/main/resources/log4j2-default.properties
 
b/dubbo-container/dubbo-container-log4j2/src/main/resources/log4j2-default.properties
new file mode 100644
index 0000000000..678f689232
--- /dev/null
+++ 
b/dubbo-container/dubbo-container-log4j2/src/main/resources/log4j2-default.properties
@@ -0,0 +1,16 @@
+status=ERROR
+name=PropertiesConfig
+
+appender.console.type=Console
+appender.console.name=DubboConsoleName
+appender.console.target=SYSTEM_OUT
+appender.console.layout.type=PatternLayout
+appender.console.layout.pattern=[%p] - [%d{yyyy-MM-dd HH\:mm\:ss}] - [%c] - 
%m%n
+
+appender.rolling.type = RollingFile
+appender.rolling.name = DubboRollingFileName
+appender.rolling.fileName=${sys:log4j2_file}
+appender.rolling.filePattern = 
${sys:log4j2_basePath}/${sys:log4j2_filename}-%d{MM-dd-yy-HH-mm-ss-SSS}-%i.gz
+appender.rolling.layout.type = PatternLayout
+appender.rolling.layout.pattern = [%p] - [%d{yyyy-MM-dd HH\:mm\:ss}] - [%c] - 
%m%n
+appender.rolling.policies.type = Policies
\ No newline at end of file
diff --git 
a/dubbo-container/dubbo-container-log4j2/src/test/java/com/alibaba/dubbo/container/log4j2/Log4J2ContainerTest.java
 
b/dubbo-container/dubbo-container-log4j2/src/test/java/com/alibaba/dubbo/container/log4j2/Log4J2ContainerTest.java
new file mode 100644
index 0000000000..c7e61cf0fd
--- /dev/null
+++ 
b/dubbo-container/dubbo-container-log4j2/src/test/java/com/alibaba/dubbo/container/log4j2/Log4J2ContainerTest.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 1999-2011 Alibaba Group.
+ *  
+ * 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 com.alibaba.dubbo.container.log4j2;
+
+import com.alibaba.dubbo.common.extension.ExtensionLoader;
+import com.alibaba.dubbo.container.Container;
+
+import com.alibaba.dubbo.container.log4j2.Log4j2Container;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.junit.Test;
+
+/**
+ * StandaloneContainerTest
+ *
+ * @author william.liangf
+ */
+public class Log4J2ContainerTest {
+
+    private final static Logger log = 
LogManager.getLogger(Log4J2ContainerTest.class);
+
+    @Test
+    public void testContainer() {
+        Log4j2Container container = (Log4j2Container) 
ExtensionLoader.getExtensionLoader(Container.class).getExtension("log4j2");
+        container.start();
+        log.debug("test");
+        container.stop();
+    }
+
+}
\ No newline at end of file
diff --git 
a/dubbo-container/dubbo-container-log4j2/src/test/resources/dubbo.properties 
b/dubbo-container/dubbo-container-log4j2/src/test/resources/dubbo.properties
new file mode 100644
index 0000000000..74e288911e
--- /dev/null
+++ b/dubbo-container/dubbo-container-log4j2/src/test/resources/dubbo.properties
@@ -0,0 +1,2 @@
+dubbo.log4j.level=debug
+dubbo.log4j.file=logs/dubbo.log
\ No newline at end of file
diff --git a/dubbo-container/pom.xml b/dubbo-container/pom.xml
index 3bab054c0f..770824ae0d 100644
--- a/dubbo-container/pom.xml
+++ b/dubbo-container/pom.xml
@@ -33,6 +33,7 @@
         <module>dubbo-container-spring</module>
         <module>dubbo-container-jetty</module>
         <module>dubbo-container-log4j</module>
+        <module>dubbo-container-log4j2</module>
         <module>dubbo-container-logback</module>
     </modules>
 </project>
diff --git a/dubbo-demo/dubbo-demo-consumer/pom.xml 
b/dubbo-demo/dubbo-demo-consumer/pom.xml
index 47d77ffd6d..1dba830508 100644
--- a/dubbo-demo/dubbo-demo-consumer/pom.xml
+++ b/dubbo-demo/dubbo-demo-consumer/pom.xml
@@ -71,6 +71,14 @@
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.caucho</groupId>
+            <artifactId>hessian</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+        </dependency>
     </dependencies>
     <build>
         <plugins>
diff --git 
a/dubbo-demo/dubbo-demo-consumer/src/main/java/com/alibaba/dubbo/demo/consumer/Consumer.java
 
b/dubbo-demo/dubbo-demo-consumer/src/main/java/com/alibaba/dubbo/demo/consumer/Consumer.java
index 25d2de8b8e..00cf74844a 100644
--- 
a/dubbo-demo/dubbo-demo-consumer/src/main/java/com/alibaba/dubbo/demo/consumer/Consumer.java
+++ 
b/dubbo-demo/dubbo-demo-consumer/src/main/java/com/alibaba/dubbo/demo/consumer/Consumer.java
@@ -1,7 +1,6 @@
 package com.alibaba.dubbo.demo.consumer;
 
 import com.alibaba.dubbo.demo.DemoService;
-
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 /**
diff --git 
a/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/HessianProtocol.java
 
b/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/HessianProtocol.java
index 33292631fb..6ba43e3979 100644
--- 
a/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/HessianProtocol.java
+++ 
b/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/HessianProtocol.java
@@ -20,8 +20,8 @@
 import com.alibaba.dubbo.remoting.http.HttpBinder;
 import com.alibaba.dubbo.remoting.http.HttpHandler;
 import com.alibaba.dubbo.remoting.http.HttpServer;
-import com.alibaba.dubbo.rpc.RpcContext;
-import com.alibaba.dubbo.rpc.RpcException;
+import com.alibaba.dubbo.rpc.*;
+import com.alibaba.dubbo.rpc.protocol.AbstractInvoker;
 import com.alibaba.dubbo.rpc.protocol.AbstractProxyProtocol;
 
 import com.caucho.hessian.HessianException;
@@ -36,6 +36,8 @@
 import java.io.IOException;
 import java.net.SocketTimeoutException;
 import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -46,6 +48,13 @@
  */
 public class HessianProtocol extends AbstractProxyProtocol {
 
+    public static final ThreadLocal<Map<String, String>> 
HEADER_MAP_THREAD_LOCAL = new ThreadLocal<Map<String, String>>() {
+        @Override
+        protected Map<String, String> initialValue() {
+            return new HashMap<String, String>();
+        }
+    };
+
     private final Map<String, HttpServer> serverMap = new 
ConcurrentHashMap<String, HttpServer>();
 
     private final Map<String, HessianSkeleton> skeletonMap = new 
ConcurrentHashMap<String, HessianSkeleton>();
@@ -84,6 +93,7 @@ public void run() {
     @SuppressWarnings("unchecked")
     protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException 
{
         HessianProxyFactory hessianProxyFactory = new HessianProxyFactory();
+        hessianProxyFactory.setOverloadEnabled(true);
         String client = url.getParameter(Constants.CLIENT_KEY, 
Constants.DEFAULT_HTTP_CLIENT);
         if ("httpclient".equals(client)) {
             hessianProxyFactory.setConnectionFactory(new 
HttpClientConnectionFactory());
@@ -138,14 +148,28 @@ public void handle(HttpServletRequest request, 
HttpServletResponse response)
                 response.setStatus(500);
             } else {
                 
RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), 
request.getRemotePort());
+
                 try {
+                    Map<String, String> headerMap = 
HEADER_MAP_THREAD_LOCAL.get();
+                    Enumeration enumeration = request.getHeaderNames();
+
+                    while (enumeration.hasMoreElements()) {
+                        String key = String.valueOf(enumeration.nextElement());
+                        if (key.startsWith(Constants.DEFAULT_EXCHANGER)) {
+                            
headerMap.put(key.replace(Constants.DEFAULT_EXCHANGER, ""), 
request.getHeader(key));
+                        }
+                    }
+
+                    RpcContext.getContext().setAttachments(headerMap);
+
                     skeleton.invoke(request.getInputStream(), 
response.getOutputStream());
                 } catch (Throwable e) {
                     throw new ServletException(e);
+                } finally {
+                    HEADER_MAP_THREAD_LOCAL.remove();
                 }
             }
         }
-
     }
 
 }
\ No newline at end of file
diff --git 
a/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/HttpClientConnectionFactory.java
 
b/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/HttpClientConnectionFactory.java
index 0b77090c1b..3bc8ea06a6 100644
--- 
a/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/HttpClientConnectionFactory.java
+++ 
b/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/HttpClientConnectionFactory.java
@@ -15,6 +15,8 @@
  */
 package com.alibaba.dubbo.rpc.protocol.hessian;
 
+import com.alibaba.dubbo.common.Constants;
+import com.alibaba.dubbo.rpc.RpcContext;
 import com.caucho.hessian.client.HessianConnection;
 import com.caucho.hessian.client.HessianConnectionFactory;
 import com.caucho.hessian.client.HessianProxyFactory;
@@ -40,7 +42,12 @@ public void setHessianProxyFactory(HessianProxyFactory 
factory) {
     }
 
     public HessianConnection open(URL url) throws IOException {
-        return new HttpClientConnection(httpClient, url);
+        HttpClientConnection httpClientConnection = new 
HttpClientConnection(httpClient, url);
+        for (String key : RpcContext.getContext().getAttachments().keySet()) {
+            //防止名称冲突, 在key前添加header
+            httpClientConnection.addHeader(Constants.DEFAULT_EXCHANGER + key, 
RpcContext.getContext().getAttachment(key));
+        }
+        return httpClientConnection;
     }
 
 }
\ No newline at end of file
diff --git 
a/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/filter/HessianFilter.java
 
b/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/filter/HessianFilter.java
new file mode 100644
index 0000000000..1d8b77c7f3
--- /dev/null
+++ 
b/dubbo-rpc/dubbo-rpc-hessian/src/main/java/com/alibaba/dubbo/rpc/protocol/hessian/filter/HessianFilter.java
@@ -0,0 +1,17 @@
+package com.alibaba.dubbo.rpc.protocol.hessian.filter;
+
+import com.alibaba.dubbo.common.Constants;
+import com.alibaba.dubbo.common.extension.Activate;
+import com.alibaba.dubbo.rpc.*;
+import com.alibaba.dubbo.rpc.protocol.hessian.HessianProtocol;
+
+@Activate(group = Constants.PROVIDER)
+public class HessianFilter implements Filter {
+    @Override
+    public Result invoke(Invoker<?> invoker, Invocation invocation) throws 
RpcException {
+        //判断是否是hessian协议
+        if (invoker.getUrl().getProtocol().equals("hessian"))
+            
RpcContext.getContext().setAttachments(HessianProtocol.HEADER_MAP_THREAD_LOCAL.get());
+        return invoker.invoke(invocation);
+    }
+}
diff --git 
a/dubbo-rpc/dubbo-rpc-hessian/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Filter
 
b/dubbo-rpc/dubbo-rpc-hessian/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Filter
new file mode 100644
index 0000000000..d50c3e2d6f
--- /dev/null
+++ 
b/dubbo-rpc/dubbo-rpc-hessian/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Filter
@@ -0,0 +1 @@
+hessianFilter=com.alibaba.dubbo.rpc.protocol.hessian.filter.HessianFilter
\ No newline at end of file
diff --git a/dubbo/pom.xml b/dubbo/pom.xml
index 53a857f9ae..aadb4a1421 100644
--- a/dubbo/pom.xml
+++ b/dubbo/pom.xml
@@ -252,6 +252,11 @@
             <artifactId>dubbo-container-log4j</artifactId>
             <version>${project.parent.version}</version>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>dubbo-container-log4j2</artifactId>
+            <version>${project.parent.version}</version>
+        </dependency>
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>dubbo-container-logback</artifactId>
@@ -351,6 +356,7 @@
                                     
<include>com.alibaba:dubbo-container-spring</include>
                                     
<include>com.alibaba:dubbo-container-jetty</include>
                                     
<include>com.alibaba:dubbo-container-log4j</include>
+                                    
<include>com.alibaba:dubbo-container-log4j2</include>
                                     
<include>com.alibaba:dubbo-container-logback</include>
                                 </includes>
                             </artifactSet>
diff --git a/pom.xml b/pom.xml
index b002fa4a15..67bd1c07e3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -116,6 +116,7 @@
         <slf4j_version>1.7.25</slf4j_version>
         <jcl_version>1.2</jcl_version>
         <log4j_version>1.2.16</log4j_version>
+        <log4j2_version>2.8.2</log4j2_version>
         <logback_version>1.2.2</logback_version>
         <!-- Test libs -->
         <junit_version>4.12</junit_version>
@@ -332,6 +333,26 @@
                 <artifactId>logback-classic</artifactId>
                 <version>${logback_version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.apache.logging.log4j</groupId>
+                <artifactId>log4j-api</artifactId>
+                <version>${log4j2_version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.apache.logging.log4j</groupId>
+                <artifactId>log4j-core</artifactId>
+                <version>${log4j2_version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.apache.logging.log4j</groupId>
+                <artifactId>log4j-1.2-api</artifactId>
+                <version>${log4j2_version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.apache.logging.log4j</groupId>
+                <artifactId>log4j-slf4j-impl</artifactId>
+                <version>${log4j2_version}</version>
+            </dependency>
             <!-- Test libs -->
             <dependency>
                 <groupId>junit</groupId>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to