1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin

File XWikiPluginManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

12
95
24
1
294
240
43
0.45
3.96
24
1.79

Classes

Class Line # Actions
XWikiPluginManager 35 95 0% 43 64
0.511450451.1%
 

Contributing tests

This file is covered by 27 tests. .

Source view

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 com.xpn.xwiki.plugin;
21   
22    import java.lang.reflect.Method;
23    import java.util.HashMap;
24    import java.util.Map;
25    import java.util.Vector;
26   
27    import org.apache.commons.lang3.StringUtils;
28    import org.slf4j.Logger;
29    import org.slf4j.LoggerFactory;
30   
31    import com.xpn.xwiki.XWikiContext;
32    import com.xpn.xwiki.XWikiException;
33    import com.xpn.xwiki.doc.XWikiAttachment;
34   
 
35    public class XWikiPluginManager
36    {
37    /** Log helper for logging messages in this class. */
38    private static final Logger LOGGER = LoggerFactory.getLogger(XWikiPluginManager.class);
39   
40    private Vector<String> plugins = new Vector<String>();
41   
42    private Vector<String> pluginClassNames = new Vector<String>();
43   
44    private Map<String, XWikiPluginInterface> plugins_classes = new HashMap<String, XWikiPluginInterface>();
45   
46    private Map<String, Vector<XWikiPluginInterface>> functionList =
47    new HashMap<String, Vector<XWikiPluginInterface>>();
48   
 
49  31 toggle public XWikiPluginManager()
50    {
51    }
52   
 
53  87 toggle public XWikiPluginManager(String classList, XWikiContext context)
54    {
55  87 String[] classNames = StringUtils.split(classList, " ,");
56  87 addPlugins(classNames, context);
57    }
58   
 
59  0 toggle public XWikiPluginManager(String[] classNames, XWikiContext context)
60    {
61  0 addPlugins(classNames, context);
62    }
63   
 
64  170 toggle @SuppressWarnings("unchecked")
65    public void addPlugin(String name, String className, XWikiContext context)
66    {
67  170 if (this.pluginClassNames.contains(className)) {
68  0 if (LOGGER.isInfoEnabled()) {
69  0 LOGGER.info(String.format("Skipping already registered plugin [%s]", name));
70    }
71  0 return;
72    }
73  170 try {
74  170 Class<?>[] classes = new Class<?>[3];
75  170 classes[0] = String.class;
76  170 classes[1] = String.class;
77  170 classes[2] = context.getClass();
78  170 Object[] args = new Object[3];
79  170 args[0] = name;
80  170 args[1] = className;
81  170 args[2] = context;
82  170 Class<XWikiPluginInterface> pluginClass =
83    (Class<XWikiPluginInterface>) Class.forName(className, true, Thread.currentThread()
84    .getContextClassLoader());
85  170 XWikiPluginInterface plugin = pluginClass.getConstructor(classes).newInstance(args);
86  170 if (plugin != null) {
87  170 this.plugins.add(plugin.getName());
88  170 this.plugins_classes.put(plugin.getName(), plugin);
89  170 this.pluginClassNames.add(className);
90  170 initPlugin(plugin, pluginClass, context);
91    }
92    } catch (Exception ex) {
93    // Log an error but do not fail
94  0 LOGGER.error("Cannot initialize plugin [" + className + "]. This plugin will not be available.", ex);
95    }
96    }
97   
 
98  0 toggle public void removePlugin(String className)
99    {
100  0 this.plugins.remove(className);
101  0 Object plugin = this.plugins_classes.get(className);
102  0 this.plugins_classes.remove(className);
103   
104  0 for (String name : this.functionList.keySet()) {
105  0 Vector<XWikiPluginInterface> pluginList = this.functionList.get(name);
106  0 pluginList.remove(plugin);
107    }
108    }
109   
 
110  119 toggle public void addPlugins(String[] classNames, XWikiContext context)
111    {
112  119 if (context.getURLFactory() == null) {
113  87 context
114    .setURLFactory(context.getWiki().getURLFactoryService().createURLFactory(context.getMode(), context));
115    }
116  119 initInterface();
117  119 for (String className : classNames) {
118  170 addPlugin(className, className, context);
119    }
120    }
121   
 
122  21955 toggle public XWikiPluginInterface getPlugin(String className)
123    {
124  21955 return this.plugins_classes.get(className);
125    }
126   
 
127  78496 toggle public Vector<String> getPlugins()
128    {
129  78480 return this.plugins;
130    }
131   
 
132  0 toggle public void setPlugins(Vector<String> plugins)
133    {
134  0 this.plugins = plugins;
135    }
136   
 
137  150 toggle public void initInterface()
138    {
139  150 for (Method method : XWikiPluginInterface.class.getMethods()) {
140  2250 String name = method.getName();
141  2250 this.functionList.put(name, new Vector<XWikiPluginInterface>());
142    }
143    }
144   
 
145  170 toggle public void initPlugin(Object plugin, Class<XWikiPluginInterface> pluginClass, XWikiContext context)
146    throws XWikiException
147    {
148  170 for (Method method : pluginClass.getDeclaredMethods()) {
149  939 String name = method.getName();
150  939 if (this.functionList.containsKey(name)) {
151  382 this.functionList.get(name).add((XWikiPluginInterface) plugin);
152    }
153    }
154  170 ((XWikiPluginInterface) plugin).init(context);
155    }
156   
 
157  14021 toggle public Vector<XWikiPluginInterface> getPlugins(String functionName)
158    {
159  14021 if (this.functionList.containsKey(functionName)) {
160  14021 return this.functionList.get(functionName);
161    }
162  0 return null;
163    }
164   
 
165  32 toggle public void virtualInit(XWikiContext context)
166    {
167  32 for (XWikiPluginInterface plugin : getPlugins("virtualInit")) {
168  8 try {
169  8 plugin.virtualInit(context);
170    } catch (Exception e) {
171    }
172    }
173    }
174   
 
175  0 toggle public void flushCache(XWikiContext context)
176    {
177  0 for (XWikiPluginInterface plugin : getPlugins("flushCache")) {
178  0 try {
179  0 plugin.flushCache(context);
180    } catch (Exception e) {
181  0 LOGGER.error("Failed to flush cache in plugin [" + plugin.getClass() + "]", e);
182    }
183    }
184    }
185   
 
186  0 toggle public String commonTagsHandler(String text, XWikiContext context)
187    {
188  0 for (XWikiPluginInterface plugin : getPlugins("commonTagsHandler")) {
189  0 try {
190  0 text = plugin.commonTagsHandler(text, context);
191    } catch (Exception e) {
192    }
193    }
194  0 return text;
195    }
196   
 
197  0 toggle public String startRenderingHandler(String text, XWikiContext context)
198    {
199  0 for (XWikiPluginInterface plugin : getPlugins("startRenderingHandler")) {
200  0 try {
201  0 text = plugin.startRenderingHandler(text, context);
202    } catch (Exception e) {
203    }
204    }
205  0 return text;
206    }
207   
 
208  0 toggle public String outsidePREHandler(String text, XWikiContext context)
209    {
210  0 for (XWikiPluginInterface plugin : getPlugins("outsidePREHandler")) {
211  0 try {
212  0 text = plugin.outsidePREHandler(text, context);
213    } catch (Exception e) {
214    }
215    }
216  0 return text;
217    }
218   
 
219  0 toggle public String insidePREHandler(String text, XWikiContext context)
220    {
221  0 for (XWikiPluginInterface plugin : getPlugins("insidePREHandler")) {
222  0 try {
223  0 text = plugin.insidePREHandler(text, context);
224    } catch (Exception e) {
225    }
226    }
227  0 return text;
228    }
229   
 
230  0 toggle public String endRenderingHandler(String text, XWikiContext context)
231    {
232  0 for (XWikiPluginInterface plugin : getPlugins("endRenderingHandler")) {
233  0 try {
234  0 text = plugin.endRenderingHandler(text, context);
235    } catch (Exception e) {
236    }
237    }
238  0 return text;
239    }
240   
 
241  0 toggle public void beginRendering(XWikiContext context)
242    {
243  0 for (XWikiPluginInterface plugin : getPlugins("beginRendering")) {
244  0 try {
245  0 plugin.beginRendering(context);
246    } catch (Exception e) {
247    }
248    }
249    }
250   
 
251  0 toggle public void endRendering(XWikiContext context)
252    {
253  0 for (XWikiPluginInterface plugin : getPlugins("endRendering")) {
254  0 try {
255  0 plugin.endRendering(context);
256    } catch (Exception e) {
257    }
258    }
259    }
260   
 
261  6893 toggle public void beginParsing(XWikiContext context)
262    {
263  6893 for (XWikiPluginInterface plugin : getPlugins("beginParsing")) {
264  0 try {
265  0 plugin.beginParsing(context);
266    } catch (Exception e) {
267    }
268    }
269    }
270   
 
271  6893 toggle public String endParsing(String content, XWikiContext context)
272    {
273  6893 for (XWikiPluginInterface plugin : getPlugins("endParsing")) {
274  34465 try {
275  34465 content = plugin.endParsing(content, context);
276    } catch (Exception e) {
277    }
278    }
279  6893 return content;
280    }
281   
 
282  207 toggle public XWikiAttachment downloadAttachment(XWikiAttachment attachment, XWikiContext context)
283    {
284  208 XWikiAttachment attach = attachment;
285  206 for (XWikiPluginInterface plugin : getPlugins("downloadAttachment")) {
286  0 try {
287  0 attach = plugin.downloadAttachment(attach, context);
288    } catch (Exception ex) {
289  0 LOGGER.warn("downloadAttachment failed for plugin [" + plugin.getName() + "]: " + ex.getMessage());
290    }
291    }
292  204 return attach;
293    }
294    }