use of voldemort.server.ServerState in project voldemort by voldemort.
the class MetadataStore method readFromDirectory.
public static MetadataStore readFromDirectory(File dir) {
if (!Utils.isReadableDir(dir))
throw new IllegalArgumentException("Metadata directory " + dir.getAbsolutePath() + " does not exist or can not be read.");
String storeDefDirPath = dir.getAbsolutePath() + File.separator + MetadataStore.STORE_DEFINITIONS_STORE_NAME;
// If config directory does not contain STORES sub directory, then
// create one by parsing the stores.xml file
List<String> configurationFiles = Arrays.asList(dir.list());
if (configurationFiles == null)
throw new IllegalArgumentException("No configuration found in " + dir.getAbsolutePath() + ".");
if (!configurationFiles.contains(STORE_DEFINITIONS_STORE_NAME)) {
// parse stores.xml and create STORES sub-dir
StoreDefinitionsMapper mapper = new StoreDefinitionsMapper();
List<StoreDefinition> storeDefinitions = null;
try {
storeDefinitions = mapper.readStoreList(new File(dir.getAbsolutePath() + File.separator + STORES_KEY));
} catch (IOException e) {
throw new VoldemortException("Cannot parse the store definitions from " + STORES_KEY + " file ", e);
}
if (storeDefinitions == null) {
throw new VoldemortException("Neither STORES nor stores.xml exist in the config directory");
}
// Create the STORES sub directory
File storeDefinitionsDir = new File(storeDefDirPath);
if (!storeDefinitionsDir.mkdir()) {
throw new VoldemortException("Unable to create " + STORE_DEFINITIONS_STORE_NAME + " sub directory");
}
for (StoreDefinition storeDef : storeDefinitions) {
try {
FileUtils.writeStringToFile(new File(storeDefDirPath + File.separator + storeDef.getName()), mapper.writeStore(storeDef));
} catch (IOException e) {
throw new VoldemortException("Cannot write store definition to file: " + storeDef.getName(), e);
}
}
}
// Create a STORES configuration engine for STORES sub-directory
StorageEngine<String, String, String> storesEngine = new ConfigurationStorageEngine(MetadataStore.STORE_DEFINITIONS_STORE_NAME, storeDefDirPath);
Store<String, String, String> innerStore = new ConfigurationStorageEngine(MetadataStore.METADATA_STORE_NAME, dir.getAbsolutePath());
MetadataStore store = new MetadataStore(innerStore, storesEngine);
ServerState state = new ServerState(store);
JmxUtils.registerMbean(state.getClass().getCanonicalName(), state);
return store;
}
Aggregations