Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
UIExtensionScriptService | 48 | 16 | 0% | 6 | 2 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software 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 GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.uiextension.script; | |
21 | ||
22 | import java.util.List; | |
23 | import java.util.Map; | |
24 | ||
25 | import javax.inject.Inject; | |
26 | import javax.inject.Named; | |
27 | import javax.inject.Provider; | |
28 | import javax.inject.Singleton; | |
29 | ||
30 | import org.slf4j.Logger; | |
31 | import org.xwiki.component.annotation.Component; | |
32 | import org.xwiki.component.manager.ComponentLookupException; | |
33 | import org.xwiki.component.manager.ComponentManager; | |
34 | import org.xwiki.script.service.ScriptService; | |
35 | import org.xwiki.uiextension.UIExtension; | |
36 | import org.xwiki.uiextension.UIExtensionFilter; | |
37 | import org.xwiki.uiextension.UIExtensionManager; | |
38 | ||
39 | /** | |
40 | * Allows scripts to easily access Interface Extensions APIs. | |
41 | * | |
42 | * @version $Id: 0c4a8573a8778d739f73e08f65271467a65ed46a $ | |
43 | * @since 4.2M3 | |
44 | */ | |
45 | @Component | |
46 | @Named("uix") | |
47 | @Singleton | |
48 | public class UIExtensionScriptService implements ScriptService | |
49 | { | |
50 | /** | |
51 | * The logger to log. | |
52 | */ | |
53 | @Inject | |
54 | private Logger logger; | |
55 | ||
56 | /** | |
57 | * We use the Context Component Manager to lookup UI Extensions registered as components. | |
58 | * The Context Component Manager allows Extensions to be registered for a specific user, for a specific wiki or for | |
59 | * a whole farm. | |
60 | */ | |
61 | @Inject | |
62 | @Named("context") | |
63 | private Provider<ComponentManager> contextComponentManagerProvider; | |
64 | ||
65 | /** | |
66 | * The default UIExtensionManager. | |
67 | */ | |
68 | @Inject | |
69 | private UIExtensionManager uiExtensionManager; | |
70 | ||
71 | /** | |
72 | * Utility method to split a list of extension names, for example {code}"Panels.Apps,Panels.QuickLinks"{code} to get | |
73 | * a List containing those names. | |
74 | * | |
75 | * @param nameList the list of extension names to split | |
76 | * @return a List containing all the names from the given String. | |
77 | */ | |
78 | 5740 | private String[] parseFilterParameters(String nameList) |
79 | { | |
80 | 5741 | return nameList.replaceAll(" ", "").split(","); |
81 | } | |
82 | ||
83 | /** | |
84 | * Retrieves all the {@link UIExtension}s for a given Extension Point. | |
85 | * | |
86 | * @param extensionPointId The ID of the Extension Point to retrieve the {@link UIExtension}s for | |
87 | * @return the list of {@link UIExtension} for the given Extension Point | |
88 | */ | |
89 | 8439 | public List<UIExtension> getExtensions(String extensionPointId) |
90 | { | |
91 | 8438 | UIExtensionManager manager = this.uiExtensionManager; |
92 | ||
93 | 8438 | ComponentManager componentManager = contextComponentManagerProvider.get(); |
94 | 8440 | if (componentManager.hasComponent(UIExtensionManager.class, extensionPointId)) { |
95 | 18 | try { |
96 | // Look for a specific UI extension manager for the given extension point | |
97 | 18 | manager = componentManager.getInstance(UIExtensionManager.class, extensionPointId); |
98 | } catch (ComponentLookupException e) { | |
99 | 0 | this.logger.error("Failed to initialize UI extension manager", e); |
100 | } | |
101 | } | |
102 | ||
103 | 8439 | return manager.get(extensionPointId); |
104 | } | |
105 | ||
106 | /** | |
107 | * Retrieves the list of {@link UIExtension} for a given Extension Point. | |
108 | * | |
109 | * Examples: | |
110 | * <ul> | |
111 | * <li>Get only the {@link UIExtension}s with the given IDs for the Extension Point "platform.example" | |
112 | * <pre>$services.uix.getExtensions('platform.example', {'select' : 'id1, id2, id3'})</pre></li> | |
113 | * <li>Get all the {@link UIExtension}s for the Extension Point "platform.example" except the | |
114 | * {@link UIExtension}s with the IDs "id2" and "id3" | |
115 | * <pre>$services.uix.getExtensions('platform.example', {'exclude' : 'id2, id3'})</pre></li> | |
116 | * <li>Get all the {@link UIExtension}s for the Extension Point "platform.example" and order them by one of their | |
117 | * parameter | |
118 | * <pre>$services.uix.getExtensions('platform.example', {'sortByParameter' : 'parameterKey'})</pre></li> | |
119 | * <li>Get only the {@link UIExtension}s with the given IDs for the Extension Point "platform.example" and order | |
120 | * them by one of their parameter | |
121 | * <pre>$services.uix.getExtensions('platform.example', | |
122 | * {'select' : 'id1, id2, id3', 'sortByParameter' : 'parameterKey'})</pre></li> | |
123 | * </ul> | |
124 | * | |
125 | * @param extensionPointId The ID of the Extension Point to retrieve the {@link UIExtension}s for | |
126 | * @param filters Optional filters to apply before retrieving the list | |
127 | * @return the list of {@link UIExtension} for the given Extension Point | |
128 | */ | |
129 | 5742 | public List<UIExtension> getExtensions(String extensionPointId, Map<String, String> filters) |
130 | { | |
131 | 5742 | List<UIExtension> extensions = getExtensions(extensionPointId); |
132 | ||
133 | 5742 | for (Map.Entry<String, String> entry : filters.entrySet()) { |
134 | 5741 | String filterHint = entry.getKey(); |
135 | ||
136 | 5741 | try { |
137 | 5741 | UIExtensionFilter filter = |
138 | contextComponentManagerProvider.get().getInstance(UIExtensionFilter.class, filterHint); | |
139 | 5742 | extensions = filter.filter(extensions, this.parseFilterParameters(entry.getValue())); |
140 | } catch (ComponentLookupException e) { | |
141 | 0 | logger.warn("Unable to find a UIExtensionFilter for hint [{}] " |
142 | + "while getting UIExtensions for extension point [{}]", filterHint, extensionPointId); | |
143 | } | |
144 | } | |
145 | ||
146 | 5740 | return extensions; |
147 | } | |
148 | } |