blob: 7f702b698631db847139be8ed31dee4753346f53 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "celix/BundleActivator.h"
#include "celix_shell_command.h"
struct MyCShellCommand : public celix_shell_command {
explicit MyCShellCommand(std::shared_ptr<celix::BundleContext> _ctx) : celix_shell_command(), ctx{std::move(_ctx)} {
handle = this;
executeCommand = [] (void *handle, const char* commandLine, FILE* outStream, FILE* /*errorStream*/) -> bool {
auto* cmdProvider = static_cast<MyCShellCommand*>(handle);
fprintf(outStream, "Hello from bundle %s with command line '%s'\n", cmdProvider->ctx->getBundle().getName().c_str(), commandLine);
return true;
};
}
const std::shared_ptr<celix::BundleContext> ctx;
};
class MyCShellCommandProviderBundleActivator {
public:
explicit MyCShellCommandProviderBundleActivator(const std::shared_ptr<celix::BundleContext>& ctx) {
auto shellCmd = std::make_shared<MyCShellCommand>(ctx);
cmdShellRegistration = ctx->registerService<celix_shell_command>(std::move(shellCmd), CELIX_SHELL_COMMAND_SERVICE_NAME)
.addProperty(CELIX_SHELL_COMMAND_NAME, "MyCCommand")
.setUnregisterAsync(false)
.build();
}
private:
//NOTE when celix::ServiceRegistration goes out of scope the underlining service will be un-registered
std::shared_ptr<celix::ServiceRegistration> cmdShellRegistration{};
};
CELIX_GEN_CXX_BUNDLE_ACTIVATOR(MyCShellCommandProviderBundleActivator)