Skip to content

Instantly share code, notes, and snippets.

@tylertreat
Created December 31, 2014 21:32
Show Gist options
  • Select an option

  • Save tylertreat/4a346782f7c0190e47ba to your computer and use it in GitHub Desktop.

Select an option

Save tylertreat/4a346782f7c0190e47ba to your computer and use it in GitHub Desktop.
private List<Bean> getClientStubBeans() {
String workingDir = System.getProperty("user.dir");
File stubDir = new File(workingDir + File.separator + "bin-test" + File.separator + "com\\foo\\client\\stub");
if (!stubDir.exists() || !stubDir.isDirectory()) {
logger.error("Unable to load client stubs");
return new ArrayList<Bean>();
}
return buildBeanDefinitions(stubDir);
}
private List<Bean> buildBeanDefinitions(File stubDirectory) {
List<Bean> beanDefinitions = new ArrayList<Bean>();
for (File file : stubDirectory.listFiles()) {
if (!file.isFile() || !file.getName().endsWith(".class"))
continue;
String path = file.getPath();
String fileName = path.substring(path.indexOf("com\\foo"));
String className = fileName.replace('\\', '.').substring(0, fileName.indexOf(".class"));
Class<?> stubClass = loadClass(className);
if (stubClass == null)
continue;
// ClientStub annotation is used to mark stub components
ClientStub stub = stubClass.getAnnotation(ClientStub.class);
if (stub == null)
continue;
BeanDefinition definition = BeanDefinitionBuilder.rootBeanDefinition(stubClass.getName()).getBeanDefinition();
beanDefinitions.add(new Bean(stub.name(), definition));
}
return beanDefinitions;
}
private Class<?> loadClass(String name) {
try {
return Class.forName(name);
} catch (Throwable tr) {
logger.error("Unable to load client stub " + name, tr);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment