Apache Celix  2.4.0
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 <memory>
24 
25 #include "celix_bundle.h"
26 
27 namespace celix {
28 
29  enum class BundleState : std::uint8_t {
30  UNKNOWN,
32  INSTALLED,
33  RESOLVED,
34  STARTING,
35  STOPPING,
36  ACTIVE,
37  };
38 
48  class Bundle {
49  public:
50  explicit Bundle(celix_bundle_t* _cBnd) : cBnd{_cBnd, [](celix_bundle_t*){/*nop*/}} {}
51 
56  long getId() const { return celix_bundle_getId(cBnd.get()); }
57 
74 #if __cplusplus >= 201703L //C++17 or higher
75  std::string getEntry(std::string_view path) const {
76  return getEntryInternal(path.data());
77  }
78 #else
79  std::string getEntry(const std::string& path) const {
80  return getEntryInternal(path.c_str());
81  }
82 #endif
83 
99 #if __cplusplus >= 201703L //C++17 or higher
100  std::string getDataFile(std::string_view path) const {
101  return getDataFileInternal(path.data());
102  }
103 #else
104  std::string getDataFile(const std::string& path) const {
105  return getDataFileInternal(path.c_str());
106  }
107 #endif
108 
114 #if __cplusplus >= 201703L //C++17 or higher
115  std::string getManifestValue(std::string_view attribute) const {
116  const char* header = celix_bundle_getManifestValue(cBnd.get(), attribute.data());
117  return header == nullptr ? std::string{} : std::string{header};
118  }
119 #else
120  std::string getManifestValue(const std::string& attribute) const {
121  const char* header = celix_bundle_getManifestValue(cBnd.get(), attribute.c_str());
122  return header == nullptr ? std::string{} : std::string{header};
123  }
124 #endif
125 
129  std::string getSymbolicName() const {
130  return std::string{celix_bundle_getSymbolicName(cBnd.get())};
131  }
132 
136  std::string getName() const {
137  return std::string{celix_bundle_getName(cBnd.get())};
138  }
139 
143  std::string getGroup() const {
144  return std::string{celix_bundle_getGroup(cBnd.get())};
145  }
146 
150  std::string getDescription() const {
151  return std::string{celix_bundle_getDescription(cBnd.get())};
152  }
153 
159  std::string getLocation() const {
160  auto* loc = celix_bundle_getLocation(cBnd.get());
161  return convertCStringToStdString(loc);
162  }
163 
168  auto cState = celix_bundle_getState(cBnd.get());
169  switch (cState) {
170  case CELIX_BUNDLE_STATE_UNINSTALLED:
172  case CELIX_BUNDLE_STATE_INSTALLED:
173  return BundleState::INSTALLED;
174  case CELIX_BUNDLE_STATE_RESOLVED:
175  return BundleState::RESOLVED;
176  case CELIX_BUNDLE_STATE_STARTING:
177  return BundleState::STARTING;
178  case CELIX_BUNDLE_STATE_STOPPING:
179  return BundleState::STOPPING;
180  case CELIX_BUNDLE_STATE_ACTIVE:
181  return BundleState::ACTIVE;
182  default:
183  return BundleState::UNKNOWN;
184  }
185  }
186 
191  bool isSystemBundle() const {
192  return celix_bundle_isSystemBundle(cBnd.get());
193  }
194 
200  celix_bundle_t* getCBundle() const {
201  return cBnd.get();
202  }
203  private:
204  std::string getEntryInternal(const char* path) const {
205  char* entry = celix_bundle_getEntry(cBnd.get(), path);
206  return convertCStringToStdString(entry);
207  }
208 
209  std::string getDataFileInternal(const char* path) const {
210  char* entry = celix_bundle_getDataFile(cBnd.get(), path);
211  return convertCStringToStdString(entry);
212  }
213 
217  std::string convertCStringToStdString(char* str) const {
218  std::unique_ptr<char, void(*)(void*)> strGuard{str, free};
219  return std::string{str == nullptr ? "" : str};
220  }
221 
222  const std::shared_ptr<celix_bundle_t> cBnd;
223  };
224 }
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
Definition: Bundle.h:27
celix_bundle_getEntry
CELIX_FRAMEWORK_EXPORT char * celix_bundle_getEntry(const celix_bundle_t *bnd, const char *path)
celix::Bundle::getEntry
std::string getEntry(std::string_view path) const
Get a use-able entry path for the provided relative path to a bundle resource cache.
Definition: Bundle.h:75
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:50
celix::Bundle::getCBundle
celix_bundle_t * getCBundle() const
Get the C bundle handle.
Definition: Bundle.h:200
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:159
celix::Bundle::getDescription
std::string getDescription() const
The description of the bundle.
Definition: Bundle.h:150
celix::BundleState::RESOLVED
@ RESOLVED
celix::Bundle::getState
celix::BundleState getState() const
The current bundle state.
Definition: Bundle.h:167
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:191
celix::Bundle
An installed bundle in the Celix framework.
Definition: Bundle.h:48
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:56
celix_bundle_getId
CELIX_FRAMEWORK_EXPORT long celix_bundle_getId(const celix_bundle_t *bnd)
Return the bundle id.
celix::Bundle::getDataFile
std::string getDataFile(std::string_view path) const
Return a use-able entry path for the provided relative path to a bundle persistent storage.
Definition: Bundle.h:100
celix::Bundle::getManifestValue
std::string getManifestValue(std::string_view attribute) const
Get a manifest attribute value from the bundle manifest.
Definition: Bundle.h:115
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:143
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:129
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:29
celix::Bundle::getName
std::string getName() const
The name of the bundle.
Definition: Bundle.h:136
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.