use of org.osgi.framework.BundleContext in project karaf by apache.
the class ProxyLoginModuleInitializer method init.
public void init() {
BundleContext context = bundleContext.getBundle(0).getBundleContext();
ProxyLoginModule.init(context);
}
use of org.osgi.framework.BundleContext in project karaf by apache.
the class BundleHelpProvider method getHelp.
@Override
public String getHelp(Session session, String path) {
if (path.indexOf('|') > 0) {
if (path.startsWith("bundle|")) {
path = path.substring("bundle|".length());
} else {
return null;
}
}
if (path.matches("[0-9]*")) {
long id = Long.parseLong(path);
BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
Bundle bundle = bundleContext.getBundle(id);
if (bundle != null) {
String title = ShellUtil.getBundleName(bundle);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
ps.println("\n" + title);
ps.println(ShellUtil.getUnderlineString(title));
URL bundleInfo = bundle.getEntry("OSGI-INF/bundle.info");
if (bundleInfo != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(bundleInfo.openStream()))) {
int maxSize = 80;
Terminal terminal = session.getTerminal();
if (terminal != null) {
maxSize = terminal.getWidth();
}
WikiVisitor visitor = new AnsiPrintingWikiVisitor(ps, maxSize);
WikiParser parser = new WikiParser(visitor);
parser.parse(reader);
} catch (Exception e) {
// ignore
}
}
ps.close();
return baos.toString();
}
}
return null;
}
use of org.osgi.framework.BundleContext in project karaf by apache.
the class JdbcLoginModuleTest method setUp.
@Before
public void setUp() throws Exception {
System.setProperty("derby.stream.error.file", "target/derby.log");
// Create datasource
dataSource = new EmbeddedDataSource40();
dataSource.setDatabaseName("memory:db");
dataSource.setCreateDatabase("create");
// Delete tables
try (Connection connection = dataSource.getConnection()) {
connection.setAutoCommit(true);
try {
try (Statement statement = connection.createStatement()) {
statement.execute("drop table USERS");
}
} catch (SQLException e) {
// Ignore
}
try {
try (Statement statement = connection.createStatement()) {
statement.execute("drop table ROLES");
}
} catch (SQLException e) {
// Ignore
}
connection.commit();
}
// Create tables
try (Connection connection = dataSource.getConnection()) {
try (Statement statement = connection.createStatement()) {
statement.execute("create table USERS (USERNAME VARCHAR(32) PRIMARY KEY, PASSWORD VARCHAR(32))");
}
try (Statement statement = connection.createStatement()) {
statement.execute("create table ROLES (USERNAME VARCHAR(32), ROLE VARCHAR(1024))");
}
connection.commit();
}
// Mocks
BundleContext context = EasyMock.createMock(BundleContext.class);
ServiceReference reference = EasyMock.createMock(ServiceReference.class);
// Create options
options = new HashMap<>();
options.put(JDBCUtils.DATASOURCE, "osgi:" + DataSource.class.getName());
options.put(BundleContext.class.getName(), context);
expect(context.getServiceReferences(DataSource.class.getName(), null)).andReturn(new ServiceReference[] { reference });
expect((DataSource) context.getService(reference)).andReturn(dataSource);
expect(context.ungetService(reference)).andReturn(true);
EasyMock.replay(context);
}
use of org.osgi.framework.BundleContext in project karaf by apache.
the class BlueprintCommand method createNewAction.
public Action createNewAction() {
Action action = (Action) blueprintContainer.getComponentInstance(actionId);
if (action instanceof BlueprintContainerAware) {
((BlueprintContainerAware) action).setBlueprintContainer(blueprintContainer);
}
if (action instanceof BundleContextAware) {
BundleContext context = (BundleContext) blueprintContainer.getComponentInstance("blueprintBundleContext");
((BundleContextAware) action).setBundleContext(context);
}
return action;
}
use of org.osgi.framework.BundleContext in project sling by apache.
the class LegacyResourceProviderWhiteboard method bindResourceProviderFactory.
@Reference(service = ResourceProviderFactory.class, cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void bindResourceProviderFactory(final ServiceReference<ResourceProviderFactory> ref) {
final BundleContext bundleContext = ref.getBundle().getBundleContext();
final ResourceProviderFactory factory = bundleContext.getService(ref);
if (factory != null) {
final String[] propertyNames = ref.getPropertyKeys();
final boolean ownsRoot = toBoolean(ref.getProperty(OWNS_ROOTS), false);
final List<ServiceRegistration> newServices = new ArrayList<>();
for (final String path : PropertiesUtil.toStringArray(ref.getProperty(ROOTS), new String[0])) {
if (path != null && !path.isEmpty()) {
final Dictionary<String, Object> newProps = new Hashtable<>();
if (PropertiesUtil.toBoolean(ref.getProperty(PROPERTY_REQUIRED), false)) {
newProps.put(PROPERTY_AUTHENTICATE, AuthType.required.toString());
} else {
newProps.put(PROPERTY_AUTHENTICATE, AuthType.lazy.toString());
}
newProps.put(PROPERTY_MODIFIABLE, true);
newProps.put(PROPERTY_ADAPTABLE, true);
newProps.put(PROPERTY_ATTRIBUTABLE, true);
newProps.put(PROPERTY_REFRESHABLE, true);
newProps.put(PROPERTY_NAME, factory.getClass().getName());
newProps.put(PROPERTY_ROOT, normalizePath(path));
if (ArrayUtils.contains(propertyNames, SERVICE_PID)) {
newProps.put(ORIGINAL_SERVICE_PID, ref.getProperty(SERVICE_PID));
}
if (ArrayUtils.contains(propertyNames, USE_RESOURCE_ACCESS_SECURITY)) {
newProps.put(PROPERTY_USE_RESOURCE_ACCESS_SECURITY, ref.getProperty(USE_RESOURCE_ACCESS_SECURITY));
}
if (ArrayUtils.contains(propertyNames, SERVICE_RANKING)) {
newProps.put(SERVICE_RANKING, ref.getProperty(SERVICE_RANKING));
}
String[] languages = PropertiesUtil.toStringArray(ref.getProperty(LANGUAGES), new String[0]);
ServiceRegistration reg = bundleContext.registerService(org.apache.sling.spi.resource.provider.ResourceProvider.class.getName(), new LegacyResourceProviderFactoryAdapter(factory, languages, ownsRoot), newProps);
newServices.add(reg);
}
}
registrations.put(factory, newServices);
}
}
Aggregations