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

org.opencastproject.util.OsgiUtil Maven / Gradle / Ivy

There is a newer version: 16.7
Show newest version
/**
 * Licensed to The Apereo Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 *
 * The Apereo Foundation licenses this file to you under the Educational
 * Community 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://opensource.org/licenses/ecl2.txt
 *
 * 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 org.opencastproject.util;

import static org.opencastproject.util.data.Monadics.mlist;
import static org.opencastproject.util.data.Option.option;

import org.opencastproject.rest.RestConstants;
import org.opencastproject.rest.SharedHttpContext;
import org.opencastproject.util.data.Collections;
import org.opencastproject.util.data.Function;
import org.opencastproject.util.data.Option;
import org.opencastproject.util.data.Tuple;
import org.opencastproject.util.data.functions.Strings;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.component.ComponentContext;

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.Servlet;

/** Contains general purpose OSGi utility functions. */
public final class OsgiUtil {

  private OsgiUtil() {
  }

  /**
   * Get a mandatory, non-blank value from the bundle context.
   *
   * @throws RuntimeException
   *           key does not exist or its value is blank
   */
  public static String getContextProperty(ComponentContext cc, String key) {
    String p = cc.getBundleContext().getProperty(key);
    if (StringUtils.isBlank(p))
      throw new RuntimeException("Please provide context property " + key);
    return StringUtils.trimToEmpty(p);
  }

  /**
   * Get an optional, non-blank value from the bundle context.
   *
   * @throws RuntimeException
   *           key does not exist or its value is blank
   */
  public static Option getOptContextProperty(ComponentContext cc, String key) {
    return option(cc.getBundleContext().getProperty(key)).bind(Strings.trimToNone);
  }

  /**
   * Get a mandatory, non-blank value from the component context.
   *
   * @throws RuntimeException
   *           key does not exist or its value is blank
   */
  public static String getComponentContextProperty(ComponentContext cc, String key) {
    String p = (String) cc.getProperties().get(key);
    if (StringUtils.isBlank(p))
      throw new RuntimeException("Please provide context property " + key);
    return StringUtils.trimToEmpty(p);
  }

  /**
   * Get a mandatory, non-blank value from a dictionary.
   *
   * @throws ConfigurationException
   *           key does not exist or its value is blank
   */
  public static String getCfg(Dictionary d, String key) throws ConfigurationException {
    Object p = d.get(key);
    if (p == null)
      throw new ConfigurationException(key, "does not exist");
    String ps = p.toString();
    if (StringUtils.isBlank(ps))
      throw new ConfigurationException(key, "is blank");
    return StringUtils.trimToEmpty(ps);
  }

  /** Get a value from a dictionary. Return none if the key does either not exist or the value is blank. */
  public static Option getOptCfg(Dictionary d, String key) {
    return option(d.get(key)).bind(Strings.asString()).bind(Strings.trimToNone);
  }

  /** Get a value from a dictionary. Return none if the key does either not exist or the value is blank. */
  public static Option getOptCfgAsInt(Dictionary d, String key) {
    return option(d.get(key)).bind(Strings.asString()).bind(Strings.toInt);
  }

  /**
   * Filter a dictionary by key prefix. For example the following map
   * {w.p.key1: "value1", w.p.key2: "value2", x: "1"} filtered by filterByPrefix(d, "w.p.")
   * returns {key1: "value1", key2: "value"}.
   */
  public static Map filterByPrefix(Dictionary d, String prefix) {
    final Map filtered = new HashMap<>();
    final int prefixLength = prefix.length();
    final Enumeration keys = d.keys();
    while (keys.hasMoreElements()) {
      final String key = keys.nextElement().toString();
      if (key.startsWith(prefix)) {
        filtered.put(key.substring(prefixLength), d.get(key).toString());
      }
    }
    return filtered;
  }

  /**
   * Get an optional boolean from a dictionary.
   */
  public static Option getOptCfgAsBoolean(Dictionary d, String key) {
    return option(d.get(key)).bind(Strings.asString()).map(Strings.toBool);
  }

  /**
   * Get a mandatory integer from a dictionary.
   *
   * @throws ConfigurationException
   *           key does not exist or is not an integer
   */
  public static int getCfgAsInt(Dictionary d, String key) throws ConfigurationException {
    try {
      return Integer.parseInt(getCfg(d, key));
    } catch (NumberFormatException e) {
      throw new ConfigurationException(key, "not an integer");
    }
  }

  /**
   * Get a mandatory boolean from a dictionary.
   *
   * @throws ConfigurationException
   *           key does not exist
   */
  public static boolean getCfgAsBoolean(Dictionary d, String key) throws ConfigurationException {
    Object p = d.get(key);
    if (p == null)
      throw new ConfigurationException(key, "does not exist");
    return BooleanUtils.toBoolean(p.toString());
  }

  /**
   * Check the existence of the given dictionary. Throw an exception if null.
   */
  public static void checkDictionary(Dictionary properties, ComponentContext componentContext)
          throws ConfigurationException {
    if (properties == null) {
      String dicName = componentContext.getProperties().get("service.pid").toString();
      throw new ConfigurationException("*", "Dictionary for " + dicName + " does not exist");
    }
  }

  /** Create a config info string suitable for logging purposes. */
  public static String showConfig(Tuple... cfg) {
    return "Config\n" + Collections.mkString(mlist(cfg).map(new Function, String>() {
      @Override
      public String apply(Tuple t) {
        return t.getA() + "=" + t.getB().toString();
      }
    }).value(), "\n");
  }

  public static ServiceRegistration registerServlet(BundleContext bundleContext, Object service, String alias) {
    Dictionary resourceProps = new Hashtable<>();
    resourceProps.put(SharedHttpContext.CONTEXT_ID, RestConstants.HTTP_CONTEXT_ID);
    resourceProps.put(SharedHttpContext.SHARED, "true");
    resourceProps.put(SharedHttpContext.ALIAS, alias);
    return bundleContext.registerService(Servlet.class.getName(), service, resourceProps);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy