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 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);
}
}
use of org.osgi.framework.BundleContext in project sling by apache.
the class SightlyCompiledScriptTest method testEvalSlingBindings.
/**
* Tests that SlingBindings are correctly handled by compiled scripts, by setting them from the script context to the request
* attributes.
* @throws ScriptException
*/
@Test
public void testEvalSlingBindings() throws ScriptException {
ScriptEngine scriptEngine = mock(ScriptEngine.class);
final RenderUnit renderUnit = mock(RenderUnit.class);
Whitebox.setInternalState(renderUnit, "subTemplates", new HashMap<String, Object>());
final BundleContext bundleContext = MockOsgi.newBundleContext();
bundleContext.registerService(ExtensionRegistryService.class.getName(), mock(ExtensionRegistryService.class), new Hashtable<String, Object>());
ResourceResolver resourceResolver = MockSling.newResourceResolver(bundleContext);
final MockSlingHttpServletRequest request = spy(new MockSlingHttpServletRequest(resourceResolver, bundleContext));
SightlyCompiledScript compiledScript = spy(new SightlyCompiledScript(scriptEngine, renderUnit));
ScriptContext scriptContext = mock(ScriptContext.class);
StringWriter writer = new StringWriter();
when(scriptContext.getWriter()).thenReturn(writer);
Bindings scriptContextBindings = new SimpleBindings() {
{
put("test", "testValue");
put(SlingBindings.REQUEST, request);
put(SlingBindings.SLING, MockSling.newSlingScriptHelper(bundleContext));
}
};
SlingBindings oldBindings = new SlingBindings();
oldBindings.put("old", "oldValue");
request.setAttribute(SlingBindings.class.getName(), oldBindings);
when(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE)).thenReturn(scriptContextBindings);
compiledScript.eval(scriptContext);
ArgumentCaptor<SlingBindings> slingBindingsArgumentCaptor = ArgumentCaptor.forClass(SlingBindings.class);
ArgumentCaptor<String> attributeNameArgumentCaptor = ArgumentCaptor.forClass(String.class);
// request.setAttribute should have been invoked 3 times: once here, twice in the compiled script
verify(request, times(3)).setAttribute(attributeNameArgumentCaptor.capture(), slingBindingsArgumentCaptor.capture());
List<SlingBindings> slingBindingsValues = slingBindingsArgumentCaptor.getAllValues();
int invocation = 1;
for (SlingBindings bindings : slingBindingsValues) {
switch(invocation) {
case 1:
assertEquals(oldBindings, bindings);
break;
case 2:
assertEquals(3, bindings.size());
for (Map.Entry<String, Object> entry : scriptContextBindings.entrySet()) {
assertEquals(entry.getValue(), bindings.get(entry.getKey()));
}
break;
case 3:
assertEquals(oldBindings, bindings);
}
invocation++;
}
for (String key : attributeNameArgumentCaptor.getAllValues()) {
assertEquals(SlingBindings.class.getName(), key);
}
}
Aggregations