[ 
https://issues.apache.org/jira/browse/LOG4J2-920?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Ludovic HOCHET updated LOG4J2-920:
----------------------------------
    Attachment: patch3.diff

patch3 is an attempt at providing a 'unit' test for this issue.
It builds on the existing, but skipped, OSGi tests in log4j-api.

In order to reenable the OSGi tests, the following changes where needed:
* in log4j-api/pom.xml, the org.apache.felix.framework dependency had to be 
moved before the org.osgi.core dependency as otherwise the classes from the 
later were used causing class errors.
* in 
log4j-api/src/test/java/org/apache/logging/log4j/osgi/BundleTestInfo.java::getVersion()
 : this was expecting a project.version.osgi property that isn't defined, 
changed it to return the version of the artifact, this caused the following 
change
* in 
log4j-api/src/test/java/org/apache/logging/log4j/osgi/AbstractLoadBundleTest.java::getExpectedBundleSymbolicName()
 : . are used instead of - in the OSGi symbolic name

Adding the new test, required two changes:
* 
log4j-core/src/main/java/org/apache/logging/log4j/core/osgi/Activator.java::scanInstalledBundlesForPlugins(..)
 : exclude the system bundle (id 0) from the search for plugins as it uses a 
ClassLoader that does not properly descend from ClassLoader in the core bundle 
when using Equinox
* 
log4j-api/src/test/java/org/apache/logging/log4j/osgi/AbstractLoadBundleTest.java::testLoadStartStop()
 : added an uninstall step as otherwise it somehow cause issues with Equinox

Now for the test itself, it is actually two tests:
* a first one that in the basic OSGi environment adds the api, core and for the 
test dependency purpose log4j-samples-configuration, then through reflection 
(in order to be in the sample bundle context) log a simple error message that's 
retrieved via a ByteArrayOutputStream override of System.out and compared to 
the expected value.
* a second derived from the first but that checks for the absence of the 
message line of exception that motivated this issue, again using reflection to 
plug into the right classes.

The fix used was to add 
"org.apache.logging.log4j.core.osgi;resolution:=optional," to the 
Import-Package of maven-bundle-plugin in log4j-api/pom.xml

I feel the OSGi tests could be made a submodule of log4j2 'pom' they would then 
not require a first build followed by a second build for actually running the 
tests, but that may be a follow up.

> ClassNotFoundException for BundleContextSelector when initialising in an OSGi 
> environment
> -----------------------------------------------------------------------------------------
>
>                 Key: LOG4J2-920
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-920
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: API
>    Affects Versions: 2.1
>         Environment: Apache Felix, Java 8
>            Reporter: Ludovic HOCHET
>         Attachments: LOG4J2-920.zip, patch.diff, patch2.diff, patch3.diff, 
> pom.xml
>
>
> When initialising Log4J2 in an Apache Felix environment, I get the following 
> exception: 
> ERROR StatusLogger Unable to create context 
> org.apache.logging.log4j.core.osgi.BundleContextSelector 
> java.lang.ClassNotFoundException: 
> org.apache.logging.log4j.core.osgi.BundleContextSelector not found by 
> org.apache.logging.log4j.api [78]
>     at 
> org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1556)
>     at 
> org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:77)
>     at 
> org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1993)
>     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
>     at java.lang.Class.forName0(Native Method)
>     at java.lang.Class.forName(Class.java:260)
>     at org.apache.logging.log4j.util.LoaderUtil.loadClass(LoaderUtil.java:117)
>     at 
> org.apache.logging.log4j.util.LoaderUtil.newInstanceOf(LoaderUtil.java:136)
>     at 
> org.apache.logging.log4j.util.LoaderUtil.newCheckedInstanceOf(LoaderUtil.java:163)
>     at 
> org.apache.logging.log4j.core.util.Loader.newCheckedInstanceOf(Loader.java:311)
>     at 
> org.apache.logging.log4j.core.impl.Log4jContextFactory.createContextSelector(Log4jContextFactory.java:96)
>     at 
> org.apache.logging.log4j.core.impl.Log4jContextFactory.<init>(Log4jContextFactory.java:54)
>     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>     at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>     at 
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>     at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
>     at java.lang.Class.newInstance(Class.java:438)
>     at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:96)
> [...]
> Due to a missing import of the org.apache.logging.log4j.core.osgi package in 
> the API OSGi metadata in its pom



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

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

Reply via email to