Prevent and Klockwork fixes.
authorspurthi.segu <spurthi.segu@samsung.com>
Thu, 1 Oct 2015 11:56:30 +0000 (17:26 +0530)
committerUze Choi <uzchoi@samsung.com>
Fri, 2 Oct 2015 02:17:42 +0000 (02:17 +0000)
Change-Id: I7da253ecb1b8ce7e0f7a0dbf4bb1e00d3d97e89e
Signed-off-by: spurthi.segu <spurthi.segu@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3371
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/simulator/examples/server/service_provider.cpp
service/simulator/java/eclipse-plugin/ClientControllerPlugin/src/oic/simulator/clientcontroller/manager/ResourceManager.java
service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java
service/simulator/java/jni/simulator_manager_jni.cpp
service/simulator/java/sdk/src/org/oic/simulator/SimulatorResourceModel.java
service/simulator/java/sdk/src/org/oic/simulator/clientcontroller/SimulatorRemoteResource.java
service/simulator/java/sdk/src/org/oic/simulator/serviceprovider/SimulatorResourceServer.java
service/simulator/src/client-controller/simulator_remote_resource_impl.cpp
service/simulator/src/common/simulator_logger.cpp
service/simulator/src/service-provider/simulator_resource_creator.cpp
service/simulator/src/service-provider/simulator_resource_server_impl.cpp

index 74e5bd0..28512a8 100644 (file)
@@ -26,7 +26,7 @@ class AppLogger : public ILogger
         void write(std::string time, ILogger::Level level, std::string message)
         {
             std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " "
-                    << message;
+                      << message;
         }
 };
 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
@@ -52,7 +52,17 @@ class SimLightResource
                 {
                     case 1 : simulateResource(configPath); break;
                     case 2 : displayResource(); break;
-                    case 3 : deleteResource(); break;
+                    case 3 :
+                        try
+                        {
+                            deleteResource();
+                        }
+                        catch (InvalidArgsException &e)
+                        {
+                            std::cout << "InvalidArgsException occured [code : " << e.code() <<
+                                      " Detail: " << e.what() << "]" << std::endl;
+                        }
+                        break;
                     case 4 : updateAttributePower(); break;
                     case 5 : updateAttributeIntensity(); break;
                     case 6 : automateResourceUpdate(); break;
@@ -114,12 +124,12 @@ class SimLightResource
                                     const SimulatorResourceModel &resModel)
         {
             std::cout << "[callback] Resource model is changed URI: " << uri.c_str()
-                    << " Count : " << resModel.size() << std::endl;
+                      << " Count : " << resModel.size() << std::endl;
             std::cout << "#### Modified attributes are ####" << std::endl;
             for (auto & attribute : resModel.getAttributes())
             {
                 std::cout << attribute.second.getName() << " :  "
-                        << attribute.second.valueToString().c_str() << std::endl;
+                          << attribute.second.valueToString().c_str() << std::endl;
             }
             std::cout << "########################" << std::endl;
         }
@@ -133,20 +143,20 @@ class SimLightResource
             try
             {
                 SimulatorResourceServerSP resource =
-                        SimulatorManager::getInstance()->createResource(configPath, callback);
+                    SimulatorManager::getInstance()->createResource(configPath, callback);
                 m_resources.push_back(resource);
                 std::cout << "Resource created successfully! URI= " << resource->getURI().c_str()
-                        << std::endl;
+                          << std::endl;
             }
             catch (InvalidArgsException &e)
             {
                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
-                        << e.what() << "]" << std::endl;
+                          << e.what() << "]" << std::endl;
             }
             catch (SimulatorException &e)
             {
                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: "
-                        << e.what() << "]" << std::endl;
+                          << e.what() << "]" << std::endl;
             }
         }
 
@@ -194,7 +204,7 @@ class SimLightResource
                         {
                             SimulatorManager::getInstance()->deleteResource(resourceType);
                             std::cout << "Resources of type \"" << resourceType << "\"" <<
-                                    " deleted successfully! " << std::endl;
+                                      " deleted successfully! " << std::endl;
                             std::vector<SimulatorResourceServerSP>::iterator ite = m_resources.begin();
                             while (ite != m_resources.end())
                             {
@@ -209,12 +219,12 @@ class SimLightResource
                         catch (InvalidArgsException &e)
                         {
                             std::cout << "InvalidArgsException occured [code : " << e.code()
-                                    << " Detail: " << e.what() << "]" << std::endl;
+                                      << " Detail: " << e.what() << "]" << std::endl;
                         }
                         catch (SimulatorException &e)
                         {
                             std::cout << "SimulatorException occured [code : " << e.code()
-                                    << " Detail: " << e.what() << "]" << std::endl;
+                                      << " Detail: " << e.what() << "]" << std::endl;
                         }
                     } break;
 
@@ -247,7 +257,7 @@ class SimLightResource
             }
 
             std::cout << "Setting the new values from allowed values list to power attribute" <<
-                    std::endl;
+                      std::endl;
             // Update all possible values from allowed values
             for (int index = 0; index < allowedValuesSize; index++)
             {
@@ -293,7 +303,7 @@ class SimLightResource
             }
 
             std::cout << "Setting the new values from allowed values list to intensity attribute"
-                    << std::endl;
+                      << std::endl;
             // Update all possible values from allowed values
             for (int index = min; index <= max; index++)
             {
@@ -340,7 +350,7 @@ class SimLightResource
             // Attributes
             SimulatorResourceModel resModel = resource->getModel();
             std::map<std::string, SimulatorResourceModel::Attribute> attributes =
-                    resModel.getAttributes();
+                resModel.getAttributes();
             std::cout << "##### Attributes [" << attributes.size() << "]" << std::endl;
             for (auto & attribute : attributes)
             {
@@ -364,7 +374,7 @@ class SimLightResource
                                          const int id)
         {
             std::cout << "Update automation is completed [URI: " << uri.c_str()
-                    << "  AutomationID: " << id << "] ###" << std::endl;
+                      << "  AutomationID: " << id << "] ###" << std::endl;
         }
 
         void automateResourceUpdate()
@@ -384,14 +394,14 @@ class SimLightResource
             {
                 int id = m_resources[index - 1]->startUpdateAutomation(type,
                          std::bind(&SimLightResource::onUpdateAutomationCompleted, this,
-                                 std::placeholders::_1, std::placeholders::_2));
+                                   std::placeholders::_1, std::placeholders::_2));
 
                 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
             }
             catch (SimulatorException &e)
             {
                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
-                        e.what() << "]" << std::endl;
+                          e.what() << "]" << std::endl;
             }
         }
 
@@ -404,7 +414,7 @@ class SimLightResource
             SimulatorResourceServerSP resource = m_resources[index - 1];
             SimulatorResourceModel resModel = resource->getModel();
             std::map<std::string, SimulatorResourceModel::Attribute> attributes =
-                    resModel.getAttributes();
+                resModel.getAttributes();
             int size = 0;
             for (auto & attribute : attributes)
             {
@@ -419,7 +429,7 @@ class SimLightResource
 
             int choice = -1;
             std::cout << "Select the attribute which you want to automate for updation: " <<
-                    std::endl;
+                      std::endl;
             std::cin >> choice;
             if (choice < 0 || choice > size)
             {
@@ -447,20 +457,20 @@ class SimLightResource
                 type = AutomationType::RECURRENT;
 
             std::cout << "Requesting attribute automation for " << attributeName.c_str() <<
-                    std::endl;
+                      std::endl;
 
             try
             {
 
                 int id = resource->startUpdateAutomation(attributeName, type,
                          std::bind(&SimLightResource::onUpdateAutomationCompleted, this,
-                                std::placeholders::_1, std::placeholders::_2));
+                                   std::placeholders::_1, std::placeholders::_2));
                 std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
             }
             catch (SimulatorException &e)
             {
                 std::cout << "SimulatorException occured [Error: " << e.code() << " Details: " <<
-                        e.what() << "]" << std::endl;
+                          e.what() << "]" << std::endl;
             }
         }
 
@@ -484,7 +494,7 @@ class SimLightResource
             if (!ids.size())
             {
                 std::cout << "No automation operation is going on this resource right now!" <<
-                        std::endl;
+                          std::endl;
                 return;
             }
 
@@ -504,7 +514,7 @@ class SimLightResource
             std::ostringstream out;
             out << "ID:  " << (int) observerInfo.id << std::endl;
             out << " [address: " << observerInfo.address << " port: " << observerInfo.port
-                    << "]" << std::endl;
+                << "]" << std::endl;
             std::cout << out.str();
         }
 
@@ -527,7 +537,7 @@ class SimLightResource
             for (auto & observerInfo : observersList)
             {
                 std::cout << " ID :  " << (int) observerInfo.id << " [address: " <<
-                        observerInfo.address << " port: " << observerInfo.port << "]" << std::endl;
+                          observerInfo.address << " port: " << observerInfo.port << "]" << std::endl;
             }
             std::cout << "########################" << std::endl;
         }
index c7d225f..565a9ae 100644 (file)
@@ -397,7 +397,7 @@ public class ResourceManager {
         // TODO: For debugging
         if (null != attributeMap) {
             RemoteResourceAttribute.printAttributes(attributeMap);
-            System.out.println("Attributes found: " + (null != attributeMap));
+            System.out.println("Attributes found: ");
             System.out.println("No of attributes: " + attributeMap.size());
 
             resource.setResourceAttributesMap(attributeMap);
@@ -581,7 +581,7 @@ public class ResourceManager {
     }
 
     public boolean isResourceObserved(String resourceURI) {
-        boolean observed = false;
+        boolean observed;
         synchronized (observedResourceURIList) {
             observed = observedResourceURIList.contains(resourceURI);
         }
index d52a1e2..febfbb6 100644 (file)
@@ -482,7 +482,7 @@ public class ResourceManager {
     }
 
     public boolean isDisplayName(String displayName) {
-        boolean exist = false;
+        boolean exist;
         synchronized (displayNameMap) {
             exist = displayNameMap.containsKey(displayName);
         }
@@ -490,7 +490,7 @@ public class ResourceManager {
     }
 
     public String getCompleteUriFromDisplayName(String displayName) {
-        String completeURI = null;
+        String completeURI;
         synchronized (displayNameMap) {
             completeURI = displayNameMap.get(displayName);
         }
@@ -566,7 +566,7 @@ public class ResourceManager {
             @Override
             public void run() {
                 Map<String, SimulatorResource> resourceTypeMap;
-                SimulatorResourceServer[] simulatorResourceServers = null;
+                SimulatorResourceServer[] simulatorResourceServers;
                 try {
                     simulatorResourceServers = SimulatorManager.createResource(
                             configFilePath, noOfInstances,
@@ -1122,12 +1122,12 @@ public class ResourceManager {
     }
 
     public List<String> getURIList() {
-        List<String> list = null;
+        List<String> list;
         synchronized (orderedResourceUriMap) {
             Set<String> typeSet = orderedResourceUriMap.keySet();
             List<String> typeList = Utility.convertSetToList(typeSet);
             if (null == typeList || typeList.size() < 1) {
-                return list;
+                return null;
             }
             list = new ArrayList<String>();
 
@@ -1240,18 +1240,18 @@ public class ResourceManager {
                         int val;
                         try {
                             val = Integer.parseInt(value);
+                            server.updateAttributeInteger(attributeName, val);
                         } catch (NumberFormatException nfe) {
                             return;
                         }
-                        server.updateAttributeInteger(attributeName, val);
                     } else if (baseType == Type.DOUBLE) {
                         double val;
                         try {
                             val = Double.parseDouble(value);
+                            server.updateAttributeDouble(attributeName, val);
                         } catch (NumberFormatException nfe) {
                             return;
                         }
-                        server.updateAttributeDouble(attributeName, val);
                     } else if (baseType == Type.BOOL) {
                         boolean val;
                         val = Boolean.parseBoolean(value);
index 0cb432e..a95e7b8 100644 (file)
@@ -217,6 +217,7 @@ class JNIFoundResourceListener
                 return;
             }
 
+            delete jniSimulatorResource;
             releaseEnv();
         }
 
@@ -560,6 +561,11 @@ Java_org_oic_simulator_SimulatorManagerNativeInterface_setDeviceInfo
     {
         throwSimulatorException(env, e.code(), e.what());
     }
+    catch (...)
+    {
+        throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
+        return;
+    }
 
     env->ReleaseStringUTFChars(deviceInfo, deviceName);
 }
@@ -593,6 +599,11 @@ Java_org_oic_simulator_SimulatorManagerNativeInterface_getDeviceInfo
     {
         throwSimulatorException(env, e.code(), e.what());
     }
+    catch (...)
+    {
+        throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
+        return;
+    }
 }
 
 JNIEXPORT void JNICALL
@@ -615,6 +626,11 @@ Java_org_oic_simulator_SimulatorManagerNativeInterface_setPlatformInfo
     {
         throwSimulatorException(env, e.code(), e.what());
     }
+    catch (...)
+    {
+        throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
+        return;
+    }
 }
 
 JNIEXPORT void JNICALL
@@ -646,6 +662,11 @@ Java_org_oic_simulator_SimulatorManagerNativeInterface_getPlatformInfo
     {
         throwSimulatorException(env, e.code(), e.what());
     }
+    catch (...)
+    {
+        throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
+        return;
+    }
 }
 
 static bool getClassRef(JNIEnv *env, const char *className, jclass &classRef)
index 4186c6e..a276e0f 100644 (file)
@@ -160,7 +160,6 @@ public class SimulatorResourceModel {
         } catch(Throwable t){
             throw t;
         } finally{
-            System.out.println("Calling finalize of Super Class");
             super.finalize();
         }
     }
index b0b7ee2..c35e1fd 100644 (file)
@@ -368,9 +368,13 @@ public class SimulatorRemoteResource {
 
     @Override
     protected void finalize() throws Throwable {
-        super.finalize();
-
-        dispose();
+        try {
+            dispose();
+        } catch(Throwable t){
+            throw t;
+        } finally{
+            super.finalize();
+        }
     }
 
     private native void dispose();
index 78cc281..0d9accd 100644 (file)
@@ -477,7 +477,6 @@ public class SimulatorResourceServer {
         } catch(Throwable t){
             throw t;
         } finally{
-            System.out.println("Calling finalize of Super Class");
             super.finalize();
         }
     }
index 9de0782..d4472f8 100644 (file)
@@ -302,8 +302,8 @@ int SimulatorRemoteResourceImpl::startVerification(RequestType type,
             if (m_getRequestSender)
             {
                 return m_autoRequestGenMngr->startOnGET(m_getRequestSender,
-                                    m_requestModelList[RequestType::RQ_TYPE_GET]->getQueryParams(),
-                                    localCallback);
+                                                        m_requestModelList[RequestType::RQ_TYPE_GET]->getQueryParams(),
+                                                        localCallback);
             }
             break;
 
@@ -311,9 +311,9 @@ int SimulatorRemoteResourceImpl::startVerification(RequestType type,
             if (m_putRequestSender)
             {
                 return m_autoRequestGenMngr->startOnPUT(m_putRequestSender,
-                                    m_requestModelList[RequestType::RQ_TYPE_PUT]->getQueryParams(),
-                                    m_requestModelList[RequestType::RQ_TYPE_PUT]->getRepSchema(),
-                                    localCallback);
+                                                        m_requestModelList[RequestType::RQ_TYPE_PUT]->getQueryParams(),
+                                                        m_requestModelList[RequestType::RQ_TYPE_PUT]->getRepSchema(),
+                                                        localCallback);
             }
             break;
 
@@ -321,9 +321,9 @@ int SimulatorRemoteResourceImpl::startVerification(RequestType type,
             if (m_postRequestSender)
             {
                 return m_autoRequestGenMngr->startOnPOST(m_putRequestSender,
-                                    m_requestModelList[RequestType::RQ_TYPE_POST]->getQueryParams(),
-                                    m_requestModelList[RequestType::RQ_TYPE_POST]->getRepSchema(),
-                                    localCallback);
+                        m_requestModelList[RequestType::RQ_TYPE_POST]->getQueryParams(),
+                        m_requestModelList[RequestType::RQ_TYPE_POST]->getRepSchema(),
+                        localCallback);
             }
             break;
 
@@ -360,7 +360,7 @@ void SimulatorRemoteResourceImpl::configure(const std::string &path)
         throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Empty path string!");
     }
 
-    RAML::RamlParser *ramlParser = new RAML::RamlParser(path);
+    std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(path);
     RAML::RamlPtr raml = ramlParser->getRamlPtr();
 
     configure(raml);
index 9ae58d4..f1fceb9 100644 (file)
@@ -77,13 +77,15 @@ bool Logger::setDefaultFileTarget(const std::string &path)
 
     time_t timeInfo = time(NULL);
     struct tm *localTime = localtime(&timeInfo);
+    if (nullptr == localTime)
+        return false;
     std::ostringstream newFileName;
     newFileName << path << "/Simulator_";
     newFileName << localTime->tm_year << localTime->tm_mon << localTime->tm_mday << localTime->tm_hour
                 << localTime->tm_min << localTime->tm_sec;
     newFileName << ".log";
 
-    std::shared_ptr<FileLogger> fileLogger(new FileLogger(newFileName.str()));
+    std::shared_ptr<FileLogger> fileLogger = std::make_shared<FileLogger>(newFileName.str());
     if (fileLogger->open())
     {
         m_target = fileLogger;
@@ -104,6 +106,8 @@ void Logger::write(ILogger::Level level, std::ostringstream &str)
     {
         time_t timeInfo = time(NULL);
         struct tm *localTime = localtime(&timeInfo);
+        if (nullptr == localTime)
+            return;
         std::ostringstream timeStr;
         timeStr << localTime->tm_hour << "." << localTime->tm_min << "." << localTime->tm_sec;
         m_target->write(timeStr.str(), level, str.str());
index f7992b1..6959f82 100644 (file)
@@ -33,7 +33,7 @@ SimulatorResourceServerImplSP SimulatorResourceCreator::createResource(
 
     try
     {
-        RAML::RamlParser *ramlParser = new RAML::RamlParser(configPath);
+        std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(configPath);
         raml = ramlParser->getRamlPtr();
     }
     catch (RAML::RamlException &e)
@@ -52,7 +52,7 @@ SimulatorResourceServerImplSP SimulatorResourceCreator::createResource(
 
     if (ramlResource)
     {
-        SimulatorResourceServerImplSP simResource(new SimulatorResourceServerImpl());
+        SimulatorResourceServerImplSP simResource = std::make_shared<SimulatorResourceServerImpl>();
         simResource->setName(ramlResource->getDisplayName());
         simResource->setURI(ramlResource->getResourceUri());
 
@@ -61,7 +61,7 @@ SimulatorResourceServerImplSP SimulatorResourceCreator::createResource(
         if (!action)
         {
             OC_LOG(ERROR, TAG, "Failed to create resource representation schema as it does not"
-                    "posess the GET request!");
+                   "posess the GET request!");
             return nullptr;
         }
 
@@ -76,7 +76,7 @@ SimulatorResourceServerImplSP SimulatorResourceCreator::createResource(
         if (responseBody)
         {
             RAML::JsonSchemaPtr resourceProperties = responseBody->getSchema()->getProperties();
-            for ( auto &propertyElement : resourceProperties->getProperties())
+            for ( auto & propertyElement : resourceProperties->getProperties())
             {
                 if (!propertyElement.second)
                     continue;
index 229e7d6..083e67f 100644 (file)
@@ -381,8 +381,10 @@ OCEntityHandlerResult SimulatorResourceServerImpl::entityHandler(
                     "] UNKNOWN type request received. \n**Payload details**" << payload)
 
             response->setResponseResult(OC_EH_ERROR);
-            OC::OCPlatform::sendResponse(response);
-            errCode = OC_EH_ERROR;
+            if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
+            {
+                errCode = OC_EH_ERROR;
+            }
         }
     }