Skip to content

Commit

Permalink
throw error if detect multiple defaults.yaml or storm.yaml files of t…
Browse files Browse the repository at this point in the history
…he same name
  • Loading branch information
Nathan Marz committed Oct 20, 2011
1 parent c50f960 commit bc9a360
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/jvm/backtype/storm/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -84,16 +86,24 @@ public static Map readYamlConfig(String path) {
}

public static Map findAndReadConfigFile(String name, boolean mustExist) {
// It's important to use Utils.class here instead of Object.class here.
// If Object.class is used, sbt can't find defaults.yaml
InputStream is = Utils.class.getResourceAsStream("/" + name);
if(is==null) {
if(mustExist) throw new RuntimeException("Could not find config file on classpath " + name);
else return new HashMap();
try {
Enumeration resources = Thread.currentThread().getContextClassLoader().getResources(name);
if(!resources.hasMoreElements()) {
if(mustExist) throw new RuntimeException("Could not find config file on classpath " + name);
else return new HashMap();
}
URL resource = (URL) resources.nextElement();
Map ret = (Map) YAML.load(new InputStreamReader(resource.openStream()));
if(ret==null) ret = new HashMap();

if(resources.hasMoreElements()) {
throw new RuntimeException("Found multiple " + name + " resources");
}
return new HashMap(ret);

} catch (IOException e) {
throw new RuntimeException(e);
}
Map ret = (Map) YAML.load(new InputStreamReader(is));
if(ret==null) ret = new HashMap();
return new HashMap(ret);
}

public static Map findAndReadConfigFile(String name) {
Expand Down

0 comments on commit bc9a360

Please sign in to comment.