Apache Celix  latest
Apache Celix is a framework for C, C++14 and C++17 to develop dynamic modular software applications using component and in-process service-oriented programming.
Bundle.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #pragma once
21 
22 #include <cstdlib>
23 #include <cstdint>
24 #include <memory>
25 
26 #include "celix_bundle.h"
27 
28 namespace celix {
29 
30  enum class BundleState : std::uint8_t {
31  UNKNOWN,
33  INSTALLED,
34  RESOLVED,
35  STARTING,
36  STOPPING,
37  ACTIVE,
38  };
39 
49  class Bundle {
50  public:
51  explicit Bundle(celix_bundle_t* _cBnd) : cBnd{_cBnd, [](celix_bundle_t*){/*nop*/}} {}
52 
57  long getId() const { return celix_bundle_getId(cBnd.get()); }
58 
75  std::string getEntry(const std::string& path) const {
76  return getEntryInternal(path.c_str());
77  }
78 
94  std::string getDataFile(const std::string& path) const {
95  return getDataFileInternal(path.c_str());
96  }
97 
103  std::string getManifestValue(const std::string& attribute) const {
104  const char* header = celix_bundle_getManifestValue(cBnd.get(), attribute.c_str());
105  return header == nullptr ? std::string{} : std::string{header};
106  }
107 
111  std::string getSymbolicName() const {
112  return std::string{celix_bundle_getSymbolicName(cBnd.get())};
113  }
114 
118  std::string getName() const {
119  return std::string{celix_bundle_getName(cBnd.get())};
120  }
121 
125  std::string getGroup() const {
126  return std::string{celix_bundle_getGroup(cBnd.get())};
127  }
128 
132  std::string getDescription() const {
133  return std::string{celix_bundle_getDescription(cBnd.get())};
134  }
135 
141  std::string getLocation() const {
142  auto* loc = celix_bundle_getLocation(cBnd.get());
143  return convertCStringToStdString(loc);
144  }
145 
150  auto cState = celix_bundle_getState(cBnd.get());
151  switch (cState) {
152  case CELIX_BUNDLE_STATE_UNINSTALLED:
154  case CELIX_BUNDLE_STATE_INSTALLED:
155  return BundleState::INSTALLED;
156  case CELIX_BUNDLE_STATE_RESOLVED:
157  return BundleState::RESOLVED;
158  case CELIX_BUNDLE_STATE_STARTING:
159  return BundleState::STARTING;
160  case CELIX_BUNDLE_STATE_STOPPING:
161  return BundleState::STOPPING;
162  case CELIX_BUNDLE_STATE_ACTIVE:
163  return BundleState::ACTIVE;
164  default:
165  return BundleState::UNKNOWN;
166  }
167  }
168 
173  bool isSystemBundle() const {
174  return celix_bundle_isSystemBundle(cBnd.get());
175  }
176 
182  celix_bundle_t* getCBundle() const {
183  return cBnd.get();
184  }
185  private:
186  std::string getEntryInternal(const char* path) const {
187  char* entry = celix_bundle_getEntry(cBnd.get(), path);
188  return convertCStringToStdString(entry);
189  }
190 
191  std::string getDataFileInternal(const char* path) const {
192  char* entry = celix_bundle_getDataFile(cBnd.get(), path);
193  return convertCStringToStdString(entry);
194  }
195 
199  std::string convertCStringToStdString(char* str) const {
200  std::unique_ptr<char, void(*)(void*)> strGuard{str, free};
201  return std::string{str == nullptr ? "" : str};
202  }
203 
204  const std::shared_ptr<celix_bundle_t> cBnd;
205  };
206 }
celix_bundle_getName
const CELIX_FRAMEWORK_EXPORT char * celix_bundle_getName(const celix_bundle_t *bnd)
Return the name of the bundle. Note the return value is valid as long as the bundle is installed.
celix::BundleState::UNINSTALLED
@ UNINSTALLED
celix::Bundle::getDataFile
std::string getDataFile(const std::string &path) const
Return a use-able entry path for the provided relative path to a bundle persistent storage.
Definition: Bundle.h:94
celix
Definition: Bundle.h:28
celix_bundle_getEntry
CELIX_FRAMEWORK_EXPORT char * celix_bundle_getEntry(const celix_bundle_t *bnd, const char *path)
celix_bundle_getSymbolicName
const CELIX_FRAMEWORK_EXPORT char * celix_bundle_getSymbolicName(const celix_bundle_t *bnd)
Return the symbolic name of the bundle. Note the return value is valid as long as the bundle is insta...
celix::Bundle::Bundle
Bundle(celix_bundle_t *_cBnd)
Definition: Bundle.h:51
celix::Bundle::getCBundle
celix_bundle_t * getCBundle() const
Get the C bundle handle.
Definition: Bundle.h:182
celix_bundle_getDataFile
CELIX_FRAMEWORK_EXPORT char * celix_bundle_getDataFile(const celix_bundle_t *bnd, const char *path)
celix::Bundle::getLocation
std::string getLocation() const
Return the update location of the bundle. The location the location passed to celix::BundleContext::i...
Definition: Bundle.h:141
celix::Bundle::getDescription
std::string getDescription() const
The description of the bundle.
Definition: Bundle.h:132
celix::BundleState::RESOLVED
@ RESOLVED
celix::Bundle::getState
celix::BundleState getState() const
The current bundle state.
Definition: Bundle.h:149
celix::BundleState::ACTIVE
@ ACTIVE
celix::BundleState::INSTALLED
@ INSTALLED
celix::BundleState::UNKNOWN
@ UNKNOWN
celix::Bundle::isSystemBundle
bool isSystemBundle() const
whether the bundle is the system (framework) bundle
Definition: Bundle.h:173
celix::Bundle
An installed bundle in the Celix framework.
Definition: Bundle.h:49
celix::Bundle::getEntry
std::string getEntry(const std::string &path) const
Get a use-able entry path for the provided relative path to a bundle resource cache.
Definition: Bundle.h:75
celix_bundle_isSystemBundle
CELIX_FRAMEWORK_EXPORT bool celix_bundle_isSystemBundle(const celix_bundle_t *bnd)
Return whether the bundle is the system bundle.
celix::Bundle::getId
long getId() const
get the bundle id.
Definition: Bundle.h:57
celix::Bundle::getManifestValue
std::string getManifestValue(const std::string &attribute) const
Get a manifest attribute value from the bundle manifest.
Definition: Bundle.h:103
celix_bundle_getId
CELIX_FRAMEWORK_EXPORT long celix_bundle_getId(const celix_bundle_t *bnd)
Return the bundle id.
celix_bundle_getLocation
CELIX_FRAMEWORK_EXPORT char * celix_bundle_getLocation(const celix_bundle_t *bnd)
Return the update location of the bundle. The location the location passed to celix_bundleContext_ins...
celix::Bundle::getGroup
std::string getGroup() const
The group of the bundle.
Definition: Bundle.h:125
celix_bundle.h
celix::BundleState::STARTING
@ STARTING
celix_bundle_getManifestValue
const CELIX_FRAMEWORK_EXPORT char * celix_bundle_getManifestValue(const celix_bundle_t *bnd, const char *attribute)
Get a manifest attribute value from the bundle manifest.
celix::Bundle::getSymbolicName
std::string getSymbolicName() const
the symbolic name of the bundle.
Definition: Bundle.h:111
celix_bundle_getDescription
const CELIX_FRAMEWORK_EXPORT char * celix_bundle_getDescription(const celix_bundle_t *bnd)
Return the description of the bundle. Note the return value is valid as long as the bundle is install...
celix::BundleState
BundleState
Definition: Bundle.h:30
celix::Bundle::getName
std::string getName() const
The name of the bundle.
Definition: Bundle.h:118
celix_bundle_getGroup
const CELIX_FRAMEWORK_EXPORT char * celix_bundle_getGroup(const celix_bundle_t *bnd)
Return the group of the bundle. Groups are used to order bundles. Note the return value is valid as l...
celix::BundleState::STOPPING
@ STOPPING
celix_bundle_getState
CELIX_FRAMEWORK_EXPORT celix_bundle_state_e celix_bundle_getState(const celix_bundle_t *bnd)
Return the bundle state.