Drools KieBuilder with custom class loader not working with ProjectClassLoader - classloader

I am using drools 6.4.0 Final. I compile java classes on-the-fly using drools NativeJavacompiler so i set the class loader using ProjectClassLoader. Once i compiled all the classes, i stored the ProjectClassLoader as a class loader in my class. Now i have .drl files as well that refers to my external java classes so i set the custom class loader as my ProjectClassLoader which has store map with my compiled classes but the drools build fails with ClassNotFoundException.
KieHelper helper = new KieHelper();
helper.setClassLoader(getMyProjectClassLoader());
If i set URLClassLoader with reference to my physical class files as class loader, it works. Why only URLClassLoader is working and not my drools ProjectClassLoader? Any help would be greatly appreciated. Thanks in Advance!

Related

How can I prevent war libraries slf4j to collide with my project slf4j?

I am trying to deploy the reddog rdap-server war file into an embedded Jetty in my project. But everytime I am getting this eror:
java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/eclipse/jetty/webapp/WebAppClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for the method's defining class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type org/slf4j/ILoggerFactory used in the signature
Problem is, the war file already contains slf4j. Any suggestions?
I tried to change my maven pom to "provider, which didn't help.
Use WebAppContext's (which it seems like from your error, and the mention of WebAppClassLoader).
Don't modify the Server or System classes lists on the WebAppContext or the WebAppClassLoader.
Don't set the WebAppContext.setParentLoaderPriority(boolean) field.
That's it.
Now you have the ClassLoader Isolation you are looking for.

Unconstrained Isolation (mocking) framework for dotnet core

I'm working on a dotnet core project, trying to mock some third party classes in my Xunit.net tests. The class I'm trying to fake is not mockable through constrained frameworks like Moq or NSubstitute. So I need an unconstrained framework to do that.
Say I want to fake DateTime.Now in my .net core test projects.
In .net 4.5 we have MSFakes(Moles), Smocks, etc. None of them support dotnet core framework yet. Shim is not ported to dotnet core yet.
Anyone knows any isolation framework or technique that will achieve my goal for .NET Core at the present time?
You could try a different way: wrap those third party libs with your own interfaces and wrapper classes. Then use a dummy object implementing that interface.
Things become more complicated with static properties like DateTime.Now. But it works similar: define an ITimeProvider interface with a Now property. Add a DateTimeWrapper : ITimeProvider with a public DateTime Now => DateTime.Now;, and - for convenience, but not required - a static class around it, e.g.
static class MyClock
{
static ITimeProvider _Instance;
static void SetInstanceForTesting(ITimeProvider instance)
{ _Instance = instance; }
static DateTime Now => _Instance.Now;
}
Alternatively, you may inject an ITimeProvider instance to the objects needing it.
A somewhat late reply, but for anyone else looking after the same kind of functionality I would recommend the open source git project prig: https://github.com/urasandesu/Prig It can fake static methods (they even have an example of faking and isolating DateTime.Now) and is an alternative to Microsoft Fakes, TypeMock and JustMock (all who cost money or require ultimate editions of visual studio to use).
Pose is an option. It's free full in memory
Https://GitHub.com/tonerdo/pose

MRUnit with HBase and MapReduce gets an error with serialization

I'm trying to test my MapReduce with MRUnit, when I do the integration test, it works. I have some unit test that I want to pass them as well.
My MRUnit driver and MapReduce class are:
MapDriver<ImmutableBytesWritable, Result, ImmutableBytesWritable, KeyValue>
public final class HashMapper extends
TableMapper<ImmutableBytesWritable, KeyValue>
When I define the input I get an error:
mapDriver.withInput(new ImmutableBytesWritable(Bytes
.toBytes("query")), new Result(kvs1));
java.lang.NullPointerException
at org.apache.hadoop.mrunit.internal.io.Serialization.copy(Serialization.java:73)
at org.apache.hadoop.mrunit.internal.io.Serialization.copy(Serialization.java:91)
at org.apache.hadoop.mrunit.internal.output.MockOutputCollector.collect(MockOutputCollector.java:48)
at org.apache.hadoop.mrunit.internal.mapreduce.AbstractMockContextWrapper$4.answer(AbstractMockContextWrapper.java:90)
at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationMatcher.java:29)
at org.mockito.internal.MockHandler.handle(MockHandler.java:95)
at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:47)
I guess that it's because it doesn't like the Result and KeyValue object since they're not Writable, but I don't undersntad why the integration tests works then. It was working before with Hbase 0.94 when all these objects implement Writable, now I'm working with HBase 0.96. Any clue how should I use MRUnit here?
With the version HBase 0.96 some classes aren't implement Writable anymore, but people from HBase have created a new serialize classes for them.
So, the solutions is to indicate in the Configuration what classes MRUnit must use:
The property is called io.serializations
The different serializes are:
Result class org.apache.hadoop.hbase.mapreduce.ResultSerialization
KeyValue class org.apache.hadoop.hbase.mapreduce.KeyValueSerialization
Put & Get classes org.apache.hadoop.hbase.mapreduce.MutationSerialization

Java 7: using URLClassLoader not working anymore

my problem is, that my code which was working on Java6 does not work any more.
Since my app needs to load jar's at runtime (plugins), i wrote myselt a simple class deriving from URLClassLoader like this
public class MyClassLoader extends java.net.URLClassLoader {
/** Creates a new instance of URLClassLoader */
public MyClassLoader(java.net.URL url)
{
super(new java.net.URL[]{url},ClassLoader.getSystemClassLoader());
}
public void addURL(java.net.URL url)
{
super.addURL(url);
}}
So if i want load a jar, i simply call addURL(pathToJar) and load that class via
Class.forName(myClass, true, myClassLoader)
This worked like a charm running on Java6.
Now i decided to make a self contained java app in Java7.
When I start the app, the jar's also get loaded at runtime, but if there's a class inside which derives from a class that is inside the classpath (not in the plugin jar) i get a ClassCastException.
So i guess something has changed in Java7.
At the moment I'm using Java7_u13 on OSX.
Can any one give me a hint on what I should do, to get the old behaviour back? Searching the net didn't get me any help yet.
Many thanks in advance.
Greetings, -chris-
Meanwhile i found the solution to my problem. I just used the 'wrong' classloader as the parent. Everything now works fine if i replace
super(new java.net.URL[]{url},ClassLoader.getSystemClassLoader());
with
super(new java.net.URL[]{url},MyClassLoader.class.getClassLoader());
Greetings, -chris-

Implicity usage of Java Custom class loaders?

I've written some customer class loader that load some classes from a certain directory (that is not in the classpath), say:
class FileSystemClassLoader extends Classloader{
// implementation details
}
I have some directory say /home/mhewedy/classes/ that is not in the classpath, this directory contains some classes that I use the previous classlaoder to load.
how to use this classloader from the my code to load classes "simplicity" without writing : such code:
Thread.currentThread().setContextClassLoader(new FileSystemClassLoader());
// some code here ...
ClassLoader contextCL = Thread.currentThread().getcontextClassLoader();
Update to respond to OP edits:
When the JVM loads a class, it will use the classloader that loaded the "current" class (per JVM spec). So if you're in method Foo.main(), which was loaded with your custom classloader, and you want to create an instance of Bar, also loaded via that classloader, you don't have to do anything special.
However, if your current method is Baz.main(), and it was loaded via the system classpath (specified with -cp on the command line), then you have to explicitly load that class via the classloader. There's no way around this. The "context classloader" is meant for application code, to load resources; the JVM ignores it.
In almost all cases, you're better off constructing a classpath that includes your special classes.