1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "ResourceContainerImpl.h"
33 #include "BundleActivator.h"
34 #include "SoftSensorResource.h"
35 #include "InternalTypes.h"
37 using namespace OIC::Service;
44 ResourceContainerImpl::ResourceContainerImpl()
49 ResourceContainerImpl::~ResourceContainerImpl()
54 bool has_suffix(const std::string &str, const std::string &suffix)
56 return str.size() >= suffix.size()
57 && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
60 void ResourceContainerImpl::startContainer(const std::string &configFile)
62 OC_LOG(INFO, CONTAINER_TAG, "Starting resource container.");
64 OC_LOG(INFO, CONTAINER_TAG, "Resource container has Java support.");
66 OC_LOG(INFO, CONTAINER_TAG, "Resource container without Java support.");
70 activationLock.lock();
71 if (!configFile.empty())
73 m_config = new Configuration(configFile);
75 if (m_config->isLoaded())
78 m_config->getConfiguredBundles(&bundles);
80 for (unsigned int i = 0; i < bundles.size(); i++)
82 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
83 bundleInfo->setPath(bundles[i][BUNDLE_PATH]);
84 bundleInfo->setVersion(bundles[i][BUNDLE_VERSION]);
85 bundleInfo->setID(bundles[i][BUNDLE_ID]);
86 if (!bundles[i][BUNDLE_ACTIVATOR].empty())
88 string activatorName = bundles[i][BUNDLE_ACTIVATOR];
89 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
90 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
91 ((BundleInfoInternal *) bundleInfo)->setLibraryPath(
92 bundles[i][BUNDLE_LIBRARY_PATH]);
95 OC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)",
96 std::string(bundles[i][BUNDLE_ID] + ";" +
97 bundles[i][BUNDLE_PATH]).c_str());
99 registerBundle(bundleInfo);
100 activateBundle(bundleInfo);
105 OC_LOG_V(ERROR, CONTAINER_TAG, "Container started with invalid configfile path.");
110 OC_LOG_V(INFO, CONTAINER_TAG, "No configuration file for the container provided.");
113 map<std::string, boost::thread >::iterator activatorIterator;
115 for (activatorIterator = m_activators.begin(); activatorIterator != m_activators.end();
118 activatorIterator->second.timed_join(
119 boost::posix_time::seconds(BUNDLE_ACTIVATION_WAIT_SEC));
120 // wait for bundles to be activated
122 activationLock.unlock();
125 void ResourceContainerImpl::stopContainer()
127 OC_LOG(INFO, CONTAINER_TAG, "Stopping resource container.");
129 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
130 it != m_bundles.end(); ++it)
132 BundleInfoInternal *bundleInfo = it->second;
133 deactivateBundle(bundleInfo);
134 unregisterBundle(bundleInfo);
137 if (!m_mapServers.empty())
139 map< std::string, RCSResourceObject::Ptr >::iterator itor = m_mapServers.begin();
141 while (itor != m_mapServers.end())
143 (itor++)->second.reset();
146 m_mapResources.clear();
147 m_mapBundleResources.clear();
154 void ResourceContainerImpl::activateBundle(RCSBundleInfo *bundleInfo)
156 activationLock.lock();
157 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
159 if (bundleInfoInternal->isLoaded())
161 activateBundle(bundleInfo->getID());
163 activationLock.unlock();
166 void ResourceContainerImpl::deactivateBundle(RCSBundleInfo *bundleInfo)
168 if (((BundleInfoInternal *) bundleInfo)->isActivated())
170 deactivateBundle(bundleInfo->getID());
174 void ResourceContainerImpl::activateBundle(const std::string &id)
176 OC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
177 std::string(m_bundles[id]->getID()).c_str());
178 activationLock.lock();
179 auto f = std::bind(&ResourceContainerImpl::activateBundleThread, this,
181 boost::thread activator(f);
182 activator.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
183 activationLock.unlock();
184 OC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
185 std::string(m_bundles[id]->getID()).c_str());
188 void ResourceContainerImpl::deactivateBundle(const std::string &id)
190 if (m_bundles[id]->getJavaBundle())
193 deactivateJavaBundle(id);
198 deactivateSoBundle(id);
203 void ResourceContainerImpl::registerBundle(RCSBundleInfo *bundleInfo)
205 OC_LOG_V(INFO, CONTAINER_TAG, "Registering bundle: (%s)",
206 std::string(bundleInfo->getPath()).c_str());
208 if (has_suffix(bundleInfo->getPath(), ".jar"))
211 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(true);
212 registerJavaBundle(bundleInfo);
217 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
218 registerSoBundle(bundleInfo);
222 void ResourceContainerImpl::unregisterBundle(RCSBundleInfo *bundleInfo)
224 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
225 if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated())
227 if (!bundleInfoInternal->getJavaBundle())
229 unregisterBundleSo(bundleInfo->getID());
234 unregisterBundleJava(bundleInfo->getID());
240 void ResourceContainerImpl::unregisterBundleSo(const std::string &id)
242 void *bundleHandle = m_bundles[id]->getBundleHandle();
244 OC_LOG_V(INFO, CONTAINER_TAG, "Unregister bundle: (%s)",
245 std::string(m_bundles[id]->getID()).c_str());
248 dlclose(bundleHandle);
250 if ((error = dlerror()) != NULL)
252 OC_LOG_V(ERROR, CONTAINER_TAG, "Error (%s)", error);
256 delete m_bundles[id];
261 int ResourceContainerImpl::registerResource(BundleResource::Ptr resource)
263 string strUri = resource->m_uri;
264 string strResourceType = resource->m_resourceType;
265 RCSResourceObject::Ptr server = nullptr;
268 OC_LOG_V(INFO, CONTAINER_TAG, "Registration of resource (%s)" ,
269 std::string(strUri + ", " + strResourceType).c_str());
271 registrationLock.lock();
272 if (m_mapResources.find(strUri) == m_mapResources.end())
274 server = buildResourceObject(strUri, strResourceType);
276 if (server != nullptr)
278 m_mapServers[strUri] = server;
279 m_mapResources[strUri] = resource;
280 m_mapBundleResources[resource->m_bundleId].push_back(strUri);
282 server->setGetRequestHandler(
283 std::bind(&ResourceContainerImpl::getRequestHandler, this,
284 std::placeholders::_1, std::placeholders::_2));
286 server->setSetRequestHandler(
287 std::bind(&ResourceContainerImpl::setRequestHandler, this,
288 std::placeholders::_1, std::placeholders::_2));
290 OC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
291 std::string(strUri + ", " +
292 strResourceType).c_str());
294 if (m_config->isHasInput(resource->m_bundleId))
296 discoverInputResource(strUri);
299 // to get notified if bundle resource attributes are updated
300 resource->registerObserver((NotificationReceiver *) this);
306 OC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)",
307 std::string(strUri + " already exists.").c_str());
310 registrationLock.unlock();
315 void ResourceContainerImpl::unregisterResource(BundleResource::Ptr resource)
317 string strUri = resource->m_uri;
318 string strResourceType = resource->m_resourceType;
320 OC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)",
321 std::string(resource->m_uri + ", " +
322 resource->m_resourceType).c_str());
324 if (m_config->isHasInput(resource->m_bundleId))
326 undiscoverInputResource(strUri);
329 if (m_mapServers.find(strUri) != m_mapServers.end())
331 m_mapServers[strUri].reset();
333 m_mapResources.erase(m_mapResources.find(strUri));
334 m_mapBundleResources[resource->m_bundleId].remove(strUri);
338 void ResourceContainerImpl::getBundleConfiguration(const std::string &bundleId,
339 configInfo *configOutput)
343 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
347 void ResourceContainerImpl::getResourceConfiguration(const std::string &bundleId,
348 std::vector< resourceInfo > *configOutput)
352 m_config->getResourceConfiguration(bundleId, configOutput);
356 RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
357 const RCSResourceAttributes &)
359 RCSResourceAttributes attr;
360 std::string strResourceUri = request.getResourceUri();
362 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
363 && m_mapResources.find(strResourceUri) != m_mapResources.end())
365 if (m_mapResources[strResourceUri])
367 auto getFunction = [this, &attr, &strResourceUri]()
369 attr = m_mapResources[strResourceUri]->handleGetAttributesRequest();
371 boost::thread getThread(getFunction);
372 getThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
377 return RCSGetResponse::create(std::move(attr), 200);
380 RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
381 const RCSResourceAttributes &attributes)
383 RCSResourceAttributes attr;
384 std::list<std::string> lstAttributes;
385 std::string strResourceUri = request.getResourceUri();
387 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
388 && m_mapResources.find(strResourceUri) != m_mapResources.end())
390 if (m_mapResources[strResourceUri])
392 auto setFunction = [this, &lstAttributes, &strResourceUri, &attributes, &attr]()
394 lstAttributes = m_mapResources[strResourceUri]->getAttributeNames();
396 for (RCSResourceAttributes::const_iterator itor = attributes.begin();
397 itor != attributes.end(); itor++)
399 if (std::find(lstAttributes.begin(), lstAttributes.end(), itor->key())
400 != lstAttributes.end())
402 attr[itor->key()] = itor->value();
406 m_mapResources[strResourceUri]->handleSetAttributesRequest(attr);
408 boost::thread setThread(setFunction);
409 setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
413 return RCSSetResponse::create(std::move(attr), 200);
416 void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
418 OC_LOG_V(INFO, CONTAINER_TAG,
419 "notification from (%s)", std::string(strResourceUri + ".").c_str());
421 if (m_mapServers.find(strResourceUri) != m_mapServers.end())
423 m_mapServers[strResourceUri]->notify();
427 ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
429 static ResourceContainerImpl m_instance;
433 RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri,
434 const std::string &strResourceType)
436 return RCSResourceObject::Builder(strUri, strResourceType,
437 "oic.if.baseline").setObservable(
438 true).setDiscoverable(true).build();
441 void ResourceContainerImpl::startBundle(const std::string &bundleId)
443 if (m_bundles.find(bundleId) != m_bundles.end())
445 if (!m_bundles[bundleId]->isActivated())
446 activateBundle(m_bundles[bundleId]);
449 OC_LOG(ERROR, CONTAINER_TAG, "Bundle already started");
454 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
455 std::string(bundleId + "\' is not registered.").c_str());
459 void ResourceContainerImpl::stopBundle(const std::string &bundleId)
461 if (m_bundles.find(bundleId) != m_bundles.end())
463 if (m_bundles[bundleId]->isActivated())
464 deactivateBundle(m_bundles[bundleId]);
467 OC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated");
472 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
473 std::string(bundleId + "\' is not registered.").c_str());
477 void ResourceContainerImpl::addBundle(const std::string &bundleId,
478 const std::string &bundleUri, const std::string &bundlePath,
479 const std::string &activator, std::map< string, string > params)
483 if (m_bundles.find(bundleId) != m_bundles.end())
484 OC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist");
488 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
489 bundleInfo->setID(bundleId);
490 bundleInfo->setPath(bundlePath);
491 bundleInfo->setActivatorName(activator);
492 if (params.find("libraryPath") != params.end())
494 string activatorName = activator; // modify activator for Java bundle
495 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
496 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
497 ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]);
500 OC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)",
501 std::string(bundleInfo->getID() + "; " +
502 bundleInfo->getPath()).c_str());
504 registerBundle(bundleInfo);
508 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
510 if (m_bundles.find(bundleId) != m_bundles.end())
512 BundleInfoInternal *bundleInfo = m_bundles[bundleId];
513 if (bundleInfo->isActivated())
514 deactivateBundle(bundleInfo);
516 if (bundleInfo->isLoaded())
517 unregisterBundle(bundleInfo);
521 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
522 std::string(bundleId + "\' is not registered.").c_str());
526 std::list<std::unique_ptr<RCSBundleInfo>> ResourceContainerImpl::listBundles()
528 std::list<std::unique_ptr<RCSBundleInfo> > ret;
529 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
530 it != m_bundles.end(); ++it)
533 std::unique_ptr<BundleInfoInternal> bundleInfo(new BundleInfoInternal);
534 (bundleInfo)->setBundleInfo(it->second);
535 ret.push_back(std::move(bundleInfo));
541 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
542 const std::string &resourceUri, std::map< string, string > params)
544 if (m_bundles.find(bundleId) != m_bundles.end())
546 if (!m_bundles[bundleId]->getJavaBundle())
548 resourceInfo newResourceInfo;
549 newResourceInfo.uri = resourceUri;
551 if (params.find(OUTPUT_RESOURCE_NAME) != params.end())
552 newResourceInfo.name = params[OUTPUT_RESOURCE_NAME];
553 if (params.find(OUTPUT_RESOURCE_TYPE) != params.end())
554 newResourceInfo.resourceType = params[OUTPUT_RESOURCE_TYPE];
555 if (params.find(OUTPUT_RESOURCE_ADDR) != params.end())
556 newResourceInfo.address = params[OUTPUT_RESOURCE_ADDR];
558 addSoBundleResource(bundleId, newResourceInfo);
563 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
564 std::string(bundleId + "\' is not registered.").c_str());
568 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
569 const std::string &resourceUri)
571 if (m_bundles.find(bundleId) != m_bundles.end())
573 if (!m_bundles[bundleId]->getJavaBundle())
575 removeSoBundleResource(bundleId, resourceUri);
580 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
581 std::string(bundleId + "\' is not registered.").c_str());
585 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
587 std::list < string > ret;
589 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
591 ret = m_mapBundleResources[bundleId];
598 void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo)
602 activator_t *bundleActivator = NULL;
603 deactivator_t *bundleDeactivator = NULL;
604 resourceCreator_t *resourceCreator = NULL;
605 resourceDestroyer_t *resourceDestroyer = NULL;
606 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
607 void *bundleHandle = NULL;
608 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
610 if (bundleHandle != NULL)
613 (activator_t *) dlsym(bundleHandle,
614 ("" + bundleInfoInternal->getActivatorName()
615 + "_externalActivateBundle").c_str());
617 (deactivator_t *) dlsym(bundleHandle,
618 ("" + bundleInfoInternal->getActivatorName()
619 + "_externalDeactivateBundle").c_str());
621 (resourceCreator_t *) dlsym(bundleHandle,
622 ("" + bundleInfoInternal->getActivatorName()
623 + "_externalCreateResource").c_str());
625 (resourceDestroyer_t *) dlsym(bundleHandle,
626 ("" + bundleInfoInternal->getActivatorName()
627 + "_externalDestroyResource").c_str());
630 if ((error = dlerror()) != NULL)
632 OC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
636 ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator);
637 ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator);
638 ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator);
639 ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer);
640 ((BundleInfoInternal *) bundleInfo)->setLoaded(true);
641 ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle);
643 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo);
648 if ((error = dlerror()) != NULL)
650 OC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
655 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
657 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
659 if (bundleActivator != NULL)
661 bundleActivator(this, m_bundles[bundleId]->getID());
662 m_bundles[bundleId]->setActivated(true);
666 //Unload module and return error
667 OC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful.");
670 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
671 bundleInfoInternal->setActivated(true);
675 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
677 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
678 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
680 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
684 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
686 auto foundOutputResource = m_mapResources.find(outputResourceUri);
687 auto resourceProperty = foundOutputResource->second->m_mapResourceProperty;
691 resourceProperty.at(INPUT_RESOURCE);
693 catch (std::out_of_range &e)
698 for (auto iter : resourceProperty)
700 if (iter.first.compare(INPUT_RESOURCE) == 0)
702 for (auto it : iter.second)
704 auto makeValue = [&](const std::string & reference) mutable -> std::string
706 std::string retStr = "";
709 retStr = it.at(reference);
711 catch (std::out_of_range &e)
717 std::string uri = makeValue(INPUT_RESOURCE_URI);
718 std::string type = makeValue(INPUT_RESOURCE_TYPE);
719 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
721 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
722 < DiscoverResourceUnit > (outputResourceUri);
723 newDiscoverUnit->startDiscover(
724 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
726 std::bind(&SoftSensorResource::onUpdatedInputResource,
727 std::static_pointer_cast< SoftSensorResource > (foundOutputResource->second),
728 std::placeholders::_1, std::placeholders::_2));
730 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
732 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
734 foundDiscoverResource->second.push_back(newDiscoverUnit);
738 m_mapDiscoverResourceUnits.insert(
739 std::make_pair(outputResourceUri,
740 std::list< DiscoverResourceUnit::Ptr >
741 { newDiscoverUnit }));
748 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
750 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
752 OC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
753 m_bundles[id]->getID()).c_str());
755 if (bundleDeactivator != NULL)
758 m_bundles[id]->setActivated(false);
762 //Unload module and return error
763 OC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
767 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
768 resourceInfo newResourceInfo)
770 resourceCreator_t *resourceCreator;
772 resourceCreator = m_bundles[bundleId]->getResourceCreator();
774 if (resourceCreator != NULL)
776 resourceCreator(newResourceInfo);
780 OC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
784 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
785 const std::string &resourceUri)
787 if (m_mapResources.find(resourceUri) != m_mapResources.end())
789 resourceDestroyer_t *resourceDestroyer =
790 m_bundles[bundleId]->getResourceDestroyer();
792 if (resourceDestroyer != NULL)
794 resourceDestroyer(m_mapResources[resourceUri]);
798 OC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
803 void ResourceContainerImpl::activateBundleThread(const std::string &id)
805 OC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
806 std::string(m_bundles[id]->getID()).c_str());
808 if (m_bundles[id]->getJavaBundle())
811 activateJavaBundle(id);
816 activateSoBundle (id);
819 OC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
820 std::string(m_bundles[id]->getID()).c_str());
824 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
826 return m_bundleVM[bundleId];
829 void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo)
831 OC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
832 std::string(bundleInfo->getID()).c_str());
835 JavaVMInitArgs vm_args;
836 JavaVMOption options[3];
838 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
840 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
844 OC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
845 std::string(bundleInfo->getPath() +
846 " available.").c_str());
850 OC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
851 std::string(bundleInfo->getPath() + " not available.").c_str());
856 char optionString[] = "-Djava.compiler=NONE";
857 options[0].optionString = optionString;
858 char classpath[1000];
859 strcpy(classpath, "-Djava.class.path=");
860 strcat(classpath, bundleInfo->getPath().c_str());
862 OC_LOG(INFO, CONTAINER_TAG,
863 std::string("Configured classpath: ").append(classpath).c_str());
865 options[1].optionString = classpath;
867 char libraryPath[1000];
868 strcpy(libraryPath, "-Djava.library.path=");
869 strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
870 options[2].optionString = libraryPath;
872 OC_LOG(INFO, CONTAINER_TAG,
873 std::string("Configured library path: ").append(libraryPath).c_str());
875 vm_args.version = JNI_VERSION_1_4;
876 vm_args.options = options;
877 vm_args.nOptions = 3;
878 vm_args.ignoreUnrecognized = 1;
881 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
885 OC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
890 OC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
893 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
895 const char *className = bundleInfoInternal->getActivatorName().c_str();
897 OC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
898 bundleInfoInternal->getActivatorName() + "|").c_str());
900 jclass bundleActivatorClass = env->FindClass(className);
902 if (bundleActivatorClass == NULL)
904 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
905 std::string( bundleInfoInternal->getID()
906 + " bundle activator(" + bundleInfoInternal->getActivatorName()
907 + ") not found ").c_str());
911 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
914 if (activateMethod == NULL)
916 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
917 std::string( bundleInfoInternal->getID()
918 + " activate bundle method not found ").c_str());
921 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
923 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
926 if (deactivateMethod == NULL)
928 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
929 std::string( bundleInfoInternal->getID()
930 + " deactivate bundle method not found ").c_str());
934 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
936 jmethodID constructor;
938 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
940 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
942 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
944 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
946 bundleInfoInternal->setLoaded(true);
948 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
951 OC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
954 void ResourceContainerImpl::activateJavaBundle(string bundleId)
956 OC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
958 JavaVM *vm = getJavaVM(bundleId);
959 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
961 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
963 if (envStat == JNI_EDETACHED)
965 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
967 OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
970 else if (envStat == JNI_EVERSION)
972 OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
975 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
976 bundleInfoInternal->getJavaBundleActivatorMethod());
978 m_bundles[bundleId]->setActivated(true);
981 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
983 OC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
985 JavaVM *vm = getJavaVM(bundleId);
986 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
988 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
990 if (envStat == JNI_EDETACHED)
992 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
994 OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
997 else if (envStat == JNI_EVERSION)
999 OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1002 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1003 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1005 m_bundles[bundleId]->setActivated(false);
1008 void ResourceContainerImpl::unregisterBundleJava(string id)
1010 OC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1011 m_bundles[id]->getID()).c_str());
1013 OC_LOG(INFO, CONTAINER_TAG, "Destroying JVM");
1015 m_bundleVM[id]->DestroyJavaVM();
1017 delete m_bundles[id];
1018 m_bundles.erase(id);