io.apptik.rhub.shield.ShieldMakerBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shield-base-maker Show documentation
Show all versions of shield-base-maker Show documentation
Reactive Event Hub connecting Publishers to Subscribers
The newest version!
package io.apptik.rhub.shield;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import io.apptik.rhub.RHub;
/**
* Base class for instantiating Hub Shield.
*
* Normally one will not use this directly but rather
* more specific helpers based on some concrete Reactive Framework.
* @param The specific type of the Hub
*/
public final class ShieldMakerBase {
/**
* Generates instance for a given shieldClass interface.
*
* @param shieldClass class type of the shield interface
* @param rHub instance of RHub to fit the shield on
* @param rHubClass class type of the RHub instance
* @param Shield interface type
* @return instance of the Shield
*/
public final T make(Class shieldClass, H rHub, Class rHubClass) {
Constructor constructor = getShieldImpl(shieldClass, rHubClass);
if (constructor == null) {
throw new RuntimeException("Unable to find implementation for " + shieldClass);
}
try {
return constructor.newInstance(rHub);
} catch (IllegalAccessException e) {
throw new RuntimeException("Unable to invoke " + constructor, e);
} catch (InstantiationException e) {
throw new RuntimeException("Unable to invoke " + constructor, e);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
if (cause instanceof Error) {
throw (Error) cause;
}
throw new RuntimeException("Unable to create shield instance.", cause);
}
}
//todo strict or not strict. if not remove Class rHubClass.
//at this moment we can trust on the shield implementations as they are generated by us.
private Constructor getShieldImpl(Class shieldClass, Class rHubClass) {
Constructor shieldConstructor = null;
String clsName = shieldClass.getName();
try {
Class> shieldImpl = Class.forName(clsName + "_Impl");
//noinspection unchecked
Constructor>[] constructors = shieldImpl.getDeclaredConstructors();
shieldConstructor = (Constructor) constructors[0];
// shieldConstructor = (Constructor) shieldImpl.getConstructor(rHubClass);
System.out.println("Shield Impl found for: " + shieldClass.getName());
} catch (ClassNotFoundException e) {
System.out.println("Shield Impl Not found for: " + shieldClass.getName());
}
// catch (NoSuchMethodException e) {
// throw new RuntimeException("Unable to find binding constructor for " + clsName, e);
// }
return shieldConstructor;
}
}