1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.wiki.internal

File DefaultWikiComponentBuilder.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
28
2
1
141
80
4
0.14
14
2
2

Classes

Class Line # Actions
DefaultWikiComponentBuilder 54 28 0% 4 1
0.9687596.9%
 

Contributing tests

This file is covered by 4 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 org.xwiki.component.wiki.internal;
21   
22    import java.lang.reflect.InvocationHandler;
23    import java.lang.reflect.Proxy;
24    import java.util.ArrayList;
25    import java.util.Arrays;
26    import java.util.List;
27   
28    import javax.inject.Inject;
29    import javax.inject.Named;
30    import javax.inject.Provider;
31    import javax.inject.Singleton;
32   
33    import org.slf4j.Logger;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.manager.ComponentManager;
36    import org.xwiki.component.util.ReflectionUtils;
37    import org.xwiki.component.wiki.WikiComponent;
38    import org.xwiki.component.wiki.WikiComponentBuilder;
39    import org.xwiki.component.wiki.WikiComponentException;
40    import org.xwiki.component.wiki.internal.bridge.WikiComponentBridge;
41    import org.xwiki.model.reference.DocumentReference;
42   
43    import com.xpn.xwiki.XWikiContext;
44    import com.xpn.xwiki.XWikiException;
45   
46    /**
47    * Default implementation of a wiki component builder, that is using the legacy XWiki core module.
48    *
49    * @version $Id: a6f8139ae34f6e68e61405a0522c675a02361dde $
50    * @since 4.2M3
51    */
52    @Component
53    @Singleton
 
54    public class DefaultWikiComponentBuilder implements WikiComponentBuilder, WikiComponentConstants
55    {
56    /**
57    * The logger to log.
58    */
59    @Inject
60    private Logger logger;
61   
62    @Inject
63    @Named("context")
64    private ComponentManager contextComponentManager;
65   
66    /**
67    * Used to access the current {@link XWikiContext}.
68    */
69    @Inject
70    private Provider<XWikiContext> xcontextProvider;
71   
72    /**
73    * Bridge to isolate the old model.
74    */
75    @Inject
76    private WikiComponentBridge componentBridge;
77   
 
78  8086 toggle @Override
79    public List<DocumentReference> getDocumentReferences()
80    {
81  8086 List<DocumentReference> results = new ArrayList<DocumentReference>();
82    // Note that the query is made to work with Oracle which treats empty strings as null.
83  8086 String query = ", BaseObject as obj, StringProperty as role where obj.className=? and obj.name=doc.fullName "
84    + "and role.id.id=obj.id and role.id.name=? "
85    + "and (role.value <> '' or (role.value is not null and '' is null))";
86  8086 List<String> parameters = new ArrayList<String>();
87  8086 parameters.add(COMPONENT_CLASS);
88  8086 parameters.add(COMPONENT_ROLE_TYPE_FIELD);
89   
90  8086 try {
91  8086 XWikiContext xcontext = xcontextProvider.get();
92  8086 results.addAll(xcontext.getWiki().getStore().searchDocumentReferences(query, parameters, xcontext));
93    } catch (XWikiException e) {
94  0 this.logger.error("Failed to search for existing wiki components [{}]", e.getMessage());
95    }
96   
97  8086 return results;
98    }
99   
 
100  2 toggle @Override
101    public List<WikiComponent> buildComponents(DocumentReference reference) throws WikiComponentException
102    {
103  2 List<WikiComponent> components = new ArrayList<WikiComponent>();
104   
105  2 if (!this.componentBridge.hasProgrammingRights(reference)) {
106  1 throw new WikiComponentException("Registering wiki components requires programming rights");
107    }
108   
109  1 DefaultWikiComponent rawComponent =
110    new DefaultWikiComponent(reference, componentBridge.getAuthorReference(reference),
111    componentBridge.getRoleType(reference), componentBridge.getRoleHint(reference),
112    componentBridge.getScope(reference));
113  1 rawComponent.setHandledMethods(componentBridge.getHandledMethods(reference));
114  1 rawComponent.setImplementedInterfaces(componentBridge.getDeclaredInterfaces(reference));
115  1 rawComponent.setDependencies(componentBridge.getDependencies(reference));
116  1 rawComponent.setSyntax(componentBridge.getSyntax(reference));
117   
118    // Create the method invocation handler of the proxy
119  1 InvocationHandler handler = new DefaultWikiComponentInvocationHandler(rawComponent, contextComponentManager);
120   
121    // Prepare a list containing the interfaces the component implements
122  1 List<Class<?>> implementedInterfaces = new ArrayList<Class<?>>();
123    // Add the main role
124  1 Class<?> roleTypeClass = ReflectionUtils.getTypeClass(rawComponent.getRoleType());
125    // Add the component role
126  1 implementedInterfaces.add(ReflectionUtils.getTypeClass(roleTypeClass));
127    // Add the additional interfaces declared through XObjects
128  1 implementedInterfaces.addAll(rawComponent.getImplementedInterfaces());
129    // Add the interfaces from the java class itself (interfaces implemented by DefaultWikiComponent)
130  1 implementedInterfaces.addAll(Arrays.asList(rawComponent.getClass().getInterfaces()));
131   
132    // Create the proxy
133  1 Class<?>[] implementedInterfacesArray = implementedInterfaces.toArray(new Class<?>[0]);
134  1 WikiComponent component = (WikiComponent) Proxy.newProxyInstance(roleTypeClass.getClassLoader(),
135    implementedInterfacesArray, handler);
136   
137  1 components.add(component);
138   
139  1 return components;
140    }
141    }