Last active
June 27, 2017 21:27
-
-
Save umeshdangat/a7a40a0f0b7818ba3053d90878cc1d2d to your computer and use it in GitHub Desktop.
Loading module in private classloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class ModuleLoader { | |
public synchronized void loadModule(){ | |
final Path modulePath = downloadModule(); //1. download the module.jar | |
createClassloaderAndLoadModule(modulePath); //2. create classloader and use that to load the jar | |
} | |
private void createClassloaderAndLoadModule(final Path modulePath){ | |
final URLClassLoader yelpySearchClassloader = new YelpSearchPrivateClassLoader( | |
new URL[]{modulePath.toUri().toURL()}, | |
this.getClass().getClassLoader() //3. Create URLClassloader | |
); | |
scorerFactory = Class.forName("com.yelp.search.module.YelpSearchScorerFactoryImpl", | |
true, | |
yelpySearchClassloader) | |
.asSubclass(ScorerFactory.class) | |
.getDeclaredConstructor(new Class[]{Environment.class}) | |
.newInstance(environment); //4. Create instance of ScorerFactory that return the Scorer | |
} | |
public Scorer createScorer(Map<String, Object> params) { | |
return scorerFactory.createScorer(params); //5. Scorer factory returning scorer, called once per query | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment