1616
1717package org .ros .helpers ;
1818
19+ import com .google .common .base .Preconditions ;
20+
1921import org .apache .commons .logging .Log ;
2022import org .ros .namespace .GraphName ;
2123import org .ros .node .AbstractNodeMain ;
@@ -47,36 +49,39 @@ public class ParameterLoaderNode extends AbstractNodeMain {
4749 * Default constructor
4850 * @param resources Array of resources with their respective namespace to load.
4951 */
50- public ParameterLoaderNode (ArrayList <Resource > resources ) {
52+ public ParameterLoaderNode (List <Resource > resources ) {
53+ Preconditions .checkNotNull (resources );
5154 for (Resource r : resources ) {
55+ Preconditions .checkNotNull (r .inputStream );
5256 addSingleYmlInput (r .inputStream , r .namespace == null ? "" : r .namespace );
5357 }
5458 }
5559
5660 private void addSingleYmlInput (InputStream ymlInputStream , String namespace ) {
57- this .params .add (new LoadedResource ((new Yaml ()).load (ymlInputStream ), namespace ));
61+ Object loadedYaml = new Yaml ().load (ymlInputStream );
62+ if (loadedYaml != null && loadedYaml instanceof Map <?, ?>) {
63+ this .params .add (new LoadedResource (Map .class .cast (loadedYaml ), namespace ));
64+ }
5865 }
5966
60- @ SuppressWarnings ("unchecked" )
61- private void addParams (ParameterTree parameterTree , String namespace , Map <String , Object > params ) {
62- for (Map .Entry <String , Object > e : params .entrySet ()) {
63- String fullKeyName = namespace + "/" + e .getKey ();
67+ private void addParams (ParameterTree parameterTree , String namespace , Map <?, ?> params ) {
68+ for (Map .Entry <?, ?> e : params .entrySet ()) {
69+ String fullKeyName = namespace + "/" + e .getKey ().toString ();
6470 if (log != null ) {
65- log .info ("Loading parameter " + fullKeyName + " \n Value = " + e .getValue ());
71+ log .debug ("Loading parameter " + fullKeyName + " \n Value = " + e .getValue ());
6672 }
67-
6873 if (e .getValue () instanceof String ) {
69- parameterTree .set (fullKeyName , ( String ) e .getValue ());
74+ parameterTree .set (fullKeyName , String . class . cast ( e .getValue () ));
7075 } else if (e .getValue () instanceof Integer ) {
71- parameterTree .set (fullKeyName , ( Integer ) e .getValue ());
76+ parameterTree .set (fullKeyName , Integer . class . cast ( e .getValue () ));
7277 } else if (e .getValue () instanceof Double ) {
73- parameterTree .set (fullKeyName , ( Double ) e .getValue ());
78+ parameterTree .set (fullKeyName , Double . class . cast ( e .getValue () ));
7479 } else if (e .getValue () instanceof Map ) {
75- parameterTree .set (fullKeyName , ( Map ) e .getValue ());
80+ parameterTree .set (fullKeyName , Map . class . cast ( e .getValue () ));
7681 } else if (e .getValue () instanceof Boolean ) {
77- parameterTree .set (fullKeyName , ( Boolean ) e .getValue ());
82+ parameterTree .set (fullKeyName , Boolean . class . cast ( e .getValue () ));
7883 } else if (e .getValue () instanceof List ) {
79- parameterTree .set (fullKeyName , ( List ) e .getValue ());
84+ parameterTree .set (fullKeyName , List . class . cast ( e .getValue () ));
8085 } else if (log != null ) {
8186 log .debug ("I don't know what type parameter " + fullKeyName + " is. Value = " + e .getValue ());
8287 log .debug ("Class name is: " + e .getValue ().getClass ().getName ());
@@ -95,18 +100,16 @@ public GraphName getDefaultNodeName() {
95100
96101 @ Override
97102 public void onStart (ConnectedNode connectedNode ) {
98- if (params != null ) {
99- ParameterTree parameterTree = connectedNode .getParameterTree ();
100- log = connectedNode .getLog ();
101-
102- // TODO: For some reason, setting the / param when using a rosjava master doesn't work
103- // It does work fine with an external master, and also setting other params of any type
104- for (LoadedResource r : params ) {
105- addParams (parameterTree , r .namespace , r .resource );
106- }
103+ ParameterTree parameterTree = connectedNode .getParameterTree ();
104+ log = connectedNode .getLog ();
107105
108- connectedNode .shutdown ();
106+ // TODO: For some reason, setting the / param when using a rosjava master doesn't work
107+ // It does work fine with an external master, and also setting other params of any type
108+ for (LoadedResource r : params ) {
109+ addParams (parameterTree , r .namespace , r .resource );
109110 }
111+
112+ connectedNode .shutdown ();
110113 }
111114
112115 /**
@@ -128,11 +131,11 @@ public Resource(InputStream inputStream, String namespace) {
128131 * keep the code simple.
129132 */
130133 private class LoadedResource {
131- public Map <String , Object > resource ;
132- public String namespace ;
134+ private Map <?, ? > resource ;
135+ private String namespace ;
133136
134- LoadedResource (Object resource , String namespace ) {
135- this .resource = ( Map < String , Object >) resource ;
137+ LoadedResource (Map resource , String namespace ) {
138+ this .resource = resource ;
136139 this .namespace = namespace ;
137140 }
138141 }
0 commit comments