1. Project Clover database Tue Dec 20 2016 21:24:09 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
146
84
4
0.14
14
2
2

Classes

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

Contributing tests

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