Stop container functionality and bug fixed
authorMarkus Jung <markus.jung@samsung.com>
Fri, 15 Jan 2016 07:06:32 +0000 (16:06 +0900)
committerUze Choi <uzchoi@samsung.com>
Fri, 22 Jan 2016 02:51:11 +0000 (02:51 +0000)
Change-Id: Ib1517da5de49d8d0665e49cb3aad9bea5d915af6
Signed-off-by: Markus Jung <markus.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4809
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-container/android/resource-container/src/main/java/org/iotivity/service/resourcecontainer/BundleResource.java
service/resource-container/android/resource-container/src/main/java/org/iotivity/service/resourcecontainer/RcsResourceContainer.java
service/resource-container/src/RemoteResourceUnit.cpp
service/resource-container/src/ResourceContainerImpl.cpp
service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp

index aad5de2..86b0b17 100644 (file)
@@ -113,6 +113,11 @@ public abstract class BundleResource {
      *            new value of the attribute
      */
     public abstract void handleSetAttributesRequest(RcsResourceAttributes value);
+    
+    /**
+     * Deactivates the resource
+     */ 
+    public abstract void deactivateResource();
 
     /**
      * Retrieve the attribute (only data)
index 8ddcd06..3eaef68 100644 (file)
@@ -36,6 +36,8 @@ import dalvik.system.DexFile;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 
+import java.util.Hashtable;
+
 // TODO null check for parameters
 /**
  * This class provides APIs for managing the container and bundles in the
@@ -98,6 +100,8 @@ public class RcsResourceContainer implements RcsResourceContainerBundleAPI {
     public RcsResourceContainer(Context appContext){
         this.appContext = appContext;
     }
+    
+    private Hashtable<String, BundleActivator> activators = new Hashtable<String, BundleActivator>();
 
     /**
      * API for starting the Container
@@ -135,8 +139,11 @@ public class RcsResourceContainer implements RcsResourceContainerBundleAPI {
                     Log.d(TAG, "Loading activator: " + className);
                     Class activatorClass = df.loadClass(className, cl);
                     if(activatorClass!= null){
-                        BundleActivator activator = (BundleActivator) activatorClass.getConstructor(RcsResourceContainerBundleAPI.class, Context.class).newInstance(this, appContext);
+                        BundleActivator activator = (BundleActivator) activatorClass.
+                                getConstructor(RcsResourceContainerBundleAPI.class, Context.class).
+                                newInstance(this, appContext);
                         activator.activateBundle();
+                        activators.put(bundleInfo.getID(), activator);
                     }else{
                         Log.e(TAG, "Activator is null.");
                     }
@@ -153,6 +160,10 @@ public class RcsResourceContainer implements RcsResourceContainerBundleAPI {
      * API for stopping the Container
      */
     public void stopContainer() {
+        // stop all android bundles
+        for(BundleActivator activator :activators.values()){
+            activator.deactivateBundle();
+        }
         nativeStopContainer();
     }
 
@@ -199,6 +210,10 @@ public class RcsResourceContainer implements RcsResourceContainerBundleAPI {
      *
      */
     public void removeBundle(String bundleId) {
+        if(activators.contains(bundleId)){
+            // deactivate android bundle
+            activators.get(bundleId).deactivateBundle();
+        }
         nativeRemoveBundle(bundleId);
     }
 
@@ -212,16 +227,49 @@ public class RcsResourceContainer implements RcsResourceContainerBundleAPI {
     public void startBundle(String bundleId) {
         Log.d(TAG, "startBundle");
         List<RcsBundleInfo> bundles = listBundles();
-        boolean androidBundle =false;
-        for(RcsBundleInfo bundleInfo : bundles){
+       
+        for(RcsBundleInfo bundleInfo : bundles){          
             if(bundleInfo.getID().equals(bundleId) && bundleInfo.getLibraryPath().endsWith(".apk")){
-                androidBundle = true;
                 Log.d(TAG, "Have to start android bundle");
+                Log.d(TAG, "bundle-id: " + bundleInfo.getID() + ", " + bundleInfo.getPath());
+                if(bundleInfo.getPath().endsWith(".apk")){
+                    String packageName = bundleInfo.getPath().replace(".apk", "");
+                    try{
+                        PackageManager packageManager = appContext.getPackageManager();
+                        ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName, 0);
+                        DexFile df = new DexFile(appInfo.sourceDir);
+                        ClassLoader cl = appContext.getClassLoader();
+                        for (Enumeration<String> iter = df.entries(); iter.hasMoreElements(); ) {
+                            String classN = iter.nextElement();
+                            if (classN.contains(packageName)) {
+                                Log.d(TAG,"Class: " + classN);
+                                df.loadClass(classN, cl);
+                            }
+                        }
+                        
+                        String className = bundleInfo.getActivatorName();
+                        Log.d(TAG, "Loading activator: " + className);
+                        Class activatorClass = df.loadClass(className, cl);
+                        if(activatorClass!= null){
+                            BundleActivator activator = (BundleActivator) activatorClass.
+                                    getConstructor(RcsResourceContainerBundleAPI.class, 
+                                            Context.class).
+                                    newInstance(this, appContext);
+                            activator.activateBundle();
+                        }else{
+                            Log.e(TAG, "Activator is null.");
+                        }
+                    }
+                    catch(Exception e){
+                        Log.e(TAG, e.getMessage(), e);
+                    }
+                    Log.d(TAG, "Have to register android bundle");
+                }
+            }else{
+                nativeStartBundle(bundleId);
             }
         }
-        if(!androidBundle){
-            nativeStartBundle(bundleId);
-        }
+      
     }
 
     /**
index 80d1523..4d2abfd 100644 (file)
@@ -19,6 +19,9 @@
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 #include "RemoteResourceUnit.h"
+#include "InternalTypes.h"
+
+#include <exception>
 
 using namespace OIC::Service;
 
@@ -36,11 +39,21 @@ RemoteResourceUnit::~RemoteResourceUnit()
     {
         if(remoteObject->isCaching())
         {
-            remoteObject->stopCaching();
+            try{
+                remoteObject->stopCaching();
+            }
+            catch(std::exception &e){
+                OC_LOG_V(ERROR, CONTAINER_TAG, "%s", e.what());
+            }
         }
         if(remoteObject->isMonitoring())
         {
-            remoteObject->stopMonitoring();
+            try{
+                remoteObject->stopMonitoring();
+            }
+            catch(std::exception &e){
+                OC_LOG_V(ERROR, CONTAINER_TAG, "%s", e.what());
+            }
         }
     }
 }
index b6ecc34..ce57b2b 100644 (file)
@@ -337,14 +337,24 @@ namespace OIC
 
             if (m_config->isHasInput(resource->m_bundleId))
             {
+                OC_LOG_V(INFO, CONTAINER_TAG, "Calling undiscover (%s)",
+                                     std::string(resource->m_uri + ", " +
+                                                 resource->m_resourceType).c_str());
                 undiscoverInputResource(strUri);
             }
 
             if (m_mapServers.find(strUri) != m_mapServers.end())
             {
+                OC_LOG_V(INFO, CONTAINER_TAG, "Resetting server (%s)",
+                                     std::string(resource->m_uri + ", " +
+                                                 resource->m_resourceType).c_str());
                 m_mapServers[strUri].reset();
 
                 m_mapResources.erase(m_mapResources.find(strUri));
+
+                OC_LOG_V(INFO, CONTAINER_TAG, "Remove bundle resource (%s)",
+                                     std::string(resource->m_uri + ", " +
+                                                 resource->m_resourceType).c_str());
                 m_mapBundleResources[resource->m_bundleId].remove(strUri);
             }
         }
@@ -763,7 +773,9 @@ namespace OIC
             auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
             if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
             {
+                OC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource.");
                 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
+                OC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource done.");
             }
         }
 
index 388ac21..ccde40c 100644 (file)
@@ -132,8 +132,14 @@ namespace OIC
         {
             SCOPE_LOG_F(DEBUG, TAG);
 
-            stopCaching();
-            stopMonitoring();
+            try{
+                stopCaching();
+                stopMonitoring();
+            }
+            catch(std::exception &e){
+                OC_LOG_V(ERROR, TAG, "%s", e.what());
+            }
+
         }
 
         bool RCSRemoteResourceObject::isMonitoring() const
@@ -238,7 +244,9 @@ namespace OIC
                 return;
             }
 
+            OC_LOG(DEBUG, TAG, "cancel resource cache");
             ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
+            OC_LOG(DEBUG, TAG, "After resource cache manager cancel resource cache");
             m_cacheId = 0;
         }