Author: isapir Date: Mon Oct 22 08:02:26 2018 New Revision: 1844531 URL: http://svn.apache.org/viewvc?rev=1844531&view=rev Log: Added JniLifecycleListener per BZ 62830
Added: tomcat/trunk/java/org/apache/catalina/core/JniLifecycleListener.java Modified: tomcat/trunk/webapps/docs/changelog.xml tomcat/trunk/webapps/docs/config/listeners.xml Added: tomcat/trunk/java/org/apache/catalina/core/JniLifecycleListener.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/JniLifecycleListener.java?rev=1844531&view=auto ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/JniLifecycleListener.java (added) +++ tomcat/trunk/java/org/apache/catalina/core/JniLifecycleListener.java Mon Oct 22 08:02:26 2018 @@ -0,0 +1,88 @@ +/* + * 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.catalina.core; + +import org.apache.catalina.Lifecycle; +import org.apache.catalina.LifecycleEvent; +import org.apache.catalina.LifecycleListener; +import org.apache.juli.logging.Log; +import org.apache.juli.logging.LogFactory; + +/** + * An implementation of LifeCycleListener that loads a native library into the JVM. + * <p> + * Native libraries are associated with the class loader of the class that loaded them, + * and the same library may not be loaded by more than one class loader. Due to that + * restriction, loading a native library from a Webapp's class loader makes it impossible + * for other Webapps to load the native library. + * <p> + * Loading the native library using this listener solves the issue as it is loaded + * by a shared class loader (typically the Common class loader, but may vary in some + * configurations). + */ +public class JniLifecycleListener implements LifecycleListener { + + private static final Log log = LogFactory.getLog(JniLifecycleListener.class); + + private String libraryName = ""; + private String libraryPath = ""; + + @Override + public void lifecycleEvent(LifecycleEvent event) { + + if (Lifecycle.BEFORE_START_EVENT.equals(event.getType())) { + + if (!libraryName.isEmpty()) { + System.loadLibrary(libraryName); + log.info("Loaded native library " + libraryName); + } else if (!libraryPath.isEmpty()) { + System.load(libraryPath); + log.info("Loaded native library from " + libraryPath); + } else { + throw new IllegalArgumentException("Either libraryName or libraryPath must be set"); + } + } + } + + public void setLibraryName(String libraryName) { + + if (!this.libraryPath.isEmpty()) { + throw new IllegalArgumentException("Either libraryName or libraryPath may be set, not both."); + } + + this.libraryName = libraryName; + } + + public String getLibraryName() { + return libraryName; + } + + public void setLibraryPath(String libraryPath) { + + if (!this.libraryName.isEmpty()) { + throw new IllegalArgumentException("Either libraryName or libraryPath may be set, not both."); + } + + this.libraryPath = libraryPath; + } + + public String getLibraryPath() { + return libraryPath; + } + +} Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1844531&r1=1844530&r2=1844531&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Mon Oct 22 08:02:26 2018 @@ -121,11 +121,10 @@ Eclipse. Based on a patch by mdfst13. (markt) </add> <add> - <bug>62830</bug>: Add static methods - <code>Library.loadLibrary(libname)</code> and - <code>Library.load(filename)</code> to load a native library by the - common class loader so that more than one application can load it. - (isapir) + <bug>62830</bug>: Added <code>JniLifeCycleListener</code> and static + methods <code>Library.loadLibrary(libraryName)</code> and + <code>Library.load(filename)</code> to load a native library by a + shared class loader so that more than one Webapp can use it. (isapir) </add> </changelog> </subsection> Modified: tomcat/trunk/webapps/docs/config/listeners.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/listeners.xml?rev=1844531&r1=1844530&r2=1844531&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/config/listeners.xml (original) +++ tomcat/trunk/webapps/docs/config/listeners.xml Mon Oct 22 08:02:26 2018 @@ -159,6 +159,29 @@ </subsection> + <subsection name="JNI Library Loading Listener - org.apache.catalina.core.JniLifecycleListener"> + + <p>The <strong>JNI Library Loading Listener</strong> makes it possible + for multiple Webapps to use a native library, by loading the native + library using a shared class loader (typically the Common class loader but + may vary in some configurations)</p> + + <p>The listener supports two mutually exclusive attributes, so one of them must be used, but you can not use both together:</p> + + <attributes> + <attribute name="libraryName" required="false"> + <p>The name of the native library, as defined in + <code>java.lang.System.loadLibrary()</code> + </p> + </attribute> + <attribute name="libraryPath" required="false"> + <p>The absolute path of the native library, as defined in + <code>java.lang.System.load()</code> + </p> + </attribute> + </attributes> + </subsection> + <subsection name="JRE Memory Leak Prevention Listener - org.apache.catalina.core.JreMemoryLeakPreventionListener"> <p>The <strong>JRE Memory Leak Prevention Listener</strong> provides --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org