Using latest Xerces with Eclipse RCP application - eclipse

I'm trying to use Xerces 2.11.0 in an Eclipse RCP application, but from everything I've tried, I'm at a loss to figure out how. To complicate matters, I'm also trying to use Batik 1.7.
I've created my own bundles for Xerces and the xml-apis, I've added the additional W3C DOM interfaces that Batik uses to my xml-apis bundle.
The first problem that occurs within Batik
java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
The fundamental reason for this appears to be because org.w3c.dom is contained in the JRE and exposed through org.eclipse.osgi (the system.bundle). This appears to trump any other bundle that wants to provide the package.
Trying to influence the system using require-bundle with my bundle before any others or using import-package with an explicit version leads to errors like
java.lang.LinkageError: loader constraint violation: loader
(instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader)
previously initiated loading for a different type with name
"org/w3c/dom/Document"
Changing the org.osgi.framework.system.packages and org.osgi.framework.bootdelegation parameters to remove org.w3c.dom and adding my bundle to osgi.framework.extensions, leads to erros like
java.lang.LinkageError: loader constraint violation in interface itable
initialization: when resolving method
"javax.xml.parsers.DocumentBuilder.setErrorHandler(
Lorg/xml/sax/ErrorHandler;)V" the class loader
(instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader)
of the current class, org/apache/log4j/xml/DOMConfigurator, and the
class loader (instance of <bootloader>) for resolved class,
javax/xml/parsers/DocumentBuilder, have different Class objects for the
type rrorHandler;)V used in the signature
I've also tried using the java.endorsed.dirs, which makes things break very quickly.
Any ideas what I've missed or may be doing wrong?

We used to have similar problems using DOM level 3 on jdk 1.4 and I think endorsed dirs is the only solution that works because you need to override the DOM APIs in the jdk.
We couldn't face going down this route again so instead ripped out references to ElementTraversal and built Xerces by hand and the problem goes away. If Batik uses ElementTraversal that's not an option though.

I think I've found a solution.
I've modified my xml-apis bundle so that it is a fragment, hosted by the system.bundle.
I've also modified my Xerces bundle to import the packages rather than requiring the xml-apis bundle (which was done originally to convince xerces to use the right packages).
From the testing I've done, this appears to be sufficient to allow each of the bundles to locate the same implementation of org.w3c.dom packages, which contains all the correct classes.

Related

Eclipse Plug-in First executes

From where an eclipse plug in will start?
As for as i know, class which extends abstract class plugin will run first.That class will start the bundle and plug-in.xml file will load.Am i correct?
If i am correct, Please tell me in Plug-in.xml which extension point will execute first and how that is determined ?
It's not as simple as you describe. Most importantly, plug-ins/bundles are initially loaded by the platform without actually executing any code from them. The platform reads and evaluates the bundle manifest plugin.xml in order to know what extension points it uses, but only when one of those is actually invoked (usually triggered by user action) does it execute any code from the plug-in (including instantiating the bundle activator class, which usually is an extension of Plugin).
There are exceptions to this (basically plug-in eager start-up, but that's heavily discouraged), but this is the general concept.
Read more details in the Eclipse wiki, help pages, and API docs.

Scala class file broken error over Java jar

I have run into a compilation error:
[warn] Class com.google.api.client.auth.oauth2.Credential not found - continuing with a stub.
[error] error while loading GoogleService, class file '....../gdata-core-1.0.jar(com/google/gdata/client/GoogleService.class)' is broken
I found this similar question but couldn't successfully use its solution for my case. How can I trace what's actually broken in this jar as per the Scala compiler (i.e. get the details of what's actually broken), to make sure what the solution aught to be?
The source where I think this Google jar builds from, is here.
Note: unlike the other questions, in my case, the google jars are included unmanaged in my project, in the lib directory, as per these google setup instructions.
As described in your linked answer, this error happens when a class contains some annotations which are no longer present on the classpath. Java considers this acceptable, but Scala does not. You'll generally only run into this problem with heavily optimized java libraries where they've deliberately excluded the "unnecessary" annotations. Google does this for a lot of their code; to be perfectly honest I don't think I've ever seen the problem in any non-google libraries.
The pragmatic solution is to use advanced search on maven central to find the jar that contains the missing class. If gdata-core had been built with maven (as most serious java libraries are these days), it would be easy to see from the <dependencies> section of pom.xml which dependencies had been declared optional, and therefore figure out where any classpath problems of this kind were likely to be. Unfortunately this particular library is still built with ant, so it's hard to determine the build classpath without reading the whole build.xml and figuring it out "by hand".
Had to find a google supplied jar file where the class mentioned in the warning (class Credential) is contained, and stick it in my lib directory used by sbt.
With really a lot of heuristic, it turned out to be the jar file google-oauth-client-1.18.0-rc.jar which I obtained here and equally exists here, after figuring that the source file that the error is for, simply does not define that class itself but rather imports it from a different package com.google.api.client.auth.oauth2. I guess the latter package must have been present at compile time, and its compiled class is necessary for Scala being able to use the classes contained in the former jar, at least when Scala is involved.
Not exactly sure how the build system at google produced all of this, and how to pin down the annotations that made the additional jar required for Scala, but the missing class is no longer missing for Scala.
Hope someone would chime in to provide a deeper answer, as to how to pin down the details of a class being broken for scala, and how to trace back where to obtain it sans my heuristic search.

Run Eclipse RCP application with Java instrumentation

Using Java instrumentation, we can access a class that is loaded by the Java classloader from the JVM and modify its bytecode by inserting our custom code, all these done at runtime. We need not worry about security, these are governed by the same security context applicable for Java classes and respective classloaders.
We are able to access some java application using this as they run in same classloader.
Now what we are trying to do is to access eclipse RCP application using java instrumentation but in RCP each bundle has its own classloader and our instrumentation code runs with java application classloader.
when we are accessing it, it is throwing "Workbench has not been created yet" exception whereas the workbench is up and running.(I hope this is because of diffrent classloaders for both of them).
I have tried doing thing from here but to no success.
Is there any way we can work RCP application out with java instrumentation.
When you instrument a class, the references of the inserted code are resolved by the ClassLoader of the modified class. If that class loader does not delegate to the application loader, e.g. because it is rule based and doesn’t know your instrumentation specific classes you can’t enforce delegation.
What you can do:
Use access override to define classes in the scope of the loader of the instrumented class. Since defineClass is final the bundle class loader can’t intercept it. However, the references of these injected classes are again resolved by the bundle loader so you have to add all required instrumentation classes this way.
Since the bundle class loader will do the parent loader delegation for the official Java API classes, you can instrument one of the core Java classes to add a helper method which will be called by the instrumented classes and delegates to your instrumentation classes which must have been added to the boostrap loader
You can use the trick described in the answer you have already linked to put a MethodHandle into the system properties. The instrumented classes can retrieve and invoke it as MethodHandle is a core class which will be correctly resolved by the bundle loader and the underlying method can be invoked without having access to its defining class (assuming that parameter and return types are all either primitive types or core classes).

How to add jars properly to a eclipse bundle

I got a little problem. I want to use hibernate in an eclipse rcp. (i'm new to osgi and eclipse rcp). So I added the jar into the plugin-project folder and the build path and the bundle build path, but when I try to use hibernate from my bundle, it crashes with a ClassNotFoundException.
What is the proper way to do this?
Pls look at eclipse buddy policy. This might help you if you are facing class not getting loaded because of osgi classloading.
Hibernate, and many other classic Java programs (ab)use dynamic class loading to to connect the different parts. They classes they use are read from a file and then loaded with Class.forName. This is fundamentally not-modular since these classes are by definition implementation classes, which should be hidden.
Since OSGi is a modularity framework it puts fences around a module (a bundle) and refuses to load anything that is not properly exported and imported. So if Hibernate does its Class.forName it will run right into this fence, as it should be to get the advantages of modularity.
Eclipse Buddy policy is like a huge hole in this fence, moving things back tot he bad old classpath: linear search. With a buddy policy, Eclipse will just start searching if there is a class somewhere that has that name. Since this ignores versions, you can no longer rely on proper version handling. The good news is that it works most of the time. The bad news is that you loose privacy and when it does not work you get weird errors.
With Hibernate, only solution is to not use the text file setup but use the API and give Hibernate the actual classes. In those cases Hibernate will use the class loader of those classes and that works. In OSGi, as long as you follow the Java language rules there are no problems.
To handle the kind of problems that class loading hacks address OSGi uses services.

JBoss AS Classloader implementation?

I'm migrating a small tool I wrote for our application that was working on WAS and WLS to JBoss AS 5.1. Basically, the tools helps me to troubleshoot classloading issues: it discovers the classloading hierarchy of a specified class, at runtime, and finds out all classes and jars loaded by each classloader. It is a fairly simple tool - it just uses the Classloader.getPArent() method to get the classloader hierarchy.
The trick that made it work for WLS and WAS was that their classloader implementations either had a 'getClassPath()' method which returned a full list of all classpath entries, or they were instances of the URLClassloader class, and I could use its 'getURLs()' method to get the same info.
Now, it looks like JBoss internal classloaders do not follow the same pattern - so I was wondering if there is some other way to get the same information somehow?
Basically, I want to get a full picture of the classloading hierarchy and which classloader loads which classes/jars at runtime. It has to be a runtime information - because I want to be able to find out this information for any instance of a class, be it a servlet, an EJB, a util class or JSP - so that I could see where in the classloader hierarchy those objects are, what their sibling loaded resources are and what their parent classloaders are.
Thanks!
Marina
In fact this dued to VFS used by JBoss, I've written a post relating this issue and the corresponding solution.