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 void ResourceContainerImpl::registerResource(BundleResource::Ptr resource)
263 string strUri = resource->m_uri;
264 string strResourceType = resource->m_resourceType;
265 RCSResourceObject::Ptr server = nullptr;
267 OC_LOG_V(INFO, CONTAINER_TAG, "Registration of resource (%s)" ,
268 std::string(strUri + ", " + strResourceType).c_str());
270 registrationLock.lock();
271 if (m_mapResources.find(strUri) == m_mapResources.end())
273 server = buildResourceObject(strUri, strResourceType);
275 if (server != nullptr)
277 m_mapServers[strUri] = server;
278 m_mapResources[strUri] = resource;
279 m_mapBundleResources[resource->m_bundleId].push_back(strUri);
281 server->setGetRequestHandler(
282 std::bind(&ResourceContainerImpl::getRequestHandler, this,
283 std::placeholders::_1, std::placeholders::_2));
285 server->setSetRequestHandler(
286 std::bind(&ResourceContainerImpl::setRequestHandler, this,
287 std::placeholders::_1, std::placeholders::_2));
289 OC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
290 std::string(strUri + ", " +
291 strResourceType).c_str());
293 if (m_config->isHasInput(resource->m_bundleId))
295 discoverInputResource(strUri);
298 // to get notified if bundle resource attributes are updated
299 resource->registerObserver((NotificationReceiver *) this);
304 OC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)",
305 std::string(strUri + " already exists.").c_str());
307 registrationLock.unlock();
310 void ResourceContainerImpl::unregisterResource(BundleResource::Ptr resource)
312 string strUri = resource->m_uri;
313 string strResourceType = resource->m_resourceType;
315 OC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)",
316 std::string(resource->m_uri + ", " +
317 resource->m_resourceType).c_str());
319 if (m_config->isHasInput(resource->m_bundleId))
321 undiscoverInputResource(strUri);
324 if (m_mapServers.find(strUri) != m_mapServers.end())
326 m_mapServers[strUri].reset();
328 m_mapResources.erase(m_mapResources.find(strUri));
329 m_mapBundleResources[resource->m_bundleId].remove(strUri);
333 void ResourceContainerImpl::getBundleConfiguration(const std::string &bundleId,
334 configInfo *configOutput)
338 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
342 void ResourceContainerImpl::getResourceConfiguration(const std::string &bundleId,
343 std::vector< resourceInfo > *configOutput)
347 m_config->getResourceConfiguration(bundleId, configOutput);
351 RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
352 const RCSResourceAttributes &)
354 RCSResourceAttributes attr;
355 std::string strResourceUri = request.getResourceUri();
357 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
358 && m_mapResources.find(strResourceUri) != m_mapResources.end())
360 if (m_mapResources[strResourceUri])
362 auto getFunction = [this, &attr, &strResourceUri]()
364 attr = m_mapResources[strResourceUri]->handleGetAttributesRequest();
366 boost::thread getThread(getFunction);
367 getThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
372 return RCSGetResponse::create(std::move(attr), 200);
375 RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
376 const RCSResourceAttributes &attributes)
378 RCSResourceAttributes attr;
379 std::list<std::string> lstAttributes;
380 std::string strResourceUri = request.getResourceUri();
382 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
383 && m_mapResources.find(strResourceUri) != m_mapResources.end())
385 if (m_mapResources[strResourceUri])
387 auto setFunction = [this, &lstAttributes, &strResourceUri, &attributes, &attr]()
389 lstAttributes = m_mapResources[strResourceUri]->getAttributeNames();
391 for (RCSResourceAttributes::const_iterator itor = attributes.begin();
392 itor != attributes.end(); itor++)
394 if (std::find(lstAttributes.begin(), lstAttributes.end(), itor->key())
395 != lstAttributes.end())
397 attr[itor->key()] = itor->value();
401 m_mapResources[strResourceUri]->handleSetAttributesRequest(attr);
403 boost::thread setThread(setFunction);
404 setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
408 return RCSSetResponse::create(std::move(attr), 200);
411 void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
413 OC_LOG_V(INFO, CONTAINER_TAG,
414 "notification from (%s)", std::string(strResourceUri + ".").c_str());
416 if (m_mapServers.find(strResourceUri) != m_mapServers.end())
418 m_mapServers[strResourceUri]->notify();
422 ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
424 static ResourceContainerImpl m_instance;
428 RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri,
429 const std::string &strResourceType)
431 return RCSResourceObject::Builder(strUri, strResourceType,
432 "oic.if.baseline").setObservable(
433 true).setDiscoverable(true).build();
436 void ResourceContainerImpl::startBundle(const std::string &bundleId)
438 if (m_bundles.find(bundleId) != m_bundles.end())
440 if (!m_bundles[bundleId]->isActivated())
441 activateBundle(m_bundles[bundleId]);
444 OC_LOG(ERROR, CONTAINER_TAG, "Bundle already started");
449 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
450 std::string(bundleId + "\' is not registered.").c_str());
454 void ResourceContainerImpl::stopBundle(const std::string &bundleId)
456 if (m_bundles.find(bundleId) != m_bundles.end())
458 if (m_bundles[bundleId]->isActivated())
459 deactivateBundle(m_bundles[bundleId]);
462 OC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated");
467 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
468 std::string(bundleId + "\' is not registered.").c_str());
472 void ResourceContainerImpl::addBundle(const std::string &bundleId,
473 const std::string &bundleUri, const std::string &bundlePath,
474 const std::string &activator, std::map< string, string > params)
478 if (m_bundles.find(bundleId) != m_bundles.end())
479 OC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist");
483 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
484 bundleInfo->setID(bundleId);
485 bundleInfo->setPath(bundlePath);
486 bundleInfo->setActivatorName(activator);
487 if (params.find("libraryPath") != params.end())
489 string activatorName = activator; // modify activator for Java bundle
490 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
491 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
492 ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]);
495 OC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)",
496 std::string(bundleInfo->getID() + "; " +
497 bundleInfo->getPath()).c_str());
499 registerBundle(bundleInfo);
503 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
505 if (m_bundles.find(bundleId) != m_bundles.end())
507 BundleInfoInternal *bundleInfo = m_bundles[bundleId];
508 if (bundleInfo->isActivated())
509 deactivateBundle(bundleInfo);
511 if (bundleInfo->isLoaded())
512 unregisterBundle(bundleInfo);
516 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
517 std::string(bundleId + "\' is not registered.").c_str());
521 std::list< RCSBundleInfo * > ResourceContainerImpl::listBundles()
523 std::list< RCSBundleInfo * > ret;
524 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
525 it != m_bundles.end(); ++it)
528 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
529 (bundleInfo)->setBundleInfo(it->second);
530 ret.push_back((RCSBundleInfo *) bundleInfo);
536 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
537 const std::string &resourceUri, std::map< string, string > params)
539 if (m_bundles.find(bundleId) != m_bundles.end())
541 if (!m_bundles[bundleId]->getJavaBundle())
543 resourceInfo newResourceInfo;
544 newResourceInfo.uri = resourceUri;
546 if (params.find(OUTPUT_RESOURCE_NAME) != params.end())
547 newResourceInfo.name = params[OUTPUT_RESOURCE_NAME];
548 if (params.find(OUTPUT_RESOURCE_TYPE) != params.end())
549 newResourceInfo.resourceType = params[OUTPUT_RESOURCE_TYPE];
550 if (params.find(OUTPUT_RESOURCE_ADDR) != params.end())
551 newResourceInfo.address = params[OUTPUT_RESOURCE_ADDR];
553 addSoBundleResource(bundleId, newResourceInfo);
558 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
559 std::string(bundleId + "\' is not registered.").c_str());
563 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
564 const std::string &resourceUri)
566 if (m_bundles.find(bundleId) != m_bundles.end())
568 if (!m_bundles[bundleId]->getJavaBundle())
570 removeSoBundleResource(bundleId, resourceUri);
575 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
576 std::string(bundleId + "\' is not registered.").c_str());
580 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
582 std::list < string > ret;
584 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
586 ret = m_mapBundleResources[bundleId];
593 void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo)
597 activator_t *bundleActivator = NULL;
598 deactivator_t *bundleDeactivator = NULL;
599 resourceCreator_t *resourceCreator = NULL;
600 resourceDestroyer_t *resourceDestroyer = NULL;
601 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
602 void *bundleHandle = NULL;
603 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
605 if (bundleHandle != NULL)
608 (activator_t *) dlsym(bundleHandle,
609 ("" + bundleInfoInternal->getActivatorName()
610 + "_externalActivateBundle").c_str());
612 (deactivator_t *) dlsym(bundleHandle,
613 ("" + bundleInfoInternal->getActivatorName()
614 + "_externalDeactivateBundle").c_str());
616 (resourceCreator_t *) dlsym(bundleHandle,
617 ("" + bundleInfoInternal->getActivatorName()
618 + "_externalCreateResource").c_str());
620 (resourceDestroyer_t *) dlsym(bundleHandle,
621 ("" + bundleInfoInternal->getActivatorName()
622 + "_externalDestroyResource").c_str());
625 if ((error = dlerror()) != NULL)
627 OC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
631 ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator);
632 ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator);
633 ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator);
634 ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer);
635 ((BundleInfoInternal *) bundleInfo)->setLoaded(true);
636 ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle);
638 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo);
643 if ((error = dlerror()) != NULL)
645 OC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
650 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
652 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
654 if (bundleActivator != NULL)
656 bundleActivator(this, m_bundles[bundleId]->getID());
657 m_bundles[bundleId]->setActivated(true);
661 //Unload module and return error
662 OC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful.");
665 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
666 bundleInfoInternal->setActivated(true);
670 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
672 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
673 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
675 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
679 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
681 auto foundOutputResource = m_mapResources.find(outputResourceUri);
682 auto resourceProperty = foundOutputResource->second->m_mapResourceProperty;
686 resourceProperty.at(INPUT_RESOURCE);
688 catch (std::out_of_range &e)
693 for (auto iter : resourceProperty)
695 if (iter.first.compare(INPUT_RESOURCE) == 0)
697 for (auto it : iter.second)
699 auto makeValue = [&](const std::string & reference) mutable -> std::string
701 std::string retStr = "";
704 retStr = it.at(reference);
706 catch (std::out_of_range &e)
712 std::string uri = makeValue(INPUT_RESOURCE_URI);
713 std::string type = makeValue(INPUT_RESOURCE_TYPE);
714 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
716 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
717 < DiscoverResourceUnit > (outputResourceUri);
718 newDiscoverUnit->startDiscover(
719 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
721 std::bind(&SoftSensorResource::onUpdatedInputResource,
722 std::static_pointer_cast< SoftSensorResource > (foundOutputResource->second),
723 std::placeholders::_1, std::placeholders::_2));
725 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
727 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
729 foundDiscoverResource->second.push_back(newDiscoverUnit);
733 m_mapDiscoverResourceUnits.insert(
734 std::make_pair(outputResourceUri,
735 std::list< DiscoverResourceUnit::Ptr >
736 { newDiscoverUnit }));
743 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
745 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
747 OC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
748 m_bundles[id]->getID()).c_str());
750 if (bundleDeactivator != NULL)
753 m_bundles[id]->setActivated(false);
757 //Unload module and return error
758 OC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
762 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
763 resourceInfo newResourceInfo)
765 resourceCreator_t *resourceCreator;
767 resourceCreator = m_bundles[bundleId]->getResourceCreator();
769 if (resourceCreator != NULL)
771 resourceCreator(newResourceInfo);
775 OC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
779 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
780 const std::string &resourceUri)
782 if (m_mapResources.find(resourceUri) != m_mapResources.end())
784 resourceDestroyer_t *resourceDestroyer =
785 m_bundles[bundleId]->getResourceDestroyer();
787 if (resourceDestroyer != NULL)
789 resourceDestroyer(m_mapResources[resourceUri]);
793 OC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
798 void ResourceContainerImpl::activateBundleThread(const std::string &id)
800 OC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
801 std::string(m_bundles[id]->getID()).c_str());
803 if (m_bundles[id]->getJavaBundle())
806 activateJavaBundle(id);
811 activateSoBundle (id);
814 OC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
815 std::string(m_bundles[id]->getID()).c_str());
819 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
821 return m_bundleVM[bundleId];
824 void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo)
826 OC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
827 std::string(bundleInfo->getID()).c_str());
830 JavaVMInitArgs vm_args;
831 JavaVMOption options[3];
833 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
835 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
839 OC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
840 std::string(bundleInfo->getPath() +
841 " available.").c_str());
845 OC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
846 std::string(bundleInfo->getPath() + " not available.").c_str());
851 char optionString[] = "-Djava.compiler=NONE";
852 options[0].optionString = optionString;
853 char classpath[1000];
854 strcpy(classpath, "-Djava.class.path=");
855 strcat(classpath, bundleInfo->getPath().c_str());
857 OC_LOG(INFO, CONTAINER_TAG,
858 std::string("Configured classpath: ").append(classpath).c_str());
860 options[1].optionString = classpath;
862 char libraryPath[1000];
863 strcpy(libraryPath, "-Djava.library.path=");
864 strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
865 options[2].optionString = libraryPath;
867 OC_LOG(INFO, CONTAINER_TAG,
868 std::string("Configured library path: ").append(libraryPath).c_str());
870 vm_args.version = JNI_VERSION_1_4;
871 vm_args.options = options;
872 vm_args.nOptions = 3;
873 vm_args.ignoreUnrecognized = 1;
876 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
880 OC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
885 OC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
888 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
890 const char *className = bundleInfoInternal->getActivatorName().c_str();
892 OC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
893 bundleInfoInternal->getActivatorName() + "|").c_str());
895 jclass bundleActivatorClass = env->FindClass(className);
897 if (bundleActivatorClass == NULL)
899 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
900 std::string( bundleInfoInternal->getID()
901 + " bundle activator(" + bundleInfoInternal->getActivatorName()
902 + ") not found ").c_str());
906 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
909 if (activateMethod == NULL)
911 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
912 std::string( bundleInfoInternal->getID()
913 + " activate bundle method not found ").c_str());
916 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
918 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
921 if (deactivateMethod == NULL)
923 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
924 std::string( bundleInfoInternal->getID()
925 + " deactivate bundle method not found ").c_str());
929 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
931 jmethodID constructor;
933 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
935 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
937 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
939 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
941 bundleInfoInternal->setLoaded(true);
943 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
946 OC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
949 void ResourceContainerImpl::activateJavaBundle(string bundleId)
951 OC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
953 JavaVM *vm = getJavaVM(bundleId);
954 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
956 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
958 if (envStat == JNI_EDETACHED)
960 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
962 OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
965 else if (envStat == JNI_EVERSION)
967 OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
970 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
971 bundleInfoInternal->getJavaBundleActivatorMethod());
973 m_bundles[bundleId]->setActivated(true);
976 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
978 OC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
980 JavaVM *vm = getJavaVM(bundleId);
981 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
983 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
985 if (envStat == JNI_EDETACHED)
987 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
989 OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
992 else if (envStat == JNI_EVERSION)
994 OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
997 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
998 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1000 m_bundles[bundleId]->setActivated(false);
1003 void ResourceContainerImpl::unregisterBundleJava(string id)
1005 OC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1006 m_bundles[id]->getID()).c_str());
1008 OC_LOG(INFO, CONTAINER_TAG, "Destroying JVM");
1010 m_bundleVM[id]->DestroyJavaVM();
1012 delete m_bundles[id];
1013 m_bundles.erase(id);