View Javadoc
1   /*
2    * scriptable-dataset (https://github.com/hazendaz/scriptable-dataset)
3    *
4    * Copyright 2011-2024 Hazendaz.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   *
11   * Contributors:
12   *     Gunnar Morling
13   *     Hazendaz (Jeremy Landis).
14   */
15  package de.gmorling.scriptabledataset.handlers;
16  
17  import java.util.ArrayList;
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.ServiceConfigurationError;
23  import java.util.ServiceLoader;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   * Manages handlers, that shall always executed for scripts in a given language. These standard handlers will be called
30   * <b>before</b> any custom handlers in the pre invocation and <b>after</b> any custom handlers in the post invocation.
31   */
32  public class StandardHandlerConfig {
33  
34      /** The standard handlers. */
35      private static Map<String, List<ScriptInvocationHandler>> standardHandlers;
36  
37      /**
38       * Instantiates a new standard handler config.
39       */
40      private StandardHandlerConfig() {
41          // Prevent Instantiation
42      }
43  
44      static {
45  
46          standardHandlers = new HashMap<>();
47  
48          ServiceLoader<ScriptInvocationHandler> serviceLoader = ServiceLoader.load(ScriptInvocationHandler.class);
49          try {
50              for (ScriptInvocationHandler scriptInvocationHandler : serviceLoader) {
51  
52                  List<ScriptInvocationHandler> handlersForLanguage = standardHandlers
53                          .get(scriptInvocationHandler.getLanguageName());
54  
55                  if (handlersForLanguage == null) {
56                      handlersForLanguage = new ArrayList<>();
57                      standardHandlers.put(scriptInvocationHandler.getLanguageName(), handlersForLanguage);
58                  }
59  
60                  handlersForLanguage.add(scriptInvocationHandler);
61              }
62          } catch (ServiceConfigurationError error) {
63              Logger logger = LoggerFactory.getLogger(StandardHandlerConfig.class);
64              logger.error(
65                      "Loading of standard script invocation handlers failed, most likely due to an unknown handler implementation given in META-INF/services {}",
66                      ScriptInvocationHandler.class.getName());
67              standardHandlers = Collections.emptyMap();
68          }
69      }
70  
71      /**
72       * Gets the standard handlers by language.
73       *
74       * @param language
75       *            the language
76       *
77       * @return the standard handlers by language
78       */
79      public static List<ScriptInvocationHandler> getStandardHandlersByLanguage(String language) {
80          if (standardHandlers.containsKey(language)) {
81              return Collections.unmodifiableList(standardHandlers.get(language));
82          }
83          return Collections.emptyList();
84      }
85  }