@@ -36,6 +36,7 @@ |
import org.codehaus.jettison.json.JSONObject; |
import org.glassfish.jersey.media.multipart.MultiPartFeature; |
import org.yaml.snakeyaml.Yaml; |
+import org.yaml.snakeyaml.scanner.ScannerException; |
|
/** |
* |
@@ -61,7 +62,6 @@ |
* @param ctx |
*/ |
void load(File file) { |
- LOG.info("Config File : " + file); |
if (file == null || !file.exists() || !file.canRead()) { |
return; |
} |
@@ -79,6 +79,10 @@ |
* @param ctx |
*/ |
void load(ServletContext ctx) { |
+ if (ctx == null) { |
+ LOG.log(Level.SEVERE, "No service context"); |
+ return; |
+ } |
Enumeration<String> params = ctx.getInitParameterNames(); |
while (params.hasMoreElements()) { |
String key = params.nextElement(); |
@@ -96,7 +100,8 @@ |
*/ |
void load(ServletContext ctx, String file) { |
if (ctx == null) { |
- throw new NullPointerException("ctx"); |
+ LOG.log(Level.SEVERE, "No service context for " + file); |
+ return; |
} |
if (file.startsWith("/")) { |
// this is a file within the context. /META-INF/config.json |
@@ -142,7 +147,7 @@ |
/** |
* Filters the services as specified in config files. |
* @param c |
- * @param orig |
+ * @param orig |
*/ |
public static void filterServices(ServletContext c, Set<Class<?>> orig) { |
try { |
@@ -151,7 +156,7 @@ |
cc.loadServices(c); |
|
if (cc.getServices().isEmpty()) { |
- // nothing to alter! |
+ // nothing to do. |
return; |
} |
|
@@ -178,9 +183,9 @@ |
|
for (String s : cc.getServices()) { |
// allow for comment lines and empty lines (properties file) |
- if (s != null && !s.isEmpty() && !s.trim().startsWith("#")) { |
+ if (s != null && !s.isEmpty()) { |
String service = s.trim(); |
- // be forgiving |
+ // be more forgiving |
if (service.startsWith("/")) { |
service = service.substring(1); |
} |
@@ -188,7 +193,8 @@ |
if (cl != null) { |
// you can only request model and data services to be added. |
// using package conventions |
- if (cl.getCanonicalName().startsWith("m.") || cl.getCanonicalName().startsWith("d.")) { |
+ if (cl.getCanonicalName().startsWith("m.") |
+ || cl.getCanonicalName().startsWith("d.")) { |
filtered_resources.add(cl); |
} |
} else { |
@@ -222,8 +228,10 @@ |
default: |
LOG.warning("Illegal configuration file: " + file); |
} |
- } catch (IOException | JSONException ex) { |
- LOG.log(Level.WARNING, null, ex); |
+ } catch (ScannerException E) { |
+ LOG.log(Level.WARNING, "Ignoring config, Error:\n" + E.getMessage()); |
+ } catch (IOException | JSONException E) { |
+ LOG.log(Level.WARNING, "Ignoring config, Error:\n" + E); |
} finally { |
try { |
is.close(); |
@@ -243,28 +251,36 @@ |
private void fromProperties(InputStream is) throws IOException { |
Properties p = new Properties(); |
p.load(is); |
- for (String key : p.stringPropertyNames()) { |
+ if (p.isEmpty()) { |
+ return; |
+ } |
+ p.stringPropertyNames().forEach((key) -> { |
conf.put(key, p.getProperty(key)); |
- } |
+ }); |
} |
|
|
private void fromYaml(InputStream is) { |
- Yaml y = new Yaml(); |
- Map<String, Object> o = (Map<String, Object>) y.load(is); |
- String apiversion = (String) o.get(KEY_API); |
+ Map<String, Object> yaml = (Map) new Yaml().load(is); |
+ if (!yaml.containsKey(KEY_API)) { |
+ throw new RuntimeException("Missing Api version: 'Api: " + API_VERSION + "', ignoring."); |
+ } |
+ String apiversion = (String) yaml.get(KEY_API); |
if (!apiversion.equals(API_VERSION)) { |
- throw new RuntimeException("Invalid Api version: " + apiversion); |
+ throw new RuntimeException("Invalid Api version: " + apiversion + " ignoring."); |
} |
- Map<String, String> c = (Map) o.get(KEY_CONFIG); |
- for (String key : c.keySet()) { |
+ if (!yaml.containsKey(KEY_CONFIG)) { |
+ throw new RuntimeException("Missing: 'Config: ...', ignoring."); |
+ } |
+ Map<String, String> c = (Map) yaml.get(KEY_CONFIG); |
+ c.keySet().forEach((key) -> { |
conf.put(key, c.get(key)); |
- } |
- if (o.containsKey(KEY_SERVICES)) { |
- List s = (List) o.get(KEY_SERVICES); |
- for (Object s1 : s) { |
- services.add(s1.toString()); |
- } |
+ }); |
+ if (yaml.containsKey(KEY_SERVICES)) { |
+ List<String> s = (List) yaml.get(KEY_SERVICES); |
+ s.forEach((s1) -> { |
+ services.add(s1); |
+ }); |
} |
} |
|