3

I have been running into a string of issues with Axiom and Mule, a background can be found here:

https://stackoverflow.com/questions/34164577/classloader-overrides-not-working-in-mule

As mentioned in the comment of the previous issue, I was able to use maven shade to override the package name of axiom-api in mule due to conflicting jars. Mule loads a version of axiom-api and axiom-impl in the server. I use a different version of axiom-api and axiom-dom in my connector. (The connector works fine testing outside of anypoint studio)

Using maven shade, I renamed:

org.apache.axiom

to

org.apache.1.2.14.axiom

This resolved my original issue of a method not found due to the conflicting jar versions. Now the problem I am running into is:

org.apache.axiom.om.impl.dom.factory.OMDOMMetaFactoryLoader cannot be cast to org.apache.1.2.14.axiom.locator.loader.OMMetaFactoryLoader (java.lang.ClassCastException)   org.apache.1.2.14.axiom.locator.ImplementationFactory:133 (null)

I believe this is due to renaming the packages of axiom-api with maven shade. My maven shade configs look like this:

<configuration>
                    <artifactSet>
                        <includes>
                            <include>org.apache.ws.commons.axiom:*</include>
                        </includes>
                    </artifactSet>
                    <relocations>
                        <relocation>
                            <pattern>org.apache.axiom</pattern>
                            <shadedPattern>org.apache.1.2.14.axiom</shadedPattern>
                        </relocation>
                    </relocations>
                </configuration>

So, it should actually be renaming axiom-dom as well but it does not.

I believe this is due to classloader only loading one instance of the axiom-dom packages. To resolve this, I believe I just need to rename:

org.apache.axiom.om.impl.dom.factory.OMDOMMetaFactoryLoader

to

org.apache.1.2.14.axiom.om.impl.dom.factory.OMDOMMetaFactoryLoader

I found an example of axiom.xml:

http://grepcode.com/file/repo1.maven.org/maven2/org.apache.ws.commons.axiom/axiom-impl/1.2.13/META-INF/axiom.xml/

I cant find any documentation over this file but I see where it is used in source code. I thought I could rewrite:

<implementation loader="org.apache.axiom.om.impl.dom.factory.OMDOMMetaFactoryLoader" name="doom">
              <feature name="dom" priority="100"/>
       </implementation>

to

<implementation loader="org.apache.1.2.14.axiom.om.impl.dom.factory.OMDOMMetaFactoryLoader" name="doom">
              <feature name="dom" priority="100"/>
       </implementation>

but that had no effect. Is there a way to override the doom loader class name?

Attila
  • 3,206
  • 2
  • 31
  • 44
MonomiDev
  • 301
  • 2
  • 8

2 Answers2

1

To do the necessary transformations automatically, you can use the following configuration:

    <plugins>
        <plugin>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                            <includes>
                                <include>org.apache.ws.commons.axiom:*</include>
                            </includes>
                        </artifactSet>
                        <transformers>
                            <transformer implementation="org.apache.axiom.buildutils.shade.axiomxml.AxiomXmlResourceTransformer" />
                        </transformers>
                        <relocations>
                            <relocation>
                                <pattern>org.apache.axiom</pattern>
                                <shadedPattern>org.apache.1.2.14.axiom</shadedPattern>
                            </relocation>
                        </relocations>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.apache.ws.commons.axiom</groupId>
                    <artifactId>shade-axiom-xml</artifactId>
                    <version>1.2.17-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>

You will need to add the Apache snapshot repository to your POM (because there is no release yet that includes the change that makes this work):

    <repositories>
        <repository>
            <id>apache.snapshots</id>
            <name>Apache Snapshot Repository</name>
            <url>http://repository.apache.org/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

Note that shade-axiom-xml version 1.2.17-SNAPSHOT should work fine with Axiom 1.2.14, i.e. you don't need to change the version of the Axiom libraries you depend on.

Also, don't forget that since you are using Axis2 and Axis2 depends on Axiom, you will need to include all Axis2 JARs in the artifact set.

Andreas Veithen
  • 8,868
  • 3
  • 25
  • 28
  • Hey Andreas, Sorry for the delay in a response. So when I use your plugin definitions, i go back to the original error of "geMetaFactory (NoSuchMethodError)" I must be doing something wrong but copy pasting your plugin definition, it no longer attempts to load "org.apache.1.2.14.axiom", it is just "org.apache.axiom" so it is using the old version again and nothing is being shaded. – MonomiDev Dec 18 '15 at 18:29
  • When you say that nothing is being shaded, did you confirm that by inspect the JAR produced by maven-shade-plugin? – Andreas Veithen Dec 19 '15 at 01:10
  • I looked at the strack trace from the error. The previous error, the stack trace mentioned: org.apache.axiom.om.impl.dom.factory.OMDOMMetaFactoryLoader cannot be cast to org.apache.1.2.14.axiom.locator.loader.OMMetaFactoryLoader (java.lang.ClassCastException) org.apache.1.2.14.axiom.locator.ImplementationFactory:133 (null) You can see the package name represents what was created via the shading. Specifically, the reference to "1.2.14". The new error no longer contains the "1.2.14", just "org.apache.axiom". The error is around no such method which is caused by using the older version. – MonomiDev Dec 20 '15 at 23:53
  • Fair enough. You choose not to answer my question, so I guess you don't need my help then. – Andreas Veithen Dec 21 '15 at 09:28
  • I thought I did answer your question. I was telling you how I came to the conclusion it was not being shaded. I have really appreciated your help, I am not sure how I offended you with my response but I apologize. – MonomiDev Dec 22 '15 at 18:23
1

When such ClassCastException occurs one can try to exclude problematic class from shading (the one that app is casting to i.e. in your situation you should exclude org.apache.axiom.locator.loader.OMMetaFactoryLoader).

You can do this with:

<relocations>
    <relocation>
        <pattern>org.apache.axiom</pattern>
        <shadedPattern>org.apache.1.2.14.axiom</shadedPattern>
        <excludes>
            <exclude>org.apache.axiom.locator.loader.OMMetaFactoryLoader</exclude>
        </excludes>
    </relocation>
</relocations>
Artur Łysik
  • 407
  • 1
  • 9
  • 14