Preparing for callback testing (creating new policy files); Adding error messsages...
[iot2.git] / iotjava / iotpolicy / IoTCompiler.java
index 651a4b8cda51d19214416a84c679631052cb552d..5af3c5ea6c98860e979ed268aa4a41fcb601dcce 100644 (file)
@@ -387,7 +387,7 @@ public class IoTCompiler {
                Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
-                       int newObjectId = mapNewIntfaceObjId.get(newIntface);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("private final static int object" + newObjectId + "Id = " + 
                                newObjectId + ";\t//" + newIntface);
                        Set<String> methodIds = intMeth.getValue();
@@ -446,7 +446,7 @@ public class IoTCompiler {
                Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
-                       int newObjectId = mapNewIntfaceObjId.get(newIntface);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("set" + newObjectId + "Allowed = Arrays.asList(object" + newObjectId +"Permission);");
                }
        }
@@ -482,7 +482,7 @@ public class IoTCompiler {
                Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
-                       int newObjectId = mapNewIntfaceObjId.get(newIntface);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
                        println("throw new Error(\"Callback object for " + intface + " is not allowed to access method: \" + methodId);");
                        println("}");
@@ -1984,7 +1984,7 @@ public class IoTCompiler {
                Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
-                       int newObjectId = mapNewIntfaceObjId.get(newIntface);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("if (_objectId == object" + newObjectId + "Id) {");
                        println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
                        println("throw new Error(\"Object with object Id: \" + _objectId + \"  is not allowed to access method: \" + methodId);");
@@ -2971,7 +2971,7 @@ public class IoTCompiler {
                Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
-                       int newObjectId = mapNewIntfaceObjId.get(newIntface);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("const static int object" + newObjectId + "Id = " + newObjectId + ";");
                        println("const static set<int> set" + newObjectId + "Allowed;");
                }
@@ -3066,7 +3066,7 @@ public class IoTCompiler {
                Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
-                       int newObjectId = mapNewIntfaceObjId.get(newIntface);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {");
                        println("cerr << \"Callback object for " + intface + " is not allowed to access method: \" << methodId;");
                        println("exit(-1);");
@@ -3299,7 +3299,7 @@ public class IoTCompiler {
                Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
-                       int newObjectId = mapNewIntfaceObjId.get(newIntface);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        print("const set<int> " + newSkelClass + "::set" + newObjectId + "Allowed {");
                        Set<String> methodIds = intMeth.getValue();
                        int i = 0;
@@ -4007,7 +4007,7 @@ public class IoTCompiler {
                Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
-                       int newObjectId = mapNewIntfaceObjId.get(newIntface);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("if (_objectId == object" + newObjectId + "Id) {");
                        println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {");
                        println("cerr << \"Object with object Id: \" << _objectId << \"  is not allowed to access method: \" << methodId << endl;");
@@ -4420,7 +4420,7 @@ public class IoTCompiler {
                        pn = (ParseNode) parse.parse().value;
                } catch (Exception e) {
                        e.printStackTrace();
-                       throw new Error("IoTCompiler: ERROR parsing policy file or wrong command line option: " + file);
+                       throw new Error("IoTCompiler: ERROR parsing policy file or wrong command line option: " + file + "\n");
                }
 
                return pn;
@@ -4530,6 +4530,21 @@ public class IoTCompiler {
        }
 
 
+       // Check and find object Id for new interface in mapNewIntfaceObjId (callbacks)
+       // Throw an error if the new interface is not found!
+       // Basically the compiler needs to parse the policy (and requires) files for callback class first
+       private int getNewIntfaceObjectId(String newIntface) {
+
+               if (!mapNewIntfaceObjId.containsKey(newIntface)) {
+                       throw new Error("IoTCompiler: Need to parse policy and requires files for callback class first! " +
+                                                       "Please place the two files for callback class in front...\n");
+               } else {
+                       int retObjId = mapNewIntfaceObjId.get(newIntface);
+                       return retObjId;
+               }
+       }
+
+
        // Return parameter category, i.e. PRIMITIVES, NONPRIMITIVES, USERDEFINED, ENUM, or STRUCT
        private ParamCategory getParamCategory(String paramType) {
 
@@ -5117,7 +5132,7 @@ public class IoTCompiler {
                        } else {
                                throw new Error("IoTCompiler: Ambiguous stub interfaces: " + setExchInt.toString() + 
                                        ". Only one new interface can be declared if the object " + intface +
-                                       " needs to be passed in as an input parameter!");
+                                       " needs to be passed in as an input parameter!\n");
                        }
                } else {
                // NULL value - this means policy files missing
@@ -5126,7 +5141,7 @@ public class IoTCompiler {
                                " If this is an array please type the brackets after the variable name," +
                                " e.g. \"String str[]\", not \"String[] str\"." +
                                " If this is a Collections (Java) / STL (C++) type, this compiler only" +
-                               " supports List/ArrayList (Java) or list (C++).");
+                               " supports List/ArrayList (Java) or list (C++).\n");
                }
        }
 
@@ -5185,12 +5200,12 @@ public class IoTCompiler {
                                        // Error checking
                                        if (!args[i].equals("-java") &&
                                                !args[i].equals("-cplus")) {
-                                               throw new Error("IoTCompiler: ERROR - unrecognized command line option: " + args[i]);
+                                               throw new Error("IoTCompiler: ERROR - unrecognized command line option: " + args[i] + "\n");
                                        } else {
                                                if (i + 1 < args.length) {
                                                        comp.setDirectory(args[i+1]);
                                                } else
-                                                       throw new Error("IoTCompiler: ERROR - please provide <directory> after option: " + args[i]);
+                                                       throw new Error("IoTCompiler: ERROR - please provide <directory> after option: " + args[i] + "\n");
 
                                                if (args[i].equals("-java")) {
                                                        comp.generateEnumJava();
@@ -5218,7 +5233,7 @@ public class IoTCompiler {
                } else {
                // Need to at least have exactly 2 parameters, i.e. main policy file and requires file
                        IoTCompiler.printUsage();
-                       throw new Error("IoTCompiler: At least two arguments (main and requires policy files) have to be provided!");
+                       throw new Error("IoTCompiler: At least two arguments (main and requires policy files) have to be provided!\n");
                }
        }
 }