Adding C++ field instrumentation using config file
[iot2.git] / iotjava / iotruntime / master / CRuntimeInstrumenterMaster.java
1 package iotruntime.master;
2
3 // Java basic packages
4 import java.util.*;
5 import java.io.*;
6
7 /** Class CRuntimeInstrumenterMaster helps instrument C++ code.
8  *  This class basically reads a C++ config file that has information
9  *  about fields of IoTSet and IoTRelation.
10  *
11  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
12  * @version     1.0
13  * @since       2017-19-01
14  */
15 public class CRuntimeInstrumenterMaster {
16
17         /**
18          * CRuntimeInstrumenterMaster class constants
19          */
20         private static final String STR_IOT_SET_TYPE = "IoTSet";
21         private static final String STR_IOT_RELATION_TYPE = "IoTRelation";
22         private static final String STR_FIELD_NUMBER = "FIELD_NUMBER";
23         private static final String STR_FIELD = "FIELD_";
24         private static final String STR_FIELD_CLASS = "FIELD_CLASS_";
25         // For IoTRelation second object class
26         private static final String STR_FIELD_CLASS_REL = "FIELD_CLASS_REL_";
27         private static final String STR_FIELD_TYPE = "FIELD_TYPE_";
28         private static final String STR_CONFIG_EXTENSION = ".config";
29         private static final String STR_CONFIG_FILE_PATH = "mysql/";
30
31         /**
32          *  CRuntimeInstrumenterMaster class properties
33          */
34         private HashMap<String,Object> hmObj;
35         private String strObjectID;
36         private boolean bVerbose;
37         private String strObjectConfigFile;
38
39         /**
40          *  Constructor
41          */
42         public CRuntimeInstrumenterMaster(String strConfigFile, String strObjID, boolean _bVerbose) {
43
44                 hmObj = new HashMap<String,Object>();
45                 strObjectID = strObjID;
46                 bVerbose = _bVerbose;
47                 strObjectConfigFile = strConfigFile;
48         }
49
50
51         /**
52          * A method to parse information from a config file
53          *
54          * @param       strCfgFileName  Config file name
55          * @param       strCfgField             Config file field name
56          * @return      String
57          */
58         private String parseConfigFile(String strCfgFileName, String strCfgField) {
59                 // Parse configuration file
60                 Properties prop = new Properties();
61                 File file = new File(strCfgFileName);
62                 FileInputStream fis = null;
63                 try {
64                         fis = new FileInputStream(file);
65                         prop.load(fis);
66                         fis.close();
67                 } catch (IOException ex) {
68                         System.out.println("CRuntimeInstrumenterMaster: Error reading config file: " + strCfgFileName + 
69                                 ". Please make sure it contains field information!");
70                         ex.printStackTrace();
71                 }
72                 System.out.println("CRuntimeInstrumenterMaster: Reading " + strCfgField +
73                         " from config file: " + strCfgFileName + " with value: " + 
74                         prop.getProperty(strCfgField, null));
75                 // NULL is returned if the property isn't found
76                 return prop.getProperty(strCfgField, null);
77         }
78
79
80         /**
81          * A method to parse field information
82          *
83          * @return      void
84          */
85         private void getFieldInfo() {
86
87                 // Parse the config file and look for field information
88                 String strFieldNum = parseConfigFile(strObjectConfigFile, STR_FIELD_NUMBER);
89                 int iNumOfField = 0;
90                 if (strFieldNum != null)
91                         iNumOfField = Integer.parseInt(strFieldNum);
92                 else
93                         throw new Error("CRuntimeInstrumenterMaster: Number of fields information not found!");
94                 for (int iFieldCounter=0; iFieldCounter<iNumOfField; iFieldCounter++) {
95                         String strFieldKey = STR_FIELD + iFieldCounter; // Start from 0
96                         String strFieldClassKey = STR_FIELD_CLASS + iFieldCounter;
97                         String strFieldTypeKey = STR_FIELD_TYPE + iFieldCounter;
98                         String strField = parseConfigFile(strObjectConfigFile, strFieldKey);
99                         String strFieldClass = parseConfigFile(strObjectConfigFile, strFieldClassKey);
100                         String strFieldType = parseConfigFile(strObjectConfigFile, strFieldTypeKey);
101                         // Check if this is a Set class, then process it
102                         if (strFieldType.equals(STR_IOT_SET_TYPE)) {
103                                 RuntimeOutput.print("CRuntimeInstrumenterMaster: IoTSet is detected!", bVerbose);
104                                 SetInstrumenter setInstrument = new
105                                         SetInstrumenter(strFieldClass, STR_CONFIG_FILE_PATH + strField + STR_CONFIG_EXTENSION, strObjectID, bVerbose);
106                                 hmObj.put(strField, setInstrument);
107                         // Check if this is a Relation class, then process it
108                         } else if (strFieldType.equals(STR_IOT_RELATION_TYPE)) {
109                                 RuntimeOutput.print("CRuntimeInstrumenterMaster: IoTRelation is detected!", bVerbose);
110                                 String strFieldClassRelKey = STR_FIELD_CLASS_REL + iFieldCounter;
111                                 String strFieldClassRel = parseConfigFile(strObjectConfigFile, strFieldClassRelKey);
112                                 RelationInstrumenter relInstrument = new
113                                         RelationInstrumenter(strFieldClass, strFieldClassRel, STR_CONFIG_FILE_PATH + strField + STR_CONFIG_EXTENSION, bVerbose);
114                                 hmObj.put(strField, relInstrument);
115                         } else
116                                 throw new Error("CRuntimeInstrumenterMaster: " + strFieldType + " not recognized!");
117                 }
118         }
119
120
121         /**
122          * A method that returns HashMap hmObj
123          *
124          * @return         HashMap<String,Object>
125          */
126         public HashMap<String,Object> getFieldObjects() {
127
128                 getFieldInfo();
129                 return hmObj;
130         }
131
132 }
133
134