Skip to content

Instantly share code, notes, and snippets.

@AnthonyClink
Created April 25, 2014 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnthonyClink/11275661 to your computer and use it in GitHub Desktop.
Save AnthonyClink/11275661 to your computer and use it in GitHub Desktop.
A usage of the data library
package com.clinkworks.neptical;
import java.io.File;
import java.util.Map;
import com.clinkworks.neptical.data.file.FileData;
import com.clinkworks.neptical.data.loaders.json.JsonDataLoader;
import com.clinkworks.neptical.util.PathUtil;
import com.google.common.collect.Maps;
public class DataRegistry {
//TODO: find a way not to hard code the loaders
private static final Map<String, DataLoader> registeredDataLoaders;
static{
registeredDataLoaders = Maps.newConcurrentMap();
registeredDataLoaders.put("json", new JsonDataLoader());
}
/**
****************************
This is the method in question, it is in a jar and treated as an entry point for external programs to utilize.
When used directly within the data application (in the jar as it were), the proper resources directory is returned.
When used outside the application such as an application importing the jar, the jars data directory is returned instead of the programs.
I am expecting the context class loader to give the current context (an application using the jar) it's resource directories data directory.
It seems I ahve a fundmental misunderstanding of how the classloaders work with the file system. Can someone shed some light on this?
**/
public static final Data loadData(){
String resourceName = Thread.currentThread().getContextClassLoader().getResource("data").getFile().replace("%20", " ");
File file = new File(resourceName);
return loadData("", "", null, null, file);
}
public static final Data loadData(String segment, String path, Data root, Data parent, File file){
if(file.isDirectory()){
return new FileData(segment, path, root, parent, file);
}
String extension = PathUtil.lastSegment(file.getName()).toLowerCase();
DataLoader dataLoader = registeredDataLoaders.get(extension);
if(dataLoader == null){
throw new IllegalStateException("No data loader has been registered for the extension: " + extension);
}
return dataLoader.loadData(segment, path, root, parent, file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment