phemanthkumar28
phemanthkumar28

Reputation: 408

OSGI ConfigurationAdmin is null while retrieving using getServiceReference in AEM

I'm working on a use case that involves accessing properties of other bundles that are not part of the current bundle. In order to achieve this, I have to modify the permissions of the current AEM bundle (AEM 6.2). I see that ConditionalPermissionAdmin of OSGi service is the way forward.

Approach Reference: http://moi.vonos.net/java/osgi-security/

I have the below code, that intends to retrieve the ConditionalPermissionAdmin but some unknown reason the this always null.

public class Activator implements BundleActivator {

   private static final Logger LOG = LoggerFactory.getLogger(Activator.class);

   private ConditionalPermissionAdmin conditionalPermissionAdmin;

  @Override
  public final void start(final BundleContext bundleContext) {
    try {
        conditionalPermissionAdmin = getConditionalPermissionAdmin(bundleContext);
        if (conditionalPermissionAdmin != null) {
            LOG.info("{0} conditionalPermissionAdmin ", conditionalPermissionAdmin.getClass());
           /* pseudocode */
        }
        LOG.info("{0} started", bundleContext.getBundle().getSymbolicName());
    }  catch (Exception ex) {
        LOG.error(ex.getMessage());
    }
  }

  private ConditionalPermissionAdmin getConditionalPermissionAdmin(BundleContext context) throws BundleException {
    ServiceReference ref = context.getServiceReference(ConditionalPermissionAdmin.class.getName());
    ConditionalPermissionAdmin permissionAdmin = null;
    if (ref != null) {
        permissionAdmin = (ConditionalPermissionAdmin) context.getService(ref);
    }
    return permissionAdmin;
  }

  @Override
  public final void stop(final BundleContext bundleContext) {
    LOG.info("{0} stopped", bundleContext.getBundle().getSymbolicName());
  }
}

enter image description here

I don't see any build or runtime issues. Even the bundle is in good shape.

Bundle Status

This low-level API is not the best approach as suggested here. But I'm unsure on the Declarative Services approach for permission.

Heads up --> I'm new to OSGi concepts and implementation.

References:

Upvotes: 1

Views: 970

Answers (2)

Ahmed Musallam
Ahmed Musallam

Reputation: 9753

Even though I do not fully understand the use-case and why a service user or user-based access in general would not work, here is what I found:

The felix framework security documentation suggest that you need to install the org.apache.felix.framework.security bundle, which is not installed by default with AEM (at least looking at my vanilla 6.3 instance)

It's listed under "Framework Security" in the Felix subprojects install page: http://felix.apache.org/downloads.cgi#subprojects

The issue is that there are no implementations of the OSGI org.osgi.service.condpermadmin.ConditionalPermissionAdmin in your instance. And that's why you need the felix security bundle.

Upvotes: 2

Christian Schneider
Christian Schneider

Reputation: 19606

Generally in OSGi it is possible that a service reference is not (yet) available. So simply getting the current service reference is very likely to fail because of timing issues.

One valid low level approach is to start a ServiceTracker and react when the service becomes available. This is quite difficult to do right though.

So as you already heard of using DS is the best and recommended approach.

Upvotes: 4

Related Questions