BrightSide Workbench Full Report + Source Code
Plugins.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.action;
20 
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.concurrent.locks.Lock;
26 import java.util.concurrent.locks.ReentrantLock;
27 import org.turro.string.Strings;
28 import org.turro.annotation.ElephantPlugin;
29 import org.turro.reflection.ClassNames;
30 import org.turro.reflection.Reflections;
31 
36 public class Plugins {
37 
38  /* Plugins */
39 
40  private static List<IElephantPlugin> _plugins;
41  private static List<Class> _pluginClass;
42  private static final Lock lock = new ReentrantLock();
43 
44  public static Map<String, Object> execute(String name, Map params) {
45  loadPlugins();
46  Map<String, Object> results = new HashMap<>();
47  for(IElephantPlugin iPlugin : _plugins) {
48  if(iPlugin.itsMe(name)) {
49  iPlugin.setContext(params, results);
50  iPlugin.execute();
51  if(iPlugin.stopPropagating()) break;
52  }
53  }
54  return results;
55  }
56 
57  public static <T> T loadImplementation(Class<T> jclass) {
58  return loadImplementation(jclass, null);
59  }
60 
61  public static <T> T loadImplementation(Class<T> jclass, String label) {
62  loadPlugins();
63  for(Class pclass : _pluginClass) {
64  if(Reflections.of(pclass).canCast(jclass)) {
65  if(Strings.isBlank(label) ||
66  ((ElephantPlugin) pclass.getAnnotation(ElephantPlugin.class)).label().equals(label)) {
67  return (T) Reflections.of(pclass).instance();
68  }
69  }
70  }
71  return null;
72  }
73 
74  private static void loadPlugins() {
75  if(_plugins == null) {
76  lock.lock();
77  try {
78  if(_plugins == null) {
79  _plugins = new ArrayList<>();
80  _pluginClass = new ArrayList<>();
81  List<String> plugins = ClassNames.cached().byAnnotation(ElephantPlugin.class.getName());
82  if(plugins != null) {
83  for(String plugin : plugins) {
84  Class iPluginClass = Reflections.check(plugin);
85  if(iPluginClass != null) {
86  _pluginClass.add(iPluginClass);
87  IElephantPlugin iPlugin = (IElephantPlugin) Reflections.of(iPluginClass)
88  .is(IElephantPlugin.class).instance();
89  if(iPlugin != null) {
90  _plugins.add(iPlugin);
91  }
92  }
93  }
94  }
95  }
96  } finally {
97  lock.unlock();
98  }
99  }
100  }
101 
102  private Plugins() {
103  }
104 
105 }
static< T > T loadImplementation(Class< T > jclass, String label)
Definition: Plugins.java:61
static Map< String, Object > execute(String name, Map params)
Definition: Plugins.java:44
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57