001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.core.metapath.function;
028
029import gov.nist.secauto.metaschema.core.metapath.IExpression;
030import gov.nist.secauto.metaschema.core.metapath.StaticMetapathException;
031import gov.nist.secauto.metaschema.core.util.ObjectUtils;
032
033import java.util.Arrays;
034import java.util.List;
035import java.util.ServiceLoader;
036import java.util.ServiceLoader.Provider;
037
038import edu.umd.cs.findbugs.annotations.NonNull;
039import nl.talsmasoftware.lazy4j.Lazy;
040
041public final class FunctionService {
042  private static final Lazy<FunctionService> INSTANCE = Lazy.lazy(() -> new FunctionService());
043  @NonNull
044  private final ServiceLoader<IFunctionLibrary> loader;
045  @NonNull
046  private IFunctionLibrary library;
047
048  /**
049   * Get the singleton instance of the function service.
050   *
051   * @return the service instance
052   */
053  public static FunctionService getInstance() {
054    return INSTANCE.get();
055  }
056
057  /**
058   * Construct a new function service.
059   */
060  @SuppressWarnings("null")
061  public FunctionService() {
062    this.loader = ServiceLoader.load(IFunctionLibrary.class);
063    this.library = load();
064  }
065
066  /**
067   * Load all known functions registered with this function service.
068   *
069   * @return the function library
070   */
071  @NonNull
072  public IFunctionLibrary load() {
073    ServiceLoader<IFunctionLibrary> loader = getLoader();
074
075    FunctionLibrary functionLibrary = new FunctionLibrary();
076    loader.stream()
077        .map(Provider<IFunctionLibrary>::get)
078        .flatMap(library -> {
079          return library.getFunctionsAsStream();
080        })
081        .forEachOrdered(function -> functionLibrary.registerFunction(ObjectUtils.requireNonNull(function)));
082
083    synchronized (this) {
084      this.library = functionLibrary;
085    }
086    return functionLibrary;
087  }
088
089  /**
090   * Get the function service loader instance.
091   *
092   * @return the service loader instance.
093   */
094  @NonNull
095  private ServiceLoader<IFunctionLibrary> getLoader() {
096    return loader;
097  }
098
099  /**
100   * Retrieve the function with the provided name that supports the signature of
101   * the provided methods, if such a function exists.
102   *
103   * @param name
104   *          the name of a group of functions
105   * @param arguments
106   *          a list of argument expressions for use in determining an argument
107   *          signature match
108   * @return the matching function or {@code null} if no match exists
109   * @throws StaticMetapathException
110   *           if a matching function was not found
111   */
112  @SuppressWarnings("null")
113  public IFunction getFunction(@NonNull String name, @NonNull IExpression... arguments) {
114    return getFunction(name, Arrays.asList(arguments));
115  }
116
117  /**
118   * Retrieve the function with the provided name that supports the signature of
119   * the provided methods, if such a function exists.
120   *
121   * @param name
122   *          the name of a group of functions
123   * @param arguments
124   *          a list of argument expressions for use in determining an argument
125   *          signature match
126   * @return the matching function
127   * @throws StaticMetapathException
128   *           if a matching function was not found
129   */
130  public IFunction getFunction(@NonNull String name, @NonNull List<IExpression> arguments) {
131    IFunction retval;
132    synchronized (this) {
133      retval = library.getFunction(name, arguments);
134    }
135
136    if (retval == null) {
137      throw new StaticMetapathException(StaticMetapathException.NO_FUNCTION_MATCH,
138          String.format("unable to find function with name '%s' having arity '%d'", name, arguments.size()));
139    }
140    return retval;
141  }
142}