You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2015/09/09 06:11:41 UTC

tomee git commit: TOMEE-1629 log4j2 shutdown registry integration

Repository: tomee
Updated Branches:
  refs/heads/master 6014cf207 -> 549d7334e


TOMEE-1629 log4j2 shutdown registry integration


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/549d7334
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/549d7334
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/549d7334

Branch: refs/heads/master
Commit: 549d7334e14f885cd83834d78c3750d74bfcddbd
Parents: 6014cf2
Author: Romain Manni-Bucau <rm...@gmail.com>
Authored: Tue Sep 8 21:11:33 2015 -0700
Committer: Romain Manni-Bucau <rm...@gmail.com>
Committed: Tue Sep 8 21:11:33 2015 -0700

----------------------------------------------------------------------
 utils/log4j2-tomee/pom.xml                      | 67 ++++++++++++++++++++
 .../log4j2/CaptureLog4j2ShutdownHooks.java      | 43 +++++++++++++
 .../log4j2/Log4j2ShutdownHooksExecutor.java     | 38 +++++++++++
 .../org/apache/tomee/log4j2/SetupLog4j2.java    | 45 +++++++++++++
 .../META-INF/org.apache.openejb.extension       |  2 +
 .../main/resources/log4j2.component.properties  | 17 +++++
 utils/pom.xml                                   |  1 +
 7 files changed, 213 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/549d7334/utils/log4j2-tomee/pom.xml
----------------------------------------------------------------------
diff --git a/utils/log4j2-tomee/pom.xml b/utils/log4j2-tomee/pom.xml
new file mode 100644
index 0000000..c64be7a
--- /dev/null
+++ b/utils/log4j2-tomee/pom.xml
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF 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.
+-->
+<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/xsd/maven-4.0.0.xsd">
+  <parent>
+    <artifactId>utils</artifactId>
+    <groupId>org.apache.tomee</groupId>
+    <version>7.0.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>log4j2-tomee</artifactId>
+  <name>OpenEJB :: Utils :: Log4j2</name>
+  <description>Add this module if you use log4j2 in TomEE/lib and rely on shutdown hook and don't want to loose logs.</description>
+
+  <properties>
+    <log4j2.version>2.3</log4j2.version>
+    <log4j.groupId>org.apache.logging.log4j</log4j.groupId>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>${log4j.groupId}</groupId>
+      <artifactId>log4j-api</artifactId>
+      <version>${log4j2.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>${log4j.groupId}</groupId>
+      <artifactId>log4j-core</artifactId>
+      <version>${log4j2.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.tomcat</groupId>
+      <artifactId>tomcat-catalina</artifactId>
+      <version>${tomcat.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.tomee</groupId>
+      <artifactId>tomee-catalina</artifactId>
+      <version>${project.version}</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+</project>

http://git-wip-us.apache.org/repos/asf/tomee/blob/549d7334/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/CaptureLog4j2ShutdownHooks.java
----------------------------------------------------------------------
diff --git a/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/CaptureLog4j2ShutdownHooks.java b/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/CaptureLog4j2ShutdownHooks.java
new file mode 100644
index 0000000..15681ce
--- /dev/null
+++ b/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/CaptureLog4j2ShutdownHooks.java
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.tomee.log4j2;
+
+import org.apache.logging.log4j.core.util.Cancellable;
+import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry;
+
+import java.util.Collection;
+import java.util.concurrent.CopyOnWriteArraySet;
+
+public class CaptureLog4j2ShutdownHooks implements ShutdownCallbackRegistry {
+    static final Collection<Runnable> HOOKS = new CopyOnWriteArraySet<Runnable>();
+
+    public Cancellable addShutdownCallback(final Runnable callback) {
+        HOOKS.add(callback);
+        return new Cancellable() {
+            @Override
+            public void cancel() {
+                HOOKS.remove(callback);
+            }
+
+            @Override
+            public void run() {
+                cancel();
+                callback.run();
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/549d7334/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/Log4j2ShutdownHooksExecutor.java
----------------------------------------------------------------------
diff --git a/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/Log4j2ShutdownHooksExecutor.java b/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/Log4j2ShutdownHooksExecutor.java
new file mode 100644
index 0000000..b43c487
--- /dev/null
+++ b/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/Log4j2ShutdownHooksExecutor.java
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.tomee.log4j2;
+
+import org.apache.catalina.Lifecycle;
+import org.apache.catalina.LifecycleEvent;
+import org.apache.catalina.LifecycleListener;
+import org.apache.catalina.Server;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+public class Log4j2ShutdownHooksExecutor implements LifecycleListener {
+    @Override
+    public void lifecycleEvent(final LifecycleEvent event) {
+        if (Server.class.isInstance(event.getSource()) && Lifecycle.AFTER_DESTROY_EVENT.equals(event.getType())) {
+            final Collection<Runnable> copy = new ArrayList<>(CaptureLog4j2ShutdownHooks.HOOKS);
+            CaptureLog4j2ShutdownHooks.HOOKS.removeAll(copy);
+            for (final Runnable runnable : copy) {
+                runnable.run();
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/549d7334/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/SetupLog4j2.java
----------------------------------------------------------------------
diff --git a/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/SetupLog4j2.java b/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/SetupLog4j2.java
new file mode 100644
index 0000000..c58e172
--- /dev/null
+++ b/utils/log4j2-tomee/src/main/java/org/apache/tomee/log4j2/SetupLog4j2.java
@@ -0,0 +1,45 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.tomee.log4j2;
+
+import org.apache.openejb.assembler.classic.event.AssemblerCreated;
+import org.apache.openejb.core.ParentClassLoaderFinder;
+import org.apache.openejb.observer.Observes;
+import org.apache.openejb.util.classloader.URLClassLoaderFirst;
+import org.apache.tomee.loader.TomcatHelper;
+
+import java.util.Collection;
+
+public class SetupLog4j2 {
+    public void setup(@Observes final AssemblerCreated initEvent) {
+        try {
+            ParentClassLoaderFinder.Helper.get().loadClass("org.apache.logging.log4j.core.util.ShutdownCallbackRegistry");
+            doSetup();
+        } catch (final ClassNotFoundException e) {
+            // no-op
+        }
+    }
+
+    private void doSetup() {
+        // org.apache.openejb.log4j2.CaptureLog4j2ShutdownHooks is likely int the container so just skip the API, luckily it has no dep :)
+        final Collection<String> forcedSkip = URLClassLoaderFirst.FORCED_SKIP;
+        forcedSkip.add("org.apache.logging.log4j.api.");
+        forcedSkip.add("org.apache.logging.log4j.core.");
+
+        TomcatHelper.getServer().addLifecycleListener(new Log4j2ShutdownHooksExecutor());
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/549d7334/utils/log4j2-tomee/src/main/resources/META-INF/org.apache.openejb.extension
----------------------------------------------------------------------
diff --git a/utils/log4j2-tomee/src/main/resources/META-INF/org.apache.openejb.extension b/utils/log4j2-tomee/src/main/resources/META-INF/org.apache.openejb.extension
new file mode 100644
index 0000000..c7d5f67
--- /dev/null
+++ b/utils/log4j2-tomee/src/main/resources/META-INF/org.apache.openejb.extension
@@ -0,0 +1,2 @@
+org.apache.tomee.log4j2.SetupLog4j2
+

http://git-wip-us.apache.org/repos/asf/tomee/blob/549d7334/utils/log4j2-tomee/src/main/resources/log4j2.component.properties
----------------------------------------------------------------------
diff --git a/utils/log4j2-tomee/src/main/resources/log4j2.component.properties b/utils/log4j2-tomee/src/main/resources/log4j2.component.properties
new file mode 100644
index 0000000..e4e8347
--- /dev/null
+++ b/utils/log4j2-tomee/src/main/resources/log4j2.component.properties
@@ -0,0 +1,17 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF 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.
+#
+log4j.shutdownCallbackRegistry = org.apache.tomee.log4j2.CaptureLog4j2ShutdownHooks

http://git-wip-us.apache.org/repos/asf/tomee/blob/549d7334/utils/pom.xml
----------------------------------------------------------------------
diff --git a/utils/pom.xml b/utils/pom.xml
index 987f67b..5effe85 100644
--- a/utils/pom.xml
+++ b/utils/pom.xml
@@ -33,5 +33,6 @@
     <module>openejb-core-eclipselink</module>
     <module>openejb-provisionning</module>
     <module>openejb-mockito</module>
+    <module>log4j2-tomee</module>
   </modules>
 </project>