All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.kestros.commons.osgiserviceutils.services.BaseServiceResolverService Maven / Gradle / Ivy

There is a newer version: 0.1.11
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.kestros.commons.osgiserviceutils.services;

import static io.kestros.commons.structuredslingmodels.utils.SlingModelUtils.getResourceAsBaseResource;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.kestros.commons.structuredslingmodels.BaseResource;
import io.kestros.commons.structuredslingmodels.exceptions.ResourceNotFoundException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.felix.hc.api.FormattingResultLog;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Deactivate;
import org.slf4j.Logger;

/**
 * Baseline OSGI Service which when activated, creates a ResourceResolver for a service User.  When
 * deactivated the ResourceResolver is closed.
 */
public abstract class BaseServiceResolverService implements ManagedService {

  private ComponentContext componentContext;

  @Nonnull
  protected abstract String getServiceUserName();

  /**
   * Activates Cache service. Opens service ResourceResolver, which is used to build cached files.
   *
   * @param ctx ComponentContext.
   */
  @Activate
  public void activate(@Nonnull final ComponentContext ctx) {
    componentContext = ctx;
  }

  /**
   * Deactivates the Service and closes the Service ResourceResolver.
   *
   * @param componentContext ComponentContext.
   */
  @Deactivate
  public void deactivate(@Nonnull ComponentContext componentContext) {
  }

  /**
   * Retrieves a resource resolver as the service user.
   *
   * @return ResourceResolver.
   *
   * @throws LoginException If unable to login as service user.
   */
  @Nonnull
  public ResourceResolver getServiceResourceResolver() throws LoginException {
    final Map params = Collections.singletonMap(
            ResourceResolverFactory.SUBSERVICE, getServiceUserName());
    if (getResourceResolverFactory() != null) {
      getLogger().debug("Getting service resource resolver for {}.",
                        getServiceUserName().replaceAll("[\r\n]", ""));
      return getResourceResolverFactory().getServiceResourceResolver(params);
    } else {
      throw new LoginException(String.format(
              "Unable to get ServiceResourceResolver for %s. ResourceResolverFactory is null.",
              getServiceUserName()));
    }
  }


  @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
  @Override
  public void runAdditionalHealthChecks(@Nonnull FormattingResultLog log) {
    try (ResourceResolver resourceResolver = getServiceResourceResolver()) {
      if (resourceResolver.isLive()) {
        log.debug("Service ResourceResolver is live.");
      } else {
        log.critical("Service ResourceResolver is not live.");
      }
      if (getRequiredResourcePaths() != null) {
        for (String path : getRequiredResourcePaths()) {
          try {
            BaseResource requiredResource = getResourceAsBaseResource(path, resourceResolver);
            log.debug(String.format("Found resource at path %s", requiredResource.getPath()));
          } catch (ResourceNotFoundException e) {
            log.critical(String.format("Failed to find resource at path %s", path));
          }
        }
      }
    } catch (LoginException e) {
      log.critical("Unable to get Service ResourceResolver: {}", e.getMessage());
    }

  }

  @Nonnull
  protected abstract Logger getLogger();

  @Nonnull
  protected abstract List getRequiredResourcePaths();

  @Nullable
  protected abstract ResourceResolverFactory getResourceResolverFactory();

  @Nonnull
  protected ComponentContext getComponentContext() {
    return this.componentContext;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy