Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
ComponentScriptService | 46 | 20 | 0% | 15 | 0 |
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.component.script; | |
21 | ||
22 | import java.lang.reflect.Type; | |
23 | ||
24 | import javax.inject.Inject; | |
25 | import javax.inject.Named; | |
26 | import javax.inject.Provider; | |
27 | import javax.inject.Singleton; | |
28 | ||
29 | import org.xwiki.bridge.DocumentAccessBridge; | |
30 | import org.xwiki.component.annotation.Component; | |
31 | import org.xwiki.component.internal.multi.ComponentManagerManager; | |
32 | import org.xwiki.component.manager.ComponentLookupException; | |
33 | import org.xwiki.component.manager.ComponentManager; | |
34 | import org.xwiki.context.Execution; | |
35 | import org.xwiki.script.service.ScriptService; | |
36 | ||
37 | /** | |
38 | * Provides Component-specific Scripting APIs. | |
39 | * | |
40 | * @version $Id: b2ef658a6b6f096255a69d6a99e45b88c0dfa061 $ | |
41 | * @since 4.1M2 | |
42 | */ | |
43 | @Component | |
44 | @Named("component") | |
45 | @Singleton | |
46 | public class ComponentScriptService implements ScriptService | |
47 | { | |
48 | /** | |
49 | * The key under which the last encountered error is stored in the current execution context. | |
50 | */ | |
51 | private static final String ERROR_KEY = "scriptservice.component.error"; | |
52 | ||
53 | /** | |
54 | * The Component Manager that we'll return to the user or use to return component instances to the user. Note that | |
55 | * we use a Context Component Manager so that the user gets all components registered in its context. | |
56 | */ | |
57 | @Inject | |
58 | @Named("context") | |
59 | private Provider<ComponentManager> contextComponentManagerProvider; | |
60 | ||
61 | @Inject | |
62 | @Named("context/root") | |
63 | @Deprecated | |
64 | private Provider<ComponentManager> contextrootComponentManagerProvider; | |
65 | ||
66 | /** | |
67 | * Used to access the component manager corresponding to a specific namespace. | |
68 | */ | |
69 | @Inject | |
70 | private ComponentManagerManager componentManagerManager; | |
71 | ||
72 | /** | |
73 | * Used to check for Programming Rights. | |
74 | */ | |
75 | @Inject | |
76 | private DocumentAccessBridge bridge; | |
77 | ||
78 | /** | |
79 | * Provides access to the current context. | |
80 | */ | |
81 | @Inject | |
82 | private Execution execution; | |
83 | ||
84 | /** | |
85 | * A Component Manager which read in contextual Component Manager and write in root component manager. | |
86 | * | |
87 | * @return the Component Manager if the document has Programming Rights or null otherwise | |
88 | * @deprecated since 6.4.1, 6.2.6, use {@link #getContextComponentManager()} or | |
89 | * {@link #getContextComponentManager()} instead | |
90 | */ | |
91 | 2 | @Deprecated |
92 | public ComponentManager getComponentManager() | |
93 | { | |
94 | 2 | return this.bridge.hasProgrammingRights() ? this.contextrootComponentManagerProvider.get() : null; |
95 | } | |
96 | ||
97 | /** | |
98 | * @return the contextual Component Manager if the document has Programming Rights or null otherwise | |
99 | * @since 6.4.1 | |
100 | * @since 6.2.6 | |
101 | */ | |
102 | 99 | public ComponentManager getContextComponentManager() |
103 | { | |
104 | 99 | return this.bridge.hasProgrammingRights() ? this.contextComponentManagerProvider.get() : null; |
105 | } | |
106 | ||
107 | /** | |
108 | * @return the root Component Manager if the document has Programming Rights or null otherwise | |
109 | * @since 6.4.1 | |
110 | * @since 6.2.6 | |
111 | */ | |
112 | 2 | public ComponentManager getRootComponentManager() |
113 | { | |
114 | 2 | return getComponentManager(null); |
115 | } | |
116 | ||
117 | /** | |
118 | * Retrieves the component manager associated with a specific namespace. The namespace is generally of the form | |
119 | * <code>prefix:subid</code> where <code>prefix</code> is used to find the proper factory. <code>:</code> can be | |
120 | * escaped using <code>\</code> and <code>\</code> need to be escaped as well. There is a namespace for each | |
121 | * document, space, wiki and user. E.g. 'wiki:drafts' is the namespace corresponding to the 'drafts' wiki. | |
122 | * Namespaces can be nested in which case they inherit from the parent namespace. E.g. the component manager for a | |
123 | * specific document has access to the components registered specifically for that document or for any of its | |
124 | * namespace ancestors (space, wiki, root). The root (top level) component manager is returned if you pass | |
125 | * {@code null}. | |
126 | * | |
127 | * @param namespace a namespace or {@code null} for the root {@link ComponentManager} | |
128 | * @return the component manager associated with the specified namespace, if any, {@code null otherwise} | |
129 | */ | |
130 | 4 | public ComponentManager getComponentManager(String namespace) |
131 | { | |
132 | 4 | return this.bridge.hasProgrammingRights() ? this.componentManagerManager.getComponentManager(namespace, false) |
133 | : null; | |
134 | } | |
135 | ||
136 | /** | |
137 | * Find a component instance that implements that passed type. If the component has a singleton lifecycle then this | |
138 | * method always return the same instance. | |
139 | * | |
140 | * @param <T> the component role type | |
141 | * @param roleType the class (aka role) that the component implements | |
142 | * @return the component instance or null if not found | |
143 | * @since 4.0RC1 and modified in 6.1M2 to be public (had package visibility) | |
144 | */ | |
145 | 94 | public <T> T getInstance(Type roleType) |
146 | { | |
147 | 94 | T result = null; |
148 | 94 | ComponentManager cm = getContextComponentManager(); |
149 | 94 | if (cm != null) { |
150 | 93 | try { |
151 | 93 | result = cm.getInstance(roleType); |
152 | } catch (ComponentLookupException e) { | |
153 | 1 | setError(e); |
154 | } | |
155 | } | |
156 | 94 | return result; |
157 | } | |
158 | ||
159 | /** | |
160 | * Find a component instance that implements that passed interface class. If the component has a singleton lifecycle | |
161 | * then this method always return the same instance. | |
162 | * | |
163 | * @param <T> the component role type | |
164 | * @param roleType the class (aka role) that the component implements | |
165 | * @param roleHint the hint that differentiates a component implementation from another one (each component is | |
166 | * registered with a hint; the "default" hint being the default) | |
167 | * @return the component instance or null if not found | |
168 | * @since 4.0RC1 and modified in 6.1M2 to be public (had package visibility) | |
169 | */ | |
170 | 3 | public <T> T getInstance(Type roleType, String roleHint) |
171 | { | |
172 | 3 | T result = null; |
173 | 3 | ComponentManager cm = getContextComponentManager(); |
174 | 3 | if (cm != null) { |
175 | 2 | try { |
176 | 2 | result = cm.getInstance(roleType, roleHint); |
177 | } catch (ComponentLookupException e) { | |
178 | 1 | setError(e); |
179 | } | |
180 | } | |
181 | 3 | return result; |
182 | } | |
183 | ||
184 | /** | |
185 | * Get the error generated while performing the previously called action. | |
186 | * | |
187 | * @return the last exception or {@code null} if no exception was thrown | |
188 | * @since 6.1M2 | |
189 | */ | |
190 | 2 | public Exception getLastError() |
191 | { | |
192 | 2 | return (Exception) this.execution.getContext().getProperty(ERROR_KEY); |
193 | } | |
194 | ||
195 | /** | |
196 | * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}. | |
197 | * | |
198 | * @param exception the exception to store, can be {@code null} to clear the previously stored exception | |
199 | * @see #getLastError() | |
200 | * @since 6.1M2 | |
201 | */ | |
202 | 2 | private void setError(Exception exception) |
203 | { | |
204 | 2 | this.execution.getContext().setProperty(ERROR_KEY, exception); |
205 | } | |
206 | ||
207 | } |