1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.plugin

File XWikiPluginManager.java

 

Coverage histogram

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

Code metrics

10
93
24
1
291
237
42
0.45
3.88
24
1.75

Classes

Class Line # Actions
XWikiPluginManager 35 93 0% 42 55
0.5669291656.7%
 

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  106 toggle public XWikiPluginManager(String classList, XWikiContext context)
54    {
55  106 String[] classNames = StringUtils.split(classList, " ,");
56  106 addPlugins(classNames, context);
57    }
58   
 
59  0 toggle public XWikiPluginManager(String[] classNames, XWikiContext context)
60    {
61  0 addPlugins(classNames, context);
62    }
63   
 
64  350 toggle @SuppressWarnings("unchecked")
65    public void addPlugin(String name, String className, XWikiContext context)
66    {
67  350 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  350 try {
74  350 Class<?>[] classes = new Class<?>[3];
75  350 classes[0] = String.class;
76  350 classes[1] = String.class;
77  350 classes[2] = context.getClass();
78  350 Object[] args = new Object[3];
79  350 args[0] = name;
80  350 args[1] = className;
81  350 args[2] = context;
82  350 Class<XWikiPluginInterface> pluginClass =
83    (Class<XWikiPluginInterface>) Class.forName(className, true, Thread.currentThread()
84    .getContextClassLoader());
85  350 XWikiPluginInterface plugin = pluginClass.getConstructor(classes).newInstance(args);
86  350 if (plugin != null) {
87  350 this.plugins.add(plugin.getName());
88  350 this.plugins_classes.put(plugin.getName(), plugin);
89  350 this.pluginClassNames.add(className);
90  350 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  145 toggle public void addPlugins(String[] classNames, XWikiContext context)
111    {
112  145 if (context.getURLFactory() == null) {
113  106 context
114    .setURLFactory(context.getWiki().getURLFactoryService().createURLFactory(context.getMode(), context));
115    }
116  145 initInterface();
117  145 for (String className : classNames) {
118  350 addPlugin(className, className, context);
119    }
120    }
121   
 
122  1677164 toggle public XWikiPluginInterface getPlugin(String className)
123    {
124  1677167 return this.plugins_classes.get(className);
125    }
126   
 
127  1744980 toggle public Vector<String> getPlugins()
128    {
129  1744965 return this.plugins;
130    }
131   
 
132  0 toggle public void setPlugins(Vector<String> plugins)
133    {
134  0 this.plugins = plugins;
135    }
136   
 
137  176 toggle public void initInterface()
138    {
139  176 for (Method method : XWikiPluginInterface.class.getMethods()) {
140  2640 String name = method.getName();
141  2640 this.functionList.put(name, new Vector<XWikiPluginInterface>());
142    }
143    }
144   
 
145  350 toggle public void initPlugin(Object plugin, Class<XWikiPluginInterface> pluginClass, XWikiContext context)
146    throws XWikiException
147    {
148  350 for (Method method : pluginClass.getDeclaredMethods()) {
149  3440 String name = method.getName();
150  3440 if (this.functionList.containsKey(name)) {
151  892 this.functionList.get(name).add((XWikiPluginInterface) plugin);
152    }
153    }
154  350 ((XWikiPluginInterface) plugin).init(context);
155    }
156   
 
157  59948 toggle public Vector<XWikiPluginInterface> getPlugins(String functionName)
158    {
159  59953 return this.functionList.get(functionName);
160    }
161   
 
162  39 toggle public void virtualInit(XWikiContext context)
163    {
164  39 for (XWikiPluginInterface plugin : getPlugins("virtualInit")) {
165  4 try {
166  4 plugin.virtualInit(context);
167    } catch (Exception e) {
168    }
169    }
170    }
171   
 
172  1 toggle public void flushCache(XWikiContext context)
173    {
174  1 for (XWikiPluginInterface plugin : getPlugins("flushCache")) {
175  5 try {
176  5 plugin.flushCache(context);
177    } catch (Exception e) {
178  0 LOGGER.error("Failed to flush cache in plugin [" + plugin.getClass() + "]", e);
179    }
180    }
181    }
182   
 
183  0 toggle public String commonTagsHandler(String text, XWikiContext context)
184    {
185  0 for (XWikiPluginInterface plugin : getPlugins("commonTagsHandler")) {
186  0 try {
187  0 text = plugin.commonTagsHandler(text, context);
188    } catch (Exception e) {
189    }
190    }
191  0 return text;
192    }
193   
 
194  0 toggle public String startRenderingHandler(String text, XWikiContext context)
195    {
196  0 for (XWikiPluginInterface plugin : getPlugins("startRenderingHandler")) {
197  0 try {
198  0 text = plugin.startRenderingHandler(text, context);
199    } catch (Exception e) {
200    }
201    }
202  0 return text;
203    }
204   
 
205  0 toggle public String outsidePREHandler(String text, XWikiContext context)
206    {
207  0 for (XWikiPluginInterface plugin : getPlugins("outsidePREHandler")) {
208  0 try {
209  0 text = plugin.outsidePREHandler(text, context);
210    } catch (Exception e) {
211    }
212    }
213  0 return text;
214    }
215   
 
216  0 toggle public String insidePREHandler(String text, XWikiContext context)
217    {
218  0 for (XWikiPluginInterface plugin : getPlugins("insidePREHandler")) {
219  0 try {
220  0 text = plugin.insidePREHandler(text, context);
221    } catch (Exception e) {
222    }
223    }
224  0 return text;
225    }
226   
 
227  0 toggle public String endRenderingHandler(String text, XWikiContext context)
228    {
229  0 for (XWikiPluginInterface plugin : getPlugins("endRenderingHandler")) {
230  0 try {
231  0 text = plugin.endRenderingHandler(text, context);
232    } catch (Exception e) {
233    }
234    }
235  0 return text;
236    }
237   
 
238  0 toggle public void beginRendering(XWikiContext context)
239    {
240  0 for (XWikiPluginInterface plugin : getPlugins("beginRendering")) {
241  0 try {
242  0 plugin.beginRendering(context);
243    } catch (Exception e) {
244    }
245    }
246    }
247   
 
248  0 toggle public void endRendering(XWikiContext context)
249    {
250  0 for (XWikiPluginInterface plugin : getPlugins("endRendering")) {
251  0 try {
252  0 plugin.endRendering(context);
253    } catch (Exception e) {
254    }
255    }
256    }
257   
 
258  29596 toggle public void beginParsing(XWikiContext context)
259    {
260  29601 for (XWikiPluginInterface plugin : getPlugins("beginParsing")) {
261  0 try {
262  0 plugin.beginParsing(context);
263    } catch (Exception e) {
264    }
265    }
266    }
267   
 
268  29625 toggle public String endParsing(String content, XWikiContext context)
269    {
270  29625 for (XWikiPluginInterface plugin : getPlugins("endParsing")) {
271  195724 try {
272  195725 content = plugin.endParsing(content, context);
273    } catch (Exception e) {
274    }
275    }
276  29627 return content;
277    }
278   
 
279  441 toggle public XWikiAttachment downloadAttachment(XWikiAttachment attachment, XWikiContext context)
280    {
281  442 XWikiAttachment attach = attachment;
282  444 for (XWikiPluginInterface plugin : getPlugins("downloadAttachment")) {
283  419 try {
284  418 attach = plugin.downloadAttachment(attach, context);
285    } catch (Exception ex) {
286  6 LOGGER.warn("downloadAttachment failed for plugin [" + plugin.getName() + "]: " + ex.getMessage());
287    }
288    }
289  447 return attach;
290    }
291    }